buggsen gefiggst

This commit is contained in:
Flummi 2016-08-23 15:50:30 +00:00
parent 2a2f379939
commit 9e29f0c839
8 changed files with 137 additions and 52 deletions

View File

@ -2,6 +2,9 @@
"name": "keinBotv2", "name": "keinBotv2",
"version": "0.1.0", "version": "0.1.0",
"main": "src/main.js", "main": "src/main.js",
"scripts": {
"start": "node src/main.js"
},
"dependencies": { "dependencies": {
"coffea": "^0.4.24", "coffea": "^0.4.24",
"fs-extra": "^0.30.0", "fs-extra": "^0.30.0",

View File

@ -18,29 +18,42 @@ function Lib(tbot, tsql, tcfg) {
this.admins = []; this.admins = [];
} }
Lib.prototype.getUser = (u, n, cbgu) => { Lib.prototype.getUser = (e, cbgu) => {
bot.write('WHOIS '+u, n, () => { var u = e.user.getNick();
bot.once('data', (err, msg) => { var n = e.network;
var params; //console.log(e.user);
var map = []; if(!e.user.hasOwnProperty('hostname')) {
map.push(u); bot.write('WHOIS '+u, n, () => {
if(msg.command == 'RPL_WHOISUSER') { bot.once('data', (err, msg) => {
params = msg.params.split(' '); var params;
map[u] = map[u] || {}; var map = [];
map[u].nick = u; map.push(u);
map[u].username = params[2]; if(msg.command == 'RPL_WHOISUSER') {
map[u].hostname = params[3]; params = msg.params.split(' ');
map[u].realname = msg.trailing; map[u] = map[u] || {};
} map[u].nick = u;
if(typeof(map[u]) === 'object') map[u].username = params[2];
cbgu(map[u]); map[u].hostname = params[3];
map[u].realname = msg.trailing;
}
if(typeof(map[u]) === 'object')
cbgu(map[u]);
});
}); });
}); }
else {
cbgu({
nick: e.user.nick,
username: e.user.username,
hostname: e.user.hostname,
realname: e.user.realname
});
}
}; };
Lib.prototype.getUserlevel = (e, tmp_channel, cb) => { Lib.prototype.getUserlevel = (e, cb) => {
Lib.prototype.getUser(e.user.getNick(), e.network, (cbgu) => { Lib.prototype.getUser(e, (cbgu) => {
var host = cbgu.username+'@'+cbgu.hostname; var host = cbgu.username+'@'+cbgu.hostname;
var lvl_channel = (cbgu.nick in tmp_channel)? cfg.level[ tmp_channel[cbgu.nick] ] : 0; var lvl_channel = (cbgu.nick in e.channel.names)? cfg.level[ e.channel.names[cbgu.nick] ] : 0;
var lvl_db = 0; var lvl_db = 0;
if(host in this.admins) if(host in this.admins)
lvl_db = (this.admins[host].server == e.network)? this.admins[host].level : 0; lvl_db = (this.admins[host].server == e.network)? this.admins[host].level : 0;
@ -98,7 +111,7 @@ Lib.prototype.dl = (url, dest, cb) => {
console.log(response.headers['content-type']); console.log(response.headers['content-type']);
if(cfg.allowedMimes.hasOwnProperty(response.headers['content-type'])) { if(cfg.allowedMimes.hasOwnProperty(response.headers['content-type'])) {
if(response.headers['content-length'] <= cfg.maxFileSize) { if(response.headers['content-length'] <= cfg.maxFileSize) {
checkRepost(url, (cbcr) => { Lib.prototype.checkRepost(url, (cbcr) => {
if(cbcr) { if(cbcr) {
var file = fs.createWriteStream(dest+"."+cfg.allowedMimes[response.headers['content-type']]); var file = fs.createWriteStream(dest+"."+cfg.allowedMimes[response.headers['content-type']]);
response.pipe(file); response.pipe(file);
@ -142,15 +155,15 @@ Lib.prototype.generateThumbs = () => {
if(!fs.existsSync(thumbnail)) { if(!fs.existsSync(thumbnail)) {
exec('ffmpegthumbnailer -i'+e.dest+' -s256 -o'+thumbnail, (error) => { exec('ffmpegthumbnailer -i'+e.dest+' -s256 -o'+thumbnail, (error) => {
if(error) { if(error) {
log('failed thumbnail for '+e.id+' ('+e.mime+') 1'); Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 1');
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) => {
if(error) if(error)
log('failed thumbnail for '+e.id+' ('+e.mime+') 2'); Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 2');
else else
log("generated thumbnail for "+e.id+" ("+e.mime+")"); Lib.prototype.log("generated thumbnail for "+e.id+" ("+e.mime+")");
}); });
} }
}); });

56
src/lib.js_tmp Normal file
View File

@ -0,0 +1,56 @@
exports.lib = (bot, sql) => {
return {
admins: [],
getUser: (u, n, cbgu) => {
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]);
});
});
},
getUserlevel: (e, tmp_channel, cb) => {
_this.getUser(e.user.getNick(), e.network, (cbgu) => {
var host = cbgu.username+'@'+cbgu.hostname;
var lvl_channel = (cbgu.nick in tmp_channel)? cfg.level[ tmp_channel[cbgu.nick] ] : 0;
var lvl_db = 0;
if(host in admins)
lvl_db = (admins[host].server == e.network)? admins[host].level : 0;
cb({
'channel': lvl_channel,
'db': lvl_db,
'level': Math.max(lvl_channel, lvl_db)
});
});
},
loadUser: (cb) => {
_this.admins = [];
sql.query("select * from `f0ck`.`user`", (err, rows, fields) => {
rows.forEach((e,i,a) => {
_this.admins.push(e.nick);
admins[e.vhost] = {
'id': e.id,
'nick': e.nick,
'vhost': e.vhost,
'level': e.level,
'server': e.server
};
});
cb(true);
}).on('error', () => {
cb(false);
});
}
}
};

View File

@ -9,15 +9,12 @@ var path = require('path');
var exec = require('child_process').exec; var exec = require('child_process').exec;
var probe = require('node-ffprobe'); var probe = require('node-ffprobe');
var Lib = require('./lib.js'); var Lib = require('./lib.js');
var swig = require('swig'); var swig = require('swig');
var templates = {}; var templates = {};
var debug = false; var debug = false;
var sql; var sql;
var haDC = () => { var haDC = () => {
sql = mysql.createConnection(cfg.mysql); sql = mysql.createConnection(cfg.mysql);
sql.connect((err) => { sql.connect((err) => {
@ -48,21 +45,29 @@ cfg.server.forEach((e,i,a) => {
bot.on('motd', (e) => { bot.on('motd', (e) => {
console.log("motd von "+e.network+" erhalten"); console.log("motd von "+e.network+" erhalten");
bot.join("#kbot-dev"); // tmp //bot.join("#kbot-dev"); // tmp
bot.write('MODE f0ck +B', e.network, (c)=>{}); // Botflag bot.write('MODE f0ckyou +B', e.network, (c)=>{}); // Botflag
}); });
bot.on('message', (e) => { bot.on('message', (e) => {
var orig = e.message;
trigger.trigger.forEach((entry,i,a) => { trigger.trigger.forEach((entry,i,a) => {
if(orig.match(new RegExp(entry.call, "gi"))) { if(e.message.match(entry.call)) {
entry.func(e); lib.getUserlevel(e, (cb) => {
if(cb.level >= entry.level) {
//setTimeout(()=>{
entry.func(e);
//}, 100);
}
else
e.reply('no permission, min level '+entry.level+' required');
});
} }
}); });
}); });
// Webserver // Webserver
http.createServer((req, res) => { http.createServer((req, res) => {
//console.log(req.connection.remoteAddress);
if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) { if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) {
var filePath = '.' + req.url; var filePath = '.' + req.url;
var url = req.url.split("/")[1]; var url = req.url.split("/")[1];
@ -135,7 +140,7 @@ http.createServer((req, res) => {
data.src = e.src; data.src = e.src;
data.dest = e.dest; data.dest = e.dest;
data.mime = e.mime; data.mime = e.mime;
data.size = formatSize(e.size); data.size = lib.formatSize(e.size);
data.userchannel = e.userchannel; data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork; data.usernetwork = e.usernetwork;
@ -266,6 +271,7 @@ var trigger = {
trigger: [], trigger: [],
add: (args) => { add: (args) => {
trigger.trigger.push({ trigger.trigger.push({
name: args.name,
call: args.call, call: args.call,
func: args.func, func: args.func,
desc: args.desc, desc: args.desc,

View File

@ -1,19 +1,12 @@
module.exports = (bot, trigger, lib) => { module.exports = (bot, trigger, lib) => {
trigger.add({ trigger.add({
name: 'delete', name: 'delete',
call: '^\!del (\d+)$', call: new RegExp('^\\!del (\\d+)$', 'i'),
level: 100, level: 100,
func: (e) => { func: (e) => {
var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; var id = e.message.split(' ')[1];
lib.getUserlevel(e, e.user.getNick(), tmp_channel, (cb) => { lib.sql.query("delete from `f0ck`.`items` where `id` = ?", id).on('end', () => {
if(cb.level >= 100) { e.reply("f0ck "+id+" deleted");
var id = orig.split(' ')[1];
sql.query("delete from `f0ck`.`items` where `id` = ?", id).on('end', () => {
e.reply("f0ck "+id+" deleted");
});
}
else
e.reply("no permission");
}); });
}, },
desc: 'delete f0ck' desc: 'delete f0ck'

View File

@ -1,11 +1,10 @@
module.exports = (bot, trigger, lib) => { module.exports = (bot, trigger, lib) => {
trigger.add({ trigger.add({
name: 'level', name: 'level',
call: '^\!level$', call: new RegExp('^\\!level$', 'i'),
level: 0, level: 0,
func: (e) => { func: (e) => {
var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; lib.getUserlevel(e, (cb) => {
lib.getUserlevel(e, tmp_channel, (cb) => {
e.reply("level from "+e.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")"); e.reply("level from "+e.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")");
}); });
}, },

View File

@ -1,10 +1,11 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
var uuid = require('uuid'); var uuid = require('uuid');
var path = require('path');
module.exports = (bot, trigger, lib) => { module.exports = (bot, trigger, lib) => {
trigger.add({ trigger.add({
name: 'parser', name: 'parser',
call: 'https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?', call: new RegExp('https?:\\/\\/[\\w-]+(\\.[\\w-]+)+\\.?(:\\d+)?(\\/\\S*)?', 'gi'),
level: 0, level: 0,
func: (e) => { func: (e) => {
if(!e.message.match(/\!ignore$/)) { if(!e.message.match(/\!ignore$/)) {
@ -13,7 +14,7 @@ module.exports = (bot, trigger, lib) => {
var tmpdest = uuid.v1().split('-')[0]; var tmpdest = uuid.v1().split('-')[0];
lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item
if(cb.status === true) { if(cb.status === true) {
lib.getUser(e.user.getNick(), e.network, (cbgu) => { lib.getUser(e, (cbgu) => {
lib.getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { lib.getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => {
lib.checkRepostCheckSum(cbcs, (cbcrcs) => { lib.checkRepostCheckSum(cbcs, (cbcrcs) => {
if(cbcrcs) { if(cbcrcs) {
@ -29,8 +30,8 @@ module.exports = (bot, trigger, lib) => {
Math.floor(new Date() / 1000), Math.floor(new Date() / 1000),
0 0
]).on('result', (result) => { ]).on('result', (result) => {
generateThumbs(); lib.generateThumbs();
e.reply("https://f0ck.me/"+result.insertId+" - "+path.parse(entry).base+" ("+cb.infos.mime+", ~"+formatSize(cb.infos.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")"); e.reply("https://f0ck.me/"+result.insertId+" - "+path.parse(entry).base+" ("+cb.infos.mime+", ~"+lib.formatSize(cb.infos.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")");
}).on('error', (msg) => { }).on('error', (msg) => {
e.reply(msg); e.reply(msg);
}); });

14
src/trigger/user.js Normal file
View File

@ -0,0 +1,14 @@
module.exports = (bot, trigger, lib) => {
trigger.add({
name: 'user',
call: new RegExp('^\\!user$', 'i'),
level: 0,
func: (e) => {
//e.reply(e.user);
lib.getUser(e, (cbgu) => {
e.reply(cbgu);
});
},
desc: 'get Userdata'
});
};