3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00

Module config now loaded from config.json , rleloaded upon reloading and various module changes to make it work right [#474] [#489]

This commit is contained in:
reality 2013-06-03 23:00:23 +00:00
parent 2471e4bca3
commit 4a7fcd57b3
6 changed files with 52 additions and 60 deletions

View File

@ -38,7 +38,7 @@ var admin = function(dbot) {
}; };
this.onLoad = function() { this.onLoad = function() {
var configMap = dbot.config; /*var configMap = dbot.config;
this.db.scan('config', function(config) { this.db.scan('config', function(config) {
if(config) { if(config) {
var currentPath = configMap, var currentPath = configMap,
@ -53,7 +53,7 @@ var admin = function(dbot) {
currentPath[key[i]] = value; currentPath[key[i]] = value;
} }
}, function(err) { }); }, function(err) { });*/
}.bind(this); }.bind(this);
}; };

View File

@ -2,7 +2,6 @@ var request = require('request');
_ = require('underscore')._; _ = require('underscore')._;
var dent = function(dbot) { var dent = function(dbot) {
this.dbot = dbot;
this.StatusRegex = { this.StatusRegex = {
identica: /\bhttps?:\/\/identi\.ca\/notice\/(\d+)\b/ig, identica: /\bhttps?:\/\/identi\.ca\/notice\/(\d+)\b/ig,
twitter: /\bhttps?:\/\/twitter\.com\/\w+\/status\/(\d+)\b/ig twitter: /\bhttps?:\/\/twitter\.com\/\w+\/status\/(\d+)\b/ig
@ -15,8 +14,8 @@ var dent = function(dbot) {
this.api = { this.api = {
'post': function(content) { 'post': function(content) {
var username = dbot.config.dent.username, var username = this.config.username,
password = dbot.config.dent.password, password = this.config.password,
info, info,
auth = "Basic " + auth = "Basic " +
new Buffer(username + ":" + password).toString("base64"); new Buffer(username + ":" + password).toString("base64");
@ -57,7 +56,7 @@ var dent = function(dbot) {
this.commands['~dent'].regex = [/^~dent (.+)$/, 2]; this.commands['~dent'].regex = [/^~dent (.+)$/, 2];
this.onLoad = function() { 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) { dbot.api.event.addHook('~qadd', function(key, text) {
if(text.indexOf('~~') == -1) { if(text.indexOf('~~') == -1) {
this.api.post(key + ': ' + text); this.api.post(key + ': ' + text);

View File

@ -30,7 +30,7 @@ var pages = function(dbot) {
/* TODO: merge back into github module */ /* TODO: merge back into github module */
var milestones; 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); milestones = JSON.parse(body);
}); });
@ -53,7 +53,7 @@ var pages = function(dbot) {
"botname": dbot.config.name "botname": dbot.config.name
}), }),
"curr839": dbot.config.language, "curr839": dbot.config.language,
"repo": dbot.config.github.defaultrepo, "repo": dbot.config.modules.github.defaultrepo,
"branch": dbot.t("branch",{ "branch": dbot.t("branch",{
"branch": branch "branch": branch
}), }),

View File

@ -14,30 +14,30 @@ var project = function(dbot) {
var list = []; var list = [];
if(_.has(dbot.modules,'dent')){ if(_.has(dbot.modules,'dent')){
list.push(dbot.t("dent-account", { 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")); list.push(dbot.t("dent-push"));
} }
} }
if(_.has(dbot.modules,'link')){ if(_.has(dbot.modules,'link')){
if(dbot.config.link.autoTitle){ if(dbot.config.modules.link.autoTitle){
list.push(dbot.t("link-autotitle")); list.push(dbot.t("link-autotitle"));
} }
} }
if(_.has(dbot.modules,'quotes')){ if(_.has(dbot.modules,'quotes')){
list.push(dbot.t("quote-rmlimit", { list.push(dbot.t("quote-rmlimit", {
"limit": dbot.config.quotes.rmLimit "limit": dbot.config.modules.quotes.rmLimit
})); }));
} }
if(_.has(dbot.modules,'report')){ if(_.has(dbot.modules,'report')){
if(dbot.config.report.notifyVoice){ if(dbot.config.modules.report.notifyVoice){
list.push(dbot.t("report-notifyvoice")); list.push(dbot.t("report-notifyvoice"));
} }
} }
if(_.has(dbot.modules,'web')){ if(_.has(dbot.modules,'web')){
list.push(dbot.t("web-port", { list.push(dbot.t("web-port", {
"port": dbot.config.web.webPort "port": dbot.config.modules.web.webPort
})); }));
} }
return list; return list;

View File

@ -3,13 +3,14 @@ var express = require('express'),
fs = require('fs'); fs = require('fs');
var webInterface = function(dbot) { var webInterface = function(dbot) {
this.config = dbot.config.modules.web;
this.pub = 'public'; this.pub = 'public';
this.app = express(); this.app = express();
this.app.use(express.static(this.pub)); this.app.use(express.static(this.pub));
this.app.set('view engine', 'jade'); 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() { this.reloadPages = function() {
var pages = dbot.pages; var pages = dbot.pages;

80
run.js
View File

@ -19,37 +19,10 @@ var DBot = function() {
this.db = {}; this.db = {};
} }
if(!_.has(this.db, 'config')) {
this.db.config = {};
}
/*** Load the fancy DB ***/ /*** Load the fancy DB ***/
this.ddb = new DatabaseDriver(); this.ddb = new DatabaseDriver();
/*** Load the Config ***/ this.reloadConfig();
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);
/*** Load main strings ***/ /*** Load main strings ***/
@ -83,6 +56,33 @@ var DBot = function() {
this.instance.connectAll(); 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 // Say something in a channel
DBot.prototype.say = function(server, channel, message) { DBot.prototype.say = function(server, channel, message) {
this.instance.say(server, channel, message); this.instance.say(server, channel, message);
@ -138,6 +138,7 @@ DBot.prototype.reloadModules = function() {
this.api = {}; this.api = {};
this.stringMap = {}; this.stringMap = {};
this.usage = {}; this.usage = {};
this.reloadConfig();
try { try {
this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8'));
@ -173,25 +174,17 @@ DBot.prototype.reloadModules = function() {
continue; continue;
} }
// Load the module config data if(!_.has(this.config.modules, name)) this.config.modules[name] = {};
config = {}; if(fs.existsSync(moduleDir + 'config.json')) {
if(_.has(this.db.config, name)) {
config = _.clone(this.db.config[name]);
}
try { try {
var defaultConfig = fs.readFileSync(moduleDir + 'config.json', 'utf-8'); var defaultConfig = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8'));
try { } catch(err) { // Syntax error
defaultConfig = JSON.parse(defaultConfig);
} catch(err) { // syntax error
this.status[name] = 'Error parsing config: ' + err + ' ' + err.stack.split('\n')[2].trim(); this.status[name] = 'Error parsing config: ' + err + ' ' + err.stack.split('\n')[2].trim();
continue; continue;
} }
config = _.defaults(config, defaultConfig); this.config.modules[name] = _.defaults(this.config.modules[name], defaultConfig);
} catch(err) {
// Invalid or no config data
} }
var config = this.config.modules[name];
// Don't shit out if dependencies not met // Don't shit out if dependencies not met
if(_.has(config, 'dependencies')) { if(_.has(config, 'dependencies')) {
@ -202,7 +195,6 @@ DBot.prototype.reloadModules = function() {
} }
}, this); }, this);
} }
this.config[name] = config;
// Groovy funky database shit // Groovy funky database shit
if(!_.has(config, 'dbType') || config.dbType == 'olde') { if(!_.has(config, 'dbType') || config.dbType == 'olde') {
@ -235,7 +227,7 @@ DBot.prototype.reloadModules = function() {
module.name = name; module.name = name;
module.db = this.ddb.databanks[name]; module.db = this.ddb.databanks[name];
module.config = this.config[name]; module.config = this.config.modules[name];
// Load the module data // Load the module data
_.each([ 'commands', 'pages', 'api' ], function(property) { _.each([ 'commands', 'pages', 'api' ], function(property) {