From f6371a7bbca2456fef6ffe16dbc512dc6fb366bf Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 17 Jan 2013 17:44:29 +0000 Subject: [PATCH] Fix [#142] by escaping regex, also improve efficiency by only compiling 1 regex --- modules/users/api.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/users/api.js b/modules/users/api.js index 1899981..232d5cd 100644 --- a/modules/users/api.js +++ b/modules/users/api.js @@ -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;