From cc88d53de46b34dc74d17a3253cb96e7c92f848b Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Mon, 13 Feb 2012 19:56:04 +0000 Subject: [PATCH] js runs in vm sandbox, documentation additions --- modules/js.js | 6 +++--- run.js | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/modules/js.js b/modules/js.js index b59dfa4..6286a1a 100644 --- a/modules/js.js +++ b/modules/js.js @@ -1,12 +1,12 @@ +var vm = require('vm'); + var js = function(dbot) { var dbot = dbot; var commands = { '~js': function(data, params) { var q = data.message.valMatch(/^~js (.*)/, 2); - if(data.user == dbot.admin) { - dbot.say(data.channel, eval(q[1])); - } + dbot.say(data.channel, vm.runInNewContext(q[1])); } }; diff --git a/run.js b/run.js index b8c6966..2a39e3f 100644 --- a/run.js +++ b/run.js @@ -4,9 +4,11 @@ var jsbot = require('./jsbot'); require('./snippets'); var DBot = function(timers) { + // Load external files this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); + // Populate bot properties with config data this.name = this.config.name || 'dbox'; this.admin = this.config.admin || 'reality'; this.password = this.config.password || 'lolturtles'; @@ -25,20 +27,24 @@ var DBot = function(timers) { } }.bind(this), this.nickserv, this.password); + // Load the modules and connect to the server this.reloadModules(); this.instance.connect(); }; +// Say something in a channel DBot.prototype.say = function(channel, data) { this.instance.say(channel, data); }; +// Save the database file DBot.prototype.save = function() { fs.writeFile('db.json', JSON.stringify(this.db, null, ' ')); }; +// Hot-reload module files. DBot.prototype.reloadModules = function() { - if(this.modules) { + if(this.modules) { // Run 'onDestroy' code for each module if it exists. this.modules.each(function(module) { if(module.onDestroy) { module.onDestroy(); @@ -52,6 +58,7 @@ DBot.prototype.reloadModules = function() { this.timers.clearTimers(); this.save(); + // Reload Javascript snippets var path = require.resolve('./snippets'); delete require.cache[path]; require('./snippets');