mirror of
https://github.com/reality/dbot.git
synced 2024-11-23 20:39:25 +01:00
Fix [#142] by escaping regex, also improve efficiency by only compiling 1 regex
This commit is contained in:
parent
54c67cb6ff
commit
f6371a7bbc
@ -1,6 +1,10 @@
|
||||
var _ = require('underscore')._;
|
||||
|
||||
var api = function(dbot) {
|
||||
var escapeRegexen = function(str) {
|
||||
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
|
||||
};
|
||||
|
||||
var api = {
|
||||
'resolveUser': function(server, nick, useLowerCase) {
|
||||
var knownUsers = this.getServerUsers(server);
|
||||
@ -13,14 +17,14 @@ var api = function(dbot) {
|
||||
} else {
|
||||
// this is retarded
|
||||
user = user.toLowerCase();
|
||||
var toMatch = new RegExp(escapeRegexen(user), "i");
|
||||
|
||||
var resolvedUser = _.find(knownUsers.users, function(nick) {
|
||||
var toMatch = new RegExp(user, "i");
|
||||
return nick.match(toMatch) !== null;
|
||||
}, this);
|
||||
|
||||
if(!resolvedUser) {
|
||||
resolvedUser = _.find(knownUsers.aliases, function(nick, alias) {
|
||||
var toMatch = new RegExp(user, "i");
|
||||
if(alias.match(toMatch) !== null) return nick;
|
||||
}, this);
|
||||
if(!_.isUndefined(resolvedUser)) user = resolvedUser;
|
||||
|
Loading…
Reference in New Issue
Block a user