f0ckv1/src/lib.js

196 lines
6.2 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-15 19:25:07 +00:00
var self = Lib.prototype;
self.bot = require('coffea')();
2016-09-14 22:05:18 +00:00
irccfg.forEach(e => {
2016-09-15 19:25:07 +00:00
self.bot.add({
2016-09-14 22:05:18 +00:00
"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-15 19:25:07 +00:00
self.admins = [];
self.debug = false;
self.trigger = {
2016-09-09 11:18:03 +00:00
trigger: [],
add: (args) => {
2016-09-15 19:25:07 +00:00
self.trigger.trigger.push({
2016-09-09 11:18:03 +00:00
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 = () => {
2016-09-15 19:25:07 +00:00
self.sql = mysql.createConnection(require('../cfg/mysql.json'));
self.sql.connect((err) => {
2016-09-14 22:05:18 +00:00
if(err) setTimeout(haDC, 2000);
});
2016-09-15 19:25:07 +00:00
self.sql.on('error', (err) => {
2016-09-14 22:05:18 +00:00
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() {
2016-09-15 19:25:07 +00:00
self.cfg = require('../cfg/main.json');
self.debug = self.cfg.debug;
self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
self.loadTrigger();
self.loadEvents();
2016-09-09 11:26:17 +00:00
}
2016-09-15 19:25:07 +00:00
self.getUserlevel = (user, network, channel, cb) => {
self.bot.whois(user, network, (err, data) => {
self.bot.names(channel, network, (names) => {
var lvl_channel = (data.nick in names)? self.cfg.level[ names[data.nick] ] : 0;
2016-09-09 08:32:35 +00:00
var lvl_db = 0;
2016-09-15 19:25:07 +00:00
if(data.account in self.admins)
lvl_db = (self.admins[data.account].network === network)? self.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
});
});
};
2016-09-15 19:25:07 +00:00
self.loadUser = (cb) => {
self.admins = [];
self.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
2016-09-07 11:03:13 +00:00
rows.forEach(e => {
2016-09-15 19:25:07 +00:00
self.admins[e.account] = {
2016-09-14 22:05:18 +00:00
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
});
}
2016-09-15 19:25:07 +00:00
self.checkRepost = (url, cbcr) => {
self.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
});
};
2016-09-15 19:25:07 +00:00
self.checkRepostCheckSum = (cs, cbcrcs) => {
self.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
});
};
2016-09-15 19:25:07 +00:00
self.formatSize = (size) => {
2016-08-23 11:23:25 +00:00
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];
};
2016-09-15 19:25:07 +00:00
self.getCheckSum = (file, cbcs) => {
2016-08-23 11:23:25 +00:00
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
};
2016-09-15 19:25:07 +00:00
self.generateThumbs = () => {
2016-09-11 15:08:16 +00:00
var outdir = './t';
2016-09-15 19:25:07 +00:00
self.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => {
2016-09-11 15:08:16 +00:00
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) {
2016-09-15 19:25:07 +00:00
self.log('failed thumbnail for '+e.id+' ('+e.mime+')');
2016-09-12 22:39:21 +00:00
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-15 19:25:07 +00:00
self.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) => {
2016-09-15 19:25:07 +00:00
self.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')');
2016-09-12 22:39:21 +00:00
});
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-15 19:25:07 +00:00
self.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')');
2016-09-11 20:12:03 +00:00
});
2016-09-12 22:39:21 +00:00
});
2016-09-11 15:08:16 +00:00
}
}
});
});
};
2016-09-15 19:25:07 +00:00
self.log = (msg) => {
if(self.debug)
self.bot.send(self.cfg.debugchannel, msg, 'n0xy');
2016-09-09 11:18:03 +00:00
};
2016-09-15 19:25:07 +00:00
self.loadTrigger = () => {
2016-09-09 11:18:03 +00:00
var files = fs.readdirSync(__dirname+'/trigger/');
files.forEach((file) => {
if(file.substr(-3, 3) === '.js') {
console.log('Loading trigger', file);
2016-09-15 19:25:07 +00:00
require(__dirname+'/trigger/' + file)(self);
2016-09-09 11:18:03 +00:00
}
});
2016-09-14 17:24:48 +00:00
};
2016-09-15 19:25:07 +00:00
self.loadEvents = () => {
2016-09-14 17:24:48 +00:00
var files = fs.readdirSync(__dirname+'/events/');
files.forEach((file) => {
if(file.substr(-3, 3) === '.js') {
console.log('Loading event', file);
2016-09-15 19:25:07 +00:00
require(__dirname+'/events/' + file)(self);
2016-09-14 17:24:48 +00:00
}
});
2016-09-15 17:17:07 +00:00
};
2016-09-15 19:25:07 +00:00
self.groupUserModes = modes => {
2016-09-15 17:17:07 +00:00
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
};