diff --git a/src/cfg.js b/src/cfg.js new file mode 100644 index 0000000..0fd6bcc --- /dev/null +++ b/src/cfg.js @@ -0,0 +1,49 @@ +var mysql = require('mysql'); + +var self = Cfg.prototype; +var haDC = () => { + self.sql = mysql.createConnection(require('../cfg/mysql.json')); + self.sql.connect((err) => { + if(err) setTimeout(haDC, 2000); + }); + self.sql.on('error', (err) => { + if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC(); + }); +}; +haDC(); + +module.exports = Cfg; +function Cfg() { + +} + +self.read = (cb, kat, key) => { + // main, websrv, irc + var out = {}; + self.sql.query("select * from `f0ck`.`cfg`" + (kat?" where `class` = '"+kat+"'"+(key?" && `key` = '"+key+"'":""):""), (err, rows) => { + if(err || rows.length < 1) + cb(err); + else { + rows.forEach(e => { + out[e.key] = ((type, value) => { + switch(type) { + case 'string': + return value; + break; + case 'int': + return parseInt(value); + break; + case 'bool': + return (value === 'true')?true:false; + break; + case 'json': + return JSON.parse(value); + break; + } + })(e.type, e.value); + }); + if(cb) + cb( key?out[key]:out ); + } + }); +}; \ No newline at end of file diff --git a/src/lib.js b/src/lib.js index ec50436..09fbc31 100644 --- a/src/lib.js +++ b/src/lib.js @@ -4,7 +4,6 @@ var https = require('https'); var exec = require('child_process').exec; var crypto = require('crypto'); var request = require('request'); -var mysql = require('mysql'); var self = Lib.prototype; self.bot = require('coffea')(); @@ -25,29 +24,16 @@ self.trigger = { }); } }; -var haDC = () => { - self.sql = mysql.createConnection(require('../cfg/mysql.json')); - self.sql.connect((err) => { - if(err) setTimeout(haDC, 2000); - }); - self.sql.on('error', (err) => { - if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC(); - }); -}; -haDC(); module.exports = Lib; -function Lib() { - self.cfg = {}; - self.getcfg('main', cfg => { - self.cfg.main = cfg; - self.debug = self.cfg.main.debug; - self.loadIRC(); - self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden")); - self.loadTrigger(); - self.loadEvents(); - self.getChanTrigger(); - }); +function Lib(config) { + self.cfg = config; + self.debug = self.cfg.main.debug; + self.loadIRC(); + self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden")); + self.loadTrigger(); + self.loadEvents(); + self.getChanTrigger(); } self.getUserlevel = (user, network, channel, cb) => { @@ -229,40 +215,9 @@ self.levelToModes = level => { return ""; }; -self.getcfg = (kat, cb, key) => { - // main, websrv, irc - var out = {}; - self.sql.query("select * from `f0ck`.`cfg` where `class` = ?"+(key?" && `key` = '"+key+"'":""), kat, (err, rows) => { - if(err || rows.length < 1) - cb(err); - else { - rows.forEach(e => { - out[e.key] = ((type, value) => { - switch(type) { - case 'string': - return value; - break; - case 'int': - return parseInt(value); - break; - case 'bool': - return (value === 'true')?true:false; - break; - case 'json': - return JSON.parse(value); - break; - } - })(e.type, e.value); - }); - if(cb) - cb( key?out[key]:out ); - } - }); -}; - self.loadIRC = () => { - self.getcfg('irc', cfg => { - self.cfg.irc = cfg; + //self.getcfg('irc', cfg => { + // self.cfg.irc = cfg; self.cfg.irc.irccfg.forEach(e => { self.bot.add({ "name": e.name, @@ -278,7 +233,7 @@ self.loadIRC = () => { }); console.log("Server "+e.name+" wurde geladen"); }); - }); + //}); }; self.getChanTrigger = () => { diff --git a/src/main.js b/src/main.js index 6633744..98779ab 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,13 @@ +var Cfg = require('./cfg.js'); var Lib = require('./lib.js'); var Websrv = require('./websrv.js'); -var lib = new Lib(); -var websrv = new Websrv(lib); \ No newline at end of file +var cfg = new Cfg(); +cfg.read(config => { + if(config) { + var lib = new Lib(config); + var websrv = new Websrv(lib); + } + else + console.log(config); +}); \ No newline at end of file