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'),
|
2013-04-11 23:21:12 +02:00
|
|
|
uuid = require('node-uuid'),
|
2013-04-09 02:10:08 +02:00
|
|
|
NoSuchThingError = databank.NoSuchThingError;
|
2013-01-12 23:19:48 +01:00
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var ignore = function(dbot) {
|
2013-04-11 22:12:29 +02:00
|
|
|
this.internalAPI = {
|
2013-06-27 17:33:18 +02:00
|
|
|
'isUserImpeded': function(user, item, by, callback) {
|
|
|
|
this.api.getUserIgnores(user, function(err, ignores) {
|
2013-04-11 22:12:29 +02:00
|
|
|
var isImpeded = false;
|
|
|
|
if(!err && ignores) {
|
2014-09-08 23:02:10 +02:00
|
|
|
if(_.has(dbot.commands, item) && !_.include(ignores[by], item)) {
|
2013-04-11 22:12:29 +02:00
|
|
|
item = dbot.commands[item].module;
|
|
|
|
}
|
|
|
|
if(_.include(ignores[by], item)) {
|
|
|
|
isImpeded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(isImpeded);
|
|
|
|
});
|
|
|
|
}.bind(this)
|
|
|
|
};
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
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) {
|
2013-06-27 17:33:18 +02:00
|
|
|
return dbot.config.modules[module].ignorable === true;
|
2013-01-22 20:33:22 +01:00
|
|
|
})
|
|
|
|
.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-06-27 17:33:18 +02:00
|
|
|
this.api.getUserIgnores(event.rUser, function(err, ignores) {
|
|
|
|
if(!ignores) {
|
|
|
|
ignores = {
|
|
|
|
'id': event.rUser.id,
|
|
|
|
'ignores': [],
|
|
|
|
'bans': []
|
|
|
|
};
|
|
|
|
}
|
2013-04-12 19:30:56 +02:00
|
|
|
|
2013-06-27 17:33:18 +02:00
|
|
|
if(!_.include(ignores.ignores, module)) {
|
|
|
|
ignores.ignores.push(module);
|
|
|
|
this.db.save('ignores', event.rUser.id, ignores, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
dbot.instance.ignoreTag(event.user, module);
|
|
|
|
event.reply(dbot.t('ignored', {
|
|
|
|
'user': event.user,
|
|
|
|
'module': module
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2013-04-12 19:30:56 +02:00
|
|
|
} else {
|
2013-06-27 17:33:18 +02:00
|
|
|
event.reply(dbot.t('already_ignoring', { 'user': event.user }));
|
2013-04-11 22:12:29 +02:00
|
|
|
}
|
2013-04-10 02:32:30 +02:00
|
|
|
}.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 03:28:53 +02:00
|
|
|
|
2013-06-27 17:33:18 +02:00
|
|
|
this.api.getUserIgnores(event.rUser, function(err, ignores) {
|
2013-04-11 22:12:29 +02:00
|
|
|
if(err || !ignores || _.isUndefined(module)) {
|
2013-06-27 17:33:18 +02:00
|
|
|
if(ignores) {
|
|
|
|
event.reply(dbot.t('unignore_usage', {
|
|
|
|
'user': event.user,
|
|
|
|
'modules': ignores.ignores.join(', ')
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('empty_unignore_usage', {
|
|
|
|
'user': event.user
|
|
|
|
}));
|
|
|
|
}
|
2013-04-11 22:12:29 +02:00
|
|
|
} else {
|
|
|
|
if(_.include(ignores.ignores, module)) {
|
|
|
|
ignores.ignores = _.without(ignores.ignores, module);
|
2013-06-27 17:33:18 +02:00
|
|
|
this.db.save('ignores', event.rUser.id, ignores, function(err) {
|
2013-04-11 22:12:29 +02:00
|
|
|
if(!err) {
|
|
|
|
dbot.instance.removeIgnore(event.user, module)
|
|
|
|
event.reply(dbot.t('unignored', {
|
|
|
|
'user': event.user,
|
|
|
|
'module': module
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
2013-04-10 02:32:30 +02:00
|
|
|
} else {
|
2013-04-11 22:12:29 +02:00
|
|
|
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
|
|
|
|
}
|
|
|
|
}
|
2013-04-10 02:32:30 +02:00
|
|
|
}.bind(this));
|
2013-01-27 18:18:38 +01:00
|
|
|
},
|
|
|
|
|
2013-03-21 11:39:57 +01:00
|
|
|
'~ban': function(event) {
|
2013-04-12 19:30:56 +02:00
|
|
|
var nick = event.input[1],
|
2013-04-11 22:12:29 +02:00
|
|
|
item = event.input[2];
|
|
|
|
|
|
|
|
if(module == '*' || _.include(dbot.config.moduleNames, item) || _.include(dbot.commands, item)) {
|
2016-05-12 09:38:18 +02:00
|
|
|
dbot.api.users.resolveUser(event.server, nick, function(err, user) {
|
2013-06-27 17:33:18 +02:00
|
|
|
this.api.getUserIgnores(user, function(err, ignores) {
|
|
|
|
if(!err) {
|
|
|
|
if(!ignores) {
|
|
|
|
ignores = {
|
|
|
|
'id': user.id,
|
|
|
|
'ignores': [],
|
|
|
|
'bans': []
|
|
|
|
};
|
|
|
|
}
|
2013-04-12 19:30:56 +02:00
|
|
|
|
2013-06-27 17:33:18 +02:00
|
|
|
if(!_.include(ignores.bans, item)) {
|
|
|
|
ignores.bans.push(item);
|
|
|
|
this.db.save('ignores', user.id, ignores, function(err) {
|
|
|
|
if(!err) {
|
|
|
|
event.reply(dbot.t('banned_success', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': nick,
|
|
|
|
'module': item
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('already_banned', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': nick
|
|
|
|
}));
|
|
|
|
}
|
2013-04-11 22:12:29 +02:00
|
|
|
}
|
2013-06-27 17:33:18 +02:00
|
|
|
}.bind(this));
|
2016-05-12 09:38:18 +02:00
|
|
|
}.bind(this));
|
2013-03-21 11:39:57 +01:00
|
|
|
} else {
|
2013-04-11 22:12:29 +02:00
|
|
|
event.reply(dbot.t('invalid_ban', { 'user': event.user }));
|
2013-03-21 11:39:57 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~unban': function(event) {
|
2013-04-12 19:30:56 +02:00
|
|
|
var nick = event.input[1],
|
|
|
|
item = event.input[2];
|
2013-03-21 11:39:57 +01:00
|
|
|
|
2013-06-27 17:33:18 +02:00
|
|
|
dbot.api.users.resolveUser(event.server, nick, function(user) {
|
|
|
|
this.api.getUserIgnores(user, function(err, ignores) {
|
|
|
|
if(err || !ignores) {
|
2013-04-11 22:12:29 +02:00
|
|
|
event.reply(dbot.t('invalid_unban', {
|
|
|
|
'user': event.user,
|
2013-06-27 17:33:18 +02:00
|
|
|
'banned': nick
|
2013-04-11 22:12:29 +02:00
|
|
|
}));
|
2013-06-27 17:33:18 +02:00
|
|
|
} else {
|
|
|
|
if(_.include(ignores.bans, item)) {
|
|
|
|
ignores.bans = _.without(ignores.bans, item);
|
|
|
|
this.db.save('ignores', user.id, ignores, function(err) {
|
|
|
|
event.reply(dbot.t('unbanned_success', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': nick,
|
|
|
|
'module': item
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('invalid_unban', {
|
|
|
|
'user': event.user,
|
|
|
|
'banned': nick
|
|
|
|
}));
|
|
|
|
}
|
2013-04-11 22:12:29 +02:00
|
|
|
}
|
2013-06-27 17:33:18 +02:00
|
|
|
}.bind(this));
|
2013-04-11 22:12:29 +02:00
|
|
|
}.bind(this));
|
2013-03-21 11:39:57 +01:00
|
|
|
},
|
|
|
|
|
2013-01-27 18:18:38 +01:00
|
|
|
'~ignorechannel': function(event) {
|
2013-04-11 23:21:12 +02:00
|
|
|
var channelName = event.input[1],
|
|
|
|
module = event.input[2];
|
2013-01-27 18:18:38 +01:00
|
|
|
|
|
|
|
// Ignoring the value of 'ignorable' at the moment
|
2013-03-21 11:40:54 +01:00
|
|
|
if(module == '*' || _.include(dbot.config.moduleNames, module)) {
|
2013-04-11 23:21:12 +02:00
|
|
|
var channel = false;
|
|
|
|
|
|
|
|
this.db.search('channel_ignores', {
|
|
|
|
'server': event.server,
|
|
|
|
'name': channelName
|
|
|
|
}, function(result) {
|
|
|
|
channel = result;
|
|
|
|
}, function(err) {
|
|
|
|
if(!channel) {
|
|
|
|
var id = uuid.v4();
|
2013-04-12 19:30:56 +02:00
|
|
|
channel = {
|
2013-04-11 23:21:12 +02:00
|
|
|
'id': id,
|
|
|
|
'server': event.server,
|
|
|
|
'name': channelName,
|
2013-04-12 19:30:56 +02:00
|
|
|
'ignores': []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.include(channel.ignores, module)) {
|
|
|
|
channel.ignores.push(module);
|
|
|
|
this.db.save('channel_ignores', channel.id, channel, function(err) {
|
2013-12-24 01:29:00 +01:00
|
|
|
dbot.instance.ignoreTag(channel.name, module);
|
2013-04-12 19:30:56 +02:00
|
|
|
event.reply(dbot.t('ignoring_channel', {
|
2013-04-11 23:21:12 +02:00
|
|
|
'module': module,
|
|
|
|
'channel': channelName
|
|
|
|
}));
|
2013-04-12 19:30:56 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('already_ignoring_channel', {
|
|
|
|
'module': module,
|
|
|
|
'channel': channelName
|
|
|
|
}));
|
2013-04-11 23:21:12 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2013-01-27 18:18:38 +01:00
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('module_not_exist', { 'module': module }));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'~unignorechannel': function(event) {
|
2013-04-11 23:21:12 +02:00
|
|
|
var channelName = event.input[1],
|
|
|
|
module = event.input[2],
|
|
|
|
channel = false;
|
|
|
|
|
|
|
|
this.db.search('channel_ignores', {
|
|
|
|
'server': event.server,
|
|
|
|
'name': channelName
|
|
|
|
}, function(result) {
|
|
|
|
channel = result;
|
|
|
|
}, function(err) {
|
|
|
|
if(channel && _.include(channel.ignores, module)) {
|
|
|
|
channel.ignores = _.without(channel.ignores, module);
|
|
|
|
this.db.save('channel_ignores', channel.id, channel, function(err) {
|
2013-12-24 01:29:00 +01:00
|
|
|
dbot.instance.removeIgnore(channel.name, module);
|
2013-04-11 23:21:12 +02:00
|
|
|
event.reply(dbot.t('unignoring_channel', {
|
|
|
|
'module': module,
|
|
|
|
'channel': channelName
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('not_ignoring_channel', {
|
|
|
|
'module': module,
|
|
|
|
'channel': channelName
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
};
|
2013-01-27 18:18:38 +01:00
|
|
|
|
2013-12-29 19:38:24 +01:00
|
|
|
commands['~ban'].regex = [/^ban ([^ ]+) ([^ ]+)$/, 3];
|
|
|
|
commands['~unban'].regex = [/^unban ([^ ]+) ([^ ]+)$/, 3];
|
|
|
|
commands['~ignorechannel'].regex = [/^ignorechannel ([^ ]+) ([^ ]+)$/, 3];
|
|
|
|
commands['~unignorechannel'].regex = [/^unignorechannel ([^ ]+) ([^ ]+)$/, 3];
|
2013-04-11 22:12:29 +02: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) {
|
2014-09-08 23:08:28 +02:00
|
|
|
dbot.api.users.getUser(ignores.id, function(err, user) {
|
2013-04-10 01:16:40 +02:00
|
|
|
if(user) {
|
|
|
|
_.each(ignores.ignores, function(module) {
|
2014-01-06 19:19:18 +01:00
|
|
|
dbot.instance.ignoreTag(user.currentNick, module);
|
2013-04-10 01:16:40 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, function(err) { });
|
2013-04-11 23:21:12 +02:00
|
|
|
|
|
|
|
this.db.scan('channel_ignores', function(channel) {
|
|
|
|
_.each(channel.ignores, function(module) {
|
2013-12-24 01:29:00 +01:00
|
|
|
dbot.instance.ignoreTag(channel.name, module);
|
2013-04-11 23:21:12 +02:00
|
|
|
});
|
|
|
|
}, function(err) { });
|
2014-01-06 19:19:18 +01:00
|
|
|
|
|
|
|
dbot.api.event.addHook('new_current_nick', function(user, oldNick) {
|
|
|
|
this.api.getUserIgnores(user, function(err, ignores) {
|
|
|
|
if(ignores) {
|
|
|
|
_.each(ignores.ignores, function(module) {
|
|
|
|
dbot.instance.removeIgnore(oldNick, module);
|
|
|
|
dbot.instance.ignoreTag(user.currentNick, module);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this);
|
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
|
|
|
};
|