3
0
mirror of https://github.com/reality/dbot.git synced 2025-01-26 03:54:18 +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. * ignoring that command.
*/ */
var command = function(dbot) { var command = function(dbot) {
var dbot = dbot;
/** /**
* Is user banned from using command? * Is user banned from using command?
*/ */
var is_banned = function(user, command) { var isBanned = function(user, command) {
var banned = false; var banned = false;
if(dbot.db.bans.hasOwnProperty(command)) { if(dbot.db.bans.hasOwnProperty(command)) {
if(dbot.db.bans[command].include(user) || dbot.db.bans['*'].include(user)) { if(dbot.db.bans[command].include(user) || dbot.db.bans['*'].include(user)) {
@ -18,19 +16,38 @@ var command = function(dbot) {
} }
} }
return banned; return banned;
} };
/** /**
* Is user ignoring command? * Is user ignoring command?
*/ */
var is_ignoring = function(user, command) { var isIgnoring = function(user, command) {
var module = dbot.commandMap[command]; var module = dbot.commandMap[command];
var ignoring = false; var ignoring = false;
if(dbot.db.ignores.hasOwnProperty(user) && dbot.db.ignores[user].include(module)) { if(dbot.db.ignores.hasOwnProperty(user) && dbot.db.ignores[user].include(module)) {
ignoring = true; ignoring = true;
} }
return ignoring; 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 { return {
'name': 'command', 'name': 'command',
@ -39,14 +56,18 @@ var command = function(dbot) {
* Run the appropriate command given the input. * Run the appropriate command given the input.
*/ */
'listener': function(event) { 'listener': function(event) {
var command_name = event.params[0]; var commandName = event.params[0];
if(dbot.commands.hasOwnProperty(command_name)) { if(dbot.commands.hasOwnProperty(commandName)) {
if(is_banned(event.user, command_name)) { if(isBanned(event.user, commandName)) {
event.reply(dbot.t('command_ban', {'user': event.user})); event.reply(dbot.t('command_ban', {'user': event.user}));
} else { } else {
if(!is_ignoring(event.user, command_name)) { if(!isIgnoring(event.user, commandName)) {
dbot.commands[command_name](event); if(applyRegex(commandName, event)) {
dbot.commands[commandName](event);
dbot.save(); 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 vm = require('vm');
var sbox = require('sandbox'); var sbox = require('sandbox');
var js = function(dbot) { var js = function(dbot) {
var dbot = dbot;
var s = new sbox(); var s = new sbox();
var commands = { var commands = {
// Run JS code sandboxed, return result to channel.
'~js': function(event) { '~js': function(event) {
console.log('hello'); s.run(event.input[1], function(output) {
var q = event.message.valMatch(/^~js (.*)/, 2);
s.run(q[1], function(output) {
event.reply(output.result); event.reply(output.result);
}.bind(this)); }.bind(this));
}, },
// Run JS code un-sandboxed, with access to DBot memory (admin-only).
'~ajs': function(event) { '~ajs': function(event) {
var q = data.message.valMatch(/^~ajs (.*)/, 2);
if(dbot.admin.include(data.user) ) { if(dbot.admin.include(data.user) ) {
var ret = eval(q[1]); var ret = eval(event.input[1]);
if(ret != undefined) { if(ret !== undefined) {
event.reply(ret); event.reply(ret);
} }
} }
} }
}; };
commands['~js'].regex = [/^~js (.*)/, 2];
commands['~ajs'].regex = [/^~ajs (.*)/, 2];
return { return {
'name': 'js',
'ignorable': true,
'onLoad': function() { 'onLoad': function() {
return commands; return commands;
}, }
'name': 'js',
'ignorable': true
}; };
}; };