2012-12-11 17:04:52 +01:00
|
|
|
/**
|
|
|
|
* Module Name: Ignore
|
|
|
|
* Description: Handles commands in which users can choose to ignore listeners
|
|
|
|
* and commands from certain modules. It also populates the JSBot instance with
|
2013-01-27 18:18:38 +01:00
|
|
|
* this information, since that actually performs the ignorance. Also provides
|
|
|
|
* commands for moderators to choose the bot to ignore certain channels.
|
2012-12-11 17:04:52 +01:00
|
|
|
*/
|
2013-04-09 02:10:08 +02:00
|
|
|
var _ = require('underscore')._,
|
|
|
|
databank = require('databank'),
|
|
|
|
NoSuchThingError = databank.NoSuchThingError;
|
2013-01-12 23:19:48 +01:00
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var ignore = function(dbot) {
|
|
|
|
var commands = {
|
|
|
|
'~ignore': function(event) {
|
2013-01-22 20:33:22 +01:00
|
|
|
var module = event.params[1];
|
|
|
|
var ignorableModules = _.chain(dbot.modules)
|
|
|
|
.filter(function(module, name) {
|
|
|
|
return dbot.config[module].ignorable === true;
|
|
|
|
})
|
|
|
|
.pluck('name')
|
|
|
|
.value();
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-12 23:19:48 +01:00
|
|
|
if(_.isUndefined(module)) {
|
|
|
|
event.reply(dbot.t('ignore_usage', {
|
|
|
|
'user': event.user,
|
|
|
|
'modules': ignorableModules.join(', ')
|
|
|
|
}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2013-03-21 11:40:54 +01:00
|
|
|
if(module == '*' || _.include(ignorableModules, module)) {
|
2013-04-09 02:10:08 +02:00
|
|
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
|
|
|
this.db.read('ignores', user.id, function(err, ignores) {
|
2013-04-10 02:32:30 +02:00
|
|
|
if(ignores == null) {
|
2013-04-09 02:10:08 +02:00
|
|
|
this.db.create('ignores', user.id, {
|
|
|
|
'id': user.id,
|
|
|
|
'ignores': [ module ]
|
|
|
|
}, function(err, result) {
|
|
|
|
if(!err) {
|
|
|
|
dbot.instance.ignoreTag(event.user, module);
|
|
|
|
event.reply(dbot.t('ignored', {
|
|
|
|
'user': event.user,
|
|
|
|
'module': module
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if(!_.include(ignores.ignores, module)) {
|
|
|
|
ignores.ignores.push(module);
|
2013-04-10 02:32:30 +02:00
|
|
|
this.db.save('ignores', user.id, ignores, function(err) {
|
2013-04-09 02:10:08 +02:00
|
|
|
if(!err) {
|
|
|
|
dbot.instance.ignoreTag(event.user, module);
|
|
|
|
event.reply(dbot.t('ignored', {
|
|
|
|
'user': event.user,
|
|
|
|
'module': module
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('already_ignoring', { 'user': event.user }));
|
|
|
|
}
|
|
|
|
}
|
2013-04-10 02:32:30 +02:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
2013-01-12 23:19:48 +01:00
|
|
|
event.reply(dbot.t('invalid_ignore', { 'user': event.user }));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~unignore': function(event) {
|
2013-01-22 20:33:22 +01:00
|
|
|
var module = event.params[1];
|
2013-04-10 02:32:30 +02:00
|
|
|
|
|
|
|
dbot.api.users.resolveUser(event.server, event.user, function(user) {
|
|
|
|
this.db.read('ignores', user.id, function(err, ignores) {
|
|
|
|
if(!ignores) {
|
|
|
|
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
|
|
|
|
} else if(_.isUndefined(module)) {
|
|
|
|
event.reply(dbot.t('unignore_usage', {
|
|
|
|
'user': event.user,
|
|
|
|
'modules': ignores.ignores.join(', ')
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
if(_.include(ignores.ignores, module)) {
|
|
|
|
ignores.ignores = _.without(ignores.ignores, module);
|
|
|
|
this.db.save('ignores', user.id, ignores, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
dbot.instance.removeIgnore(event.user, module)
|
|
|
|
event.reply(dbot.t('unignored', {
|
|
|
|
'user': event.user,
|
|
|
|
'module': module
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2013-04-10 01:16:40 +02:00
|
|
|
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
|
|
|
|
}
|
2013-04-10 02:32:30 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2013-01-27 18:18:38 +01:00
|
|
|
},
|
|
|
|
|
2013-03-21 11:39:57 +01:00
|
|
|
'~ban': function(event) {
|
|
|
|
var user = event.params[1];
|
|
|
|
var module = event.params[2];
|
|
|
|
|
|
|
|
if(_.isUndefined(user) || _.isUndefined(module)) {
|
|
|
|
event.reply(dbot.t('ban_usage', {'user': event.user}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(module == '*' || _.include(dbot.config.moduleNames, module) || _.include(dbot.commands, module)) {
|
|
|
|
if(_.has(dbot.db.bans, user) && _.include(dbot.db.bans[user], module)) {
|
|
|
|
event.reply(dbot.t('already_banned', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': user
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.has(dbot.db.bans, event.params[1])) {
|
|
|
|
dbot.db.bans[event.params[1]].push(module);
|
|
|
|
} else {
|
|
|
|
dbot.db.bans[event.params[1]] = [module];
|
|
|
|
}
|
|
|
|
|
|
|
|
event.reply(dbot.t('banned_success', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': user,
|
|
|
|
'module': module
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('invalid_ban', {'user': event.user}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~unban': function(event) {
|
|
|
|
var bannedModules = [];
|
|
|
|
|
|
|
|
var user = event.params[1];
|
|
|
|
var module = event.params[2];
|
|
|
|
|
|
|
|
if(_.isUndefined(user) || _.isUndefined(module)) {
|
|
|
|
event.reply(dbot.t('unban_usage', {'user': event.user}));
|
|
|
|
} else {
|
|
|
|
if(_.has(dbot.db.bans, user) && _.include(dbot.db.bans[user], module)) {
|
|
|
|
dbot.db.bans[user].splice(dbot.db.bans[user].indexOf(module), 1);
|
|
|
|
|
|
|
|
event.reply(dbot.t('unbanned_success', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': user,
|
|
|
|
'module': module
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('invalid_unban', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': user
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-01-27 18:18:38 +01:00
|
|
|
'~ignorechannel': function(event) {
|
|
|
|
var channel = ((event.params[1] == '@') ? event.channel.name : event.params[1]);
|
|
|
|
var module = event.params[2];
|
|
|
|
|
|
|
|
// Ignoring the value of 'ignorable' at the moment
|
2013-03-21 11:40:54 +01:00
|
|
|
if(module == '*' || _.include(dbot.config.moduleNames, module)) {
|
2013-01-27 18:18:38 +01:00
|
|
|
if(!_.has(dbot.db.ignores, channel)) dbot.db.ignores[channel] = [];
|
|
|
|
if(!_.include(dbot.db.ignores[channel], module)) {
|
|
|
|
dbot.db.ignores[channel].push(module);
|
|
|
|
dbot.instance.ignoreTag(channel, module);
|
|
|
|
event.reply(dbot.t('ignoring_channel', {
|
|
|
|
'module': module,
|
|
|
|
'channel': channel
|
2013-01-27 20:45:56 +01:00
|
|
|
}));
|
2013-01-27 18:18:38 +01:00
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('already_ignoring_channel', {
|
|
|
|
'module': module,
|
|
|
|
'channel': channel
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('module_not_exist', { 'module': module }));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~unignorechannel': function(event) {
|
|
|
|
var channel = ((event.params[1] == '@') ? event.channel.name : event.params[1]);
|
|
|
|
var module = event.params[2];
|
|
|
|
|
|
|
|
if(!_.has(dbot.db.ignores, channel)) dbot.db.ignores[channel] = [];
|
|
|
|
if(_.include(dbot.db.ignores[channel], module)) {
|
|
|
|
dbot.db.ignores[channel] = _.without(dbot.db.ignores[channel], module);
|
|
|
|
dbot.instance.removeIgnore(channel, module);
|
|
|
|
event.reply(dbot.t('unignoring_channel', {
|
|
|
|
'module': module,
|
|
|
|
'channel': channel
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('not_ignoring_channel', {
|
|
|
|
'module': module,
|
|
|
|
'channel': channel
|
|
|
|
}));
|
|
|
|
}
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
};
|
2013-01-27 18:18:38 +01:00
|
|
|
|
2013-03-21 11:39:57 +01:00
|
|
|
commands['~ban'].access = 'moderator';
|
|
|
|
commands['~unban'].access = 'moderator';
|
2013-01-27 18:18:38 +01:00
|
|
|
commands['~ignorechannel'].access = 'moderator';
|
|
|
|
commands['~unignorechannel'].access = 'moderator';
|
|
|
|
|
2013-01-15 00:02:55 +01:00
|
|
|
this.commands = commands;
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-15 00:02:55 +01:00
|
|
|
this.onLoad = function() {
|
|
|
|
dbot.instance.clearIgnores();
|
2013-04-10 01:16:40 +02:00
|
|
|
|
|
|
|
this.db.scan('ignores', function(ignores) {
|
|
|
|
dbot.api.users.getUser(ignores.id, function(user) {
|
|
|
|
if(user) {
|
|
|
|
_.each(ignores.ignores, function(module) {
|
|
|
|
dbot.instance.ignoreTag(user.primaryNick, module);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, function(err) { });
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-15 00:02:55 +01:00
|
|
|
return new ignore(dbot);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|