f0ckv1/src/lib.js

169 lines
5.4 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-08-24 12:18:51 +00:00
var Mime = require('mime');
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
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-09 11:26:17 +00:00
module.exports = Lib;
function Lib(tbot, tsql) {
this.bot = bot = tbot;
Lib.prototype.sql = tsql;
Lib.prototype.cfg = require('../cfg/main.json');
2016-09-09 11:26:17 +00:00
Lib.prototype.debug = Lib.prototype.cfg.debug;
2016-09-10 15:53:13 +00:00
2016-09-09 11:26:17 +00:00
Lib.prototype.loadUser((cb) => {
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
});
Lib.prototype.loadTrigger();
}
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)? Lib.prototype.cfg.level[ names[data.nick] ] : 0;
2016-09-09 08:32:35 +00:00
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-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 => {
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-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');
var s = fs.ReadStream(file);
s.on('data', (d) => {
sha256sum.update(d);
});
s.on('end', () => {
cbcs(sha256sum.digest('hex'));
});
};
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)) {
var cmd;
switch(e.mime) {
case "video/mp4":
case "video/webm":
case "audio/mpeg":
case "audio/ogg":
case "image/gif":
cmd = 'ffmpeg -i '+e.dest+' -vframes 1 -filter "scale=-1:256,crop=128:128" '+thumbnail;
break;
case "image/png":
case "image/jpeg":
cmd = 'convert -thumbnail 256x256 '+e.dest+' -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.generateThumbs = () => {
2016-08-23 11:23:25 +00:00
var outdir = './t/';
2016-09-09 11:26:17 +00:00
Lib.prototype.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => {
2016-08-23 11:23:25 +00:00
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-09-09 21:06:39 +00:00
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') '+JSON.stringify(error));
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
});
}
});
}
});
});
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)
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);
require(__dirname+'/trigger/' + file)(bot, Lib.prototype);
}
});
2016-08-23 11:23:25 +00:00
};