f0ckv1/src/lib.js

196 lines
6.9 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 probe = require('node-ffprobe');
var crypto = require('crypto');
2016-08-24 12:18:51 +00:00
var Mime = require('mime');
2016-08-29 20:07:50 +00:00
var cfg = require('../cfg/main.json');
2016-08-23 11:23:25 +00:00
2016-08-29 20:07:50 +00:00
var bot, sql, debug;
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-24 20:21:02 +00:00
this.debug = debug = false;
2016-08-23 11:23:25 +00:00
this.admins = [];
2016-08-29 20:30:36 +00:00
Lib.prototype.loadUser((cb) => {
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
});
2016-08-23 11:23:25 +00:00
}
2016-08-23 15:50:30 +00:00
Lib.prototype.getUser = (e, cbgu) => {
var u = e.user.getNick();
var n = e.network;
if(!e.user.hasOwnProperty('hostname')) {
2016-08-24 17:15:14 +00:00
setTimeout(()=>{
bot.write('WHOIS '+u, n, () => {
bot.once('data', (err, msg) => {
var params;
var map = [];
map.push(u);
if(msg.command == 'RPL_WHOISUSER') {
params = msg.params.split(' ');
map[u] = map[u] || {};
map[u].nick = u;
map[u].username = params[2];
map[u].hostname = params[3];
map[u].realname = msg.trailing;
}
if(typeof(map[u]) === 'object')
cbgu(map[u]);
});
2016-08-23 15:50:30 +00:00
});
2016-08-24 17:15:14 +00:00
}, 100);
2016-08-23 15:50:30 +00:00
}
else {
cbgu({
nick: e.user.nick,
username: e.user.username,
hostname: e.user.hostname,
realname: e.user.realname
});
}
2016-08-23 11:23:25 +00:00
};
2016-08-23 15:50:30 +00:00
Lib.prototype.getUserlevel = (e, cb) => {
Lib.prototype.getUser(e, (cbgu) => {
2016-08-23 11:23:25 +00:00
var host = cbgu.username+'@'+cbgu.hostname;
2016-08-23 15:50:30 +00:00
var lvl_channel = (cbgu.nick in e.channel.names)? cfg.level[ e.channel.names[cbgu.nick] ] : 0;
2016-08-23 11:23:25 +00:00
var lvl_db = 0;
if(host in this.admins)
lvl_db = (this.admins[host].server == e.network)? this.admins[host].level : 0;
cb({
'channel': lvl_channel,
'db': lvl_db,
'level': Math.max(lvl_channel, lvl_db)
});
});
};
Lib.prototype.loadUser = (cb) => {
this.admins = [];
sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
rows.forEach((e,i,a) => {
this.admins.push(e.nick);
this.admins[e.vhost] = {
'id': e.id,
'nick': e.nick,
'vhost': e.vhost,
'level': e.level,
'server': e.server
};
});
cb(true);
}).on('error', () => {
cb(false);
});
}
Lib.prototype.checkRepost = (url, cbcr) => {
sql.query("select count(*) as count from `f0ck`.`items` where `src` = ?", url, (err, rows, fields) => {
cbcr((rows[0].count == 0)?true:false);
});
};
Lib.prototype.checkRepostCheckSum = (cs, cbcrcs) => {
sql.query("select count(*) as count from `f0ck`.`items` where `checksum` = ?", cs, (err, rows, fields) => {
cbcrcs((rows[0].count == 0)?true:false);
});
};
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.dl = (url, dest, cb) => {
var request = (url.match(/^https/)?https:http).get(url, (response) => { // type:1=post,type:2=stfu
console.log(response.headers['content-type']);
if(cfg.allowedMimes.hasOwnProperty(response.headers['content-type'])) {
if(response.headers['content-length'] <= cfg.maxFileSize) {
2016-08-23 15:50:30 +00:00
Lib.prototype.checkRepost(url, (cbcr) => {
2016-08-23 11:23:25 +00:00
if(cbcr) {
var file = fs.createWriteStream(dest+"."+cfg.allowedMimes[response.headers['content-type']]);
response.pipe(file);
file.on('finish', () => {
file.close();
2016-08-24 12:18:51 +00:00
var mime = Mime.lookup(dest+"."+cfg.allowedMimes[response.headers['content-type']]);
if(cfg.allowedMimes.hasOwnProperty(mime)) {
probe(dest+"."+cfg.allowedMimes[response.headers['content-type']], (err, probeData) => {
if(probeData.streams[0].height !== undefined || probeData.streams[0].width !== undefined) {
2016-08-24 17:15:14 +00:00
if(probeData.streams[0].height + probeData.streams[0].width <= cfg.minRes)
cb({'status':false, 'msg':'f0ck! your shitpost is too small', 'type':1});
2016-08-24 12:18:51 +00:00
else
cb({'status':true, 'msg':'downloaded '+dest, 'type':1, 'infos':{'mime':response.headers['content-type'], 'size':response.headers['content-length'], 'ext':cfg.allowedMimes[response.headers['content-type']]}});
}
2016-08-23 11:23:25 +00:00
else
cb({'status':true, 'msg':'downloaded '+dest, 'type':1, 'infos':{'mime':response.headers['content-type'], 'size':response.headers['content-length'], 'ext':cfg.allowedMimes[response.headers['content-type']]}});
2016-08-24 12:18:51 +00:00
});
}
else
cb({'status':false, 'msg':'lol, go f0ck yourself', 'type':1});
2016-08-23 11:23:25 +00:00
});
file.on('error', (err) => {
fs.unlink(dest+"."+cfg.allowedMimes[response.headers['content-type']]);
file.close();
cb({'status':false, 'msg':err.message, 'type':1});
});
}
else
cb({'status':false, 'msg':'repost motherf0cker', 'type':1});
});
}
else
cb({'status':false, 'msg':'f0ck! your file is too big (~'+formatSize(response.headers['content-length'])+'), max '+formatSize(cfg.maxFileSize)+' allowed', 'type':1});
}
else
cb({'status':false, 'msg':'f0ck you', 'type':2});
}).on('error', (msg) => {
cb({'status':false, 'msg':msg, 'type':2});
});
};
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) => {
if(debug)
2016-08-23 11:23:25 +00:00
bot.send("#f0ck", msg, 'n0xy');
};
Lib.prototype.toggleDebug = () => {
if(debug) {
debug = false;
return 'debugmode deactivated';
}
else {
debug = true;
return 'debugmode activated';
}
2016-08-23 11:23:25 +00:00
};