2012-12-11 17:04:52 +01:00
|
|
|
/**
|
|
|
|
* 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 commands = {
|
|
|
|
// Run JS code sandboxed, return result to channel.
|
|
|
|
'~js': function(event) {
|
|
|
|
try {
|
2013-01-14 23:03:58 +01:00
|
|
|
var s = new sbox();
|
2012-12-11 17:04:52 +01:00
|
|
|
s.run(event.input[1], function(output) {
|
|
|
|
event.reply(output.result);
|
|
|
|
}.bind(this));
|
|
|
|
} catch(err) {}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Run JS code un-sandboxed, with access to DBot memory (admin-only).
|
|
|
|
'~ajs': function(event) {
|
2013-05-26 22:24:05 +02:00
|
|
|
var callback = function() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
for(var i=0;i<args.length;i++) {
|
|
|
|
var arg = args[i];
|
|
|
|
if(_.isObject(arg) && !_.isArray(arg)) {
|
|
|
|
arg = '[object Object]: ' + _.keys(arg).join(', ');
|
|
|
|
}
|
|
|
|
event.reply('Callback[' + i + ']: ' + arg);
|
|
|
|
}
|
|
|
|
};
|
2013-01-12 10:49:41 +01:00
|
|
|
var ret = eval(event.input[1]);
|
|
|
|
if(ret !== undefined) {
|
|
|
|
event.reply(ret);
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-12-29 19:38:24 +01:00
|
|
|
commands['~js'].regex = [/^js (.*)/, 2];
|
|
|
|
commands['~ajs'].regex = [/^ajs (.*)/, 2];
|
2013-01-12 10:49:41 +01:00
|
|
|
commands['~ajs'].access = 'admin';
|
|
|
|
|
2013-01-14 23:03:58 +01:00
|
|
|
this.name = 'js';
|
|
|
|
this.ignorable = true;
|
|
|
|
this.commands = commands;
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-14 23:03:58 +01:00
|
|
|
return new js(dbot);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|