From 4a7fcd57b3a182480d5cc85302f9083b00f717ae Mon Sep 17 00:00:00 2001 From: reality Date: Mon, 3 Jun 2013 23:00:23 +0000 Subject: [PATCH] Module config now loaded from config.json , rleloaded upon reloading and various module changes to make it work right [#474] [#489] --- modules/admin/admin.js | 4 +- modules/dent/dent.js | 7 ++-- modules/project/pages.js | 4 +- modules/project/project.js | 12 +++--- modules/web/web.js | 5 ++- run.js | 80 +++++++++++++++++--------------------- 6 files changed, 52 insertions(+), 60 deletions(-) diff --git a/modules/admin/admin.js b/modules/admin/admin.js index d596e51..93a0ae5 100644 --- a/modules/admin/admin.js +++ b/modules/admin/admin.js @@ -38,7 +38,7 @@ var admin = function(dbot) { }; this.onLoad = function() { - var configMap = dbot.config; + /*var configMap = dbot.config; this.db.scan('config', function(config) { if(config) { var currentPath = configMap, @@ -53,7 +53,7 @@ var admin = function(dbot) { currentPath[key[i]] = value; } - }, function(err) { }); + }, function(err) { });*/ }.bind(this); }; diff --git a/modules/dent/dent.js b/modules/dent/dent.js index 756b13d..9b03b45 100644 --- a/modules/dent/dent.js +++ b/modules/dent/dent.js @@ -2,7 +2,6 @@ var request = require('request'); _ = require('underscore')._; var dent = function(dbot) { - this.dbot = dbot; this.StatusRegex = { identica: /\bhttps?:\/\/identi\.ca\/notice\/(\d+)\b/ig, twitter: /\bhttps?:\/\/twitter\.com\/\w+\/status\/(\d+)\b/ig @@ -15,8 +14,8 @@ var dent = function(dbot) { this.api = { 'post': function(content) { - var username = dbot.config.dent.username, - password = dbot.config.dent.password, + var username = this.config.username, + password = this.config.password, info, auth = "Basic " + new Buffer(username + ":" + password).toString("base64"); @@ -57,7 +56,7 @@ var dent = function(dbot) { this.commands['~dent'].regex = [/^~dent (.+)$/, 2]; this.onLoad = function() { - if(dbot.config.dent.dentQuotes === true && _.has(dbot.modules, 'quotes')) { + if(this.config.dentQuotes === true && _.has(dbot.modules, 'quotes')) { dbot.api.event.addHook('~qadd', function(key, text) { if(text.indexOf('~~') == -1) { this.api.post(key + ': ' + text); diff --git a/modules/project/pages.js b/modules/project/pages.js index 929d7a8..a713f8a 100644 --- a/modules/project/pages.js +++ b/modules/project/pages.js @@ -30,7 +30,7 @@ var pages = function(dbot) { /* TODO: merge back into github module */ var milestones; - request({"url":"https://api.github.com/repos/" + dbot.config.github.defaultrepo + "/milestones?state=open","headers":{"User-Agent":"reality/depressionbot (project module)"}}, function(error, response, body){ + request({"url":"https://api.github.com/repos/" + dbot.config.modules.github.defaultrepo + "/milestones?state=open","headers":{"User-Agent":"reality/depressionbot (project module)"}}, function(error, response, body){ milestones = JSON.parse(body); }); @@ -53,7 +53,7 @@ var pages = function(dbot) { "botname": dbot.config.name }), "curr839": dbot.config.language, - "repo": dbot.config.github.defaultrepo, + "repo": dbot.config.modules.github.defaultrepo, "branch": dbot.t("branch",{ "branch": branch }), diff --git a/modules/project/project.js b/modules/project/project.js index 5bb291e..13a6b9d 100644 --- a/modules/project/project.js +++ b/modules/project/project.js @@ -14,30 +14,30 @@ var project = function(dbot) { var list = []; if(_.has(dbot.modules,'dent')){ list.push(dbot.t("dent-account", { - "username": dbot.config.dent.username + "username": dbot.config.modules.dent.username })); - if(_.has(dbot.config.dent.dentQuotes)) { + if(_.has(dbot.config.modules.dent.dentQuotes)) { list.push(dbot.t("dent-push")); } } if(_.has(dbot.modules,'link')){ - if(dbot.config.link.autoTitle){ + if(dbot.config.modules.link.autoTitle){ list.push(dbot.t("link-autotitle")); } } if(_.has(dbot.modules,'quotes')){ list.push(dbot.t("quote-rmlimit", { - "limit": dbot.config.quotes.rmLimit + "limit": dbot.config.modules.quotes.rmLimit })); } if(_.has(dbot.modules,'report')){ - if(dbot.config.report.notifyVoice){ + if(dbot.config.modules.report.notifyVoice){ list.push(dbot.t("report-notifyvoice")); } } if(_.has(dbot.modules,'web')){ list.push(dbot.t("web-port", { - "port": dbot.config.web.webPort + "port": dbot.config.modules.web.webPort })); } return list; diff --git a/modules/web/web.js b/modules/web/web.js index 81a15ed..75d3c08 100644 --- a/modules/web/web.js +++ b/modules/web/web.js @@ -3,13 +3,14 @@ var express = require('express'), fs = require('fs'); var webInterface = function(dbot) { + this.config = dbot.config.modules.web; this.pub = 'public'; this.app = express(); this.app.use(express.static(this.pub)); this.app.set('view engine', 'jade'); - - var server = this.app.listen(dbot.config.web.webPort); + + var server = this.app.listen(this.config.webPort); this.reloadPages = function() { var pages = dbot.pages; diff --git a/run.js b/run.js index 7210c3f..a78d525 100644 --- a/run.js +++ b/run.js @@ -19,37 +19,10 @@ var DBot = function() { this.db = {}; } - if(!_.has(this.db, 'config')) { - this.db.config = {}; - } - /*** Load the fancy DB ***/ this.ddb = new DatabaseDriver(); - /*** Load the Config ***/ - - if(!fs.existsSync('config.json')) { - console.log('Error: config.json file does not exist. Stopping'); - process.exit(); - } - - this.config = _.clone(this.db.config); - try { - _.defaults(this.config, JSON.parse(fs.readFileSync('config.json', 'utf-8'))); - } catch(err) { - console.log('Config file is invalid. Stopping: ' + err); - process.exit(); - } - - try { - var defaultConfig = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8')); - } catch(err) { - console.log('Error loading sample config. Bugger off this should not even be edited. Stopping.'); - process.exit(); - } - - // Load missing config directives from sample file - _.defaults(this.config, defaultConfig); + this.reloadConfig(); /*** Load main strings ***/ @@ -83,6 +56,33 @@ var DBot = function() { this.instance.connectAll(); }; +DBot.prototype.reloadConfig = function() { + this.config = {}; + + if(!fs.existsSync('config.json')) { + console.log('Error: config.json file does not exist. Stopping'); + process.exit(); + } + + try { + this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); + } catch(err) { + console.log('Config file is invalid. Stopping: ' + err); + process.exit(); + } + + try { + var defaultConfig = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8')); + } catch(err) { + console.log('Error loading sample config. Bugger off this should not even be edited. Stopping.'); + process.exit(); + } + + // Load missing config directives from sample file + if(!_.has(this.config, 'modules')) this.config.modules = {}; + _.defaults(this.config, defaultConfig); +}; + // Say something in a channel DBot.prototype.say = function(server, channel, message) { this.instance.say(server, channel, message); @@ -138,6 +138,7 @@ DBot.prototype.reloadModules = function() { this.api = {}; this.stringMap = {}; this.usage = {}; + this.reloadConfig(); try { this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); @@ -173,25 +174,17 @@ DBot.prototype.reloadModules = function() { continue; } - // Load the module config data - config = {}; - - if(_.has(this.db.config, name)) { - config = _.clone(this.db.config[name]); - } - - try { - var defaultConfig = fs.readFileSync(moduleDir + 'config.json', 'utf-8'); + if(!_.has(this.config.modules, name)) this.config.modules[name] = {}; + if(fs.existsSync(moduleDir + 'config.json')) { try { - defaultConfig = JSON.parse(defaultConfig); - } catch(err) { // syntax error + var defaultConfig = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8')); + } catch(err) { // Syntax error this.status[name] = 'Error parsing config: ' + err + ' ' + err.stack.split('\n')[2].trim(); continue; } - config = _.defaults(config, defaultConfig); - } catch(err) { - // Invalid or no config data + this.config.modules[name] = _.defaults(this.config.modules[name], defaultConfig); } + var config = this.config.modules[name]; // Don't shit out if dependencies not met if(_.has(config, 'dependencies')) { @@ -202,7 +195,6 @@ DBot.prototype.reloadModules = function() { } }, this); } - this.config[name] = config; // Groovy funky database shit if(!_.has(config, 'dbType') || config.dbType == 'olde') { @@ -235,7 +227,7 @@ DBot.prototype.reloadModules = function() { module.name = name; module.db = this.ddb.databanks[name]; - module.config = this.config[name]; + module.config = this.config.modules[name]; // Load the module data _.each([ 'commands', 'pages', 'api' ], function(property) {