This commit is contained in:
Flummi 2016-11-22 06:26:12 +01:00
parent a6495dd213
commit a291e4f0a1
3 changed files with 70 additions and 58 deletions

49
src/cfg.js Normal file
View File

@ -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 );
}
});
};

View File

@ -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 = () => {

View File

@ -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);
var cfg = new Cfg();
cfg.read(config => {
if(config) {
var lib = new Lib(config);
var websrv = new Websrv(lib);
}
else
console.log(config);
});