2012-12-23 17:19:01 +01:00
|
|
|
/**
|
|
|
|
* Name: Users
|
|
|
|
* Description: Track known users
|
|
|
|
*/
|
|
|
|
var users = function(dbot) {
|
|
|
|
var knownUsers = dbot.db.knownUsers;
|
2012-12-23 18:11:56 +01:00
|
|
|
var getServerUsers = function(event) {
|
|
|
|
if(!knownUsers.hasOwnProperty(event.server)) {
|
2012-12-24 01:42:28 +01:00
|
|
|
knownUsers[event.server] = { 'users': [], 'aliases': {} };
|
2012-12-23 17:19:01 +01:00
|
|
|
}
|
2012-12-23 18:11:56 +01:00
|
|
|
return knownUsers[event.server];
|
2012-12-23 17:19:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
dbot.instance.addListener('366', 'users', function(event) {
|
2012-12-23 18:11:56 +01:00
|
|
|
var knownUsers = getServerUsers(event);
|
2012-12-23 17:19:01 +01:00
|
|
|
for(var nick in event.channel.nicks) {
|
2012-12-24 01:42:28 +01:00
|
|
|
if(!knownUsers.users.include(nick) && !knownUsers.aliases.hasOwnProperty(nick) &&
|
|
|
|
event.channel.nicks.hasOwnProperty(nick)) {
|
|
|
|
knownUsers.users.push(nick);
|
2012-12-23 17:19:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-12-29 19:45:19 +01:00
|
|
|
var commands = {
|
|
|
|
'~alias': function(event) {
|
|
|
|
var knownUsers = getServerUsers(event);
|
|
|
|
var alias = event.params[1].trim();
|
|
|
|
if(knownUsers.aliases.hasOwnProperty(alias)) {
|
|
|
|
event.reply(alias + ' is an alias of ' + knownUsers.aliases[alias]);
|
|
|
|
} else {
|
|
|
|
event.reply(alias + ' is not known as an alias to me.');
|
|
|
|
}
|
|
|
|
},
|
2012-12-24 01:42:28 +01:00
|
|
|
|
2012-12-29 19:45:19 +01:00
|
|
|
'~setaliasparent': function(event) {
|
|
|
|
if(dbot.config.admins.include(event.user)) {
|
2012-12-24 01:42:28 +01:00
|
|
|
var knownUsers = getServerUsers(event);
|
2012-12-29 19:45:19 +01:00
|
|
|
var newParent = event.params[1];
|
|
|
|
|
|
|
|
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[usersIndex] = newParent;
|
|
|
|
|
|
|
|
// Remove alias for new parent & add alias for new alias
|
|
|
|
delete knownUsers.aliases[newParent];
|
|
|
|
knownUsers.aliases[newAlias] = newParent;
|
|
|
|
|
|
|
|
// Update aliases to point to new primary user
|
|
|
|
for(var alias in knownUsers.aliases) {
|
|
|
|
if(knownUsers.aliases.hasOwnProperty(alias)) {
|
|
|
|
if(knownUsers.aliases[alias] === newAlias) {
|
|
|
|
knownUsers.aliases[alias] = newParent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.reply(newParent + ' is now the parent user, and ' +
|
|
|
|
newAlias + ' is an alias.');
|
2012-12-24 01:42:28 +01:00
|
|
|
} else {
|
2012-12-29 19:45:19 +01:00
|
|
|
event.reply('Given new parent does not currently exist as an alias.');
|
2012-12-24 01:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-29 19:45:19 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
'name': 'users',
|
|
|
|
'ignorable': false,
|
|
|
|
'commands': commands,
|
2012-12-23 17:19:01 +01:00
|
|
|
|
|
|
|
'listener': function(event) {
|
2012-12-23 18:11:56 +01:00
|
|
|
var knownUsers = getServerUsers(event);
|
2012-12-24 01:42:28 +01:00
|
|
|
if(event.action == 'JOIN') {
|
|
|
|
if(!knownUsers.users.include(event.user)) {
|
|
|
|
knownUsers.users.push(event.user);
|
|
|
|
}
|
|
|
|
} else if(event.action == 'NICK') {
|
|
|
|
var newNick = event.params.substr(1);
|
2012-12-29 16:01:03 +01:00
|
|
|
if(!knownUsers.users.include(newNick)) {
|
|
|
|
knownUsers.aliases[newNick] = event.user;
|
|
|
|
}
|
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;
|
|
|
|
for(var conn in connections) {
|
|
|
|
if(connections.hasOwnProperty(conn)) {
|
|
|
|
connections[conn].updateNickLists();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-23 17:19:01 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return users(dbot);
|
|
|
|
};
|