3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 14:29:29 +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 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;