From 6c7fc838d4f83fff24a7f6d97c5023c96403f401 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Wed, 23 May 2012 15:45:09 +0100 Subject: [PATCH] Apply regex in command.js module and add results in input property on event object. --- modules/command.js | 45 +++++++++++++++++++++++++++++++++------------ modules/js.js | 29 +++++++++++++++++------------ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/modules/command.js b/modules/command.js index 300fedc..15586b5 100644 --- a/modules/command.js +++ b/modules/command.js @@ -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')); + } } } } diff --git a/modules/js.js b/modules/js.js index b4efb78..408a40b 100644 --- a/modules/js.js +++ b/modules/js.js @@ -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 + } }; };