2012-12-23 17:19:01 +01:00
|
|
|
/**
|
|
|
|
* Name: Users
|
|
|
|
* Description: Track known users
|
|
|
|
*/
|
2013-01-13 16:26:44 +01:00
|
|
|
var web = require('./web'),
|
|
|
|
_ = require('underscore')._;
|
2013-01-07 13:27:24 +01:00
|
|
|
|
2012-12-23 17:19:01 +01:00
|
|
|
var users = function(dbot) {
|
|
|
|
var knownUsers = dbot.db.knownUsers;
|
2013-01-03 20:56:30 +01:00
|
|
|
var getServerUsers = function(server) {
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.has(knownUsers, server)) {
|
2013-01-05 20:08:17 +01:00
|
|
|
knownUsers[server] = { 'users': [], 'aliases': {}, 'channelUsers': {} };
|
2012-12-23 17:19:01 +01:00
|
|
|
}
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.has(knownUsers[server], 'channelUsers')) {
|
2013-01-09 07:12:04 +01:00
|
|
|
knownUsers[server].channelUsers = {};
|
|
|
|
}
|
2013-01-03 20:56:30 +01:00
|
|
|
return knownUsers[server];
|
2012-12-23 17:19:01 +01:00
|
|
|
};
|
|
|
|
|
2013-01-02 22:06:43 +01:00
|
|
|
var updateAliases = function(event, oldUser, newUser) {
|
2013-01-03 20:56:30 +01:00
|
|
|
var knownUsers = getServerUsers(event.server);
|
2013-01-13 16:26:44 +01:00
|
|
|
_.each(knownUsers.aliases, function(user, alias) {
|
|
|
|
if(user == oldUser) {
|
|
|
|
knownUsers.aliases[alias] = newUser;
|
2013-01-02 22:06:43 +01:00
|
|
|
}
|
2013-01-13 16:26:44 +01:00
|
|
|
}, this);
|
|
|
|
};
|
2013-01-02 22:06:43 +01:00
|
|
|
|
2013-01-05 20:08:17 +01:00
|
|
|
var updateChannels = function(event, oldUser, newUser) {
|
|
|
|
var channelUsers = getServerUsers(event.server).channelUsers;
|
2013-01-13 16:26:44 +01:00
|
|
|
channelUsers = _.each(channelUsers, function(channel, channelName) {
|
|
|
|
channelUsers[channelName] = _.without(channel, oldUser);
|
|
|
|
channelUsers[channelName].push(newUser);
|
|
|
|
}, this);
|
|
|
|
};
|
2013-01-05 20:08:17 +01:00
|
|
|
|
2012-12-23 17:19:01 +01:00
|
|
|
dbot.instance.addListener('366', 'users', function(event) {
|
2013-01-03 20:56:30 +01:00
|
|
|
var knownUsers = getServerUsers(event.server);
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.has(knownUsers.channelUsers, event.channel.name)) {
|
2013-01-05 20:08:17 +01:00
|
|
|
knownUsers.channelUsers[event.channel.name] = [];
|
|
|
|
}
|
|
|
|
var channelUsers = knownUsers.channelUsers[event.channel.name];
|
|
|
|
|
2013-01-13 16:26:44 +01:00
|
|
|
_.each(event.channel.nicks, function(nick) {
|
2013-01-05 20:08:17 +01:00
|
|
|
nick = nick.name;
|
|
|
|
if(api.isKnownUser(event.server, nick)) {
|
2013-01-08 07:14:58 +01:00
|
|
|
nick = api.resolveUser(event.server, nick);
|
2013-01-05 20:08:17 +01:00
|
|
|
} else {
|
2012-12-24 01:42:28 +01:00
|
|
|
knownUsers.users.push(nick);
|
2012-12-23 17:19:01 +01:00
|
|
|
}
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.include(channelUsers, nick)) {
|
2013-01-05 20:08:17 +01:00
|
|
|
channelUsers.push(nick);
|
|
|
|
}
|
2013-01-13 16:26:44 +01:00
|
|
|
}, this);
|
2012-12-23 17:19:01 +01:00
|
|
|
});
|
|
|
|
|
2012-12-24 06:47:47 +01:00
|
|
|
|
2013-01-03 20:56:30 +01:00
|
|
|
var api = {
|
2013-01-04 19:12:33 +01:00
|
|
|
'resolveUser': function(server, nick, useLowercase) {
|
2013-01-03 20:56:30 +01:00
|
|
|
var knownUsers = getServerUsers(server);
|
|
|
|
var user = nick;
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.include(knownUsers.users, nick) && _.has(knownUsers.aliases, nick)) {
|
2013-01-03 20:56:30 +01:00
|
|
|
user = knownUsers.aliases[nick];
|
|
|
|
}
|
|
|
|
|
2013-01-04 18:58:35 +01:00
|
|
|
if(useLowercase) user = user.toLowerCase();
|
2013-01-03 20:56:30 +01:00
|
|
|
return user;
|
2013-01-05 20:08:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'isKnownUser': function(server, nick) {
|
|
|
|
var knownUsers = getServerUsers(server);
|
2013-01-13 16:26:44 +01:00
|
|
|
return (_.include(knownUsers.users, nick) || _.has(knownUsers.aliases, nick));
|
2013-01-13 18:01:50 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'isPrimaryUser': function(server, nick) {
|
|
|
|
return _.include(knownUsers.users, nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
'getAliases': function(server, nick) {
|
|
|
|
var knownUsers = getServerUsers(server);
|
|
|
|
return _.chain(knownUsers.aliases)
|
|
|
|
.keys()
|
|
|
|
.filter(function(user) {
|
|
|
|
return knownUsers.aliases[user] == nick;
|
|
|
|
}, this)
|
|
|
|
.value();
|
2013-01-03 20:56:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-29 19:45:19 +01:00
|
|
|
var commands = {
|
|
|
|
'~alias': function(event) {
|
2013-01-13 16:26:44 +01:00
|
|
|
var knownUsers = getServerUsers(event.server),
|
|
|
|
alias = event.params[1].trim();
|
|
|
|
|
|
|
|
if(_.include(knownUsers.users, alias)) {
|
|
|
|
var aliasCount = _.reduce(knownUsers.aliases, function(memo, user) {
|
|
|
|
if(user == alias) return memo += 1;
|
|
|
|
}, 0, this);
|
|
|
|
|
|
|
|
event.reply(dbot.t('primary', {
|
|
|
|
'user': alias,
|
|
|
|
'count': aliasCount
|
|
|
|
}));
|
|
|
|
} else if(_.has(knownUsers.aliases, alias)) {
|
|
|
|
event.reply(dbot.t('alias', {
|
|
|
|
'alias': alias,
|
|
|
|
'user': knownUsers.aliases[alias]
|
|
|
|
}));
|
2012-12-29 19:45:19 +01:00
|
|
|
} else {
|
2012-12-29 21:07:14 +01:00
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': alias }));
|
2012-12-29 19:45:19 +01:00
|
|
|
}
|
|
|
|
},
|
2012-12-24 01:42:28 +01:00
|
|
|
|
2012-12-29 19:45:19 +01:00
|
|
|
'~setaliasparent': function(event) {
|
2013-01-12 10:49:41 +01:00
|
|
|
var knownUsers = getServerUsers(event.server);
|
|
|
|
var newParent = event.params[1];
|
2012-12-29 19:45:19 +01:00
|
|
|
|
2013-01-13 16:26:44 +01:00
|
|
|
if(_.has(knownUsers.aliases, newParent)) {
|
2013-01-12 10:49:41 +01:00
|
|
|
var newAlias = knownUsers.aliases[newParent];
|
2012-12-24 01:42:28 +01:00
|
|
|
|
2013-01-13 16:26:44 +01:00
|
|
|
// Replace user entry
|
|
|
|
knownUsers.users = _.without(knownUsers.users, newAlias);
|
2013-01-12 10:49:41 +01:00
|
|
|
knownUsers.users.push(newParent);
|
2012-12-29 19:45:19 +01:00
|
|
|
|
2013-01-12 10:49:41 +01:00
|
|
|
// Replace channels entries with new primary user
|
|
|
|
updateChannels(event, newAlias, newParent);
|
2013-01-05 20:08:17 +01:00
|
|
|
|
2013-01-12 10:49:41 +01:00
|
|
|
// Remove alias for new parent & add alias for new alias
|
|
|
|
delete knownUsers.aliases[newParent];
|
|
|
|
knownUsers.aliases[newAlias] = newParent;
|
2012-12-29 19:45:19 +01:00
|
|
|
|
2013-01-12 10:49:41 +01:00
|
|
|
// Update aliases to point to new primary user
|
|
|
|
updateAliases(event, newAlias, newParent);
|
2012-12-29 19:45:19 +01:00
|
|
|
|
2013-01-13 16:26:44 +01:00
|
|
|
event.reply(dbot.t('aliasparentset', {
|
|
|
|
'newParent': newParent,
|
|
|
|
'newAlias': newAlias
|
|
|
|
}));
|
2012-12-29 22:08:45 +01:00
|
|
|
|
2013-01-12 10:49:41 +01:00
|
|
|
dbot.api.stats.fixStats(event.server, newAlias);
|
|
|
|
} else {
|
2013-01-13 16:26:44 +01:00
|
|
|
event.reply(dbot.t('unknown_alias', { 'alias': newParent }));
|
2012-12-24 01:42:28 +01:00
|
|
|
}
|
|
|
|
},
|
2012-12-24 06:47:47 +01:00
|
|
|
|
2013-01-02 22:06:43 +01:00
|
|
|
'~mergeusers': function(event) {
|
2013-01-12 10:49:41 +01:00
|
|
|
var knownUsers = getServerUsers(event.server);
|
|
|
|
var primaryUser = event.params[1];
|
|
|
|
var secondaryUser = event.params[2];
|
|
|
|
|
2013-01-13 16:26:44 +01:00
|
|
|
if(_.include(knownUsers.users, primaryUser) && _.include(knownUsers.users, secondaryUser)) {
|
2013-01-13 16:27:45 +01:00
|
|
|
knownUsers.users = _.without(knownUsers.users, secondaryUser);
|
2013-01-12 10:49:41 +01:00
|
|
|
knownUsers.aliases[secondaryUser] = primaryUser;
|
|
|
|
updateAliases(event, secondaryUser, primaryUser);
|
|
|
|
updateChannels(event, secondaryUser, primaryUser);
|
|
|
|
|
|
|
|
event.reply(dbot.t('merged_users', {
|
|
|
|
'old_user': secondaryUser,
|
|
|
|
'new_user': primaryUser
|
|
|
|
}));
|
|
|
|
|
|
|
|
dbot.api.stats.fixStats(event.server, secondaryUser);
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('unprimary_error'));
|
|
|
|
}
|
|
|
|
}
|
2012-12-29 19:45:19 +01:00
|
|
|
};
|
|
|
|
|
2013-01-12 10:49:41 +01:00
|
|
|
commands['~setaliasparent'].access = 'moderator';
|
|
|
|
commands['~mergeusers'].access = 'moderator';
|
|
|
|
|
2012-12-29 19:45:19 +01:00
|
|
|
return {
|
|
|
|
'name': 'users',
|
|
|
|
'ignorable': false,
|
|
|
|
'commands': commands,
|
2013-01-03 20:56:30 +01:00
|
|
|
'api': api,
|
2013-01-07 13:27:24 +01:00
|
|
|
'pages': web.getPages(dbot),
|
2012-12-23 17:19:01 +01:00
|
|
|
|
|
|
|
'listener': function(event) {
|
2013-01-03 20:56:30 +01:00
|
|
|
var knownUsers = getServerUsers(event.server);
|
2013-01-05 20:08:17 +01:00
|
|
|
var nick = event.user;
|
|
|
|
|
2012-12-24 01:42:28 +01:00
|
|
|
if(event.action == 'JOIN') {
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.has(knownUsers.channelUsers, event.channel.name)) {
|
2013-01-05 20:08:17 +01:00
|
|
|
knownUsers.channelUsers[event.channel.name] = [];
|
|
|
|
}
|
|
|
|
var channelUsers = knownUsers.channelUsers[event.channel.name];
|
|
|
|
|
|
|
|
if(api.isKnownUser(event.server, nick)) {
|
2013-01-08 07:14:58 +01:00
|
|
|
nick = api.resolveUser(event.server, nick);
|
2013-01-05 20:08:17 +01:00
|
|
|
} else {
|
|
|
|
knownUsers.users.push(nick);
|
|
|
|
}
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.include(channelUsers, nick)) {
|
2013-01-05 20:08:17 +01:00
|
|
|
channelUsers.push(nick);
|
2012-12-24 01:42:28 +01:00
|
|
|
}
|
|
|
|
} else if(event.action == 'NICK') {
|
|
|
|
var newNick = event.params.substr(1);
|
2013-01-13 16:26:44 +01:00
|
|
|
if(_.has(knownUsers.aliases, event.user)) {
|
2012-12-31 00:24:05 +01:00
|
|
|
knownUsers.aliases[newNick] = knownUsers.aliases[event.user];
|
|
|
|
} else {
|
2013-01-13 16:26:44 +01:00
|
|
|
if(!_.include(knownUsers.users, newNick)) {
|
2012-12-31 00:24:05 +01:00
|
|
|
knownUsers.aliases[newNick] = event.user;
|
|
|
|
}
|
2012-12-29 16:01:03 +01:00
|
|
|
}
|
2012-12-23 17:19:01 +01:00
|
|
|
}
|
|
|
|
},
|
2012-12-24 01:42:28 +01:00
|
|
|
'on': ['JOIN', 'NICK'],
|
2012-12-23 17:46:33 +01:00
|
|
|
|
|
|
|
'onLoad': function() {
|
|
|
|
// Trigger updateNickLists to stat current users in channel
|
|
|
|
var connections = dbot.instance.connections;
|
2013-01-13 16:26:44 +01:00
|
|
|
_.each(connections, function(connection) {
|
|
|
|
connection.updateNickLists();
|
|
|
|
});
|
2012-12-23 17:46:33 +01:00
|
|
|
}
|
2012-12-23 17:19:01 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return users(dbot);
|
|
|
|
};
|