3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00
dbot/modules/report/report.js

94 lines
3.4 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
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) {
2014-10-03 22:45:51 +02:00
console.log('sending msg to ' + nick);
2013-08-18 17:19:15 +02:00
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";
}
2014-07-21 10:56:57 +02:00
message = message.replace(/(#[\w\-]+)/g, '\u000312$1\u000f');
2014-05-25 19:16:53 +02:00
2013-08-28 00:01:44 +02:00
_.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
};
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';
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);
};