3
0
mirror of https://github.com/reality/dbot.git synced 2025-01-11 20:42:37 +01:00

Apply regex in command.js module and add results in input property on event object.

This commit is contained in:
Luke Slater 2012-05-23 15:45:09 +01:00
parent 1f02e29a85
commit 6c7fc838d4
2 changed files with 50 additions and 24 deletions

View File

@ -5,12 +5,10 @@
* ignoring that command.
*/
var command = function(dbot) {
var dbot = dbot;
/**
* Is user banned from using command?
*/
var is_banned = function(user, command) {
var isBanned = function(user, command) {
var banned = false;
if(dbot.db.bans.hasOwnProperty(command)) {
if(dbot.db.bans[command].include(user) || dbot.db.bans['*'].include(user)) {
@ -18,19 +16,38 @@ var command = function(dbot) {
}
}
return banned;
}
};
/**
* Is user ignoring command?
*/
var is_ignoring = function(user, command) {
var isIgnoring = function(user, command) {
var module = dbot.commandMap[command];
var ignoring = false;
if(dbot.db.ignores.hasOwnProperty(user) && dbot.db.ignores[user].include(module)) {
ignoring = true;
}
return ignoring;
}
};
/**
* 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;
};
return {
'name': 'command',
@ -39,14 +56,18 @@ var command = function(dbot) {
* Run the appropriate command given the input.
*/
'listener': function(event) {
var command_name = event.params[0];
if(dbot.commands.hasOwnProperty(command_name)) {
if(is_banned(event.user, command_name)) {
var commandName = event.params[0];
if(dbot.commands.hasOwnProperty(commandName)) {
if(isBanned(event.user, commandName)) {
event.reply(dbot.t('command_ban', {'user': event.user}));
} else {
if(!is_ignoring(event.user, command_name)) {
dbot.commands[command_name](event);
dbot.save();
if(!isIgnoring(event.user, commandName)) {
if(applyRegex(commandName, event)) {
dbot.commands[commandName](event);
dbot.save();
} else {
event.reply(dbot.t('syntax_error'));
}
}
}
}

View File

@ -1,38 +1,43 @@
/**
* Module Name: JS
* Description: Allows users to run sandboxed JS code, printing the result in
* the channel. Also allows admins to run un-sandboxed Javascript code with
* access to the DepressionBot instance memory.
*/
var vm = require('vm');
var sbox = require('sandbox');
var js = function(dbot) {
var dbot = dbot;
var s = new sbox();
var commands = {
// Run JS code sandboxed, return result to channel.
'~js': function(event) {
console.log('hello');
var q = event.message.valMatch(/^~js (.*)/, 2);
s.run(q[1], function(output) {
s.run(event.input[1], function(output) {
event.reply(output.result);
}.bind(this));
},
// Run JS code un-sandboxed, with access to DBot memory (admin-only).
'~ajs': function(event) {
var q = data.message.valMatch(/^~ajs (.*)/, 2);
if(dbot.admin.include(data.user) ) {
var ret = eval(q[1]);
if(ret != undefined) {
var ret = eval(event.input[1]);
if(ret !== undefined) {
event.reply(ret);
}
}
}
};
commands['~js'].regex = [/^~js (.*)/, 2];
commands['~ajs'].regex = [/^~ajs (.*)/, 2];
return {
'name': 'js',
'ignorable': true,
'onLoad': function() {
return commands;
},
'name': 'js',
'ignorable': true
}
};
};