dbot/modules/users/users.js

147 lines
4.8 KiB
JavaScript
Raw Normal View History

/**
* Name: Users
* Description: Track known users
*/
2014-09-04 11:36:57 +02:00
var _ = require('underscore')._;
2013-01-07 13:27:24 +01:00
var users = function(dbot) {
/*** Internal API ***/
2014-09-04 11:36:57 +02:00
this.internalAPI = {
2014-09-04 11:36:57 +02:00
// Create new user record
2013-06-22 18:32:18 +02:00
'createUser': function(server, nick, callback) {
2014-09-04 11:36:57 +02:00
var id = nick + '.' + server;
this.db.create('users', id, {
2013-06-22 22:23:47 +02:00
'id': id,
'server': server,
2014-09-04 11:36:57 +02:00
'primaryNick': nick,
'currentNick': nick
}, function(err, result) {
if(!err) {
dbot.api.event.emit('new_user', [ result ]);
2014-09-04 11:36:57 +02:00
callback(null, result);
} else {
callback(true, null);
}
});
2014-09-04 11:36:57 +02:00
},
2014-09-04 11:36:57 +02:00
// Add new user alias
'createAlias': function(alias, user, callback) {
var id = alias + '.' + user.server;
this.db.create('user_aliases', id, {
2013-06-22 22:23:47 +02:00
'id': id,
2014-09-04 11:36:57 +02:00
'alias': alias,
'user': user.id
}, function(err, result) {
if(!err) {
2014-09-04 11:36:57 +02:00
dbot.api.event.emit('new_user_alias', [ event.rUser, event.newNick ]);
callback(null, result);
} else {
callback(true, null);
}
});
2014-09-04 11:36:57 +02:00
},
2014-09-04 11:36:57 +02:00
// Remove an alias record
'removeAlias': function(server, alias) {
var id = alias + '.' + server;
this.db.del('user_aliases', id, function(err) {
callback(err);
});
},
// Update current nick of user record
'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);
}
});
},
2013-06-22 18:25:31 +02:00
2014-09-04 11:36:57 +02:00
// Merge two user records and aliases
'mergeUsers': function(oldUser, newUser, callback) {
this.db.search('user_aliases', { 'user': oldUser.id }, function(alias) {
alias.user = newUser.id;
this.db.save('user_aliases', alias.id, alias, function(){});
}.bind(this), function(){});
2013-07-14 15:25:11 +02:00
2014-09-04 11:36:57 +02:00
this.db.del('users', oldUser.id, function(err) {
if(!err) {
dbot.api.event.emit('merged_users', [
oldUser,
newUser
]);
callback(null);
} else {
callback(true);
}
});
},
2013-07-14 15:25:11 +02:00
2014-09-04 11:36:57 +02:00
// Set a new nick as the parent for a user (so just recreate and merge)
'reparentUser': function(user, newPrimary, callback) {
this.internalAPI.createUser(user.server, newPrimary, function(err, newUser) {
this.internalAPI.mergeUsers(user, newUser, function(err) {
2013-06-22 18:25:31 +02:00
callback(err);
});
2013-06-22 22:23:47 +02:00
}.bind(this));
2014-09-04 11:36:57 +02:00
}
};
2014-09-04 11:36:57 +02:00
/*** Listener ***/
// Track nick changes
this.listener = function(event) {
2014-09-04 11:36:57 +02:00
// Update current nick
this.internalAPI.updateCurrentNick(event.rUser, event.newNick, function(){});
2014-09-04 11:36:57 +02:00
// 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'];
2014-09-04 11:36:57 +02:00
/*** Pre-emit ***/
2014-09-04 11:36:57 +02:00
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);
2014-01-04 18:59:14 +01:00
} else {
2014-09-04 11:36:57 +02:00
if(user.currentNick !== event.user) {
this.internalAPI.updateCurrentNick(user, event.user, done);
} else {
2014-09-04 11:36:57 +02:00
done(null, user);
}
}
2014-09-04 11:36:57 +02:00
}.bind(this));
};
2013-08-19 19:11:07 +02:00
2014-09-04 11:36:57 +02:00
dbot.instance.addPreEmitHook(function(event, callback) {
if(event.user && _.include(['JOIN', 'PRIVMSG'], event.action)) {
checkUser(function(err, user) {
event.rUser = user;
callback(null);
});
2013-12-31 17:06:19 +01:00
} else {
2014-09-04 11:36:57 +02:00
callback(null);
2013-06-22 22:23:47 +02:00
}
2014-09-04 11:36:57 +02:00
});
}.bind(this);
};
exports.fetch = function(dbot) {
return new users(dbot);
};