WIP: cfgzeugs

This commit is contained in:
Flummi 2016-11-22 07:02:40 +01:00
parent a291e4f0a1
commit 5ffb07c14c
6 changed files with 350 additions and 363 deletions

View File

@ -14,18 +14,23 @@ haDC();
module.exports = Cfg; module.exports = Cfg;
function Cfg() { function Cfg() {
self.cfg;
} }
self.read = (cb, kat, key) => { self.read = (cb, kat, key) => {
// main, websrv, irc // main, websrv, irc
var out = {}; var out = {
irc: {},
main: {},
websrv: {},
trigger: {}
};
self.sql.query("select * from `f0ck`.`cfg`" + (kat?" where `class` = '"+kat+"'"+(key?" && `key` = '"+key+"'":""):""), (err, rows) => { self.sql.query("select * from `f0ck`.`cfg`" + (kat?" where `class` = '"+kat+"'"+(key?" && `key` = '"+key+"'":""):""), (err, rows) => {
if(err || rows.length < 1) if(err || rows.length < 1)
cb(err); cb(err);
else { else {
rows.forEach(e => { rows.forEach(e => {
out[e.key] = ((type, value) => { out[e.class][e.key] = ((type, value) => {
switch(type) { switch(type) {
case 'string': case 'string':
return value; return value;
@ -43,7 +48,27 @@ self.read = (cb, kat, key) => {
})(e.type, e.value); })(e.type, e.value);
}); });
if(cb) if(cb)
cb( key?out[key]:out ); cb( key?out[kat][key]:out );
else
if(!kat && !key)
self.cfg = out;
}
});
};
self.write = (kat, key, value, type, cb) => {
self.sql.query("select * from `f0ck`.`cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => {
if(rows.length > 0) {
self.sql.query("update `f0ck`.`cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => {
self.read();
if(cb) cb(err);
});
}
else {
self.sql.query("insert into `f0ck`.`cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, type], (err) => {
self.read();
if(cb) cb(err);
});
} }
}); });
}; };

View File

