diff --git a/modules/users/newapi.js b/modules/users/newapi.js new file mode 100644 index 0000000..86f48be --- /dev/null +++ b/modules/users/newapi.js @@ -0,0 +1,65 @@ +var _ = require('underscore')._; + +var api = function(dbot) { + this.api = { + // Retrieve a user record given a server and nickname + 'resolveUser': function(server, nick, callback) { + var id = nick + '.' + server; + this.api.getUser(id, function(err, result) { + if(!err) { + callback(null, result); + } else { + this.db.read('user_aliases', id, function(err, result) { + if(!err) { + this.api.getUser(result.user, callback); + } else { + callback(true, null); + } + }.bind(this)); + } + }.bind(this)); + }, + + // Retrive a user record given its ID + 'getUser': function(id, callback) { + this.db.read('users', id, function(err, result) { + if(!err) { + callback(null, result); + } else { + callback(true, null); + } + }); + }, + + // Retrieve user aliases given a user ID + 'getUserAliases': function(id, callback) { + var aliases = []; + this.db.search('user_aliases', { 'user': id }, function(result) { + aliases.push(result.alias); + }, function(err) { + if(!err) { + callback(null, aliases); + } else { + callback(true, null); + } + }); + }, + + // Retrieve a channel record given a server and a channel name + 'resolveChannel': function(server, channel, callback) { + var id = channel + '.' + server; + this.api.getChannel(id, callback); + }, + + // Retrieve a channel record given its ID + 'getChannel': function(id, callback) { + this.db.read('channels', id, function(err, result) { + if(!err) { + callback(null, result); + } else { + callback(true, null); + } + }); + } + }; +}; diff --git a/modules/users/newcommands.js b/modules/users/newcommands.js new file mode 100644 index 0000000..b4ca3da --- /dev/null +++ b/modules/users/newcommands.js @@ -0,0 +1,66 @@ +var _ = require('underscore')._; + +var commands = function(dbot) { + this.commands = { + '~alias': function(event) { + var nick = event.input[1] || event.user; + this.api.resolveUser(event.server, nick, function(err, user) { + if(user) { + this.api.getUserAliases(user.id, function(err, aliases) { + var including = _.first(aliases, 10).join(', '); + + if(nick === user.primaryNick) { + if(aliases.length === 0) { + event.reply(dbot.t('primary_no_alias', { + 'user': user.primaryNick, + 'currentNick': user.currentNick + })); + } else { + event.reply(dbot.t('primary', { + 'user': user.primaryNick, + 'currentNick': user.currentNick, + 'count': aliases.length, + 'including': including + })); + } + } else { + event.reply(dbot.t('alias', { + 'alias': nick, + 'user': user.primaryNick + })); + } + }); + } else { + event.reply(dbot.t('unknown_alias', { 'alias': nick })); + } + }.bind(this)); + }, + + '~addalias': function(event) { + var nick = event.input[1], + alias = event.input[2]; + + this.api.resolveUser(event.server, nick, function(err, user) { + if(user) { + this.api.resolveUser(event.server, alias, function(err, aUser) { + if(!aUser) { + this.internalAPI.createAlias(alias, user, function(err) { + event.reply(dbot.t('alias_added', { + 'user': user.primaryNick, + 'alias': alias + })); + }); + } else { + event.reply(dbot.t('alias_exists', { + 'alias': alias, + 'user': aUser.primaryNick + })); + } + }); + } else { + event.reply(dbot.t('unknown_alias', { 'alias': nick })); + } + }.bind(this)); + } + }; +}; diff --git a/modules/users/newusers.js b/modules/users/newusers.js new file mode 100644 index 0000000..03603f1 --- /dev/null +++ b/modules/users/newusers.js @@ -0,0 +1,94 @@ +/** + * Name: Users + * Description: Track known users + */ +var _ = require('underscore')._; + +var users = function(dbot) { + /*** Internal API ***/ + + this.internalAPI = { + // Create new user record + 'createUser': function(server, nick, callback) { + var id = nick + '.' + server; + this.db.create('users', id, { + 'id': id, + 'server': server, + 'primaryNick': nick, + 'currentNick': nick + }, function(err, result) { + if(!err) { + dbot.api.event.emit('new_user', [ result ]); + callback(null, result); + } else { + callback(true, null); + } + }); + }, + + // Add new user alias + 'createAlias': function(alias, user, callback) { + var id = alias + '.' + user.server; + this.db.create('user_aliases', id, { + 'id': id, + 'alias': alias, + 'user': user.id + }, function(err, result) { + if(!err) { + dbot.api.event.emit('new_user_alias', [ event.rUser, event.newNick ]); + callback(null, result); + } else { + callback(true, null); + } + }); + }, + + 'updateCurrentNick': function(user, newNick, callback) { + user.currentNick = newNick; + this.db.save('users', user.id, user, function(err, result) { + if(!err) { + dbot.api.event.emit('new_current_nick', [ user, newNick ]); + callback(null, result); + } else { + callback(true, null); + } + }); + } + }; + + /*** Listener ***/ + + // Track nick changes + this.listener = function(event) { + // Update current nick + this.internalAPI.updateCurrentNick(event.rUser, event.newNick, function(){}); + + // Add new alias record if nick is not already claimed + this.api.resolveUser(event.server, event.newNick, function(err, user) { + if(!user) { + this.internalAPI.createAlias(event.newNick, event.rUser, function(){}); + } + }.bind(this)); + }.bind(this); + this.on = ['NICK']; + + /*** Pre-emit ***/ + this.onLoad = function() { + // Create non-existing users and update current nicks + var checkUser = function(done) { + this.api.resolveUser(event.server, event.user, function(err, user) { + if(!user) { + this.internalAPI.createUser(event.server, event.user, done); + } else { + this.internalAPI.updateCurrentNick(user, event.user, done); + } + }.bind(this)); + }; + + dbot.instance.addPreEmitHook(function(event, callback) { + if(event.user && _.include(['JOIN', 'PRIVMSG'], event.action)) { + checkUser(callback); + } + }); + }; +};