Merge branch 'f0ckdev' into 'master'

new configsystem

See merge request !31
This commit is contained in:
Flummi 2016-11-07 01:30:03 +00:00
commit 4fab49e884
3 changed files with 354 additions and 319 deletions

View File

@ -5,25 +5,9 @@ var exec = require('child_process').exec;
var crypto = require('crypto');
var request = require('request');
var mysql = require('mysql');
var irccfg = require('../cfg/irc.json');
var self = Lib.prototype;
self.bot = require('coffea')();
irccfg.forEach(e => {
self.bot.add({
"name": e.name,
"host": e.host,
"port": e.port,
"ssl": e.ssl,
"ssl_allow_invalid": e.ssl_allow_invalid,
"pass": e.pass,
"nick": e.nick,
"username": e.username,
"realname": e.realname,
"throttling": e.throttling
});
console.log("Server "+e.name+" wurde geladen");
});
self.admins = [];
self.debug = false;
@ -53,18 +37,21 @@ haDC();
module.exports = Lib;
function Lib() {
self.cfg = require('../cfg/main.json');
self.debug = self.cfg.debug;
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.getUserlevel = (user, network, channel, cb) => {
self.bot.whois(user, network, (err, data) => {
self.bot.names(channel, network, (names) => {
var lvl_channel = (data.nick in names)? self.cfg.level[ names[data.nick] ] : 0;
var lvl_channel = (data.nick in names)? self.cfg.main.level[ names[data.nick] ] : 0;
var lvl_db = 0;
if(data.account in self.admins)
lvl_db = (self.admins[data.account].network === network)? self.admins[data.account].level : 0;
@ -161,7 +148,7 @@ self.generateThumbs = () => {
};
self.log = (msg) => {
if(self.debug)
self.bot.send(self.cfg.debugchannel, msg, 'n0xy');
self.bot.send(self.cfg.main.debugchannel, msg, 'n0xy');
};
self.loadTrigger = () => {
var files = fs.readdirSync(__dirname+'/trigger/');
@ -238,3 +225,50 @@ self.levelToModes = level => {
return "v";
return "";
};
self.getcfg = (kat, cb) => {
// main, websrv, irc
var out = {};
self.sql.query("select * from `f0ck`.`cfg` where `class` = ?", kat, (err, rows) => {
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(out);
});
};
self.loadIRC = () => {
self.getcfg('irc', cfg => {
self.cfg.irc = cfg;
self.cfg.irc.irccfg.forEach(e => {
self.bot.add({
"name": e.name,
"host": e.host,
"port": e.port,
"ssl": e.ssl,
"ssl_allow_invalid": e.ssl_allow_invalid,
"pass": e.pass,
"nick": e.nick,
"username": e.username,
"realname": e.realname,
"throttling": e.throttling
});
console.log("Server "+e.name+" wurde geladen");
});
});
};

View File

@ -7,7 +7,6 @@ var fileType = require('file-type');
var request = require('request');
var ytdl = require('ytdl-core');
var Readable = require('stream').Readable;
var cfg = require('../../cfg/main.json');
module.exports = (lib) => {
lib.trigger.add({
@ -40,7 +39,7 @@ module.exports = (lib) => {
(cb.info.thumb !== null)?cb.info.thumb:''
]).on('result', (result) => {
lib.generateThumbs();
e.reply(cfg.url+"/"+result.insertId+" - "+cb.info.title+" ("+cb.info.mime+", ~"+lib.formatSize(cb.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")");
e.reply(lib.cfg.main.url+"/"+result.insertId+" - "+cb.info.title+" ("+cb.info.mime+", ~"+lib.formatSize(cb.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")");
}).on('error', (msg) => {
e.reply(msg);
});
@ -82,10 +81,10 @@ module.exports = (lib) => {
try {
ytdl.downloadFromInfo(inf, { filter: (format) => { return format.container === 'webm'; } })
.on('response', (res) => {
if(res.headers['content-length'] > cfg.maxFileSize) {
if(res.headers['content-length'] > lib.cfg.main.maxFileSize) {
res.destroy();
dat.end();
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(cfg.maxFileSize)+' allowed' });
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(lib.cfg.main.maxFileSize)+' allowed' });
}
else {
info = {
@ -111,10 +110,10 @@ module.exports = (lib) => {
});
}
else if(url.match(sc)) { // scdl
request('https://api.soundcloud.com/resolve.json?client_id=' + cfg.scclientid + '&url=' + url, (err, res, body) => {
request('https://api.soundcloud.com/resolve.json?client_id=' + lib.cfg.main.scclientid + '&url=' + url, (err, res, body) => {
if(!err && res.statusCode === 200) {
var data = JSON.parse(body);
request(data.stream_url + ((data.stream_url.indexOf('?') === -1)?'?':'&') + 'client_id=' + cfg.scclientid)
request(data.stream_url + ((data.stream_url.indexOf('?') === -1)?'?':'&') + 'client_id=' + lib.cfg.main.scclientid)
.pipe(dat);
info = {
type: 'soundcloud',
@ -141,8 +140,8 @@ module.exports = (lib) => {
var type = res.headers['content-type'];
lib.log('MimeType: '+type);
var length = res.headers['content-length'];
if(cfg.allowedMimes.hasOwnProperty(type)) {
if(data.length <= cfg.maxFileSize) {
if(lib.cfg.main.allowedMimes.hasOwnProperty(type)) {
if(data.length <= lib.cfg.main.maxFileSize) {
var s = new Readable
s.push(data);
s.push(null);
@ -151,13 +150,13 @@ module.exports = (lib) => {
type: 'other',
title: path.parse(url).base,
mime: type,
ext: cfg.allowedMimes[type],
ext: lib.cfg.main.allowedMimes[type],
thumb: null
};
}
else {
dat.end();
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(data.length)+'), max '+lib.formatSize(cfg.maxFileSize)+' allowed' });
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(data.length)+'), max '+lib.formatSize(lib.cfg.main.maxFileSize)+' allowed' });
}
}
else {
@ -176,8 +175,8 @@ module.exports = (lib) => {
.on('finish', () => {
var size = dat.bytesWritten;
dat.end();
if(size > cfg.maxFileSize)
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(size)+'), max '+lib.formatSize(cfg.maxFileSize)+' allowed' });
if(size > lib.cfg.main.maxFileSize)
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(size)+'), max '+lib.formatSize(lib.cfg.main.maxFileSize)+' allowed' });
else {
fs.stat('./b/' + tmpdest, (err, stat) => {
if(!err && stat.isFile() && stat.size > 300) {
@ -186,13 +185,13 @@ module.exports = (lib) => {
lib.checkRepostCheckSum(cbcs, (cbcrcs) => {
if(cbcrcs === true) {
var mime = fileType(readChunk.sync('./b/' + tmpdest, 0, 262));
if(cfg.allowedMimes.hasOwnProperty(mime.mime) || info.type === 'soundcloud')
if(lib.cfg.main.allowedMimes.hasOwnProperty(mime.mime) || info.type === 'soundcloud')
cb({ success: true, info: info, size: size, file: './b/' + tmpdest, checksum: cbcs });
else
cb({ success: false, file: tmpdest, msg: 'lol, go f0ck yourself ('+mime+')' });
}
else
cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+cfg.url+'/'+cbcrcs });
cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+lib.cfg.main.url+'/'+cbcrcs });
});
});
}
@ -204,7 +203,7 @@ module.exports = (lib) => {
});
}
else
cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+cfg.url+'/'+cbcr });
cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+lib.cfg.main.url+'/'+cbcr });
});
};
};

View File

@ -3,7 +3,6 @@ var http = require('http');
var path = require('path');
var swig = require('swig');
var urlm = require('url');
var cfg = require('../cfg/websrv.json');
var exec = require('child_process').exec;
var templates = {};
@ -13,16 +12,18 @@ module.exports = Websrv;
function Websrv(tlib) {
this.lib = lib = tlib;
lib.getcfg('websrv', cfg => {
lib.cfg.websrv = cfg;
Websrv.prototype.getTpls();
http.createServer((req, res) => {
if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) {
if(lib.cfg.websrv.wlip.hasOwnProperty(req.connection.remoteAddress)) {
var filePath = '.' + req.url;
var url = req.url.split("/")[1];
if(filePath == './')
filePath = './index.html';
if(req.method == 'POST') {
if(filePath == './git' && req.headers['x-gitlab-token'] == cfg.gittoken) {
if(filePath == './git' && req.headers['x-gitlab-token'] == lib.cfg.websrv.gittoken) {
var body = '';
req.on('data', (data) => {
body += data;
@ -43,12 +44,12 @@ function Websrv(tlib) {
catch(ex) {
commit = body.commits[body.commits.length-2].message.replace('\n','').trim();
}
lib.bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+') in branch '+branch, 'n0xy' );
if( lib.cfg.debugchannel === '#f0ck' ) {
lib.bot.send( lib.cfg.main.debugchannel, eventname + ' from ' + autor + ' ('+commit+') in branch '+branch, 'n0xy' );
if( lib.cfg.main.debugchannel === '#f0ck' ) {
exec('cd ../ & git pull', (error, stdout) => {
if(error === null) {
if(!lib.debug)
lib.bot.send(lib.cfg.debugchannel, 'git pull suxxessfully.', 'n0xy');
lib.bot.send(lib.cfg.main.debugchannel, 'git pull suxxessfully.', 'n0xy');
else
lib.log(stdout);
}
@ -315,7 +316,8 @@ function Websrv(tlib) {
res.writeHead(403);
res.end('403 - forbidden');
}
}).listen(cfg.port);
}).listen(lib.cfg.websrv.port);
});
}
Websrv.prototype.getTpls = () => {