dbot/modules/js.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

/**
* 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');
2011-10-12 17:42:19 +02:00
var js = function(dbot) {
var s = new sbox();
2011-10-12 17:42:19 +02:00
var commands = {
// Run JS code sandboxed, return result to channel.
'~js': function(event) {
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) {
2012-05-23 16:56:13 +02:00
if(dbot.admin.include(event.user) ) {
var ret = eval(event.input[1]);
if(ret !== undefined) {
event.reply(ret);
}
}
2011-10-12 17:42:19 +02:00
}
};
commands['~js'].regex = [/^~js (.*)/, 2];
commands['~ajs'].regex = [/^~ajs (.*)/, 2];
2011-10-12 17:43:09 +02:00
return {
'name': 'js',
'ignorable': true,
'onLoad': function() {
return commands;
}
2011-10-12 17:43:09 +02:00
};
};
exports.fetch = function(dbot) {
return js(dbot);
};