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')._,
|
2013-06-22 19:19:04 +02:00
|
|
|
uuid = require('node-uuid'),
|
|
|
|
async = require('async');
|
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 = {
|
2013-06-22 18:32:18 +02:00
|
|
|
'createUser': function(server, nick, callback) {
|
2013-06-22 22:23:47 +02:00
|
|
|
var id = uuid.v4();
|
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
|
|
|
'primaryNick': nick,
|
|
|
|
'currentNick': nick,
|
|
|
|
'server': server,
|
2013-07-14 15:25:11 +02:00
|
|
|
'channels': [],
|
2013-03-24 13:54:19 +01:00
|
|
|
'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) {
|
2013-06-22 22:23:47 +02:00
|
|
|
var id = uuid.v4();
|
2013-04-14 22:00:53 +02:00
|
|
|
this.db.create('channel_users', id, {
|
2013-06-22 22:23:47 +02:00
|
|
|
'id': id,
|
2013-04-14 22:00:53 +02:00
|
|
|
'server': server,
|
|
|
|
'name': name,
|
2013-07-14 15:25:11 +02:00
|
|
|
'users': [],
|
|
|
|
'op': [],
|
|
|
|
'voice': []
|
2013-04-14 22:00:53 +02:00
|
|
|
}, 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),
|
|
|
|
|
2013-07-14 15:25:11 +02:00
|
|
|
'addChannelUser': function(channel, user, staff, callback) {
|
2013-06-22 18:25:31 +02:00
|
|
|
if(!_.include(channel.users, user.id)) {
|
2013-04-14 22:00:53 +02:00
|
|
|
channel.users.push(user.id);
|
2013-06-22 18:25:31 +02:00
|
|
|
}
|
2013-06-22 22:23:47 +02:00
|
|
|
if(!_.include(user.channels, channel.id)) {
|
2013-06-22 18:25:31 +02:00
|
|
|
user.channels.push(channel.id);
|
|
|
|
}
|
|
|
|
|
2013-07-14 15:25:11 +02:00
|
|
|
if(!channel.op) channel.op = [];
|
|
|
|
if(!channel.voice) channel.voice = [];
|
|
|
|
|
|
|
|
if(staff.op) {
|
|
|
|
channel.op.push(user.id);
|
|
|
|
} else if(staff.voice) {
|
|
|
|
channel.voice.push(user.id);
|
|
|
|
}
|
|
|
|
|
2013-06-22 18:25:31 +02:00
|
|
|
this.db.save('users', user.id, user, function(err) {
|
2013-04-14 22:00:53 +02:00
|
|
|
this.db.save('channel_users', channel.id, channel, function(err) {
|
2013-06-22 18:25:31 +02:00
|
|
|
dbot.api.event.emit('new_channel_user', [ user, channel ]);
|
|
|
|
callback(err);
|
2013-04-14 22:00:53 +02:00
|
|
|
});
|
2013-06-22 22:23:47 +02:00
|
|
|
}.bind(this));
|
2013-07-14 15:25:11 +02:00
|
|
|
}.bind(this),
|
|
|
|
|
2013-08-17 19:09:55 +02:00
|
|
|
'mergeChannelUsers': function(oldUser, newUser) {
|
2013-05-26 14:37:37 +02:00
|
|
|
newUser.channels = _.union(oldUser.channels, newUser.channels);
|
2013-08-17 19:09:55 +02:00
|
|
|
_.each(newUser.channels, function(uuid) {
|
|
|
|
this.api.getChannel(uuid, function(channel) {
|
2013-05-26 14:37:37 +02:00
|
|
|
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-06-22 18:25:31 +02:00
|
|
|
if(!event.rUser) {
|
2013-06-22 18:32:18 +02:00
|
|
|
this.internalAPI.createUser(event.server, event.user, function(user) {
|
2013-07-14 15:25:11 +02:00
|
|
|
this.internalAPI.addChannelUser(event.rChannel, user, {}, function() {});
|
2013-06-22 22:23:47 +02:00
|
|
|
}.bind(this));
|
2013-06-22 18:25:31 +02:00
|
|
|
} else if(!_.include(event.rUser.channels, event.rChannel.id)) {
|
2013-07-14 15:25:11 +02:00
|
|
|
this.internalAPI.addChannelUser(event.rChannel, event.rUser, {}, function() {});
|
2013-06-22 18:25:31 +02:00
|
|
|
}
|
2013-07-14 11:29:22 +02:00
|
|
|
|
|
|
|
if(event.rUser.currentNick != event.user) {
|
|
|
|
event.rUser.currentNick = event.user;
|
|
|
|
this.db.save('users', event.rUser.id, event.rUser, function() {});
|
|
|
|
}
|
2013-01-14 22:03:47 +01:00
|
|
|
} else if(event.action == 'NICK') {
|
2013-06-22 18:25:31 +02:00
|
|
|
this.api.isKnownUser(event.server, event.newNick, function(isKnown) {
|
|
|
|
event.rUser.currentNick = event.newNick;
|
2013-03-25 14:55:47 +01:00
|
|
|
|
2013-06-22 18:25:31 +02:00
|
|
|
if(!isKnown) {
|
|
|
|
event.rUser.aliases.push(event.newNick);
|
|
|
|
}
|
2013-03-25 14:55:47 +01:00
|
|
|
|
2013-06-22 18:25:31 +02:00
|
|
|
this.db.save('users', event.rUser.id, event.rUser, function(err) {
|
|
|
|
dbot.api.event.emit('new_user_alias', [ event.rUser, event.newNick ]);
|
|
|
|
});
|
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);
|
2013-06-22 22:23:47 +02:00
|
|
|
this.on = ['JOIN', 'NICK'];
|
2013-03-24 13:54:19 +01:00
|
|
|
|
2013-01-14 22:03:47 +01:00
|
|
|
this.onLoad = function() {
|
2013-06-21 20:11:22 +02:00
|
|
|
dbot.instance.addPreEmitHook(function(event, callback) {
|
2013-08-18 21:36:53 +02:00
|
|
|
// 1. Attempt to resolve channel
|
|
|
|
// 2. Create channel if it doesn't exist
|
|
|
|
// 3. Attempt to resolve user
|
|
|
|
// 4. Create user if it doesn't exist
|
|
|
|
// 5. Make sure channel objects are up to date
|
|
|
|
// 6. Update staff channel lists
|
|
|
|
|
|
|
|
var checkChannel = function(done) {
|
|
|
|
this.api.resolveChannel(event.server, event.channel.name, function(channel) {
|
|
|
|
if(!channel) {
|
|
|
|
this.internalAPI.createChannel(event.server, event.channel.name, done);
|
2013-08-17 18:35:57 +02:00
|
|
|
} else {
|
2013-08-18 21:36:53 +02:00
|
|
|
done(channel);
|
2013-08-17 18:35:57 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-08-18 21:36:53 +02:00
|
|
|
}.bind(this);
|
|
|
|
var checkUser = function(done) {
|
|
|
|
this.api.resolveUser(event.server, event.user, function(user) {
|
|
|
|
if(!user) {
|
|
|
|
this.internalAPI.createUser(event.server, event.user, done);
|
|
|
|
} else {
|
|
|
|
if(event.user != user.currentNick) {
|
|
|
|
user.currentNick = event.user;
|
|
|
|
this.db.save('users', user.id, user, function() {
|
|
|
|
done(user);
|
|
|
|
});
|
2013-08-05 19:41:20 +02:00
|
|
|
} else {
|
2013-08-18 21:36:53 +02:00
|
|
|
done(user);
|
2013-08-05 19:41:20 +02:00
|
|
|
}
|
2013-08-18 21:36:53 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this);
|
|
|
|
var checkChannelUsers = function(done) {
|
|
|
|
var needsUpdating = false;
|
|
|
|
|
|
|
|
if(!_.include(event.rUser.channels, event.rChannel.id)) {
|
|
|
|
event.rUser.channels.push(event.rChannel.id);
|
|
|
|
event.rChannel.users.push(event.rUser.id);
|
|
|
|
needsUpdating = true;
|
2013-08-19 10:28:40 +02:00
|
|
|
|
|
|
|
dbot.api.event.emit('new_channel_user', [ event.rUser, event.rChannel ]);
|
2013-08-18 21:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.has(event.channel, 'nicks') || !_.has(event.channel.nicks, event.rUser.currentNick)) {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
var cUser = event.channel.nicks[event.rUser.currentNick];
|
|
|
|
|
|
|
|
if(!_.include(event.rChannel.op, event.rUser.id) && cUser.op) {
|
|
|
|
event.rChannel.op.push(event.rUser.id);
|
|
|
|
needsUpdating = true;
|
|
|
|
} else if(!_.include(event.rChannel.voice, event.rUser.id) && cUser.voice) {
|
|
|
|
event.rChannel.voice.push(event.rUser.id);
|
|
|
|
needsUpdating = true;
|
|
|
|
} else if(_.include(event.rChannel.op, event.rUser.id) && !cUser.op) {
|
|
|
|
event.rChannel.op = _.without(event.rChannel.op, event.rUser.id);
|
|
|
|
needsUpdating = true;
|
|
|
|
} else if(_.include(event.rChannel.voice, event.rUser.id) && !cUser.voice) {
|
|
|
|
event.rChannel.voice = _.without(event.rChannel.voice, event.rUser.id);
|
|
|
|
needsUpdating = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(needsUpdating) {
|
|
|
|
this.db.save('users', event.rUser.id, event.rUser, function() {
|
|
|
|
this.db.save('channel_users', event.rChannel.id, event.rChannel, done);
|
2013-07-26 19:03:52 +02:00
|
|
|
}.bind(this));
|
2013-08-18 21:36:53 +02:00
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
2013-06-22 22:23:47 +02:00
|
|
|
}.bind(this);
|
|
|
|
|
2013-08-18 21:36:53 +02:00
|
|
|
if(event.user && event.channel && _.include(['JOIN', 'MODE', 'PRIVMSG'], event.action)) {
|
|
|
|
checkChannel(function(channel) {
|
|
|
|
event.rChannel = channel;
|
|
|
|
checkUser(function(user) {
|
|
|
|
event.rUser = user;
|
|
|
|
checkChannelUsers(function() {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if(event.user) {
|
|
|
|
this.api.resolveUser(event.server, event.user, function(user) {
|
|
|
|
if(user) event.rUser = user;
|
|
|
|
if(event.channel) {
|
|
|
|
this.api.resolveChannel(event.server, event.channel.name, function(channel) {
|
|
|
|
if(channel) event.rChannel = channel;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-06-22 22:23:47 +02:00
|
|
|
}
|
2013-03-24 17:04:01 +01:00
|
|
|
}.bind(this));
|
2013-03-24 13:54:19 +01:00
|
|
|
|
2013-06-22 18:25:31 +02:00
|
|
|
_.each(dbot.instance.connections, function(connection) {
|
2013-03-24 17:04:01 +01:00
|
|
|
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
|
|
|
};
|