dbot/modules/users/users.js

207 lines
7.4 KiB
JavaScript
Raw Normal View History

/**
* Name: Users
* Description: Track known users
*/
2013-01-07 13:27:24 +01:00
var web = require('./web');
var users = function(dbot) {
var knownUsers = dbot.db.knownUsers;
var getServerUsers = function(server) {
if(!knownUsers.hasOwnProperty(server)) {
2013-01-05 20:08:17 +01:00
knownUsers[server] = { 'users': [], 'aliases': {}, 'channelUsers': {} };
}
2013-01-09 07:12:04 +01:00
if(!knownUsers[server].hasOwnProperty('channelUsers')) {
knownUsers[server].channelUsers = {};
}
return knownUsers[server];
};
2013-01-02 22:06:43 +01:00
var updateAliases = function(event, oldUser, newUser) {
var knownUsers = getServerUsers(event.server);
2013-01-02 22:06:43 +01:00
for(var alias in knownUsers.aliases) {
if(knownUsers.aliases.hasOwnProperty(alias)) {
if(knownUsers.aliases[alias] === oldUser) {
knownUsers.aliases[alias] = newUser;
}
}
}
}
2013-01-05 20:08:17 +01:00
var updateChannels = function(event, oldUser, newUser) {
var channelUsers = getServerUsers(event.server).channelUsers;
channelUsers.each(function(channel) {
if(channel.include(oldUser)) {
channel.splice(channel.indexOf(oldUser), 1);
channel.push(newUser);
}
}.bind(this));
}
dbot.instance.addListener('366', 'users', function(event) {
var knownUsers = getServerUsers(event.server);
2013-01-05 20:08:17 +01:00
if(!knownUsers.channelUsers.hasOwnProperty(event.channel.name)) {
knownUsers.channelUsers[event.channel.name] = [];
}
var channelUsers = knownUsers.channelUsers[event.channel.name];
event.channel.nicks.each(function(nick) {
nick = nick.name;
if(api.isKnownUser(event.server, nick)) {
nick = api.resolveUser(event.server, nick);
2013-01-05 20:08:17 +01:00
} else {
knownUsers.users.push(nick);
}
2013-01-05 20:08:17 +01:00
if(!channelUsers.include(nick)) {
channelUsers.push(nick);
}
}.bind(this));
});
2012-12-24 06:47:47 +01:00
var api = {
2013-01-04 19:12:33 +01:00
'resolveUser': function(server, nick, useLowercase) {
var knownUsers = getServerUsers(server);
var user = nick;
if(!knownUsers.users.include(nick) && knownUsers.aliases.hasOwnProperty(nick)) {
user = knownUsers.aliases[nick];
}
2013-01-04 18:58:35 +01:00
if(useLowercase) user = user.toLowerCase();
return user;
2013-01-05 20:08:17 +01:00
},
'isKnownUser': function(server, nick) {
var knownUsers = getServerUsers(server);
return (knownUsers.users.include(nick) || knownUsers.aliases.hasOwnProperty(nick));
}
};
2012-12-29 19:45:19 +01:00
var commands = {
'~alias': function(event) {
var knownUsers = getServerUsers(event.server);
2012-12-29 19:45:19 +01:00
var alias = event.params[1].trim();
2013-01-02 20:00:44 +01:00
if(knownUsers.users.include(alias)) {
var aliasCount = 0;
knownUsers.aliases.each(function(primaryUser) {
if(primaryUser == alias) aliasCount += 1;
}.bind(this));
event.reply(dbot.t('primary', { 'user': alias, 'count': aliasCount }));
} else if(knownUsers.aliases.hasOwnProperty(alias)) {
2012-12-29 21:07:14 +01:00
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-29 19:45:19 +01:00
'~setaliasparent': function(event) {
var knownUsers = getServerUsers(event.server);
var newParent = event.params[1];
2012-12-29 19:45:19 +01:00
if(knownUsers.aliases.hasOwnProperty(newParent)) {
var newAlias = knownUsers.aliases[newParent];
// Replace users entry with new primary user
var usersIndex = knownUsers.users.indexOf(newAlias);
knownUsers.users.splice(usersIndex, 1);
knownUsers.users.push(newParent);
2012-12-29 19:45:19 +01:00
// Replace channels entries with new primary user
updateChannels(event, newAlias, newParent);
2013-01-05 20:08:17 +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
// Update aliases to point to new primary user
updateAliases(event, newAlias, newParent);
2012-12-29 19:45:19 +01:00
event.reply(dbot.t('aliasparentset', { 'newParent': newParent,
'newAlias': newAlias }));
2012-12-29 22:08:45 +01:00
dbot.api.stats.fixStats(event.server, newAlias);
} else {
event.reply(dbot.t('unknown_alias', { 'alias': newParent}));
}
},
2012-12-24 06:47:47 +01:00
2013-01-02 22:06:43 +01:00
'~mergeusers': function(event) {
var knownUsers = getServerUsers(event.server);
var primaryUser = event.params[1];
var secondaryUser = event.params[2];
if(knownUsers.users.include(primaryUser) && knownUsers.users.include(secondaryUser)) {
knownUsers.users.splice(knownUsers.users.indexOf(secondaryUser), 1);
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
};
commands['~setaliasparent'].access = 'moderator';
commands['~mergeusers'].access = 'moderator';
2012-12-29 19:45:19 +01:00
return {
'name': 'users',
'ignorable': false,
'commands': commands,
'api': api,
2013-01-07 13:27:24 +01:00
'pages': web.getPages(dbot),
'listener': function(event) {
var knownUsers = getServerUsers(event.server);
2013-01-05 20:08:17 +01:00
var nick = event.user;
if(event.action == 'JOIN') {
2013-01-05 20:08:17 +01:00
if(!knownUsers.channelUsers.hasOwnProperty(event.channel.name)) {
knownUsers.channelUsers[event.channel.name] = [];
}
var channelUsers = knownUsers.channelUsers[event.channel.name];
if(api.isKnownUser(event.server, nick)) {
nick = api.resolveUser(event.server, nick);
2013-01-05 20:08:17 +01:00
} else {
knownUsers.users.push(nick);
}
if(!channelUsers.include(nick)) {
channelUsers.push(nick);
}
} else if(event.action == 'NICK') {
var newNick = event.params.substr(1);
2012-12-31 00:24:05 +01:00
if(knownUsers.aliases.hasOwnProperty(event.user)) {
knownUsers.aliases[newNick] = knownUsers.aliases[event.user];
} else {
if(!knownUsers.users.include(newNick)) {
knownUsers.aliases[newNick] = event.user;
}
2012-12-29 16:01:03 +01:00
}
}
},
'on': ['JOIN', 'NICK'],
'onLoad': function() {
// Trigger updateNickLists to stat current users in channel
var connections = dbot.instance.connections;
for(var conn in connections) {
if(connections.hasOwnProperty(conn)) {
connections[conn].updateNickLists();
}
}
}
};
};
exports.fetch = function(dbot) {
return users(dbot);
};