2012-12-23 17:19:01 +01:00
|
|
|
/**
|
|
|
|
* Name: Users
|
|
|
|
* Description: Track known users
|
|
|
|
*/
|
2013-01-14 22:03:47 +01:00
|
|
|
var _ = require('underscore')._;
|
2013-01-07 13:27:24 +01:00
|
|
|
|
2012-12-23 17:19:01 +01:00
|
|
|
var users = function(dbot) {
|
2013-01-14 22:03:47 +01:00
|
|
|
this.knownUsers = dbot.db.knownUsers;
|
|
|
|
this.getServerUsers = function(server) {
|
|
|
|
var knownUsers = this.knownUsers;
|
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-14 22:03:47 +01:00
|
|
|
this.updateAliases = function(event, oldUser, newUser) {
|
|
|
|
var knownUsers = this.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-14 22:03:47 +01:00
|
|
|
this.updateChannels = function(event, oldUser, newUser) {
|
|
|
|
var channelUsers = this.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-14 22:03:47 +01:00
|
|
|
|
|
|
|
this.listener = function(event) {
|
|
|
|
var knownUsers = this.getServerUsers(event.server);
|
|
|
|
var nick = event.user;
|
|
|
|
|
2013-03-21 09:38:16 +01:00
|
|
|
if(event.action == 'JOIN' && nick != dbot.config.name) {
|
2013-01-14 22:03:47 +01:00
|
|
|
if(!_.has(knownUsers.channelUsers, event.channel.name)) {
|
|
|
|
knownUsers.channelUsers[event.channel.name] = [];
|
|
|
|
}
|
|
|
|
var channelUsers = knownUsers.channelUsers[event.channel.name];
|
2013-01-05 20:08:17 +01:00
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
if(this.api.isKnownUser(event.server, nick)) {
|
|
|
|
nick = this.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);
|
2013-01-29 00:40:32 +01:00
|
|
|
dbot.api.event.emit('new_user', [ event.server, nick ]);
|
2012-12-23 17:19:01 +01:00
|
|
|
}
|
2013-01-20 20:04:12 +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-14 22:03:47 +01:00
|
|
|
} else if(event.action == 'NICK') {
|
2013-03-21 20:58:33 +01:00
|
|
|
var newNick = event.newNick;
|
2013-01-20 19:44:21 +01:00
|
|
|
if(!this.api.isKnownUser(newNick)) {
|
|
|
|
knownUsers.aliases[newNick] = this.api.resolveUser(event.server, event.user);
|
2013-01-24 19:55:28 +01:00
|
|
|
dbot.api.event.emit('nick_change', [ event.server, newNick ]);
|
2012-12-24 01:42:28 +01:00
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
this.on = ['JOIN', 'NICK'];
|
|
|
|
|
|
|
|
this.onLoad = function() {
|
|
|
|
// Trigger updateNickLists to stat current users in channel
|
|
|
|
dbot.instance.addListener('366', 'users', function(event) {
|
|
|
|
var knownUsers = this.getServerUsers(event.server);
|
|
|
|
if(!_.has(knownUsers.channelUsers, event.channel.name)) {
|
|
|
|
knownUsers.channelUsers[event.channel.name] = [];
|
2013-01-12 10:49:41 +01:00
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
var channelUsers = knownUsers.channelUsers[event.channel.name];
|
2013-01-05 20:08:17 +01:00
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
_.each(event.channel.nicks, function(nick) {
|
|
|
|
nick = nick.name;
|
|
|
|
if(this.api.isKnownUser(event.server, nick)) {
|
|
|
|
nick = this.api.resolveUser(event.server, nick);
|
2013-01-05 20:08:17 +01:00
|
|
|
} else {
|
|
|
|
knownUsers.users.push(nick);
|
2013-02-03 17:49:10 +01:00
|
|
|
dbot.api.event.emit('new_user', [ event.server, nick ]);
|
2013-01-05 20:08:17 +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);
|
2012-12-24 01:42:28 +01:00
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
}, this);
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
var connections = dbot.instance.connections;
|
|
|
|
_.each(connections, function(connection) {
|
|
|
|
connection.updateNickLists();
|
|
|
|
});
|
2012-12-23 17:19:01 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-14 22:03:47 +01:00
|
|
|
return new users(dbot);
|
2012-12-23 17:19:01 +01:00
|
|
|
};
|