3
0
mirror of https://github.com/reality/dbot.git synced 2025-01-23 02:24:17 +01:00

isOnline and resolveUser checks lower case properly

This commit is contained in:
reality 2013-01-15 19:09:54 +00:00
parent de075a3a2d
commit be9ee61e13

View File

@ -2,14 +2,31 @@ var _ = require('underscore')._;
var api = function(dbot) {
var api = {
'resolveUser': function(server, nick, useLowercase) {
'resolveUser': function(server, nick, useLowerCase) {
var knownUsers = this.getServerUsers(server);
var user = nick;
if(!useLowerCase) {
if(!_.include(knownUsers.users, nick) && _.has(knownUsers.aliases, nick)) {
user = knownUsers.aliases[nick];
}
} else {
// this is retarded
user = user.toLowerCase();
var resolvedUser = _.find(knownUsers.users, function(nick) {
var toMatch = new RegExp("/"+user+"/i").compile();
return nick.match(toMatch);
}, this);
if(!resolvedUser) {
resolvedUser = _.find(knownUsers.aliases, function(nick, alias) {
var toMatch = new RegExp("/"+user+"/i").compile();
return alias.match(toMatch);
}, this);
user = knownUsers.aliases[resolvedUser];
}
}
if(useLowercase) user = user.toLowerCase();
return user;
},
@ -31,6 +48,22 @@ var api = function(dbot) {
return knownUsers.aliases[user] == nick;
}, this)
.value();
},
'isOnline': function(server, user, channel, useLowerCase) {
var user = this.api.resolveUser(server, user, useLowerCase);
var possiNicks = [user].concat(this.api.getAliases(server, user));
var onlineNicks = dbot.instance.connections[server].channels[channel].nicks;
if(useLowerCase) {
possiNicks = _.map(possiNicks, function(nick) {
return nick.toLowerCase();
});
}
return _.any(onlineNicks, function(nick) {
nick = nick.name;
return _.include(possiNicks, nick);
}, this);
}
};