2012-05-23 16:45:09 +02: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.
|
|
|
|
*/
|
2012-02-13 20:56:04 +01:00
|
|
|
var vm = require('vm');
|
2012-02-13 21:21:57 +01:00
|
|
|
var sbox = require('sandbox');
|
2012-02-13 20:56:04 +01:00
|
|
|
|
2011-10-12 17:42:19 +02:00
|
|
|
var js = function(dbot) {
|
2012-02-13 21:21:57 +01:00
|
|
|
var s = new sbox();
|
2011-10-12 17:42:19 +02:00
|
|
|
|
|
|
|
var commands = {
|
2012-05-23 16:45:09 +02:00
|
|
|
// Run JS code sandboxed, return result to channel.
|
2012-05-19 20:14:07 +02:00
|
|
|
'~js': function(event) {
|
2012-05-23 16:45:09 +02:00
|
|
|
s.run(event.input[1], function(output) {
|
2012-05-19 20:14:07 +02:00
|
|
|
event.reply(output.result);
|
|
|
|
}.bind(this));
|
2012-02-13 21:01:22 +01:00
|
|
|
},
|
|
|
|
|
2012-05-23 16:45:09 +02:00
|
|
|
// Run JS code un-sandboxed, with access to DBot memory (admin-only).
|
2012-05-19 20:14:07 +02:00
|
|
|
'~ajs': function(event) {
|
2012-03-10 15:56:06 +01:00
|
|
|
if(dbot.admin.include(data.user) ) {
|
2012-05-23 16:45:09 +02:00
|
|
|
var ret = eval(event.input[1]);
|
|
|
|
if(ret !== undefined) {
|
2012-05-19 20:14:07 +02:00
|
|
|
event.reply(ret);
|
2012-03-10 15:56:06 +01:00
|
|
|
}
|
2012-02-13 21:01:22 +01:00
|
|
|
}
|
2011-10-12 17:42:19 +02:00
|
|
|
}
|
|
|
|
};
|
2012-05-23 16:45:09 +02:00
|
|
|
commands['~js'].regex = [/^~js (.*)/, 2];
|
|
|
|
commands['~ajs'].regex = [/^~ajs (.*)/, 2];
|
2011-10-12 17:43:09 +02:00
|
|
|
|
|
|
|
return {
|
2012-04-15 22:43:02 +02:00
|
|
|
'name': 'js',
|
2012-05-23 16:45:09 +02:00
|
|
|
'ignorable': true,
|
2012-04-15 22:43:02 +02:00
|
|
|
|
2012-05-23 16:45:09 +02:00
|
|
|
'onLoad': function() {
|
|
|
|
return commands;
|
|
|
|
}
|
2011-10-12 17:43:09 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return js(dbot);
|
|
|
|
};
|