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

236 lines
8.5 KiB
JavaScript
Raw Normal View History

/**
* Name: Users
* Description: Track known users
*/
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
var users = function(dbot) {
/*** Internal API ***/
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();
this.db.create('users', id, {
2013-06-22 22:23:47 +02:00
'id': id,
'primaryNick': nick,
'currentNick': nick,
'server': server,
2013-07-14 15:25:11 +02:00
'channels': [],
'aliases': []
}, function(err, result) {
if(!err) {
dbot.api.event.emit('new_user', [ result ]);
callback(result);
}
});
}.bind(this),
'createChannel': function(server, name, callback) {
2013-06-22 22:23:47 +02:00
var id = uuid.v4();
this.db.create('channel_users', id, {
2013-06-22 22:23:47 +02:00
'id': id,
'server': server,
'name': name,
2013-07-14 15:25:11 +02:00
'users': [],
'op': [],
'voice': []
}, function(err, result) {
if(!err) {
dbot.api.event.emit('new_channel', [ result ]);
callback(result);
}
});
}.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)) {
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) {
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-06-22 22:23:47 +02:00
}.bind(this));
2013-07-14 15:25:11 +02:00
}.bind(this),
'modChannelStaff': function(channel, user, staff, callback) {
var needsUpdating = false;
if(!channel.op) {
channel.op = [];
needsUpdating = true;
}
if(!channel.voice) {
channel.voice = [];
needsUpdating = true;
}
if(!_.include(channel.op, user.id) && staff.op) {
channel.op.push(user.id);
needsUpdating = true;
} else if(!_.include(channel.voice, user.id) && staff.voice) {
channel.voice.push(user.id);
needsUpdating = true;
} else if(_.include(channel.op, user.id) && !staff.op) {
channel.op = _.without(channel.op, user.id);
needsUpdating = true;
} else if(_.include(channel.voice, user.id) && !staff.voice) {
channel.voice = _.without(channel.voice, user.id);
needsUpdating = true;
}
if(needsUpdating) {
this.db.save('channel_users', channel.id, channel, function(err) {
callback(err);
});
} else {
callback();
}
}.bind(this),
'updateChannelPrimaryUser': function(server, oldUser, newUser) {
this.db.search('channel_users', { 'server': server }, function(channel) {
channel.users = _.without(channel.users, oldUser);
if(!_.include(channel.users, newUser)) channel.users.push(newUser);
this.db.save('channel_users', channel.id, channel, function(err) {
if(err) {
// QQ
}
});
}.bind(this), function(err) {
if(err) {
// QQ
}
});
}.bind(this),
'mergeChannelUsers': function(server, oldUser, newUser) {
newUser.channels = _.union(oldUser.channels, newUser.channels);
_.each(newUser.channels, function(name) {
this.api.getChannel(server, name, function(channel) {
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));
}, this);
2013-05-26 15:11:09 +02:00
}.bind(this)
};
this.listener = function(event) {
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
}
if(event.rUser.currentNick != event.user) {
event.rUser.currentNick = event.user;
this.db.save('users', event.rUser.id, event.rUser, function() {});
}
} 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-06-22 18:25:31 +02:00
if(!isKnown) {
event.rUser.aliases.push(event.newNick);
}
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 ]);
});
}.bind(this));
}
}.bind(this);
2013-06-22 22:23:47 +02:00
this.on = ['JOIN', 'NICK'];
this.onLoad = function() {
dbot.instance.addPreEmitHook(function(event, callback) {
if(event.user) {
this.api.resolveUser(event.server, event.user, function(user) {
event.rUser = user;
callback(false);
});
2013-06-22 22:23:47 +02:00
} else {
callback(false);
}
}.bind(this));
2013-06-21 20:40:00 +02:00
dbot.instance.addPreEmitHook(function(event, callback) {
if(event.channel) {
this.api.getChannel(event.server, event.channel.name, function(channel) {
event.rChannel = channel;
callback(false);
});
2013-06-22 22:23:47 +02:00
} else {
callback(false);
2013-06-21 20:40:00 +02:00
}
}.bind(this));
dbot.instance.addListener('366', 'users', function(event) {
var checkChannel = function(channel) {
2013-06-22 22:23:47 +02:00
async.eachSeries(_.keys(event.channel.nicks), function(nick, next) {
2013-07-14 15:25:11 +02:00
var staff = event.channel.nicks[nick];
this.api.resolveUser(event.server, nick, function(user) {
var checkChannelUser = function(user) {
2013-06-22 22:23:47 +02:00
if(!_.include(channel.users, user.id)) {
2013-07-14 15:25:11 +02:00
this.internalAPI.addChannelUser(channel, user, staff, next);
} else {
2013-07-26 01:22:47 +02:00
console.log(channel);
2013-07-14 15:25:11 +02:00
this.internalAPI.modChannelStaff(channel, user, staff, next);
}
}.bind(this);
if(user) {
checkChannelUser(user);
} else {
2013-06-22 18:32:18 +02:00
this.internalAPI.createUser(event.server, nick, checkChannelUser);
}
}.bind(this));
2013-06-22 22:23:47 +02:00
}.bind(this), function(err) {});
}.bind(this);
if(!event.rChannel) {
this.internalAPI.createChannel(event.server, event.channel.name, checkChannel);
} else {
checkChannel(event.rChannel);
}
}.bind(this));
2013-06-22 18:25:31 +02:00
_.each(dbot.instance.connections, function(connection) {
connection.updateNickLists();
});
};
};
exports.fetch = function(dbot) {
return new users(dbot);
};