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-08-24 12:18:51 +00:00
|
|
|
var Mime = require('mime');
|
2016-08-30 21:18:18 +00:00
|
|
|
var cfg;
|
2016-08-23 11:23:25 +00:00
|
|
|
|
2016-09-07 11:03:13 +00:00
|
|
|
var bot, sql;
|
2016-08-23 11:23:25 +00:00
|
|
|
|
|
|
|
module.exports = Lib;
|
2016-08-29 20:07:50 +00:00
|
|
|
function Lib(tbot, tsql) {
|
2016-08-23 11:23:25 +00:00
|
|
|
this.bot = bot = tbot;
|
|
|
|
this.sql = sql = tsql;
|
2016-08-30 21:18:18 +00:00
|
|
|
this.cfg = cfg = require('../cfg/main.json');
|
2016-08-29 20:30:36 +00:00
|
|
|
|
|
|
|
Lib.prototype.loadUser((cb) => {
|
|
|
|
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
|
|
|
|
});
|
2016-09-09 11:18:03 +00:00
|
|
|
Lib.prototype.loadTrigger();
|
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-07 11:03:13 +00:00
|
|
|
|
2016-09-08 15:42:38 +00:00
|
|
|
Lib.prototype.getUserlevel = (user, network, channel, cb) => {
|
|
|
|
bot.whois(user, network, (err, data) => {
|
|
|
|
var host = data.username+'@'+data.hostname;
|
2016-09-09 08:32:35 +00:00
|
|
|
bot.names(channel, network, (names) => {
|
|
|
|
var lvl_channel = (data.nick in names)? 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)
|
|
|
|
});
|
2016-08-23 11:23:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Lib.prototype.loadUser = (cb) => {
|
2016-09-07 11:03:13 +00:00
|
|
|
Lib.prototype.admins = [];
|
2016-08-23 11:23:25 +00:00
|
|
|
sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
|
2016-09-07 11:03:13 +00:00
|
|
|
rows.forEach(e => {
|
|
|
|
Lib.prototype.admins.push(e.vhost);
|
|
|
|
Lib.prototype.admins[e.vhost] = {
|
2016-08-23 11:23:25 +00:00
|
|
|
'id': e.id,
|
|
|
|
'nick': e.nick,
|
|
|
|
'vhost': e.vhost,
|
|
|
|
'level': e.level,
|
|
|
|
'server': e.server
|
|
|
|
};
|
|
|
|
});
|
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-08-31 07:37:41 +00:00
|
|
|
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);
|
2016-08-23 11:23:25 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
Lib.prototype.checkRepostCheckSum = (cs, cbcrcs) => {
|
2016-08-31 07:37:41 +00:00
|
|
|
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);
|
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');
|
|
|
|
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/';
|
|
|
|
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-08-24 17:15:14 +00:00
|
|
|
exec('ffmpegthumbnailer -i'+e.dest+' -s1024 -o'+thumbnail, (error) => {
|
2016-08-23 11:23:25 +00:00
|
|
|
if(error) {
|
2016-08-23 15:50:30 +00:00
|
|
|
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 1');
|
2016-08-23 11:23:25 +00:00
|
|
|
fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
|
|
|
|
if(error)
|
2016-08-23 15:50:30 +00:00
|
|
|
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 2');
|
2016-08-23 11:23:25 +00:00
|
|
|
else
|
2016-08-23 15:50:30 +00:00
|
|
|
Lib.prototype.log("generated thumbnail for "+e.id+" ("+e.mime+")");
|
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-08-23 11:23:25 +00:00
|
|
|
bot.send("#f0ck", 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);
|
|
|
|
require(__dirname+'/trigger/' + file)(bot, Lib.prototype);
|
|
|
|
}
|
|
|
|
});
|
2016-08-23 11:23:25 +00:00
|
|
|
};
|