2013-07-06 20:45:21 +02:00
|
|
|
var _ = require('underscore')._,
|
2013-07-14 16:55:15 +02:00
|
|
|
async = require('async');
|
2013-01-12 22:54:02 +01:00
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var report = function(dbot) {
|
2013-07-14 16:55:15 +02:00
|
|
|
if(!dbot.db.pending) dbot.db.pending = {};
|
2013-08-13 19:53:30 +02:00
|
|
|
if(!dbot.db.pNotify) dbot.db.pNotify = {};
|
2013-07-14 16:55:15 +02:00
|
|
|
this.pending = dbot.db.pending;
|
2013-08-13 19:53:30 +02:00
|
|
|
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
|
|
|
|
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-10-21 17:40:09 +02:00
|
|
|
|
2013-07-14 16:55:15 +02:00
|
|
|
this.listener = function(event) {
|
2013-08-28 22:22:56 +02:00
|
|
|
if(_.has(this.pending, event.rUser.id) && this.pNotify[event.rUser.id] === true && !_.include(event.rUser.mobile, event.rUser.currentNick)) {
|
2013-08-12 23:58:30 +02:00
|
|
|
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-12 23:58:30 +02:00
|
|
|
}));
|
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';
|
|
|
|
|
2013-08-24 15:56:45 +02:00
|
|
|
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);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-15 17:54:51 +01:00
|
|
|
return new report(dbot);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|