3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 21:09:31 +01:00
dbot/modules/report/api.js

133 lines
6.0 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 = {
'notify': function(type, server, user, cName, message) {
2014-05-25 20:24:13 +02:00
var id = uuid.v4(),
tags = [];
_.each(message.match(/(#\w+)/g), function(match) {
tags.push(match.toLowerCase());
});
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,
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 = {};
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();
2014-09-05 12:42:30 +02:00
});
2014-09-15 23:12:30 +02:00
}, 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);
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));
} 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);
2014-09-15 23:12:30 +02:00
this.db.read('nunsubs', cName + '.' + server, function(err, nunsubs) {
2014-09-05 12:42:30 +02:00
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() {
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));
}
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);
};