dbot/modules/report/report.js

127 lines
4.6 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
uuid = require('node-uuid'),
async = require('async');
var report = function(dbot) {
2013-07-14 16:55:15 +02:00
if(!dbot.db.pending) dbot.db.pending = {};
this.pending = dbot.db.pending;
this.api = {
'notify': function(server, channel, message) {
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-07-14 16:55:15 +02:00
dbot.api.users.getChannel(server, channel, function(channel) {
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] = [];
this.pending[id].push(message);
}.bind(this));
}.bind(this));
}
}.bind(this));
2013-07-06 20:47:47 +02:00
var i = 0;
var notifyChannel = function(ops) {
if(i >= ops.length) return;
dbot.say(server, ops[i].name, message);
setTimeout(function() {
i++; notifyChannel(ops);
}, 1000);
};
notifyChannel(ops);
}
};
2013-07-14 16:55:15 +02:00
this.listener = function(event) {
if(_.has(this.pending, event.rUser.id)) {
_.each(this.pending[event.rUser.id], function(message) {
dbot.say(event.server, event.rUser.currentNick, message);
});
delete this.pending[event.rUser.id];
}
}.bind(this);
this.on = 'JOIN';
var commands = {
'~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-06-27 21:16:18 +02:00
reason = event.input[3];
2013-06-27 21:16:18 +02:00
dbot.api.users.resolveUser(event.server, nick, function(reportee) {
if(_.has(event.allChannels, channelName)) {
if(reportee) {
this.api.notify(event.server, channelName, dbot.t('report', {
'reporter': event.user,
'reported': nick,
'channel': channelName,
'reason': reason
}));
event.reply(dbot.t('reported', { 'reported': nick }));
} else {
event.reply(dbot.t('user_not_found', {
'reported': nick,
'channel': channelName
}));
}
} 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)) {
this.api.notify(event.server, channelName, dbot.t('notify', {
'channel': channelName,
'notifier': event.user,
'message': message
}));
2013-07-06 20:45:21 +02:00
var id = uuid.v4();
this.db.save('notifies', id, {
'id': id,
'server': event.server,
'channel': channelName,
'user': event.user,
'time': new Date().getTime(),
'message': message
}, function() {});
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 }));
}
}
};
commands['~report'].regex = [/^~report ([^ ]+) ([^ ]+) (.+)$/, 4];
2013-06-02 15:34:51 +02:00
commands['~notify'].regex = [/^~notify ([^ ]+) (.+)$/, 3];
this.commands = commands;
};
exports.fetch = function(dbot) {
return new report(dbot);
};