2012-12-23 17:19:01 +01:00
|
|
|
/**
|
|
|
|
* Name: Users
|
|
|
|
* Description: Track known users
|
|
|
|
*/
|
2015-07-18 01:38:17 +02:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
moment = require('moment');
|
2013-01-07 13:27:24 +01:00
|
|
|
|
2012-12-23 17:19:01 +01:00
|
|
|
var users = function(dbot) {
|
2013-03-24 17:04:01 +01:00
|
|
|
/*** Internal API ***/
|
2014-09-04 11:36:57 +02:00
|
|
|
|
2013-03-24 13:54:19 +01: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;
|
2013-03-24 13:54:19 +01:00
|
|
|
this.db.create('users', id, {
|
2013-06-22 22:23:47 +02:00
|
|
|
'id': id,
|
2013-03-24 13:54:19 +01:00
|
|
|
'server': server,
|
2014-09-04 11:36:57 +02:00
|
|
|
'primaryNick': nick,
|
|
|
|
'currentNick': nick
|
2013-03-24 13:54:19 +01:00
|
|
|
}, function(err, result) {
|
|
|
|
if(!err) {
|
2013-03-24 17:04:01 +01:00
|
|
|
dbot.api.event.emit('new_user', [ result ]);
|
2014-09-04 11:36:57 +02:00
|
|
|
callback(null, result);
|
|
|
|
} else {
|
|
|
|
callback(true, null);
|
2013-03-24 13:54:19 +01:00
|
|
|
}
|
|
|
|
});
|
2014-09-04 12:15:52 +02:00
|
|
|
}.bind(this),
|
2013-03-24 13:54:19 +01: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
|
2013-04-14 22:00:53 +02:00
|
|
|
}, function(err, result) {
|
2013-03-24 13:54:19 +01:00
|
|
|
if(!err) {
|
2014-10-21 17:08:26 +02:00
|
|
|
dbot.api.event.emit('new_user_alias', [ result, alias ]);
|
2014-09-04 11:36:57 +02:00
|
|
|
callback(null, result);
|
|
|
|
} else {
|
|
|
|
callback(true, null);
|
2013-03-24 13:54:19 +01:00
|
|
|
}
|
2013-04-14 22:00:53 +02:00
|
|
|
});
|
2014-09-04 12:15:52 +02:00
|
|
|
}.bind(this),
|
2013-04-14 22:00:53 +02:00
|
|
|
|
2014-09-04 11:36:57 +02:00
|
|
|
// Remove an alias record
|
2014-09-08 12:42:59 +02:00
|
|
|
'removeAlias': function(server, alias, callback) {
|
2014-09-04 11:36:57 +02:00
|
|
|
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) {
|
2014-09-04 14:15:11 +02:00
|
|
|
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-04-14 22:00:53 +02:00
|
|
|
});
|
2013-06-22 22:23:47 +02:00
|
|
|
}.bind(this));
|
2014-09-04 12:15:52 +02:00
|
|
|
}.bind(this)
|
2013-03-24 13:54:19 +01:00
|
|
|
};
|
|
|
|
|
2014-09-04 11:36:57 +02:00
|
|
|
/*** Listener ***/
|
|
|
|
|
|
|
|
// Track nick changes
|
2013-01-14 22:03:47 +01:00
|
|
|
this.listener = function(event) {
|
2014-09-04 11:36:57 +02:00
|
|
|
// Update current nick
|
2014-09-04 14:15:11 +02:00
|
|
|
this.api.resolveUser(event.server, event.user, function(err, user) {
|
2014-09-05 12:42:30 +02:00
|
|
|
if(user) {
|
|
|
|
this.api.resolveUser(event.server, event.newNick, function(err, eUser) {
|
|
|
|
if(!eUser) {
|
|
|
|
this.internalAPI.createAlias(event.newNick, user, function(){});
|
2014-09-09 11:52:45 +02:00
|
|
|
this.internalAPI.updateCurrentNick(user, event.newNick, function(){});
|
|
|
|
} else if(user.id === eUser.id) {
|
|
|
|
this.internalAPI.updateCurrentNick(user, event.newNick, function(){});
|
2014-09-05 12:42:30 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2013-08-19 19:32:19 +02:00
|
|
|
}.bind(this));
|
2013-01-14 22:03:47 +01:00
|
|
|
}.bind(this);
|
2013-08-19 19:32:19 +02:00
|
|
|
this.on = ['NICK'];
|
2013-03-24 13:54:19 +01:00
|
|
|
|
2014-09-04 11:36:57 +02:00
|
|
|
/*** Pre-emit ***/
|
2013-08-18 21:36:53 +02:00
|
|
|
|
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);
|
2013-08-18 21:36:53 +02:00
|
|
|
} else {
|
2014-09-04 11:36:57 +02:00
|
|
|
done(null, user);
|
2013-08-25 17:37:32 +02:00
|
|
|
}
|
2013-08-18 21:36:53 +02:00
|
|
|
}
|
2014-09-04 11:36:57 +02:00
|
|
|
}.bind(this));
|
2014-09-04 12:15:52 +02:00
|
|
|
}.bind(this);
|
2015-07-18 01:37:24 +02:00
|
|
|
var checkUserTime = function(user, done) {
|
2015-07-18 01:39:19 +02:00
|
|
|
if(!_.has(user, 'lastUsed') || (_.has(user, 'lastUsed') && moment(user.lastUsed).diff(moment(user.lastUsed).add(1, 'day'), 'days') > 0)) {
|
2015-07-18 01:37:24 +02:00
|
|
|
dbot.api.log.log('tripsit', 'tripbot', '[USERDEBUG] Updating last used date for ' + user.primaryNick);
|
|
|
|
user.lastUsed = moment().unix();
|
|
|
|
this.db.save('users', user.id, user, done);
|
|
|
|
} else {
|
|
|
|
done(null)
|
|
|
|
}
|
|
|
|
}.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) {
|
2015-07-18 01:37:24 +02:00
|
|
|
checkUserTime(user, function() {
|
|
|
|
event.rUser = user;
|
|
|
|
callback(null);
|
|
|
|
});
|
2014-09-04 11:36:57 +02:00
|
|
|
});
|
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);
|
2012-12-23 17:19:01 +01:00
|
|
|
};
|
2014-09-04 11:51:39 +02:00
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return new users(dbot);
|
|
|
|
};
|