codeoverhaul

This commit is contained in:
Flummi 2016-09-15 00:05:18 +02:00
parent e87499e705
commit 32b830f929
20 changed files with 130 additions and 150 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ node_modules/
cfg/*.json
b/
log/*.log
t/

4
bot.js
View File

@ -16,6 +16,7 @@ var getDateTimeString = () => {
};
var log_forever = './log/forever_'+getDateTimeString()+'.log';
var log_bot = './log/bot_'+getDateTimeString()+'.log';
var err_bot = './log/err_'+getDateTimeString()+'.log';
var putlog = (msg) => {
fs.appendFileSync(log_forever, msg, 'utf8');
};
@ -31,9 +32,8 @@ var child = new (forever.Monitor)('./src/main.js', {
'*.log', 'b/*', 't/*', 'node_modules/*'
],
watchDirectory: __dirname,
logFile: log_bot,
outFile: log_bot,
errFile: log_bot
errFile: err_bot
});
child.on('watch:restart', (info) => {

View File

@ -1,6 +1,6 @@
module.exports = (bot, lib) => {
bot.on('join', (e) => {
bot.whois(e.user.getNick(), e.network, (err, data) => {
module.exports = (lib) => {
lib.bot.on('join', (e) => {
lib.bot.whois(e.user.getNick(), e.network, (err, data) => {
if(data.account !== null) {
lib.getUserlevel(e.user.getNick(), e.network, e.channel, (cb) => {
var modes = false;
@ -13,7 +13,7 @@ module.exports = (bot, lib) => {
else if(cb.level >= 10)
modes = "+v";
if(modes !== false)
bot.mode(e.channel, modes + (' '+e.user.getNick()).repeat(modes.length-1), e.network);
lib.bot.mode(e.channel, modes + (' '+e.user.getNick()).repeat(modes.length-1), e.network);
});
}
});

15
src/events/message.js Normal file
View File

@ -0,0 +1,15 @@
module.exports = (lib) => {
lib.bot.on('message', (e) => {
lib.trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call) && entry.active == 1) {
lib.log('trigger: '+entry.name);
lib.getUserlevel(e.user.getNick(), e.network, e.channel.getName(), (cb) => {
if(cb.level >= entry.level)
entry.func(e, cb.level);
else
e.reply('no permission, min level '+entry.level+' required');
});
}
});
});
};

6
src/events/motd.js Normal file
View File

@ -0,0 +1,6 @@
module.exports = (lib) => {
lib.bot.on('motd', (e) => {
console.log("motd von "+e.network+" erhalten");
lib.bot.mode(lib.bot.me.nick, "+B", e.network);
});
};

View File

@ -4,8 +4,24 @@ var https = require('https');
var exec = require('child_process').exec;
var crypto = require('crypto');
var request = require('request');
var mysql = require('mysql');
var irccfg = require('../cfg/irc.json');
var bot, sql;
Lib.prototype.bot = require('coffea')();
irccfg.forEach(e => {
Lib.prototype.bot.add({
"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");
});
Lib.prototype.admins = [];
Lib.prototype.debug = false;
@ -22,32 +38,34 @@ Lib.prototype.trigger = {
});
}
};
var haDC = () => {
Lib.prototype.sql = mysql.createConnection(require('../cfg/mysql.json'));
Lib.prototype.sql.connect((err) => {
if(err) setTimeout(haDC, 2000);
});
Lib.prototype.sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
};
haDC();
module.exports = Lib;
function Lib(tbot, tsql) {
this.bot = bot = tbot;
Lib.prototype.sql = tsql;
function Lib() {
Lib.prototype.cfg = require('../cfg/main.json');
Lib.prototype.debug = Lib.prototype.cfg.debug;
Lib.prototype.loadUser((cb) => {
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
});
Lib.prototype.loadUser((cb) => console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"));
Lib.prototype.loadTrigger();
Lib.prototype.loadEvents();
}
Lib.prototype.getUserlevel = (user, network, channel, cb) => {
bot.whois(user, network, (err, data) => {
var host = data.username+'@'+data.hostname;
bot.names(channel, network, (names) => {
Lib.prototype.bot.whois(user, network, (err, data) => {
Lib.prototype.bot.names(channel, network, (names) => {
var lvl_channel = (data.nick in names)? Lib.prototype.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;
if(data.account in Lib.prototype.admins)
lvl_db = (Lib.prototype.admins[data.account].network === network)? Lib.prototype.admins[data.account].level : 0;
cb({
'channel': lvl_channel,
'db': lvl_db,
@ -60,13 +78,11 @@ Lib.prototype.loadUser = (cb) => {
Lib.prototype.admins = [];
Lib.prototype.sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
rows.forEach(e => {
Lib.prototype.admins.push(e.vhost);
Lib.prototype.admins[e.vhost] = {
'id': e.id,
'nick': e.nick,
'vhost': e.vhost,
'level': e.level,
'server': e.server
Lib.prototype.admins[e.account] = {
id: e.id,
account: e.account,
level: e.level,
network: e.network
};
});
if(cb) cb(true);
@ -90,13 +106,9 @@ Lib.prototype.formatSize = (size) => {
};
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'));
});
fs.ReadStream(file)
.on('data', d => sha256sum.update(d))
.on('end', () => cbcs(sha256sum.digest('hex')));
};
Lib.prototype.generateThumbs = () => {
var outdir = './t';
@ -117,14 +129,10 @@ Lib.prototype.generateThumbs = () => {
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+')');
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)
Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 2');
else
Lib.prototype.log("generated thumbnail for "+e.id+" ("+e.mime+")");
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+')');
});
}
});
break;
case "image/png":
@ -151,14 +159,14 @@ Lib.prototype.generateThumbs = () => {
};
Lib.prototype.log = (msg) => {
if(Lib.prototype.debug)
bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy');
Lib.prototype.bot.send(Lib.prototype.cfg.debugchannel, msg, 'n0xy');
};
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);
require(__dirname+'/trigger/' + file)(Lib.prototype);
}
});
};
@ -167,7 +175,7 @@ Lib.prototype.loadEvents = () => {
files.forEach((file) => {
if(file.substr(-3, 3) === '.js') {
console.log('Loading event', file);
require(__dirname+'/events/' + file)(bot, Lib.prototype);
require(__dirname+'/events/' + file)(Lib.prototype);
}
});
};

View File

@ -1,55 +1,5 @@
var cfg = require('../cfg/irc.json');
var fs = require('fs-extra');
var mysql = require('mysql');
var bot = require('coffea')();
var Lib = require('./lib.js');
var Websrv = require('./websrv.js');
var sql;
var haDC = () => {
sql = mysql.createConnection(require('../cfg/mysql.json'));
sql.connect((err) => {
if(err) setTimeout(haDC, 2000);
});
sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
};
haDC();
var lib = new Lib(bot, sql);
var websrv = new Websrv(bot, sql, lib);
cfg.forEach((e,i,a) => {
bot.add({
"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");
});
bot.on('motd', (e) => {
console.log("motd von "+e.network+" erhalten");
bot.mode(bot.me, "+B", e.network);
});
bot.on('message', (e) => {
lib.trigger.trigger.forEach((entry,i,a) => {
if(e.message.match(entry.call) && entry.active == 1) {
lib.log('trigger: '+entry.name);
lib.getUserlevel(e.user.getNick(), e.network, e.channel.getName(), (cb) => {
if(cb.level >= entry.level)
entry.func(e, cb.level);
else
e.reply('no permission, min level '+entry.level+' required');
});
}
});
});
var lib = new Lib();
var websrv = new Websrv(lib);

View File

@ -1,6 +1,6 @@
var fs = require('fs-extra');
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'clean',
call: /^\!clean/i,

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'debug',
call: /^\!debug$/i,

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'delete',
call: /^\!del (\d+)$/i,

View File

@ -1,6 +1,6 @@
var exec = require('child_process').exec;
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'gitpull',
call: /^\!pull$/,

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'level',
call: /^\!level(?: +\@.*)?/i,
@ -13,32 +13,34 @@ module.exports = (bot, lib) => {
var level = (args[2]>=100)?100:args[2];
if( name in e.channel.names ) {
bot.whois(name.trim(), e.network, (err, data) => {
console.log(lib.admins);
lib.bot.whois(name.trim(), e.network, (err, data) => {
if(!err) {
if(data.account !== null) {
var host = data.username+'@'+data.hostname;
if(host in lib.admins) {
if(data.account in lib.admins) {
if(level <= 0) {
lib.sql.query('delete from `f0ck`.`user` where `id` = ? limit 1', [lib.admins[host].id], () => {
lib.sql.query('delete from `f0ck`.`user` where `id` = ? limit 1', [lib.admins[data.account].id], () => {
lib.loadUser();
e.reply('deleted user '+data.nick+' ('+host+')');
e.reply('deleted user '+data.nick+' ('+data.account+')');
});
}
else {
lib.sql.query('update `f0ck`.`user` set `level` = ? where `id` = ?', [level, lib.admins[host].id], () => {
lib.sql.query('update `f0ck`.`user` set `level` = ? where `id` = ?', [level, lib.admins[data.account].id], () => {
lib.loadUser();
e.reply('changed user '+data.nick+' ('+host+') to level '+level);
e.reply('changed user '+data.nick+' ('+data.account+') to level '+level);
});
}
}
else {
lib.sql.query('insert into `f0ck`.`user` (`nick`,`vhost`,`level`,`server`) values (?,?,?,?)', [data.nick, host, level, e.network], () => {
lib.sql.query('insert into `f0ck`.`user` (`account`,`level`,`network`) values (?,?,?)', [data.account, level, e.network], () => {
lib.loadUser();
e.reply('added new user '+data.nick+' ('+host+') with level '+level);
e.reply('added new user '+data.nick+' ('+data.account+') with level '+level);
});
}
}
else
e.reply('user '+data.nick+' isn\'t registered');
}
});
}
else

View File

@ -9,7 +9,7 @@ var ytdl = require('ytdl-core');
var Readable = require('stream').Readable;
var cfg = require('../../cfg/main.json');
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'parser',
call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi,
@ -25,7 +25,7 @@ module.exports = (bot, lib) => {
if(cb.success === true) {
fs.move(cb.file, cb.file + '.' + cb.info.ext, (err) => {
if(!err) {
bot.whois(e.user.getNick(), e.network, (err, cbgu) => {
lib.bot.whois(e.user.getNick(), e.network, (err, cbgu) => {
lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`,`thumb`) values (?,?,?,?,?,?,?,?,?,?,?)", [
entry,
cb.file + '.' + cb.info.ext,

View File

@ -1,6 +1,6 @@
var du = require('du');
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'stats',
call: /^\!stats$/i,

View File

@ -1,19 +1,19 @@
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'sync',
call: /^!sync$/i,
level: 100,
active: 1,
func: (e) => {
bot.names(e.channel, e.network, (names) => {
lib.bot.names(e.channel, e.network, (names) => {
Object.keys(names).forEach(key => {
bot.whois(key, e.network, (err, data) => {
if(data.nick != bot.me.nick) {
bot.mode(e.channel, '-qaohv' + (' '+data.nick).repeat(5), e.network);
lib.bot.whois(key, e.network, (err, data) => {
if(data.nick != lib.bot.me.nick) {
lib.bot.mode(e.channel, '-qaohv' + (' '+data.nick).repeat(5), e.network);
var level = 0;
var modes = false;
if((data.username+'@'+data.hostname) in lib.admins) {
level = lib.admins[data.username+'@'+data.hostname].level;
if(data.account in lib.admins) {
level = lib.admins[data.account].level;
if(level >= 70)
modes = "+ao";
else if(level >= 60)
@ -24,7 +24,7 @@ module.exports = (bot, lib) => {
modes = "+v";
}
if(modes !== false)
bot.mode(e.channel, modes + (' '+data.nick).repeat(modes.length-1), e.network);
lib.bot.mode(e.channel, modes + (' '+data.nick).repeat(modes.length-1), e.network);
}
});
});

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'test',
call: /^!test$/i,

View File

@ -1,6 +1,6 @@
var fs = require('fs-extra');
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'thumbnailer',
call: /^\!thumb(.*\d+)?$/i,

View File

@ -2,7 +2,7 @@ var allowedModes = [
'q','a','o','h','v'
];
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'umode',
call: /^\!umode .*/i,
@ -17,7 +17,7 @@ module.exports = (bot, lib) => {
else
tmp.pop();
var modes;
if(user.toLowerCase() != bot.me.nick.toLowerCase()) {
if(user.toLowerCase() != lib.bot.me.nick.toLowerCase()) {
var modes = processModes(tmp.join(''));
if(modes.length) {
if(checkModes(modes, allowedModes)) {
@ -32,7 +32,7 @@ module.exports = (bot, lib) => {
modesToApply.push(currentPrefix + queuedModes + (' ' + user).repeat(queuedModes.length));
for(var i = 0; i < queuedModes.length; i++) modes.shift();
}
modesToApply.forEach(modeStr => bot.mode(e.channel, modeStr, e.network));
modesToApply.forEach(modeStr => lib.bot.mode(e.channel, modeStr, e.network));
}
else
e.reply('Error! Allowed modes are: ' + allowedModes.join(','));

View File

@ -1,4 +1,4 @@
module.exports = (bot, lib) => {
module.exports = (lib) => {
lib.trigger.add({
name: 'user',
call: /^\!user( \@.*)?/i,
@ -9,7 +9,7 @@ module.exports = (bot, lib) => {
user = e.user.getNick();
else
user = user.trim();
bot.whois(user, e.network, (err, data) => {
lib.bot.whois(user, e.network, (err, data) => {
e.reply(data);
});
},

View File

@ -7,12 +7,10 @@ var cfg = require('../cfg/websrv.json');
var exec = require('child_process').exec;
var templates = {};
var bot, sql, lib;
var lib;
module.exports = Websrv;
function Websrv(tbot, tsql, tlib) {
this.bot = bot = tbot;
this.sql = sql = tsql;
function Websrv(tlib) {
this.lib = lib = tlib;
Websrv.prototype.getTpls();
@ -43,12 +41,12 @@ function Websrv(tbot, tsql, tlib) {
catch(ex) {
commit = body.commits[body.commits.length-2].message.replace('\n','').trim();
}
bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+')', 'n0xy' );
lib.bot.send( lib.cfg.debugchannel, eventname + ' from ' + autor + ' ('+commit+')', 'n0xy' );
if( lib.cfg.debugchannel === '#f0ck' ) {
exec('cd ../ & git pull', (error, stdout) => {
if(error === null)
bot.send( lib.cfg.debugchannel, stdout, 'n0xy' );
lib.bot.send( lib.cfg.debugchannel, stdout, 'n0xy' );
});
}
});
@ -81,7 +79,7 @@ function Websrv(tbot, tsql, tlib) {
items: [],
last: 10000
};
sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => {
lib.sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => {
rows.forEach((e,i,a) => {
data.items.push({ "id": e.id, "mime": e.mime });
data.last = e.id;
@ -94,7 +92,7 @@ function Websrv(tbot, tsql, tlib) {
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
sql.query(query, [url, url, url], (err, rows, fields) => {
lib.sql.query(query, [url, url, url], (err, rows, fields) => {
var tpl = swig.compile(templates.item);
var data = {
id: '',
@ -149,7 +147,7 @@ function Websrv(tbot, tsql, tlib) {
});
}
else if(filePath == "./random") {
sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
lib.sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
res.writeHead(301, {
'Cache-Control': 'no-cache, public',
'Location': '/' + rows[0].id
@ -237,7 +235,7 @@ function Websrv(tbot, tsql, tlib) {
var url = filePath.split('/');
if(url[2] === undefined) { // Mainpage
var query = "select * from `f0ck`.`items`";
sql.query(query, (err, rows, fields) => {
lib.sql.query(query, (err, rows, fields) => {
var items = [];
rows.forEach((e,i,a) => {
items.push({
@ -253,7 +251,7 @@ function Websrv(tbot, tsql, tlib) {
var eps = 50;
var id = url[3];
sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
lib.sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
var items = {
"items": [],
"last": id
@ -273,7 +271,7 @@ function Websrv(tbot, tsql, tlib) {
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => {
lib.sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => {
var data;
if(rows[0].length) {
var e = rows[0][0];