3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-26 12:42:51 +01:00
dbot/modules/report/api.js

128 lines
5.4 KiB
JavaScript
Raw Normal View History

2013-10-21 17:40:45 +02:00
var uuid = require('node-uuid'),
_ = require('underscore')._,
async = require('async');
var api = function(dbot) {
var api = {
2015-07-23 11:58:31 +02:00
'notify': function(type, server, user, cName, message, host, target) {
2014-05-25 20:24:13 +02:00
var id = uuid.v4(),
tags = [];
_.each(message.match(/(#\w+)/g), function(match) {
tags.push(match.toLowerCase());
});
2015-01-29 22:41:01 +01:00
if(_.has(this.config.full_redirs, cName)) {
message = '(redirect from ' + cName + ') ' + message;
cName = this.config.full_redirs[cName];
}
2013-10-21 17:40:45 +02:00
this.db.save('notifies', id, {
'id': id,
'server': server,
'type': type,
'channel': cName,
'user': user.id,
'time': new Date().getTime(),
2014-05-25 19:16:53 +02:00
'message': message,
2015-03-14 10:24:51 +01:00
'host': host, // Host of the target, if passed
2015-07-23 11:58:31 +02:00
'target': target,
2014-05-25 20:24:13 +02:00
'tags': tags
2014-07-15 00:54:23 +02:00
}, function(err, notify) {
dbot.api.event.emit('new_notify', [ notify, user.primaryNick ]);
});
2013-10-21 17:40:45 +02:00
var channel = dbot.instance.connections[server].channels[cName];
2014-09-05 12:42:30 +02:00
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));
2014-10-03 22:45:51 +02:00
ops = _.without(ops, undefined);
console.log(ops);
2014-09-15 23:12:30 +02:00
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)) {
2014-10-03 22:45:51 +02:00
console.log('removing ' + uPart);
2014-09-15 23:12:30 +02:00
ops = _.without(ops, uPart);
}
2014-09-05 12:42:30 +02:00
});
}
2013-10-21 17:40:45 +02:00
2014-09-15 23:12:30 +02:00
var offlineOps = {};
2015-04-22 01:40:45 +02:00
async.eachSeries(ops, function(op, done) {
2014-09-15 23:12:30 +02:00
dbot.api.users.isOnline(server, cName, op, function(err, user, online) {
2015-01-29 22:42:09 +01:00
if(user) {
2014-09-15 23:12:30 +02:00
if(!err && !online) offlineOps[op] = user;
if(user.currentNick !== op) {
ops = _.without(ops, op);
ops.push(user.currentNick);
}
2015-01-29 22:42:09 +01:00
}
2014-09-15 23:12:30 +02:00
done();
2014-09-05 12:42:30 +02:00
});
2014-09-15 23:12:30 +02:00
}, function() {
// Queue notifies for offline ops
2016-01-28 18:36:19 +01:00
if(this.config.offlineReporting == true) {
2015-02-18 17:12:48 +01:00
if(!_.include(this.config.noMissingChans, cName)) {
_.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);
}
2016-01-28 18:36:19 +01:00
}
2014-09-05 12:42:30 +02:00
2014-09-15 23:12:30 +02:00
// 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));
2013-10-21 17:40:45 +02:00
}.bind(this));
2014-09-05 12:42:30 +02:00
}.bind(this));
2015-04-22 01:40:45 +02:00
} else { // no atheme
2014-09-05 12:42:30 +02:00
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');
2015-01-29 22:42:09 +01:00
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);
}
2014-09-05 12:42:30 +02:00
}
2013-10-21 17:40:45 +02:00
},
'notifyUsers': function(server, users, message) {
this.internalAPI.notify(server, users, message);
}
};
return api;
};
exports.fetch = function(dbot) {
return api(dbot);
};