3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-18 08:32:42 +01:00
dbot/modules/report/commands.js

364 lines
16 KiB
JavaScript
Raw Normal View History

2014-05-25 23:16:28 +02:00
var _ = require('underscore')._,
2015-01-19 23:30:04 +01:00
moment = require('moment');
2013-10-21 17:40:45 +02:00
var commands = function(dbot) {
var commands = {
2014-05-25 22:47:30 +02:00
'~ncount': function(event) {
var chanCounts = {},
2015-01-02 22:38:37 +01:00
typeCounts = {},
2014-05-25 23:16:28 +02:00
total = 0,
offString = event.params[1] || null;
offset = moment().subtract(offString, 1).valueOf() || null;
/*if(!offset || !offset.isValid()) {
event.reply('Invalid timescale. Try \'week\'');
return;
}*/
2014-05-25 22:47:30 +02:00
this.db.scan('notifies', function(notify) {
if(notify.user == event.rUser.id) {
2014-05-25 23:16:28 +02:00
if(!offString) {
if(!_.has(chanCounts, notify.channel)) chanCounts[notify.channel] = 0;
2015-01-02 22:38:37 +01:00
if(!_.has(typeCounts, notify.type)) typeCounts[notify.type] = 0;
2014-05-25 23:16:28 +02:00
chanCounts[notify.channel]++;
2015-01-02 22:38:37 +01:00
typeCounts[notify.type]++;
2014-05-25 23:16:28 +02:00
total++;
} else {
if(notify.time > offset) {
if(!_.has(chanCounts, notify.channel)) chanCounts[notify.channel] = 0;
2015-01-02 22:38:37 +01:00
if(!_.has(typeCounts, notify.type)) typeCounts[notify.type] = 0;
2014-05-25 23:16:28 +02:00
chanCounts[notify.channel]++;
2015-01-02 22:38:37 +01:00
typeCounts[notify.type]++;
2014-05-25 23:16:28 +02:00
total++;
}
}
2014-05-25 22:47:30 +02:00
}
}, function() {
var cCounts = _.chain(chanCounts)
.pairs()
.sortBy(function(p) { return p[1]; })
.reverse()
.first(10)
.value();
var cString = '';
for(var i=0;i<cCounts.length;i++) {
cString += cCounts[i][0] + " (" + cCounts[i][1] + "), ";
}
cString = cString.slice(0, -2);
2015-01-02 22:38:37 +01:00
var tCounts = _.chain(typeCounts)
.pairs()
.sortBy(function(p) { return p[1]; })
.reverse()
.first(10)
.value();
2015-01-02 22:39:45 +01:00
var tString = '';
2015-01-02 22:38:37 +01:00
for(var i=0;i<tCounts.length;i++) {
tString += tCounts[i][0] + " (" + tCounts[i][1] + "), ";
}
tString = tString.slice(0, -2);
2014-05-25 23:16:28 +02:00
if(offString) {
event.reply(dbot.t('timed_notifies', {
'user': event.user,
'count': total,
'offString': offString,
2015-01-02 22:38:37 +01:00
'cString': cString,
'tString': tString
2014-05-25 23:16:28 +02:00
}));
} else {
event.reply(dbot.t('total_notifies', {
'user': event.user,
'count': total,
2015-01-02 22:38:37 +01:00
'cString': cString,
'tString': tString
2014-05-25 23:16:28 +02:00
}));
}
2014-05-25 22:47:30 +02:00
});
},
2015-01-15 03:41:11 +01:00
'~sustatus': function(event) {
var user = event.input[1];
dbot.api.users.resolveUser(event.server, user, function(err, user) {
if(user) {
2015-03-14 14:45:15 +01:00
var ban = 0,
2015-03-14 14:47:31 +01:00
latest_ban = {'time':0},
latest_unban = {'time':0},
2015-03-14 14:45:15 +01:00
unban = 0,
2015-01-15 03:41:11 +01:00
quiet = 0,
warn = 0,
2015-01-19 22:57:27 +01:00
report = 0,
2015-01-19 23:06:33 +01:00
items = {};
2015-01-15 03:41:11 +01:00
// i'll fix it later
dbot.modules.report.db.search('notifies', {
'server': event.server
}, function(notify) {
2015-01-19 22:57:27 +01:00
if(notify.message.match('banned ' + user.primaryNick) ||
2015-03-14 14:45:15 +01:00
notify.message.match(user.primaryNick + ' has been unbanned') ||
2015-01-19 22:57:27 +01:00
notify.message.match('issued a warning to ' + user.primaryNick) ||
notify.message.match('has quieted ' + user.primaryNick) ||
notify.message.match('has reported ' + user.primaryNick)) {
2015-01-15 03:41:11 +01:00
if(notify.type == 'ban') {
2015-03-14 14:45:15 +01:00
ban++;
if(notify.time > latest_ban.time) {
latest_ban = notify;
}
} else if(notify.type == 'unban') {
unban++;
if(notify.time > latest_unban.time) {
latest_unban = notify;
}
2015-01-15 03:41:11 +01:00
} else if(notify.type == 'quiet') {
quiet++;
} else if(notify.type == 'warn') {
warn++;
2015-01-19 22:57:27 +01:00
} else if(notify.type == 'report') {
report++;
2015-01-15 03:41:11 +01:00
}
2015-01-19 23:06:33 +01:00
items[notify.time] = notify.message;
2015-01-15 03:41:11 +01:00
}
}, function() {
2015-01-19 23:06:33 +01:00
if(quiet != 0 || warn != 0 || report != 0) {
2015-01-19 22:57:27 +01:00
event.reply(user.primaryNick + ' has been warned ' + warn + ' times, quieted ' + quiet + ' times, and reported ' + report + ' times.');
2015-01-19 23:06:33 +01:00
2015-01-19 23:30:04 +01:00
_.each(_.keys(items).sort(function(a, b) {
2015-01-19 23:33:28 +01:00
return moment(a).unix() - moment(b).unix();
2015-01-19 23:30:04 +01:00
}), function(time) {
2015-01-19 23:16:22 +01:00
event.reply('[' + moment(parseInt(time)).format('DD/MM/YYYY') + '] ' + items[time]);
2015-01-19 23:31:18 +01:00
console.log('[' + moment(parseInt(time)).format('DD/MM/YYYY') + '] ' + items[time]);
2015-01-19 23:30:04 +01:00
});
2015-01-19 23:06:33 +01:00
2015-03-14 14:50:31 +01:00
if(latest_ban.time != 0) {
2015-03-14 14:47:31 +01:00
if(latest_unban.time == 0 || (latest_unban.time < latest_ban.time)) {
2015-03-14 14:58:15 +01:00
event.reply('Current Ban Status: \u00034Banned\u000f since ' + moment(latest_ban.time).fromNow() + ' (' + moment(parseInt(latest_ban.time)).format('DD/MM/YYYY') + ')');
2015-03-14 14:45:15 +01:00
event.reply('Reason: ' + latest_ban.message);
} else {
var a = moment(latest_ban.time);
var b = moment(latest_unban.time);
2015-03-14 15:00:56 +01:00
event.reply('Current Ban Status: \u00037Unbanned\u000f since ' + moment(parseInt(latest_unban.time)).format('DD/MM/YYYY') + ' after being banned for ' + b.diff(a, 'days') + ' days');
2015-03-14 14:45:15 +01:00
event.reply('Most recent ban reason: ' + latest_ban.message);
}
} else {
2015-03-14 14:58:15 +01:00
event.reply('Current Ban Status: \u00033Never banned (\u00037probably\u00033)\u000f');
2015-01-19 22:57:27 +01:00
}
2015-01-15 03:41:11 +01:00
} else {
event.reply(user.primaryNick + ' has no record.');
}
});
} else {
event.reply('never heard of em');
}
}.bind(this));
},
2014-12-16 03:50:03 +01:00
'~ustatus': function(event) {
var user = event.input[1];
dbot.api.users.resolveUser(event.server, user, function(err, user) {
if(user) {
var ban = null,
quiet = 0
warn = 0;
// i'll fix it later
dbot.modules.report.db.search('notifies', {
'server': event.server
}, function(notify) {
if(notify.message.match('banned ' + user.primaryNick) || notify.message.match('issued a warning to ' + user.primaryNick) || notify.message.match('has quieted ' + user.primaryNick)) {
if(notify.type == 'ban') {
ban = notify.time;
} else if(notify.type == 'quiet') {
quiet++;
} else if(notify.type == 'warn') {
warn++;
}
}
}, function() {
if(ban) {
event.reply(user.primaryNick + ' was banned on ' + new Date(ban).toUTCString());
} else if(quiet != 0 || warn != 0) {
event.reply(user.primaryNick + ' has been warned ' + warn + ' times, and quieted ' + quiet + ' times.');
} else {
event.reply(user.primaryNick + ' has no record.');
}
});
} else {
event.reply('never heard of em');
}
}.bind(this));
},
2013-10-21 17:40:45 +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'));
}
},
'~report': function(event) {
2014-09-04 17:25:21 +02:00
var channelName = (event.input[1] || event.channel.toString()),
2013-10-21 17:40:45 +02:00
nick = event.input[2],
reason = event.input[3].trim();
2014-09-04 17:25:21 +02:00
channelName = channelName.trim();
2013-10-21 17:40:45 +02:00
2014-02-04 01:58:09 +01:00
if(channelName == event.user) {
channelName = dbot.config.servers[event.server].admin_channel;
}
2013-10-21 17:40:45 +02:00
if(reason.charAt(reason.length - 1) != '.') reason += '.';
2014-09-04 16:54:49 +02:00
dbot.api.users.resolveUser(event.server, nick, function(err, reportee) {
2013-10-21 17:40:45 +02:00
if(_.has(event.allChannels, channelName)) {
if(reportee) {
this.api.notify('report', event.server, event.rUser,
channelName, dbot.t('report', {
'reporter': event.rUser.primaryNick,
'reportee': nick,
'reason': reason
}));
event.reply(dbot.t('reported', { 'reported': nick }));
} else {
event.reply(dbot.t('user_not_found', {
'reported': nick,
'channel': channelName
}));
}
} else {
event.reply(dbot.t('not_in_channel', { 'channel': channelName }));
}
}.bind(this));
},
'~notify': function(event) {
var channelName = event.input[1],
message = event.input[2];
if(_.has(event.allChannels, channelName)) {
if(this.config.firstHost) {
var first = message.split(' ')[0];
2014-09-04 16:54:49 +02:00
dbot.api.users.resolveUser(event.server, first, function(err, user) {
if(user && _.include(this.config.host_lookup, channelName)) {
2013-10-21 17:40:45 +02:00
dbot.api.nickserv.getUserHost(event.server, first, function(host) {
message = message.replace(first, first + ' [' + host + ']');
2015-03-14 10:24:51 +01:00
this.api.notify('notify', event.server, event.rUser, channelName, message, host);
2013-10-21 17:40:45 +02:00
}.bind(this));
} else {
this.api.notify('notify', event.server, event.rUser, channelName, message);
}
}.bind(this));
} else {
this.api.notify('notify', event.server, event.rUser, channelName, message);
}
event.reply(dbot.t('notified', {
'user': event.user,
'channel': channelName
}));
} else {
event.reply(dbot.t('not_in_channel', { 'channel': channelName }));
}
},
'~nunsub': function(event) {
2014-09-15 23:12:30 +02:00
var cName = event.input[1],
cId = event.input[1] + '.' + event.server;
2013-10-21 17:40:45 +02:00
2014-09-15 23:12:30 +02:00
if(_.has(dbot.instance.connections[event.server].channels, cName)) {
this.db.read('nunsubs', cId, function(err, nunsubs) {
if(!nunsubs) {
var nunsubs = {
'id': cId,
'users': []
2013-10-21 17:40:45 +02:00
}
2014-09-15 23:12:30 +02:00
}
if(!_.include(nunsubs, event.rUser.id)) {
nunsubs.users.push(event.rUser.id);
this.db.save('nunsubs', cId, nunsubs, function() {
var reply = dbot.t('nunsubbed', { 'cName': cName })
if(_.has(this.config.chan_redirs, cName)) {
reply += dbot.t('n_also_found', { 'afaName' : this.config.chan_redirs[cName] });
}
event.reply(reply);
}.bind(this));
} else {
event.reply(dbot.t('already_nunsubbed', { 'cName': cName }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
2013-10-21 17:40:45 +02:00
},
'~ununsub': function(event) {
2014-09-15 23:12:30 +02:00
var cName = event.input[1],
cId = event.input[1] + '.' + event.server;
if(_.has(dbot.instance.connections[event.server].channels, cName)) {
this.db.read('nunsubs', cId, function(err, nunsubs) {
if(!_.isUndefined(nunsubs) && _.include(nunsubs.users, event.rUser.id)) {
nunsubs.users = _.without(nunsubs.users, event.rUser.id);
this.db.save('nunsubs', cId, nunsubs, function() {
event.reply(dbot.t('ununsubbed', { 'cName': cName }));
});
} else {
event.reply(dbot.t('not_nunsubbed', { 'cName': cName }));
}
}.bind(this));
} else {
event.reply('Channel not known.');
}
2015-03-14 10:42:13 +01:00
},
'~concerning': function(event) {
var nick = event.params[1].trim();
dbot.api.nickserv.getUserHost(event.server, nick, function(host) {
if(host) {
var results = [];
this.db.scan('notifies', function(notify) {
2015-03-14 10:59:16 +01:00
if(notify && _.has(notify, 'host') && (notify.host == host || notify.message.split(' ')[0] == nick)) {
2015-03-14 11:16:17 +01:00
results.push(notify);
2015-03-14 10:54:41 +01:00
}
2015-03-14 10:42:13 +01:00
}, function() {
2015-03-14 10:59:16 +01:00
event.reply(nick + ' has sought help ' + results.length + ' times under the host ' + host + ' or nick ' + nick);
2015-03-14 10:42:13 +01:00
_.each(results, function(n) {
2015-03-14 11:17:19 +01:00
event.reply('[' + moment(parseInt(n.time)).format('DD/MM/YYYY') + '][' + n.user.split('.')[0] + '] ' + n.message);
2015-03-14 10:42:13 +01:00
});
});
} else {
dbot.api.quotes.getQuote('deal with it', function(quote) {
var out = 'Couldn\'t find user\'s host, but that doesn\'t necessarily mean they don\'t exist. This is the lazy way of doing it for now... ';
if(quote) {
out += quote;
}
event.reply(out);
});
}
2015-03-14 10:54:41 +01:00
}.bind(this));
2013-10-21 17:40:45 +02:00
}
};
2014-02-04 01:58:09 +01:00
commands['~report'].regex = /^report (#[^ ]+ )?([^ ]+) (.*)$/;
2013-12-29 19:38:24 +01:00
commands['~notify'].regex = [/^notify ([^ ]+) (.+)$/, 3];
commands['~nunsub'].regex = [/^nunsub ([^ ]+)$/, 2];
commands['~ununsub'].regex = [/^ununsub ([^ ]+)$/, 2];
2014-12-16 03:50:03 +01:00
commands['~ustatus'].regex = [/^ustatus ([^ ]+)$/, 2];
2015-01-15 03:41:11 +01:00
commands['~sustatus'].regex = [/^sustatus ([^ ]+)$/, 2];
2015-01-15 03:46:02 +01:00
commands['~ustatus'].access = 'power_user';
commands['~sustatus'].access = 'power_user';
2015-03-14 10:42:13 +01:00
commands['~sustatus'].access = 'concerning';
2013-10-21 17:40:45 +02:00
return commands;
};
exports.fetch = function(dbot) {
return commands(dbot);
};