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');
|
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) {
|
|
|
|
dbot.say(server, nick, message);
|
|
|
|
setTimeout(function() {
|
|
|
|
next();
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-14 16:33:47 +02:00
|
|
|
this.api = {
|
2013-05-06 23:56:45 +02:00
|
|
|
'notify': function(server, channel, message) {
|
2013-04-14 16:33:47 +02:00
|
|
|
var channel = dbot.instance.connections[server].channels[channel];
|
|
|
|
var ops = _.filter(channel.nicks, function(user) {
|
|
|
|
if(this.config.notifyVoice) {
|
|
|
|
return user.op || user.voice;
|
|
|
|
} else {
|
|
|
|
return user.op;
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
2013-08-17 21:07:48 +02:00
|
|
|
dbot.api.users.resolveChannel(server, channel, function(channel) {
|
2013-07-14 16:55:15 +02:00
|
|
|
if(channel) {
|
|
|
|
var perOps = channel.op;
|
|
|
|
if(this.config.notifyVoice) pOps = _.union(perOps, channel.voice);
|
|
|
|
|
|
|
|
async.eachSeries(ops, function(nick, next) {
|
|
|
|
dbot.api.users.resolveUser(server, nick, function(user) {
|
|
|
|
perOps = _.without(perOps, user.id); next();
|
|
|
|
});
|
|
|
|
}, function() {
|
|
|
|
offlineUsers = perOps;
|
|
|
|
_.each(offlineUsers, function(id) {
|
|
|
|
if(!this.pending[id]) this.pending[id] = [];
|
2013-08-12 23:58:30 +02:00
|
|
|
this.pending[id].push({
|
|
|
|
'time': new Date().getTime(),
|
|
|
|
'message': message
|
|
|
|
});
|
2013-08-13 19:53:30 +02:00
|
|
|
this.pNotify[id] = true;
|
2013-07-14 16:55:15 +02:00
|
|
|
}.bind(this));
|
2013-08-18 17:19:15 +02:00
|
|
|
|
|
|
|
this.internalAPI.notify(server, _.pluck(ops, 'name'), message);
|
2013-07-14 16:55:15 +02:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
}.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-04-14 16:33:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-14 16:55:15 +02:00
|
|
|
this.listener = function(event) {
|
2013-08-13 19:53:30 +02:00
|
|
|
if(_.has(this.pending, event.rUser.id) && this.pNotify[event.rUser.id] === true) {
|
2013-08-12 23:58:30 +02:00
|
|
|
dbot.say(event.server, event.user, dbot.t('missed_notifies', {
|
|
|
|
'user': event.rUser.primaryNick,
|
2013-08-17 23:04:42 +02:00
|
|
|
'link': dbot.api.web.getUrl('report/' + event.server + '/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';
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var commands = {
|
2013-08-12 23:58:30 +02:00
|
|
|
'~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'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
'~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],
|
2013-08-05 21:51:38 +02:00
|
|
|
reason = event.input[3].trim();
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-07-28 18:26:54 +02:00
|
|
|
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-21 23:10:06 +02:00
|
|
|
var notifier = '[' + event.user + ']',
|
|
|
|
cChan = channelName,
|
|
|
|
type = 'report',
|
|
|
|
reporter = event.user;
|
|
|
|
if(_.has(this.config.colours, event.server)) {
|
|
|
|
var colours = this.config.colours[event.server];
|
|
|
|
|
|
|
|
reporter = colours['nicks'] + reporter + '\u000f';
|
|
|
|
nick = colours['nicks'] + nick + '\u000f';
|
|
|
|
type = colours['type'][type] + type + '\u000f';
|
|
|
|
if(_.has(colours['channels'], channelName)) {
|
|
|
|
cChan = colours['channels'][channelName] +
|
|
|
|
cChan + "\u000f";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:16:18 +02:00
|
|
|
this.api.notify(event.server, channelName, dbot.t('report', {
|
2013-08-21 23:10:06 +02:00
|
|
|
'type': type,
|
|
|
|
'reporter': reporter,
|
2013-06-27 21:16:18 +02:00
|
|
|
'reported': nick,
|
2013-08-21 23:10:06 +02:00
|
|
|
'channel': cChan,
|
2013-06-27 21:16:18 +02:00
|
|
|
'reason': reason
|
|
|
|
}));
|
|
|
|
event.reply(dbot.t('reported', { 'reported': nick }));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('user_not_found', {
|
|
|
|
'reported': nick,
|
|
|
|
'channel': channelName
|
|
|
|
}));
|
|
|
|
}
|
2012-12-11 17:04:52 +01:00
|
|
|
} 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-07-06 20:45:21 +02:00
|
|
|
var id = uuid.v4();
|
|
|
|
this.db.save('notifies', id, {
|
|
|
|
'id': id,
|
|
|
|
'server': event.server,
|
|
|
|
'channel': channelName,
|
2013-08-24 19:39:04 +02:00
|
|
|
'user': event.rUser.id,
|
2013-07-06 20:45:21 +02:00
|
|
|
'time': new Date().getTime(),
|
|
|
|
'message': message
|
|
|
|
}, function() {});
|
|
|
|
|
2013-08-21 21:51:41 +02:00
|
|
|
var notifier = '[' + event.user + ']',
|
2013-08-21 21:10:10 +02:00
|
|
|
cChan = channelName,
|
|
|
|
type = 'notify';
|
2013-08-21 20:12:43 +02:00
|
|
|
if(_.has(this.config.colours, event.server)) {
|
2013-08-21 20:32:12 +02:00
|
|
|
var colours = this.config.colours[event.server];
|
2013-08-21 20:12:43 +02:00
|
|
|
|
2013-08-21 21:51:41 +02:00
|
|
|
notifier = '[' + colours['nicks'] + event.user + '\u000f]';
|
2013-08-21 23:10:06 +02:00
|
|
|
type = colours['type'][type] + type + '\u000f';
|
2013-08-21 20:12:43 +02:00
|
|
|
if(_.has(colours['channels'], channelName)) {
|
2013-08-21 20:34:31 +02:00
|
|
|
cChan = colours['channels'][channelName] +
|
|
|
|
cChan + "\u000f";
|
2013-08-21 20:12:43 +02:00
|
|
|
}
|
2013-08-21 21:14:55 +02:00
|
|
|
|
2013-08-21 21:57:19 +02:00
|
|
|
_.each(message.match(/@([\d\w\s*|-]+?)( |$|,|\.)/g), function(user) {
|
|
|
|
user = user.replace(/@([\d\w\s*|-]+?)( |$|,|\.)/, "$1");
|
2013-08-21 21:51:41 +02:00
|
|
|
notifier += '[' + colours['nicks'] + user + '\u000f]';
|
2013-08-21 21:26:45 +02:00
|
|
|
});
|
2013-08-21 21:57:19 +02:00
|
|
|
message = message.replace(/@([\d\w\s*|-]+?)( |$|,|\.)/g, colours['nicks'] +
|
|
|
|
"@$1\u000f$2");
|
2013-08-21 20:12:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.api.notify(event.server, channelName, dbot.t('notify', {
|
2013-08-21 21:10:10 +02:00
|
|
|
'type': type,
|
2013-08-21 20:34:31 +02:00
|
|
|
'channel': cChan,
|
2013-08-21 20:12:43 +02:00
|
|
|
'notifier': notifier,
|
|
|
|
'message': message
|
|
|
|
}));
|
|
|
|
|
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 }));
|
|
|
|
}
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
commands['~report'].regex = [/^~report ([^ ]+) ([^ ]+) (.+)$/, 4];
|
2013-06-02 15:34:51 +02:00
|
|
|
commands['~notify'].regex = [/^~notify ([^ ]+) (.+)$/, 3];
|
2013-01-15 17:54:51 +01:00
|
|
|
this.commands = commands;
|
2013-08-24 15:56:45 +02:00
|
|
|
|
|
|
|
this.onLoad = function() {
|
|
|
|
if(_.has(dbot.modules, 'web')) {
|
|
|
|
dbot.api.web.addIndexLink('/notify', 'Notifications');
|
|
|
|
}
|
|
|
|
};
|
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
|
|
|
};
|