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
|
|
|
|
2013-04-14 22:00:53 +02:00
|
|
|
'createChannel': function(server, name, callback) {
|
|
|
|
var id = uuid.v4();
|
|
|
|
this.db.create('channel_users', id, {
|
|
|
|
'id': id,
|
|
|
|
'server': server,
|
|
|
|
'name': name,
|
|
|
|
'users': []
|
|
|
|
}, function(err, result) {
|
2013-03-24 13:54:19 +01:00
|
|
|
if(!err) {
|
2013-04-14 22:00:53 +02:00
|
|
|
dbot.api.event.emit('new_channel', [ result ]);
|
|
|
|
callback(result);
|
2013-03-24 13:54:19 +01:00
|
|
|
}
|
2013-04-14 22:00:53 +02:00
|
|
|
});
|
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'addChannelUser': function(user, channelName, callback) {
|
|
|
|
this.api.getChannel(user.server, channelName, function(channel) {
|
|
|
|
channel.users.push(user.id);
|
|
|
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
dbot.api.event.emit('new_channel_user', [ user, channel ]);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
}
|
|
|
|
});
|
2013-05-26 14:37:37 +02:00
|
|
|
}.bind(this),
|
|
|
|
|
|
|
|
'mergeChannelUsers': function(server, oldUser, newUser) {
|
|
|
|
newUser.channels = _.union(oldUser.channels, newUser.channels);
|
|
|
|
_.each(newUser.channels, function(name) {
|
|
|
|
this.api.getChannel(server, name, function(channel) {
|
|
|
|
if(_.include(channel.users, oldUser.id)) {
|
|
|
|
channel.users = _.without(channel.users, oldUser.id);
|
|
|
|
}
|
|
|
|
if(!_.include(channel.users, newUser.id)) {
|
|
|
|
channel.users.push(newUser.id);
|
|
|
|
}
|
|
|
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
|
|
|
if(err) {
|
|
|
|
// QQ
|
|
|
|
}
|
|
|
|
});
|
2013-05-26 15:11:09 +02:00
|
|
|
}.bind(this));
|
2013-05-26 14:37:37 +02:00
|
|
|
}, this);
|
2013-05-26 15:11:09 +02:00
|
|
|
}.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) {
|
2013-05-02 20:25:10 +02:00
|
|
|
var needsUpdating = false;
|
|
|
|
|
2013-03-24 13:26:33 +01:00
|
|
|
if(!user) { // User does not yet exist
|
2013-05-02 20:25:10 +02:00
|
|
|
user = {
|
|
|
|
'id': uuid.v4(),
|
|
|
|
'primaryNick': event.user,
|
|
|
|
'currentNick': event.user,
|
|
|
|
'server': event.server,
|
|
|
|
'channels': [],
|
|
|
|
'aliases': []
|
|
|
|
};
|
|
|
|
needsUpdating = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.include(user.channels, event.channel.name)) { // User not yet channel user
|
|
|
|
user.channels.push(event.channel.name);
|
|
|
|
this.internalAPI.addChannelUser(user, event.channel.name, function(err) { });
|
|
|
|
needsUpdating = true;
|
|
|
|
}
|
2013-03-25 14:55:47 +01:00
|
|
|
|
2013-05-02 20:25:10 +02:00
|
|
|
if(user.currentNick != event.user) {
|
2013-03-25 14:55:47 +01:00
|
|
|
user.currentNick = event.user;
|
2013-05-02 20:25:10 +02:00
|
|
|
needsUpdating = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(needsUpdating == true) {
|
|
|
|
this.db.save('users', user.id, user, function(err) { });
|
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-25 14:55:47 +01:00
|
|
|
this.api.resolveUser(event.server, event.user, function(user) {
|
|
|
|
this.api.isKnownUser(event.server, event.newNick, function(isKnown) {
|
|
|
|
user.currentNick = event.newNick;
|
|
|
|
|
|
|
|
if(!isKnown) {
|
2013-03-24 17:04:01 +01:00
|
|
|
user.aliases.push(event.newNick);
|
2013-03-25 14:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.db.save('users', user.id, user, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
dbot.api.event.emit('new_user_alias', [ user, event.newNick ]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2013-03-24 17:04:01 +01:00
|
|
|
}.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-04-14 22:00:53 +02:00
|
|
|
var checkChannelUsers = function(channel) {
|
2013-05-21 22:17:22 +02:00
|
|
|
var i = 0;
|
|
|
|
var checkChannelNicks = function(chanicks) {
|
|
|
|
if(i == chanicks.length) return;
|
|
|
|
var nick = chanicks[i]; i++;
|
2013-04-14 22:00:53 +02:00
|
|
|
this.api.resolveUser(event.server, nick, function(user) {
|
|
|
|
if(!user) {
|
|
|
|
this.internalAPI.createUser(event.server, nick, event.channel.name, function(result) {
|
2013-05-21 22:17:22 +02:00
|
|
|
this.internalAPI.addChannelUser(result, event.channel.name, function(err) {
|
|
|
|
checkChannelNicks(chanicks);
|
|
|
|
});
|
2013-04-14 22:00:53 +02:00
|
|
|
}.bind(this));
|
|
|
|
} else {
|
2013-04-21 16:17:29 +02:00
|
|
|
if(!_.include(user.channels, event.channel.name)) {
|
2013-05-21 22:17:22 +02:00
|
|
|
this.internalAPI.addChannelUser(user, event.channel.name, function(err) {
|
|
|
|
checkChannelNicks(chanicks);
|
|
|
|
});
|
2013-04-21 16:17:29 +02:00
|
|
|
user.channels.push(event.channel.name);
|
|
|
|
this.db.save('users', user.id, user, function(err) { });
|
2013-05-21 22:17:22 +02:00
|
|
|
} else {
|
|
|
|
checkChannelNicks(chanicks);
|
2013-03-24 17:04:01 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-14 22:00:53 +02:00
|
|
|
}.bind(this));
|
2013-05-21 22:17:22 +02:00
|
|
|
}.bind(this);
|
|
|
|
checkChannelNicks(_.keys(event.channel.nicks));
|
2013-04-14 22:00:53 +02:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
if(!channel) { // Channel does not yet exist
|
|
|
|
this.internalAPI.createChannel(event.server, event.channel.name, checkChannelUsers);
|
|
|
|
} else {
|
|
|
|
checkChannelUsers(channel);
|
|
|
|
}
|
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
|
|
|
};
|