3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00

Fix [#142] by escaping regex, also improve efficiency by only compiling 1 regex

This commit is contained in:
reality 2013-01-17 17:44:29 +00:00
parent 54c67cb6ff
commit f6371a7bbc

View File

@ -1,6 +1,10 @@
var _ = require('underscore')._; var _ = require('underscore')._;
var api = function(dbot) { var api = function(dbot) {
var escapeRegexen = function(str) {
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};
var api = { var api = {
'resolveUser': function(server, nick, useLowerCase) { 'resolveUser': function(server, nick, useLowerCase) {
var knownUsers = this.getServerUsers(server); var knownUsers = this.getServerUsers(server);
@ -13,14 +17,14 @@ var api = function(dbot) {
} else { } else {
// this is retarded // this is retarded
user = user.toLowerCase(); user = user.toLowerCase();
var toMatch = new RegExp(escapeRegexen(user), "i");
var resolvedUser = _.find(knownUsers.users, function(nick) { var resolvedUser = _.find(knownUsers.users, function(nick) {
var toMatch = new RegExp(user, "i");
return nick.match(toMatch) !== null; return nick.match(toMatch) !== null;
}, this); }, this);
if(!resolvedUser) { if(!resolvedUser) {
resolvedUser = _.find(knownUsers.aliases, function(nick, alias) { resolvedUser = _.find(knownUsers.aliases, function(nick, alias) {
var toMatch = new RegExp(user, "i");
if(alias.match(toMatch) !== null) return nick; if(alias.match(toMatch) !== null) return nick;
}, this); }, this);
if(!_.isUndefined(resolvedUser)) user = resolvedUser; if(!_.isUndefined(resolvedUser)) user = resolvedUser;