dbot/modules/report/report.js

93 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) {
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
2015-06-25 16:28:21 +02:00
_.each(_.union(message.match(/ @([\d\w*|-]+)/g), message.match(/([\d\w*|-]+)@ ?/g)), function(u) {
2015-06-25 16:30:14 +02:00
u = u.replace(/\s|@/g, '');
2013-08-28 00:01:44 +02:00
message = message.replace(u, colours['nicks'] + u + "\u000f");
2015-06-25 16:28:21 +02:00
notifier += '[' + colours['nicks'] + u + '\u000f]';
2013-08-28 00:01:44 +02:00
});
}
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,
'link': dbot.api.web.getUrl('notify/stream/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);
};