3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-27 05:02:36 +01:00
dbot/modules/report/report.js

294 lines
12 KiB
JavaScript
Raw Normal View History

2013-07-06 20:45:21 +02:00
var _ = require('underscore')._,
2013-07-14 16:55:15 +02:00
uuid = require('node-uuid'),
async = require('async');
var report = function(dbot) {
2013-07-14 16:55:15 +02:00
if(!dbot.db.pending) dbot.db.pending = {};
if(!dbot.db.pNotify) dbot.db.pNotify = {};
2013-07-14 16:55:15 +02:00
this.pending = dbot.db.pending;
this.pNotify = dbot.db.pNotify;
2013-07-14 16:55:15 +02:00
2013-08-18 17:19:15 +02:00
this.internalAPI = {
'notify': function(server, users, message) {
async.eachSeries(users, function(nick, next) {
setTimeout(function() {
2013-08-26 00:37:31 +02:00
dbot.say(server, nick, message);
2013-08-18 17:19:15 +02:00
next();
2013-08-26 00:14:39 +02:00
}, 1000);
2013-08-18 17:19:15 +02:00
});
2013-08-28 00:01:44 +02:00
},
'formatNotify': function(type, server, user, channel, message) {
var notifier = '[' + user.primaryNick + ']';
if(_.has(this.config.colours, server)) {
var colours = this.config.colours[server];
notifier = '[' + colours['nicks'] + user.primaryNick + '\u000f]';
if(_.has(colours.type, type)) {
type = colours['type'][type] + type + '\u000f';
}
if(_.has(colours['channels'], channel)) {
channel = colours['channels'][channel] +
channel + "\u000f";
}
_.each(message.match(/ @([\d\w*|-]+)/g), function(u) {
u = u.substr(1);
message = message.replace(u, colours['nicks'] + u + "\u000f");
notifier += '[' + colours['nicks'] + u.substr(1) + '\u000f]';
});
}
return dbot.t('notify', {
'type': type,
'channel': channel,
'notifier': notifier,
'message': message
});
}.bind(this)
2013-08-18 17:19:15 +02:00
};
this.api = {
2013-08-28 00:01:44 +02:00
'notify': function(type, server, user, cName, message) {
var id = uuid.v4();
this.db.save('notifies', id, {
'id': id,
'server': server,
'type': type,
2013-08-28 00:08:28 +02:00
'channel': cName,
2013-08-28 00:01:44 +02:00
'user': user.id,
'time': new Date().getTime(),
'message': message
}, function() {});
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);
2013-10-13 03:25:33 +02:00
ops = _.pluck(ops, 'name');
2013-08-28 00:01:44 +02:00
dbot.api.users.resolveChannel(server, cName, function(channel) {
2013-07-14 16:55:15 +02:00
if(channel) {
var perOps = channel.op;
if(this.config.notifyVoice) perOps = _.union(perOps, channel.voice);
2013-07-14 16:55:15 +02:00
2013-10-13 03:25:33 +02:00
this.db.read('nunsubs', channel.id, function(err, nunsubs) {
async.eachSeries(ops, function(nick, next) {
dbot.api.users.resolveUser(server, nick, function(user) {
if(!_.include(user.mobile, user.currentNick)) {
perOps = _.without(perOps, user.id);
}
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));
}
2013-10-13 03:25:33 +02: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);
}
}.bind(this));
}.bind(this));
2013-07-14 16:55:15 +02:00
}
}.bind(this));
2013-08-18 17:19:15 +02:00
},
2013-07-14 16:55:15 +02:00
2013-08-18 17:19:15 +02:00
'notifyUsers': function(server, users, message) {
this.internalAPI.notify(server, users, message);
}
};
2013-07-14 16:55:15 +02:00
this.listener = function(event) {
if(_.has(this.pending, event.rUser.id) && this.pNotify[event.rUser.id] === true && !_.include(event.rUser.mobile, event.rUser.currentNick)) {
dbot.say(event.server, event.user, dbot.t('missed_notifies', {
'user': event.rUser.primaryNick,
2013-08-31 17:23:27 +02:00
'link': dbot.api.web.getUrl('notify/missing')
}));
2013-08-21 22:38:51 +02:00
this.pNotify[event.rUser.id] = false;
2013-07-14 16:55:15 +02:00
}
}.bind(this);
this.on = 'JOIN';
var commands = {
'~clearmissing': function(event) {
if(_.has(this.pending, event.rUser.id)) {
var count = this.pending[event.rUser.id].length;
delete this.pending[event.rUser.id];
event.reply(dbot.t('cleared_notifies', { 'count': count }));
} else {
event.reply(dbot.t('no_missed_notifies'));
}
},
'~report': function(event) {
2013-06-27 21:16:18 +02:00
var channelName = event.input[1],
2013-07-14 16:55:15 +02:00
nick = event.input[2],
reason = event.input[3].trim();
if(reason.charAt(reason.length - 1) != '.') reason += '.';
2013-06-27 21:16:18 +02:00
dbot.api.users.resolveUser(event.server, nick, function(reportee) {
if(_.has(event.allChannels, channelName)) {
if(reportee) {
2013-08-28 00:09:59 +02:00
this.api.notify('report', event.server, event.rUser,
channelName, dbot.t('report', {
'reporter': event.rUser.primaryNick,
'reportee': nick,
'reason': reason
}));
2013-06-27 21:16:18 +02:00
event.reply(dbot.t('reported', { 'reported': nick }));
} else {
event.reply(dbot.t('user_not_found', {
'reported': nick,
'channel': channelName
}));
}
} else {
2013-06-27 21:16:18 +02:00
event.reply(dbot.t('not_in_channel', { 'channel': channelName }));
}
}.bind(this));
2013-06-02 15:34:51 +02:00
},
'~notify': function(event) {
2013-06-27 21:16:18 +02:00
var channelName = event.input[1],
message = event.input[2];
2013-06-02 15:34:51 +02:00
if(_.has(event.allChannels, channelName)) {
2013-08-28 00:52:26 +02:00
if(this.config.firstHost) {
var first = message.split(' ')[0];
dbot.api.users.resolveUser(event.server, first, function(user) {
if(user) {
dbot.api.nickserv.getUserHost(event.server, first, function(host) {
message = message.replace(first, first + ' [' + host + ']');
this.api.notify('notify', event.server, event.rUser, channelName, message);
}.bind(this));
} else {
this.api.notify('notify', event.server, event.rUser, channelName, message);
}
}.bind(this));
} else {
this.api.notify('notify', event.server, event.rUser, channelName, message);
}
2013-08-21 20:12:43 +02:00
2013-06-02 15:34:51 +02:00
event.reply(dbot.t('notified', {
'user': event.user,
'channel': channelName
}));
} else {
event.reply(dbot.t('not_in_channel', { 'channel': channelName }));
}
2013-10-13 03:25:33 +02:00
},
'~nunsub': function(event) {
var cName = event.input[1];
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 }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
}.bind(this));
},
'~ununsub': function(event) {
var cName = event.input[1];
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));
}
};
commands['~report'].regex = [/^~report ([^ ]+) ([^ ]+) (.+)$/, 4];
2013-06-02 15:34:51 +02:00
commands['~notify'].regex = [/^~notify ([^ ]+) (.+)$/, 3];
2013-10-13 03:25:33 +02:00
commands['~nunsub'].regex = [/^~nunsub ([^ ]+)$/, 2];
commands['~ununsub'].regex = [/^~ununsub ([^ ]+)$/, 2];
this.commands = commands;
this.onLoad = function() {
if(_.has(dbot.modules, 'web')) {
dbot.api.web.addIndexLink('/notify', 'Notifications');
}
2013-08-24 22:42:30 +02:00
dbot.api.event.addHook('~mergeusers', function(server, oldUser, newUser) {
this.db.search('notifies', { 'user': oldUser.id }, function(notify) {
notify.user = newUser.id;
this.db.save('notifies', notify.id, notify, function() {});
}.bind(this), function() {});
}.bind(this));
2013-08-29 22:07:30 +02:00
dbot.api.event.addHook('new_current_nick', function(user) {
if(_.has(this.pending, user.id) && this.pNotify[user.id] === true
&& !_.include(user.mobile, user.currentNick)) {
dbot.say(user.server, user.currentNick, dbot.t('missed_notifies', {
'user': user.primaryNick,
2013-08-31 17:23:27 +02:00
'link': dbot.api.web.getUrl('notify/missing')
2013-08-29 22:07:30 +02:00
}));
this.pNotify[user.id] = false;
}
}.bind(this));
2013-08-24 22:42:30 +02:00
}.bind(this);
};
exports.fetch = function(dbot) {
return new report(dbot);
};