var fs = require('fs-extra'); var http = require('http'); var https = require('https'); var exec = require('child_process').exec; var crypto = require('crypto'); var Mime = require('mime'); var bot, sql; Lib.prototype.admins = []; Lib.prototype.debug = false; Lib.prototype.trigger = { trigger: [], add: (args) => { Lib.prototype.trigger.trigger.push({ name: args.name, call: args.call, func: args.func, desc: args.desc, level: args.level, active: args.active }); } }; module.exports = Lib; function Lib(tbot, tsql) { this.bot = bot = tbot; Lib.prototype.sql = tsql; 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.getUserlevel = (user, network, channel, cb) => { bot.whois(user, network, (err, data) => { var host = data.username+'@'+data.hostname; bot.names(channel, network, (names) => { var lvl_channel = (data.nick in names)? Lib.prototype.cfg.level[ names[data.nick] ] : 0; var lvl_db = 0; if(host in Lib.prototype.admins) lvl_db = (Lib.prototype.admins[host].server == network)? Lib.prototype.admins[host].level : 0; cb({ 'channel': lvl_channel, 'db': lvl_db, 'level': Math.max(lvl_channel, lvl_db) }); }); }); }; Lib.prototype.loadUser = (cb) => { Lib.prototype.admins = []; Lib.prototype.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => { rows.forEach(e => { Lib.prototype.admins.push(e.vhost); Lib.prototype.admins[e.vhost] = { 'id': e.id, 'nick': e.nick, 'vhost': e.vhost, 'level': e.level, 'server': e.server }; }); if(cb) cb(true); }).on('error', () => { 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) => { 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) => { cbcrcs((rows[0].count == 0)?true:rows[0].id); }); }; Lib.prototype.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) => { var sha256sum = crypto.createHash('sha256'); var s = fs.ReadStream(file); s.on('data', (d) => { sha256sum.update(d); }); s.on('end', () => { cbcs(sha256sum.digest('hex')); }); }; Lib.prototype.generateThumbs = () => { var outdir = './t'; Lib.prototype.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => { rows.forEach((e,i,a) => { var thumbnail = outdir+'/'+e.id+'.png'; if(!fs.existsSync(thumbnail)) { var cmd; switch(e.mime) { case "video/mp4": case "video/webm": case "audio/mpeg": case "audio/ogg": case "image/gif": cmd = 'ffmpeg -ss 2 -i '+e.dest+' -vframes 1 -filter "scale=-1:256,crop=128:128" '+thumbnail; break; case "image/png": case "image/jpeg": cmd = 'convert '+e.dest+' -resize "200x200^" -gravity center -crop 128x128+0+0 +repage '+thumbnail; break; } exec(cmd, (error) => { if(error) { Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') '+JSON.stringify(error)); fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail } else Lib.prototype.log("generated thumbnail for "+e.id+" ("+e.mime+")"); }); } }); }); }; Lib.prototype.log = (msg) => { if(Lib.prototype.debug) bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy'); }; Lib.prototype.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)(bot, Lib.prototype); } }); };