self yay
This commit is contained in:
parent
cb00071105
commit
2bc7f45fb6
89
src/lib.js
89
src/lib.js
|
@ -7,9 +7,10 @@ var request = require('request');
|
||||||
var mysql = require('mysql');
|
var mysql = require('mysql');
|
||||||
var irccfg = require('../cfg/irc.json');
|
var irccfg = require('../cfg/irc.json');
|
||||||
|
|
||||||
Lib.prototype.bot = require('coffea')();
|
var self = Lib.prototype;
|
||||||
|
self.bot = require('coffea')();
|
||||||
irccfg.forEach(e => {
|
irccfg.forEach(e => {
|
||||||
Lib.prototype.bot.add({
|
self.bot.add({
|
||||||
"name": e.name,
|
"name": e.name,
|
||||||
"host": e.host,
|
"host": e.host,
|
||||||
"port": e.port,
|
"port": e.port,
|
||||||
|
@ -23,12 +24,12 @@ irccfg.forEach(e => {
|
||||||
console.log("Server "+e.name+" wurde geladen");
|
console.log("Server "+e.name+" wurde geladen");
|
||||||
});
|
});
|
||||||
|
|
||||||
Lib.prototype.admins = [];
|
self.admins = [];
|
||||||
Lib.prototype.debug = false;
|
self.debug = false;
|
||||||
Lib.prototype.trigger = {
|
self.trigger = {
|
||||||
trigger: [],
|
trigger: [],
|
||||||
add: (args) => {
|
add: (args) => {
|
||||||
Lib.prototype.trigger.trigger.push({
|
self.trigger.trigger.push({
|
||||||
name: args.name,
|
name: args.name,
|
||||||
call: args.call,
|
call: args.call,
|
||||||
func: args.func,
|
func: args.func,
|
||||||
|
@ -39,11 +40,11 @@ Lib.prototype.trigger = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
var haDC = () => {
|
var haDC = () => {
|
||||||
Lib.prototype.sql = mysql.createConnection(require('../cfg/mysql.json'));
|
self.sql = mysql.createConnection(require('../cfg/mysql.json'));
|
||||||
Lib.prototype.sql.connect((err) => {
|
self.sql.connect((err) => {
|
||||||
if(err) setTimeout(haDC, 2000);
|
if(err) setTimeout(haDC, 2000);
|
||||||
});
|
});
|
||||||
Lib.prototype.sql.on('error', (err) => {
|
self.sql.on('error', (err) => {
|
||||||
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
|
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -51,21 +52,21 @@ haDC();
|
||||||
|
|
||||||
module.exports = Lib;
|
module.exports = Lib;
|
||||||
function Lib() {
|
function Lib() {
|
||||||
Lib.prototype.cfg = require('../cfg/main.json');
|
self.cfg = require('../cfg/main.json');
|
||||||
Lib.prototype.debug = Lib.prototype.cfg.debug;
|
self.debug = self.cfg.debug;
|
||||||
Lib.prototype.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
|
self.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
|
||||||
Lib.prototype.loadTrigger();
|
self.loadTrigger();
|
||||||
Lib.prototype.loadEvents();
|
self.loadEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Lib.prototype.getUserlevel = (user, network, channel, cb) => {
|
self.getUserlevel = (user, network, channel, cb) => {
|
||||||
Lib.prototype.bot.whois(user, network, (err, data) => {
|
self.bot.whois(user, network, (err, data) => {
|
||||||
Lib.prototype.bot.names(channel, network, (names) => {
|
self.bot.names(channel, network, (names) => {
|
||||||
var lvl_channel = (data.nick in names)? Lib.prototype.cfg.level[ names[data.nick] ] : 0;
|
var lvl_channel = (data.nick in names)? self.cfg.level[ names[data.nick] ] : 0;
|
||||||
var lvl_db = 0;
|
var lvl_db = 0;
|
||||||
if(data.account in Lib.prototype.admins)
|
if(data.account in self.admins)
|
||||||
lvl_db = (Lib.prototype.admins[data.account].network === network)? Lib.prototype.admins[data.account].level : 0;
|
lvl_db = (self.admins[data.account].network === network)? self.admins[data.account].level : 0;
|
||||||
cb({
|
cb({
|
||||||
'channel': lvl_channel,
|
'channel': lvl_channel,
|
||||||
'db': lvl_db,
|
'db': lvl_db,
|
||||||
|
@ -74,11 +75,11 @@ Lib.prototype.getUserlevel = (user, network, channel, cb) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Lib.prototype.loadUser = (cb) => {
|
self.loadUser = (cb) => {
|
||||||
Lib.prototype.admins = [];
|
self.admins = [];
|
||||||
Lib.prototype.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
|
self.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
|
||||||
rows.forEach(e => {
|
rows.forEach(e => {
|
||||||
Lib.prototype.admins[e.account] = {
|
self.admins[e.account] = {
|
||||||
id: e.id,
|
id: e.id,
|
||||||
account: e.account,
|
account: e.account,
|
||||||
level: e.level,
|
level: e.level,
|
||||||
|
@ -90,29 +91,29 @@ Lib.prototype.loadUser = (cb) => {
|
||||||
if(cb) cb(false);
|
if(cb) cb(false);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Lib.prototype.checkRepost = (url, cbcr) => {
|
self.checkRepost = (url, cbcr) => {
|
||||||
Lib.prototype.sql.query("select count(id) as count, id from `f0ck`.`items` where `src` = ?", url, (err, rows, fields) => {
|
self.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);
|
cbcr((rows[0].count == 0)?true:rows[0].id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Lib.prototype.checkRepostCheckSum = (cs, cbcrcs) => {
|
self.checkRepostCheckSum = (cs, cbcrcs) => {
|
||||||
Lib.prototype.sql.query("select count(id) as count, id from `f0ck`.`items` where `checksum` = ?", cs, (err, rows, fields) => {
|
self.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);
|
cbcrcs((rows[0].count == 0)?true:rows[0].id);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Lib.prototype.formatSize = (size) => {
|
self.formatSize = (size) => {
|
||||||
var i = Math.floor(Math.log(size) / Math.log(1024));
|
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];
|
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
|
||||||
};
|
};
|
||||||
Lib.prototype.getCheckSum = (file, cbcs) => {
|
self.getCheckSum = (file, cbcs) => {
|
||||||
var sha256sum = crypto.createHash('sha256');
|
var sha256sum = crypto.createHash('sha256');
|
||||||
fs.ReadStream(file)
|
fs.ReadStream(file)
|
||||||
.on('data', d => sha256sum.update(d))
|
.on('data', d => sha256sum.update(d))
|
||||||
.on('end', () => cbcs(sha256sum.digest('hex')));
|
.on('end', () => cbcs(sha256sum.digest('hex')));
|
||||||
};
|
};
|
||||||
Lib.prototype.generateThumbs = () => {
|
self.generateThumbs = () => {
|
||||||
var outdir = './t';
|
var outdir = './t';
|
||||||
Lib.prototype.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => {
|
self.sql.query("select * from `f0ck`.`items`", (err, rows, fields) => {
|
||||||
rows.forEach((e,i,a) => {
|
rows.forEach((e,i,a) => {
|
||||||
var thumbnail = outdir+'/'+e.id+'.png';
|
var thumbnail = outdir+'/'+e.id+'.png';
|
||||||
if(!fs.existsSync(thumbnail)) {
|
if(!fs.existsSync(thumbnail)) {
|
||||||
|
@ -126,19 +127,19 @@ Lib.prototype.generateThumbs = () => {
|
||||||
case "image/gif":
|
case "image/gif":
|
||||||
exec('ffmpegthumbnailer -i'+e.dest+' -s1024 -o'+thumbnail, (error) => {
|
exec('ffmpegthumbnailer -i'+e.dest+' -s1024 -o'+thumbnail, (error) => {
|
||||||
if(error) {
|
if(error) {
|
||||||
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+')');
|
self.log('failed thumbnail for '+e.id+' ('+e.mime+')');
|
||||||
fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail
|
fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
|
exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
|
||||||
Lib.prototype.log((error?'failed':'generated') + ' thumbnail for '+e.id+' ('+e.mime+')');
|
self.log((error?'failed':'generated') + ' thumbnail for '+e.id+' ('+e.mime+')');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "image/png":
|
case "image/png":
|
||||||
case "image/jpeg":
|
case "image/jpeg":
|
||||||
exec('convert '+e.dest+' -resize "200x200^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
|
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+')');
|
self.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')');
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -149,7 +150,7 @@ Lib.prototype.generateThumbs = () => {
|
||||||
dat.on('finish', () => {
|
dat.on('finish', () => {
|
||||||
dat.close();
|
dat.close();
|
||||||
exec('convert '+thumbnail+' -resize "200x200^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => {
|
exec('convert '+thumbnail+' -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+')');
|
self.log((error)?'failed thumbnail for '+e.id+' ('+e.mime+')':'generated thumbnail for '+e.id+' ('+e.mime+')');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -157,29 +158,29 @@ Lib.prototype.generateThumbs = () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Lib.prototype.log = (msg) => {
|
self.log = (msg) => {
|
||||||
if(Lib.prototype.debug)
|
if(self.debug)
|
||||||
Lib.prototype.bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy');
|
self.bot.send(self.cfg.debugchannel, msg, 'n0xy');
|
||||||
};
|
};
|
||||||
Lib.prototype.loadTrigger = () => {
|
self.loadTrigger = () => {
|
||||||
var files = fs.readdirSync(__dirname+'/trigger/');
|
var files = fs.readdirSync(__dirname+'/trigger/');
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
if(file.substr(-3, 3) === '.js') {
|
if(file.substr(-3, 3) === '.js') {
|
||||||
console.log('Loading trigger', file);
|
console.log('Loading trigger', file);
|
||||||
require(__dirname+'/trigger/' + file)(Lib.prototype);
|
require(__dirname+'/trigger/' + file)(self);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Lib.prototype.loadEvents = () => {
|
self.loadEvents = () => {
|
||||||
var files = fs.readdirSync(__dirname+'/events/');
|
var files = fs.readdirSync(__dirname+'/events/');
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
if(file.substr(-3, 3) === '.js') {
|
if(file.substr(-3, 3) === '.js') {
|
||||||
console.log('Loading event', file);
|
console.log('Loading event', file);
|
||||||
require(__dirname+'/events/' + file)(Lib.prototype);
|
require(__dirname+'/events/' + file)(self);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Lib.prototype.groupUserModes = modes => {
|
self.groupUserModes = modes => {
|
||||||
var modesToApply = [], currentPrefix, queuedModes;
|
var modesToApply = [], currentPrefix, queuedModes;
|
||||||
while(modes.length) {
|
while(modes.length) {
|
||||||
queuedModes = "";
|
queuedModes = "";
|
||||||
|
|
Loading…
Reference in New Issue
Block a user