3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00
This commit is contained in:
Luke Slater 2014-09-15 21:20:59 +00:00
commit f8bc0be366
2 changed files with 83 additions and 75 deletions

View File

@ -35,38 +35,48 @@ var api = function(dbot) {
return k;
}
}.bind(this));
var offlineOps = {};
async.each(ops, function(op, done) {
dbot.api.users.isOnline(server, cName, op, function(err, user, online) {
if(!err && !online) offlineOps[op] = user;
if(user && user.currentNick !== op) {
ops = _.without(ops, op);
ops.push(user.currentNick);
}
done();
});
}, function() {
// Queue notifies for offline ops
_.each(offlineOps, function(op) {
if(!this.pending[op.id]) this.pending[op.id] = [];
this.pending[op.id].push({
'time': new Date().getTime(),
'channel': cName,
'user': user.id,
'message': message
this.db.read('nunsubs', cName + '.' + server, function(err, nunsubs) {
if(nunsubs) {
_.each(nunsubs.users, function(user) {
var uPart = user.split('.')[0];
if(_.include(ops, uPart)) {
ops = _.without(ops, uPart);
}
});
this.pNotify[op.id] = true;
}, this);
// Send notifies to online ops
ops = _.difference(ops, _.keys(offlineOps));
message = this.internalAPI.formatNotify(type, server,
user, cName, message);
this.internalAPI.notify(server, ops, message);
if(_.has(this.config.chan_redirs, cName)) {
dbot.say(server, this.config.chan_redirs[cName], message);
}
var offlineOps = {};
async.each(ops, function(op, done) {
dbot.api.users.isOnline(server, cName, op, function(err, user, online) {
if(!err && !online) offlineOps[op] = user;
if(user.currentNick !== op) {
ops = _.without(ops, op);
ops.push(user.currentNick);
}
done();
});
}, function() {
// Queue notifies for offline ops
_.each(offlineOps, function(op) {
if(!this.pending[op.id]) this.pending[op.id] = [];
this.pending[op.id].push({
'time': new Date().getTime(),
'channel': cName,
'user': user.id,
'message': message
});
this.pNotify[op.id] = true;
}, this);
// Send notifies to online ops
ops = _.difference(ops, _.keys(offlineOps));
message = this.internalAPI.formatNotify(type, server,
user, cName, message);
this.internalAPI.notify(server, ops, message);
if(_.has(this.config.chan_redirs, cName)) {
dbot.say(server, this.config.chan_redirs[cName], message);
}
}.bind(this));
}.bind(this));
}.bind(this));
} else {
@ -85,7 +95,7 @@ var api = function(dbot) {
var perOps = channel.op;
if(this.config.notifyVoice) perOps = _.union(perOps, channel.voice);
this.db.read('nunsubs', channel.id, function(err, nunsubs) {
this.db.read('nunsubs', cName + '.' + server, function(err, nunsubs) {
async.eachSeries(ops, function(nick, next) {
dbot.api.users.resolveUser(server, nick, function(user) {
if(nunsubs && _.include(nunsubs.users, user.id)) {

View File

@ -139,56 +139,54 @@ var commands = function(dbot) {
},
'~nunsub': function(event) {
var cName = event.input[1];
var cName = event.input[1],
cId = event.input[1] + '.' + event.server;
dbot.api.users.resolveChannel(event.server, cName, function(channel) {
if(channel) {
this.db.read('nunsubs', channel.id, function(err, nunsubs) {
if(!nunsubs) {
var nunsubs = {
'id': channel.id,
'users': []
}
}
if(!_.include(nunsubs, event.rUser.id)) {
nunsubs.users.push(event.rUser.id);
this.db.save('nunsubs', channel.id, nunsubs, function() {
var reply = dbot.t('nunsubbed', { 'cName': cName })
if(_.has(this.config.chan_redirs, cName)) {
reply += dbot.t('n_also_found', { 'afaName' : this.config.chan_redirs[cName] });
}
event.reply(reply);
}.bind(this));
} else {
event.reply(dbot.t('already_nunsubbed', { 'cName': cName }));
if(_.has(dbot.instance.connections[event.server].channels, cName)) {
this.db.read('nunsubs', cId, function(err, nunsubs) {
if(!nunsubs) {
var nunsubs = {
'id': cId,
'users': []
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
}.bind(this));
}
if(!_.include(nunsubs, event.rUser.id)) {
nunsubs.users.push(event.rUser.id);
this.db.save('nunsubs', cId, nunsubs, function() {
var reply = dbot.t('nunsubbed', { 'cName': cName })
if(_.has(this.config.chan_redirs, cName)) {
reply += dbot.t('n_also_found', { 'afaName' : this.config.chan_redirs[cName] });
}
event.reply(reply);
}.bind(this));
} else {
event.reply(dbot.t('already_nunsubbed', { 'cName': cName }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
},
'~ununsub': function(event) {
var cName = event.input[1];
var cName = event.input[1],
cId = event.input[1] + '.' + event.server;
dbot.api.users.resolveChannel(event.server, cName, function(channel) {
if(channel) {
this.db.read('nunsubs', channel.id, function(err, nunsubs) {
if(!_.isUndefined(nunsubs) && _.include(nunsubs.users, event.rUser.id)) {
nunsubs.users = _.without(nunsubs.users, event.rUser.id);
this.db.save('nunsubs', channel.id, nunsubs, function() {
event.reply(dbot.t('ununsubbed', { 'cName': cName }));
});
} else {
event.reply(dbot.t('not_nunsubbed', { 'cName': cName }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
}.bind(this));
if(_.has(dbot.instance.connections[event.server].channels, cName)) {
this.db.read('nunsubs', cId, function(err, nunsubs) {
if(!_.isUndefined(nunsubs) && _.include(nunsubs.users, event.rUser.id)) {
nunsubs.users = _.without(nunsubs.users, event.rUser.id);
this.db.save('nunsubs', cId, nunsubs, function() {
event.reply(dbot.t('ununsubbed', { 'cName': cName }));
});
} else {
event.reply(dbot.t('not_nunsubbed', { 'cName': cName }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
}
};
commands['~report'].regex = /^report (#[^ ]+ )?([^ ]+) (.*)$/;