2013-01-14 22:03:47 +01:00
|
|
|
var _ = require('underscore')._;
|
|
|
|
|
|
|
|
var api = function(dbot) {
|
2013-01-17 18:44:29 +01:00
|
|
|
var escapeRegexen = function(str) {
|
|
|
|
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
|
|
|
|
};
|
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
var api = {
|
2013-01-15 20:09:54 +01:00
|
|
|
'resolveUser': function(server, nick, useLowerCase) {
|
2013-01-14 22:03:47 +01:00
|
|
|
var knownUsers = this.getServerUsers(server);
|
|
|
|
var user = nick;
|
2013-01-15 20:09:54 +01:00
|
|
|
|
|
|
|
if(!useLowerCase) {
|
|
|
|
if(!_.include(knownUsers.users, nick) && _.has(knownUsers.aliases, nick)) {
|
|
|
|
user = knownUsers.aliases[nick];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// this is retarded
|
|
|
|
user = user.toLowerCase();
|
2013-01-18 15:34:04 +01:00
|
|
|
var toMatch = new RegExp("^" + escapeRegexen(user) + "$", "i");
|
2013-01-17 18:44:29 +01:00
|
|
|
|
2013-01-15 20:09:54 +01:00
|
|
|
var resolvedUser = _.find(knownUsers.users, function(nick) {
|
2013-01-15 20:44:34 +01:00
|
|
|
return nick.match(toMatch) !== null;
|
2013-01-15 20:09:54 +01:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
if(!resolvedUser) {
|
|
|
|
resolvedUser = _.find(knownUsers.aliases, function(nick, alias) {
|
2013-01-16 00:32:45 +01:00
|
|
|
if(alias.match(toMatch) !== null) return nick;
|
2013-01-15 20:09:54 +01:00
|
|
|
}, this);
|
2013-01-16 21:21:28 +01:00
|
|
|
if(!_.isUndefined(resolvedUser)) user = resolvedUser;
|
2013-01-15 20:09:54 +01:00
|
|
|
}
|
2013-01-16 22:55:14 +01:00
|
|
|
else{
|
|
|
|
user = resolvedUser;
|
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
}
|
|
|
|
return user;
|
|
|
|
},
|
|
|
|
|
2013-01-22 01:00:11 +01:00
|
|
|
'getServerUsers': function(server) {
|
|
|
|
return dbot.db.knownUsers[server].users;
|
|
|
|
},
|
|
|
|
|
2013-01-22 01:24:12 +01:00
|
|
|
'getAllUsers': function() {
|
2013-01-22 01:59:11 +01:00
|
|
|
return _.reduce(dbot.db.knownUsers, function(memo, server, name) {
|
2013-01-22 01:53:30 +01:00
|
|
|
memo[name] = server.users;
|
2013-01-22 02:06:29 +01:00
|
|
|
return memo;
|
2013-01-22 02:00:56 +01:00
|
|
|
}, {}, this);
|
2013-01-22 01:24:12 +01:00
|
|
|
},
|
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
'isKnownUser': function(server, nick) {
|
|
|
|
var knownUsers = this.getServerUsers(server);
|
|
|
|
return (_.include(knownUsers.users, nick) || _.has(knownUsers.aliases, nick));
|
|
|
|
},
|
|
|
|
|
|
|
|
'isPrimaryUser': function(server, nick) {
|
|
|
|
var knownUsers = this.getServerUsers(server);
|
|
|
|
return _.include(knownUsers.users, nick);
|
|
|
|
},
|
|
|
|
|
|
|
|
'getAliases': function(server, nick) {
|
|
|
|
var knownUsers = this.getServerUsers(server);
|
|
|
|
return _.chain(knownUsers.aliases)
|
|
|
|
.keys()
|
|
|
|
.filter(function(user) {
|
|
|
|
return knownUsers.aliases[user] == nick;
|
|
|
|
}, this)
|
|
|
|
.value();
|
2013-01-15 20:09:54 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'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;
|
|
|
|
|
|
|
|
return _.any(onlineNicks, function(nick) {
|
|
|
|
nick = nick.name;
|
|
|
|
return _.include(possiNicks, nick);
|
|
|
|
}, this);
|
2013-01-20 20:04:12 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'isChannelUser': function(server, user, channel, useLowerCase) {
|
|
|
|
var knownUsers = this.getServerUsers(server);
|
|
|
|
var user = this.api.resolveUser(server, user, useLowerCase);
|
|
|
|
|
|
|
|
if(!_.has(knownUsers.channelUsers, channel)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _.include(knownUsers.channelUsers[channel], user);
|
2013-01-14 22:03:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return api;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return api(dbot);
|
|
|
|
};
|