dbot/modules/report/report.js

93 lines
3.4 KiB
JavaScript

var _ = require('underscore')._,
async = require('async');
var report = function(dbot) {
if(!dbot.db.pending) dbot.db.pending = {};
if(!dbot.db.pNotify) dbot.db.pNotify = {};
this.pending = dbot.db.pending;
this.pNotify = dbot.db.pNotify;
this.internalAPI = {
'notify': function(server, users, message) {
async.eachSeries(users, function(nick, next) {
setTimeout(function() {
dbot.say(server, nick, message);
next();
}, 1000);
});
},
'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";
}
message = message.replace(/(#[\w\-]+)/g, '\u000312$1\u000f');
_.each(_.union(message.match(/ @([\d\w*|-]+)/g), message.match(/([\d\w*|-]+)@ ?/g)), function(u) {
u = u.replace(/\s|@/g, '');
message = message.replace(u, colours['nicks'] + u + "\u000f");
notifier += '[' + colours['nicks'] + u + '\u000f]';
});
}
return dbot.t('notify', {
'type': type,
'channel': channel,
'notifier': notifier,
'message': message
});
}.bind(this)
};
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')
}));
this.pNotify[event.rUser.id] = false;
}
}.bind(this);
this.on = 'JOIN';
this.onLoad = function() {
if(_.has(dbot.modules, 'web')) {
dbot.api.web.addIndexLink('/notify', 'Notifications');
}
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));
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,
'link': dbot.api.web.getUrl('notify/missing')
}));
this.pNotify[user.id] = false;
}
}.bind(this));
}.bind(this);
};
exports.fetch = function(dbot) {
return new report(dbot);
};