From 8c5e87164008660d58a99adca006360f024d2925 Mon Sep 17 00:00:00 2001 From: reality Date: Fri, 5 Sep 2014 10:42:30 +0000 Subject: [PATCH] report report (it works) --- modules/report/api.js | 136 ++++++++++++++++++++++++++++------------- modules/users/api.js | 20 ++++++ modules/users/users.js | 15 ++--- 3 files changed, 120 insertions(+), 51 deletions(-) diff --git a/modules/report/api.js b/modules/report/api.js index 6967c96..f80be88 100644 --- a/modules/report/api.js +++ b/modules/report/api.js @@ -24,53 +24,101 @@ var api = function(dbot) { }); var channel = dbot.instance.connections[server].channels[cName]; - var ops = _.filter(channel.nicks, function(user) { - if(this.config.notifyVoice) { - return user.op || user.voice; - } else { - return user.op; - } - }, this); - ops = _.pluck(ops, 'name'); + if(_.has(dbot.modules, 'atheme')) { + dbot.api.atheme.getChannelFlags(server, cName, function(err, flags) { + var ops = _.map(flags, function(f, k) { + var staff = (f.indexOf('O') !== -1); + if(this.config.notifyVoice && !staff) { + staff = (f.indexOf('V') !== -1); + } + if(staff) { + return k; + } + }.bind(this)); - dbot.api.users.resolveChannel(server, cName, function(channel) { - if(channel) { - var perOps = channel.op; - if(this.config.notifyVoice) perOps = _.union(perOps, channel.voice); + 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': op.id, + 'message': message + }); + this.pNotify[op.id] = true; + }, this); - this.db.read('nunsubs', channel.id, function(err, nunsubs) { - async.eachSeries(ops, function(nick, next) { - dbot.api.users.resolveUser(server, nick, function(user) { - if(nunsubs && _.include(nunsubs.users, user.id)) { - ops = _.without(ops, user.currentNick); - } - next(); - }); - }, function() { - offlineUsers = perOps; - if(!_.include(this.config.noMissingChans, cName)) { - _.each(offlineUsers, function(id) { - if(!this.pending[id]) this.pending[id] = []; - this.pending[id].push({ - 'time': new Date().getTime(), - 'channel': cName, - 'user': user.primaryNick, - 'message': message - }); - this.pNotify[id] = true; - }.bind(this)); - } - - 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)); + // 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 { + var channel = dbot.instance.connections[server].channels[cName]; + var ops = _.filter(channel.nicks, function(user) { + if(this.config.notifyVoice) { + return user.op || user.voice; + } else { + return user.op; + } + }, this); + ops = _.pluck(ops, 'name'); + + dbot.api.users.resolveChannel(server, cName, function(channel) { + if(channel) { + var perOps = channel.op; + if(this.config.notifyVoice) perOps = _.union(perOps, channel.voice); + + this.db.read('nunsubs', channel.id, function(err, nunsubs) { + async.eachSeries(ops, function(nick, next) { + dbot.api.users.resolveUser(server, nick, function(user) { + if(nunsubs && _.include(nunsubs.users, user.id)) { + ops = _.without(ops, user.currentNick); + } + next(); + }); + }, function() { + offlineUsers = perOps; + if(!_.include(this.config.noMissingChans, cName)) { + _.each(offlineUsers, function(id) { + if(!this.pending[id]) this.pending[id] = []; + this.pending[id].push({ + 'time': new Date().gettime(), + 'channel': cname, + 'user': user.primarynick, + 'message': message + }); + this.pnotify[id] = true; + }.bind(this)); + } + + 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)); + } }, 'notifyUsers': function(server, users, message) { diff --git a/modules/users/api.js b/modules/users/api.js index 6c7fdfa..bbc171d 100644 --- a/modules/users/api.js +++ b/modules/users/api.js @@ -43,6 +43,26 @@ var api = function(dbot) { callback(true, null); } }); + }, + + // Check if a nick is online under a given alias + 'isOnline': function(server, channel, nick, callback) { + this.api.resolveUser(server, nick, function(err, user) { + if(user) { + this.api.getUserAliases(user.id, function(err, aliases) { + aliases.push(nick); + + var onlineNicks = _.keys(dbot.instance.connections[server].channels[channel].nicks); + var isOnline = _.any(onlineNicks, function(nick) { + return _.include(aliases, nick); + }, this); + + callback(null, user, isOnline); + }); + } else { + callback(true, null, null); + } + }.bind(this)); } }; diff --git a/modules/users/users.js b/modules/users/users.js index ad76cfc..c6de710 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -106,13 +106,14 @@ var users = function(dbot) { this.listener = function(event) { // Update current nick this.api.resolveUser(event.server, event.user, function(err, user) { - console.log(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)); + 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'];