2012-12-23 17:19:01 +01:00
|
|
|
/**
|
|
|
|
* Name: Users
|
|
|
|
* Description: Track known users
|
|
|
|
*/
|
2013-03-24 17:04:01 +01:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
uuid = require('node-uuid');
|
2013-01-07 13:27:24 +01:00
|
|
|
|
2012-12-23 17:19:01 +01:00
|
|
|
var users = function(dbot) {
|
2013-03-24 13:54:19 +01:00
|
|
|
|
2013-03-24 17:04:01 +01:00
|
|
|
/*** Internal API ***/
|
2013-03-24 13:54:19 +01:00
|
|
|
this.internalAPI = {
|
|
|
|
'createUser': function(server, nick, channel, callback) {
|
|
|
|
var id = uuid.v4();
|
|
|
|
this.db.create('users', id, {
|
2013-03-24 19:05:57 +01:00
|
|
|
'id': id,
|
2013-03-24 13:54:19 +01:00
|
|
|
'primaryNick': nick,
|
|
|
|
'currentNick': nick,
|
|
|
|
'server': server,
|
|
|
|
'channels': [ channel ],
|
|
|
|
'aliases': []
|
|
|
|
}, function(err, result) {
|
|
|
|
if(!err) {
|
2013-03-24 17:04:01 +01:00
|
|
|
dbot.api.event.emit('new_user', [ result ]);
|
2013-03-24 13:54:19 +01:00
|
|
|
callback(result);
|
|
|
|
}
|
|
|
|
});
|
2013-03-24 17:04:01 +01:00
|
|
|
}.bind(this),
|
2013-03-24 13:54:19 +01:00
|
|
|
|
|
|
|
'addChannelUser': function(user, channelName) {
|
|
|
|
user.channels.push(channelName);
|
|
|
|
this.db.save('users', user.id, user, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
this.api.getChannel(user.server, channelName, function(channel) {
|
|
|
|
channel.users.push(user.primaryNick);
|
|
|
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
dbot.api.event.emit('new_channel_user', [ user, channel]);
|
|
|
|
}
|
|
|
|
});
|
2013-03-24 19:05:57 +01:00
|
|
|
}.bind(this));
|
2013-03-24 13:54:19 +01:00
|
|
|
}
|
2013-03-24 17:59:26 +01:00
|
|
|
}.bind(this));
|
2013-03-24 17:04:01 +01:00
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'updateChannelPrimaryUser': function(server, oldUser, newUser) {
|
|
|
|
this.db.search('channel_users', { 'server': server }, function(channel) {
|
|
|
|
channel.users = _.without(channel.users, oldUser);
|
|
|
|
if(!_.include(channel.users, newUser)) channel.users.push(newUser);
|
|
|
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
|
|
|
if(err) {
|
|
|
|
// QQ
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this), function(err) {
|
|
|
|
if(err) {
|
|
|
|
// QQ
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this)
|
2013-03-24 13:54:19 +01:00
|
|
|
};
|
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
this.listener = function(event) {
|
2013-03-24 17:04:01 +01:00
|
|
|
if(event.action == 'JOIN' && event.user != dbot.config.name) {
|
2013-03-24 13:26:33 +01:00
|
|
|
this.api.resolveUser(event.server, event.user, function(user) {
|
|
|
|
if(!user) { // User does not yet exist
|
2013-03-24 13:54:19 +01:00
|
|
|
this.internalAPI.createUser(event.server, event.user, event.channel, function(result) {
|
|
|
|
user = result;
|
2013-03-24 17:04:01 +01:00
|
|
|
if(!_.include(user.channels, event.channel)) { // User not yet channel user
|
|
|
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
|
|
|
}
|
2013-03-24 13:26:33 +01:00
|
|
|
});
|
2013-03-24 17:04:01 +01:00
|
|
|
} else {
|
|
|
|
if(!_.include(user.channels, event.channel)) { // User not yet channel user
|
|
|
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
|
|
|
}
|
2013-03-24 13:26:33 +01:00
|
|
|
}
|
2013-03-24 17:04:01 +01:00
|
|
|
}.bind(this));
|
2013-01-14 22:03:47 +01:00
|
|
|
} else if(event.action == 'NICK') {
|
2013-03-24 17:04:01 +01:00
|
|
|
this.api.isKnownUser(event.server, event.newNick, function(isKnown) {
|
|
|
|
if(!isKnown) {
|
|
|
|
this.api.resolveUser(event.server, event.user, function(user) {
|
|
|
|
user.aliases.push(event.newNick);
|
|
|
|
this.db.save('users', user.id, user, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
dbot.api.event.emit('new_user_alias', [ user, event.newNick ]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-03-24 13:26:33 +01:00
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
}.bind(this);
|
|
|
|
this.on = ['JOIN', 'NICK'];
|
2013-03-24 13:54:19 +01:00
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
this.onLoad = function() {
|
|
|
|
dbot.instance.addListener('366', 'users', function(event) {
|
2013-03-24 17:04:01 +01:00
|
|
|
this.api.getChannel(event.server, event.channel.name, function(channel) {
|
2013-03-24 13:54:19 +01:00
|
|
|
if(!channel) { // Channel does not yet exist
|
|
|
|
var id = uuid.v4();
|
|
|
|
this.db.create('channel_users', id, {
|
2013-03-24 19:05:57 +01:00
|
|
|
'id': id,
|
2013-03-24 13:54:19 +01:00
|
|
|
'server': event.server,
|
2013-03-24 17:04:01 +01:00
|
|
|
'name': event.channel.name,
|
2013-03-24 13:54:19 +01:00
|
|
|
'users': []
|
|
|
|
}, function(err, result) {
|
|
|
|
if(!err) {
|
|
|
|
channel = result;
|
|
|
|
dbot.api.event.emit('new_channel', [ channel ]);
|
|
|
|
}
|
|
|
|
});
|
2012-12-24 01:42:28 +01:00
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
|
2013-03-24 13:54:19 +01:00
|
|
|
_.each(event.channel.nicks, function(nick) {
|
2013-03-24 17:04:01 +01:00
|
|
|
var nick = nick.name;
|
2013-03-24 13:54:19 +01:00
|
|
|
this.api.resolveUser(event.server, nick, function(user) {
|
|
|
|
if(!user) {
|
|
|
|
this.internalAPI.createUser(event.server, nick, event.channel, function(result) {
|
|
|
|
user = result;
|
2013-03-24 17:04:01 +01:00
|
|
|
if(!_.include(user.channels, event.channel)) {
|
|
|
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
|
|
|
}
|
2013-03-24 13:54:19 +01:00
|
|
|
});
|
2013-03-24 17:04:01 +01:00
|
|
|
} else {
|
|
|
|
if(!_.include(user.channels, event.channel)) {
|
|
|
|
this.internalAPI.addChannelUser(user, event.channel.name);
|
|
|
|
}
|
2013-03-24 13:54:19 +01:00
|
|
|
}
|
2013-03-24 17:04:01 +01:00
|
|
|
}.bind(this));
|
2013-03-24 13:54:19 +01:00
|
|
|
}, this);
|
2013-03-24 17:04:01 +01:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2013-03-24 13:54:19 +01:00
|
|
|
|
2013-03-24 17:04:01 +01:00
|
|
|
var connections = dbot.instance.connections;
|
|
|
|
_.each(connections, function(connection) {
|
|
|
|
connection.updateNickLists();
|
2013-03-24 13:54:19 +01:00
|
|
|
});
|
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
|
|
|
};
|