@ -3,8 +3,8 @@ module.exports = (lib) => {
lib.trigger.trigger.forEach((entry,i,a) => { lib.trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call)) { if(e.message.match(entry.call)) {
let active = false; let active = false;
if(lib.trigger.chans[e.network+e.channel.getName()] !== undefined) { if(lib.cfg.trigger[e.network+e.channel.getName()] !== undefined) {
if(lib.trigger.chans[e.network+e.channel.getName()][entry.name]) if(lib.cfg.trigger[e.network+e.channel.getName()][entry.name])
active = true; active = true;
} }
else else

View File

@ -27,13 +27,14 @@ self.trigger = {
module.exports = Lib; module.exports = Lib;
function Lib(config) { function Lib(config) {
self.cfg = config; self.config = config;
self.sql = config.sql;
self.cfg = config.cfg;
self.debug = self.cfg.main.debug; self.debug = self.cfg.main.debug;
self.loadIRC(); self.loadIRC();
self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden")); self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
self.loadTrigger(); self.loadTrigger();
self.loadEvents(); self.loadEvents();
self.getChanTrigger();
} }
self.getUserlevel = (user, network, channel, cb) => { self.getUserlevel = (user, network, channel, cb) => {
@ -216,8 +217,6 @@ self.levelToModes = level => {
}; };
self.loadIRC = () => { self.loadIRC = () => {
//self.getcfg('irc', cfg => {
// self.cfg.irc = cfg;
self.cfg.irc.irccfg.forEach(e => { self.cfg.irc.irccfg.forEach(e => {
self.bot.add({ self.bot.add({
"name": e.name, "name": e.name,
@ -233,15 +232,6 @@ self.loadIRC = () => {
}); });
console.log("Server "+e.name+" wurde geladen"); console.log("Server "+e.name+" wurde geladen");
}); });
//});
};
self.getChanTrigger = () => {
self.trigger.chans = {};
self.getcfg('trigger', cb => {
for(let tmp in cb)
self.trigger.chans[tmp] = cb[tmp];
});
}; };
self.genStdTrigger = () => { self.genStdTrigger = () => {

View File

@ -5,7 +5,8 @@ var Websrv = require('./websrv.js');
var cfg = new Cfg(); var cfg = new Cfg();
cfg.read(config => { cfg.read(config => {
if(config) { if(config) {
var lib = new Lib(config); cfg.cfg = config;
var lib = new Lib(cfg);
var websrv = new Websrv(lib); var websrv = new Websrv(lib);
} }
else else

View File

@ -6,21 +6,18 @@ module.exports = (lib) => {
active: 1, active: 1,
func: (e) => { func: (e) => {
var args = e.message.split(' '); var args = e.message.split(' ');
lib.getcfg('trigger', cb => { if(!lib.cfg.trigger[e.network+e.channel])
if(!cb) lib.cfg.trigger[e.network+e.channel] = lib.genStdTrigger();
cb = lib.genStdTrigger();
switch(args[1]) { switch(args[1]) {
case "std": case "std":
e.reply(JSON.stringify(lib.genStdTrigger())); e.reply(JSON.stringify(lib.genStdTrigger()));
break; break;
case "toggle": case "toggle":
if(trigger = args[2].toLowerCase()) { if(trigger = args[2].toLowerCase()) {
cb = triggertoggle(cb, trigger); lib.cfg.trigger[e.network+e.channel] = triggertoggle(lib.cfg.trigger[e.network+e.channel], trigger);
writecfg('trigger', e.network+e.channel, JSON.stringify(cb), cbwc => { lib.config.write('trigger', e.network+e.channel, JSON.stringify(lib.cfg.trigger[e.network+e.channel]), 'json', cbwc => {
if(!cbwc) { if(!cbwc)
e.reply("trigger "+ trigger +" in channel "+e.channel.getName() + lib.bot.format.get(cb[trigger]?'green':'red')+" "+(cb[trigger]?"activated":"deactivated")); e.reply("trigger "+ trigger +" in channel "+e.channel.getName() + lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][trigger]?'green':'red')+" "+(lib.cfg.trigger[e.network+e.channel][trigger]?'activated':'deactivated'));
lib.getChanTrigger();
}
else else
e.reply(cbwc); e.reply(cbwc);
}); });
@ -29,12 +26,11 @@ module.exports = (lib) => {
default: default:
var output = ""; var output = "";
e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):"); e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):");
for(var key in cb) for(var key in lib.cfg.trigger[e.network+e.channel])
output += lib.bot.format.get(cb[key]?'green':'red')+" "+key; output += lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][key]?'green':'red')+" "+key;
e.reply(output); e.reply(output);
break; break;
} }
}, e.network+e.channel);
}, },
desc: 'muh' desc: 'muh'
}); });
@ -44,19 +40,4 @@ module.exports = (lib) => {
trigger[key] = !trigger[key]; trigger[key] = !trigger[key];
return trigger; return trigger;
}; };
var writecfg = (kat, key, value, cb) => {
lib.sql.query("select * from `f0ck`.`cfg` where `class` = ? && `key` = ? limit 1", [kat, key], (err, rows) => {
if(rows.length > 0) {
lib.sql.query("update `f0ck`.`cfg` set `value` = ? where `class` = ? && `key` = ?", [value, kat, key], (err) => {
if(cb) cb(err);
});
}
else {
lib.sql.query("insert into `f0ck`.`cfg` (`class`,`key`,`value`,`type`) values (?,?,?,?)", [kat, key, value, 'json'], (err) => {
if(cb) cb(err);
});
}
});
};
}; };

View File

@ -11,11 +11,7 @@ var lib;
module.exports = Websrv; module.exports = Websrv;
function Websrv(tlib) { function Websrv(tlib) {
this.lib = lib = tlib; this.lib = lib = tlib;
lib.getcfg('websrv', cfg => {
lib.cfg.websrv = cfg;
Websrv.prototype.getTpls(); Websrv.prototype.getTpls();
http.createServer((req, res) => { http.createServer((req, res) => {
if(lib.cfg.websrv.wlip.hasOwnProperty(req.connection.remoteAddress)) { if(lib.cfg.websrv.wlip.hasOwnProperty(req.connection.remoteAddress)) {
var filePath = '.' + req.url; var filePath = '.' + req.url;
@ -35,7 +31,6 @@ function Websrv(tlib) {
var eventname = body.event_name; var eventname = body.event_name;
var autor = body.user_name; var autor = body.user_name;
var branch = body.ref.split('/')[2]; var branch = body.ref.split('/')[2];
if(branch === "master") { if(branch === "master") {
var commit = ""; var commit = "";
try { try {
@ -60,10 +55,9 @@ function Websrv(tlib) {
res.writeHead(200, { 'Content-Type': 'text/html' }); res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('muh', 'utf-8'); res.end('muh', 'utf-8');
} }
else { else
res.writeHead(403); res.writeHead(403);
} }
}
else { else {
var extname = String(path.extname(filePath)).toLowerCase(); var extname = String(path.extname(filePath)).toLowerCase();
var contentType = 'text/html'; var contentType = 'text/html';
@ -79,7 +73,7 @@ function Websrv(tlib) {
'.webm': 'video/webm', '.webm': 'video/webm',
'.ogg': 'audio/ogg', '.ogg': 'audio/ogg',
'.mov': 'video/quicktime' '.mov': 'video/quicktime'
}; }; // Todo: hat hier nichts zu suchen!
if(filePath == "./index.html") { // mainpage if(filePath == "./index.html") { // mainpage
var tpl = swig.compile(templates.index); var tpl = swig.compile(templates.index);
var data = { var data = {
@ -135,11 +129,9 @@ function Websrv(tlib) {
} }
data.id = e.id; data.id = e.id;
data.username = e.username; data.username = e.username;
data.srcurl = e.src; data.srcurl = e.src;
data.src = urlm.parse(e.src).hostname; data.src = urlm.parse(e.src).hostname;
data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null; data.thumb = (e.thumb != '' && e.thumb.match(/sndcdn\.com/i))?e.thumb:null;
data.dest = e.dest; data.dest = e.dest;
data.mime = e.mime; data.mime = e.mime;
data.size = lib.formatSize(e.size); data.size = lib.formatSize(e.size);
@ -260,7 +252,6 @@ function Websrv(tlib) {
else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination
var eps = 50; var eps = 50;
var id = url[3]; var id = url[3];
lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => { lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
var items = { var items = {
"items": [], "items": [],
@ -320,7 +311,6 @@ function Websrv(tlib) {
res.end('403 - forbidden'); res.end('403 - forbidden');
} }
}).listen(lib.cfg.websrv.port); }).listen(lib.cfg.websrv.port);
});
} }
Websrv.prototype.getTpls = () => { Websrv.prototype.getTpls = () => {