2012-05-19 20:14:07 +02:00
|
|
|
/**
|
|
|
|
* Module Name: Command
|
|
|
|
* Description: An essential module which maps PRIVMSG input to an appropriate
|
|
|
|
* command and then runs that command, given the user isn't banned from or
|
|
|
|
* ignoring that command.
|
|
|
|
*/
|
2012-03-10 16:28:42 +01:00
|
|
|
var command = function(dbot) {
|
2012-05-19 20:14:07 +02:00
|
|
|
/**
|
|
|
|
* Is user banned from using command?
|
|
|
|
*/
|
2012-05-23 16:45:09 +02:00
|
|
|
var isBanned = function(user, command) {
|
2012-05-19 20:14:07 +02:00
|
|
|
var banned = false;
|
|
|
|
if(dbot.db.bans.hasOwnProperty(command)) {
|
|
|
|
if(dbot.db.bans[command].include(user) || dbot.db.bans['*'].include(user)) {
|
|
|
|
banned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return banned;
|
2012-05-23 16:45:09 +02:00
|
|
|
};
|
2012-05-19 20:14:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is user ignoring command?
|
|
|
|
*/
|
2012-05-23 16:45:09 +02:00
|
|
|
var isIgnoring = function(user, command) {
|
2012-05-19 20:14:07 +02:00
|
|
|
var module = dbot.commandMap[command];
|
|
|
|
var ignoring = false;
|
|
|
|
if(dbot.db.ignores.hasOwnProperty(user) && dbot.db.ignores[user].include(module)) {
|
|
|
|
ignoring = true;
|
|
|
|
}
|
|
|
|
return ignoring;
|
2012-05-23 16:45:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply Regex to event message, store result. Return false if it doesn't
|
|
|
|
* apply.
|
|
|
|
*/
|
|
|
|
var applyRegex = function(commandName, event) {
|
|
|
|
var applies = false;
|
|
|
|
if(dbot.commands[commandName].hasOwnProperty(regex)) {
|
|
|
|
var cRegex = dbot.commands[commandName].regex;
|
|
|
|
var q = event.message.valMatch(cRegex[0], cRegex[1]);
|
|
|
|
if(q) {
|
|
|
|
applies = true;
|
|
|
|
event.input = q;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
applies = true;
|
|
|
|
}
|
|
|
|
return applies;
|
|
|
|
};
|
2012-04-15 22:43:02 +02:00
|
|
|
|
2012-05-19 20:14:07 +02:00
|
|
|
return {
|
|
|
|
'name': 'command',
|
2012-04-14 07:06:40 +02:00
|
|
|
|
2012-05-19 20:14:07 +02:00
|
|
|
/**
|
|
|
|
* Run the appropriate command given the input.
|
|
|
|
*/
|
|
|
|
'listener': function(event) {
|
2012-05-23 16:45:09 +02:00
|
|
|
var commandName = event.params[0];
|
|
|
|
if(dbot.commands.hasOwnProperty(commandName)) {
|
|
|
|
if(isBanned(event.user, commandName)) {
|
2012-05-19 20:14:07 +02:00
|
|
|
event.reply(dbot.t('command_ban', {'user': event.user}));
|
2012-03-12 14:45:52 +01:00
|
|
|
} else {
|
2012-05-23 16:45:09 +02:00
|
|
|
if(!isIgnoring(event.user, commandName)) {
|
|
|
|
if(applyRegex(commandName, event)) {
|
|
|
|
dbot.commands[commandName](event);
|
|
|
|
dbot.save();
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('syntax_error'));
|
|
|
|
}
|
2012-04-15 22:43:02 +02:00
|
|
|
}
|
2012-03-10 16:28:42 +01:00
|
|
|
}
|
2012-05-19 20:14:07 +02:00
|
|
|
}
|
2012-03-10 16:28:42 +01:00
|
|
|
},
|
|
|
|
|
2012-04-15 22:43:02 +02:00
|
|
|
'on': 'PRIVMSG',
|
|
|
|
'ignorable': false
|
2012-03-10 16:28:42 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return command(dbot);
|
|
|
|
};
|
|
|
|
|