From 2bc7f45fb61203fb6557476cd86e71fd864f1440 Mon Sep 17 00:00:00 2001 From: Flummi Date: Thu, 15 Sep 2016 21:25:07 +0200 Subject: [PATCH] self yay --- src/lib.js | 89 +++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/src/lib.js b/src/lib.js index 7070ecb..4ebb7d5 100644 --- a/src/lib.js +++ b/src/lib.js @@ -7,9 +7,10 @@ var request = require('request'); var mysql = require('mysql'); var irccfg = require('../cfg/irc.json'); -Lib.prototype.bot = require('coffea')(); +var self = Lib.prototype; +self.bot = require('coffea')(); irccfg.forEach(e => { - Lib.prototype.bot.add({ + self.bot.add({ "name": e.name, "host": e.host, "port": e.port, @@ -23,12 +24,12 @@ irccfg.forEach(e => { console.log("Server "+e.name+" wurde geladen"); }); -Lib.prototype.admins = []; -Lib.prototype.debug = false; -Lib.prototype.trigger = { +self.admins = []; +self.debug = false; +self.trigger = { trigger: [], add: (args) => { - Lib.prototype.trigger.trigger.push({ + self.trigger.trigger.push({ name: args.name, call: args.call, func: args.func, @@ -39,11 +40,11 @@ Lib.prototype.trigger = { } }; var haDC = () => { - Lib.prototype.sql = mysql.createConnection(require('../cfg/mysql.json')); - Lib.prototype.sql.connect((err) => { + self.sql = mysql.createConnection(require('../cfg/mysql.json')); + self.sql.connect((err) => { if(err) setTimeout(haDC, 2000); }); - Lib.prototype.sql.on('error', (err) => { + self.sql.on('error', (err) => { if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC(); }); }; @@ -51,21 +52,21 @@ haDC(); module.exports = Lib; function Lib() { - Lib.prototype.cfg = require('../cfg/main.json'); - Lib.prototype.debug = Lib.prototype.cfg.debug; - Lib.prototype.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden")); - Lib.prototype.loadTrigger(); - Lib.prototype.loadEvents(); + self.cfg = require('../cfg/main.json'); + self.debug = self.cfg.debug; + self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden")); + self.loadTrigger(); + self.loadEvents(); } -Lib.prototype.getUserlevel = (user, network, channel, cb) => { - Lib.prototype.bot.whois(user, network, (err, data) => { - Lib.prototype.bot.names(channel, network, (names) => { - var lvl_channel = (data.nick in names)? Lib.prototype.cfg.level[ names[data.nick] ] : 0; +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_db = 0; - if(data.account in Lib.prototype.admins) - lvl_db = (Lib.prototype.admins[data.account].network === network)? Lib.prototype.admins[data.account].level : 0; + if(data.account in self.admins) + lvl_db = (self.admins[data.account].network === network)? self.admins[data.account].level : 0; cb({ 'channel': lvl_channel, 'db': lvl_db, @@ -74,11 +75,11 @@ Lib.prototype.getUserlevel = (user, network, channel, cb) => { }); }); }; -Lib.prototype.loadUser = (cb) => { - Lib.prototype.admins = []; - Lib.prototype.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => { +self.loadUser = (cb) => { + self.admins = []; + self.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => { rows.forEach(e => { - Lib.prototype.admins[e.account] = { + self.admins[e.account] = { id: e.id, account: e.account, level: e.level, @@ -90,29 +91,29 @@ Lib.prototype.loadUser = (cb) => { if(cb) cb(false); }); } -Lib.prototype.checkRepost = (url, cbcr) => { - Lib.prototype.sql.query("select count(id) as count, id from `f0ck`.`items` where `src` = ?", url, (err, rows, fields) => { +self.checkRepost = (url, cbcr) => { + self.sql.query("select count(id) as count, id from `f0ck`.`items` where `src` = ?", url, (err, rows, fields) => { cbcr((rows[0].count == 0)?true:rows[0].id); }); }; -Lib.prototype.checkRepostCheckSum = (cs, cbcrcs) => { - Lib.prototype.sql.query("select count(id) as count, id from `f0ck`.`items` where `checksum` = ?", cs, (err, rows, fields) => { +self.checkRepostCheckSum = (cs, cbcrcs) => { + self.sql.query("select count(id) as count, id from `f0ck`.`items` where `checksum` = ?", cs, (err, rows, fields) => { cbcrcs((rows[0].count == 0)?true:rows[0].id); }); }; -Lib.prototype.formatSize = (size) => { +self.formatSize = (size) => { var i = Math.floor(Math.log(size) / Math.log(1024)); return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; }; -Lib.prototype.getCheckSum = (file, cbcs) => { +self.getCheckSum = (file, cbcs) => { var sha256sum = crypto.createHash('sha256'); fs.ReadStream(file) .on('data', d => sha256sum.update(d)) .on('end', () => cbcs(sha256sum.digest('hex'))); }; -Lib.prototype.generateThumbs = () => { +self.generateThumbs = () => { var outdir = './t'; - Lib.prototype.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => { + self.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => { rows.forEach((e,i,a) => { var thumbnail = outdir+'/'+e.id+'.png'; if(!fs.existsSync(thumbnail)) { @@ -126,19 +127,19 @@ Lib.prototype.generateThumbs = () => { case "image/gif": exec('ffmpegthumbnailer -i'+e.dest+' -s1024 -o'+thumbnail, (error) => { if(error) { - Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+')'); + self.log('failed thumbnail for '+e.id+' ('+e.mime+')'); fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail } else exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => { - Lib.prototype.log((error?'failed':'generated') + ' thumbnail for '+e.id+' ('+e.mime+')'); + self.log((error?'failed':'generated') + ' thumbnail for '+e.id+' ('+e.mime+')'); }); }); break; case "image/png": case "image/jpeg": exec('convert '+e.dest+' -resize "200x200^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => { - Lib.prototype.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')'); + self.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')'); }); break; } @@ -149,7 +150,7 @@ Lib.prototype.generateThumbs = () => { dat.on('finish', () => { dat.close(); exec('convert '+thumbnail+' -resize "200x200^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => { - Lib.prototype.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')'); + self.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')'); }); }); } @@ -157,29 +158,29 @@ Lib.prototype.generateThumbs = () => { }); }); }; -Lib.prototype.log = (msg) => { - if(Lib.prototype.debug) - Lib.prototype.bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy'); +self.log = (msg) => { + if(self.debug) + self.bot.send(self.cfg.debugchannel, msg, 'n0xy'); }; -Lib.prototype.loadTrigger = () => { +self.loadTrigger = () => { var files = fs.readdirSync(__dirname+'/trigger/'); files.forEach((file) => { if(file.substr(-3, 3) === '.js') { console.log('Loading trigger', file); - require(__dirname+'/trigger/' + file)(Lib.prototype); + require(__dirname+'/trigger/' + file)(self); } }); }; -Lib.prototype.loadEvents = () => { +self.loadEvents = () => { var files = fs.readdirSync(__dirname+'/events/'); files.forEach((file) => { if(file.substr(-3, 3) === '.js') { console.log('Loading event', file); - require(__dirname+'/events/' + file)(Lib.prototype); + require(__dirname+'/events/' + file)(self); } }); }; -Lib.prototype.groupUserModes = modes => { +self.groupUserModes = modes => { var modesToApply = [], currentPrefix, queuedModes; while(modes.length) { queuedModes = "";