3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 20:39:25 +01:00

js runs in vm sandbox, documentation additions

This commit is contained in:
Luke Slater 2012-02-13 19:56:04 +00:00
parent c50e20d7bb
commit cc88d53de4
2 changed files with 11 additions and 4 deletions

View File

@ -1,12 +1,12 @@
var vm = require('vm');
var js = function(dbot) { var js = function(dbot) {
var dbot = dbot; var dbot = dbot;
var commands = { var commands = {
'~js': function(data, params) { '~js': function(data, params) {
var q = data.message.valMatch(/^~js (.*)/, 2); var q = data.message.valMatch(/^~js (.*)/, 2);
if(data.user == dbot.admin) { dbot.say(data.channel, vm.runInNewContext(q[1]));
dbot.say(data.channel, eval(q[1]));
}
} }
}; };

9
run.js
View File

@ -4,9 +4,11 @@ var jsbot = require('./jsbot');
require('./snippets'); require('./snippets');
var DBot = function(timers) { var DBot = function(timers) {
// Load external files
this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
this.db = JSON.parse(fs.readFileSync('db.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.name = this.config.name || 'dbox';
this.admin = this.config.admin || 'reality'; this.admin = this.config.admin || 'reality';
this.password = this.config.password || 'lolturtles'; this.password = this.config.password || 'lolturtles';
@ -25,20 +27,24 @@ var DBot = function(timers) {
} }
}.bind(this), this.nickserv, this.password); }.bind(this), this.nickserv, this.password);
// Load the modules and connect to the server
this.reloadModules(); this.reloadModules();
this.instance.connect(); this.instance.connect();
}; };
// Say something in a channel
DBot.prototype.say = function(channel, data) { DBot.prototype.say = function(channel, data) {
this.instance.say(channel, data); this.instance.say(channel, data);
}; };
// Save the database file
DBot.prototype.save = function() { DBot.prototype.save = function() {
fs.writeFile('db.json', JSON.stringify(this.db, null, ' ')); fs.writeFile('db.json', JSON.stringify(this.db, null, ' '));
}; };
// Hot-reload module files.
DBot.prototype.reloadModules = function() { DBot.prototype.reloadModules = function() {
if(this.modules) { if(this.modules) { // Run 'onDestroy' code for each module if it exists.
this.modules.each(function(module) { this.modules.each(function(module) {
if(module.onDestroy) { if(module.onDestroy) {
module.onDestroy(); module.onDestroy();
@ -52,6 +58,7 @@ DBot.prototype.reloadModules = function() {
this.timers.clearTimers(); this.timers.clearTimers();
this.save(); this.save();
// Reload Javascript snippets
var path = require.resolve('./snippets'); var path = require.resolve('./snippets');
delete require.cache[path]; delete require.cache[path];
require('./snippets'); require('./snippets');