3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 04:02:39 +01:00
dbot/modules/users/users.js

155 lines
5.2 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 12:15:52 +02:00
}.bind(this),
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) {
dbot.api.event.emit('new_user_alias', [ user, alias ]);
2014-09-04 11:36:57 +02:00
callback(null, result);
} else {
callback(true, null);
}
});
2014-09-04 12:15:52 +02:00
}.bind(this),
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);
});
2014-09-04 12:15:52 +02:00
}.bind(this),
2014-09-04 11:36:57 +02:00
// 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);
}
});
2014-09-04 12:15:52 +02:00
}.bind(this),
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) {
if(alias.alias === newUser.primaryNick) {
this.db.del('user_aliases', alias.id, function(){});
} else {
alias.user = newUser.id;
this.db.save('user_aliases', alias.id, alias, function(){});
}
}.bind(this), function(){
this.internalAPI.createAlias(oldUser.primaryNick, newUser, function(){});
}.bind(this));
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);
}
});
2014-09-04 12:15:52 +02:00
}.bind(this),
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 12:15:52 +02:00
}.bind(this)
};
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.api.resolveUser(event.server, event.user, function(err, user) {
2014-09-05 12:42:30 +02:00
if(user) {
this.internalAPI.updateCurrentNick(user, event.newNick, function(){});
this.api.resolveUser(event.server, event.newNick, function(err, eUser) {
if(!eUser) {
this.internalAPI.createAlias(event.newNick, user, function(){});
}
}.bind(this));
}
}.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
2014-09-04 12:15:52 +02:00
var checkUser = function(event, done) {
2014-09-04 11:36:57 +02:00
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));
2014-09-04 12:15:52 +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)) {
2014-09-04 12:15:52 +02:00
checkUser(event, function(err, user) {
2014-09-04 11:36:57 +02:00
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);
};