f0ckv1/src/lib.js

195 lines
6.6 KiB
JavaScript
Raw Normal View History

2016-08-23 11:23:25 +00:00
var fs = require('fs-extra');
var http = require('http');
var https = require('https');
var exec = require('child_process').exec;
var crypto = require('crypto');
2016-09-12 22:39:21 +00:00
var request = require('request');
2016-09-14 22:05:18 +00:00
var mysql = require('mysql');
var irccfg = require('../cfg/irc.json');
2016-08-23 11:23:25 +00:00
2016-09-14 22:05:18 +00:00
Lib.prototype.bot = require('coffea')();
irccfg.forEach(e => {
Lib.prototype.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
});
console.log("Server "+e.name+" wurde geladen");
});
2016-08-23 11:23:25 +00:00
2016-09-07 11:03:13 +00:00
Lib.prototype.admins = [];
Lib.prototype.debug = false;
2016-09-09 11:18:03 +00:00
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
});
}
};
2016-09-14 22:05:18 +00:00
var haDC = () => {
Lib.prototype.sql = mysql.createConnection(require('../cfg/mysql.json'));
Lib.prototype.sql.connect((err) => {
if(err) setTimeout(haDC, 2000);
});
Lib.prototype.sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
};
haDC();
2016-09-07 11:03:13 +00:00
2016-09-09 11:26:17 +00:00
module.exports = Lib;
2016-09-14 22:05:18 +00:00
function Lib() {
Lib.prototype.cfg = require('../cfg/main.json');
Lib.prototype.debug = Lib.prototype.cfg.debug;
2016-09-14 22:05:18 +00:00
Lib.prototype.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
2016-09-09 11:26:17 +00:00
Lib.prototype.loadTrigger();
2016-09-14 17:24:48 +00:00
Lib.prototype.loadEvents();
2016-09-09 11:26:17 +00:00
}
2016-09-08 15:42:38 +00:00
Lib.prototype.getUserlevel = (user, network, channel, cb) => {
2016-09-14 22:05:18 +00:00
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;
2016-09-09 08:32:35 +00:00
var lvl_db = 0;
2016-09-14 22:05:18 +00:00
if(data.account in Lib.prototype.admins)
lvl_db = (Lib.prototype.admins[data.account].network === network)? Lib.prototype.admins[data.account].level : 0;
2016-09-09 08:32:35 +00:00
cb({
'channel': lvl_channel,
'db': lvl_db,
'level': Math.max(lvl_channel, lvl_db)
});
2016-08-23 11:23:25 +00:00
});
});
};
Lib.prototype.loadUser = (cb) => {
2016-09-07 11:03:13 +00:00
Lib.prototype.admins = [];
2016-09-09 11:26:17 +00:00
Lib.prototype.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
2016-09-07 11:03:13 +00:00
rows.forEach(e => {
2016-09-14 22:05:18 +00:00
Lib.prototype.admins[e.account] = {
id: e.id,
account: e.account,
level: e.level,
network: e.network
2016-08-23 11:23:25 +00:00
};
});
2016-09-07 11:03:13 +00:00
if(cb) cb(true);
2016-08-23 11:23:25 +00:00
}).on('error', () => {
2016-09-07 11:03:13 +00:00
if(cb) cb(false);
2016-08-23 11:23:25 +00:00
});
}
Lib.prototype.checkRepost = (url, cbcr) => {
2016-09-09 11:26:17 +00:00
Lib.prototype.sql.query("select count(id) as count, id from `f0ck`.`items` where `src` = ?", url, (err, rows, fields) => {
2016-08-31 07:37:41 +00:00
cbcr((rows[0].count == 0)?true:rows[0].id);
2016-08-23 11:23:25 +00:00
});
};
Lib.prototype.checkRepostCheckSum = (cs, cbcrcs) => {
2016-09-09 11:26:17 +00:00
Lib.prototype.sql.query("select count(id) as count, id from `f0ck`.`items` where `checksum` = ?", cs, (err, rows, fields) => {
2016-08-31 07:37:41 +00:00
cbcrcs((rows[0].count == 0)?true:rows[0].id);
2016-08-23 11:23:25 +00:00
});
};
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');
2016-09-14 22:05:18 +00:00
fs.ReadStream(file)
.on('data', d => sha256sum.update(d))
.on('end', () => cbcs(sha256sum.digest('hex')));
2016-08-23 11:23:25 +00:00
};
Lib.prototype.generateThumbs = () => {
2016-09-11 15:08:16 +00:00
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)) {
2016-09-12 22:39:21 +00:00
if(e.thumb == '') {
var cmd;
switch(e.mime) {
case "video/mp4":
case "video/webm":
case "audio/mpeg":
case "audio/ogg":
case "image/gif":
exec('ffmpegthumbnailer -i'+e.dest+' -s1024 -o'+thumbnail, (error) => {
if(error) {
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+')');
fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail
}
2016-09-14 22:05:18 +00:00
else
2016-09-12 22:39:21 +00:00
exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
2016-09-14 22:05:18 +00:00
Lib.prototype.log((error?'failed':'generated') + ' thumbnail for '+e.id+' ('+e.mime+')');
2016-09-12 22:39:21 +00:00
});
});
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+')');
});
break;
}
}
else {
var dat = fs.createWriteStream(thumbnail);
request(e.thumb).pipe(dat);
dat.on('finish', () => {
dat.close();
exec('convert '+thumbnail+' -resize "200x200^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
2016-09-11 20:12:03 +00:00
Lib.prototype.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')');
});
2016-09-12 22:39:21 +00:00
});
2016-09-11 15:08:16 +00:00
}
}
});
});
};
2016-08-23 11:23:25 +00:00
Lib.prototype.log = (msg) => {
2016-09-07 11:03:13 +00:00
if(Lib.prototype.debug)
2016-09-14 22:05:18 +00:00
Lib.prototype.bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy');
2016-09-09 11:18:03 +00:00
};
Lib.prototype.loadTrigger = () => {
var files = fs.readdirSync(__dirname+'/trigger/');
files.forEach((file) => {
if(file.substr(-3, 3) === '.js') {
console.log('Loading trigger', file);
2016-09-14 22:05:18 +00:00
require(__dirname+'/trigger/' + file)(Lib.prototype);
2016-09-09 11:18:03 +00:00
}
});
2016-09-14 17:24:48 +00:00
};
Lib.prototype.loadEvents = () => {
var files = fs.readdirSync(__dirname+'/events/');
files.forEach((file) => {
if(file.substr(-3, 3) === '.js') {
console.log('Loading event', file);
2016-09-14 22:05:18 +00:00
require(__dirname+'/events/' + file)(Lib.prototype);
2016-09-14 17:24:48 +00:00
}
});
2016-09-15 17:17:07 +00:00
};
Lib.prototype.groupUserModes = modes => {
var modesToApply = [], currentPrefix, queuedModes;
while(modes.length) {
queuedModes = "";
currentPrefix = modes[0][0];
modes.some(mode => {
if(mode[0] === currentPrefix) queuedModes += mode[1];
else return true;
});
modesToApply.push(currentPrefix + queuedModes);
for(var i = 0; i < queuedModes.length; i++) modes.shift();
}
return modesToApply;
2016-08-23 11:23:25 +00:00
};