From 2a2f379939738aafebbc864ecd53fef67ce65375 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 11:23:25 +0000 Subject: [PATCH 01/24] test --- package.json | 3 +- src/lib.js | 164 ++++++++++++++++++++++ src/main.js | 313 ++++++------------------------------------ src/trigger/del.js | 37 +++-- src/trigger/level.js | 14 ++ src/trigger/parser.js | 55 ++++++++ 6 files changed, 293 insertions(+), 293 deletions(-) create mode 100644 src/lib.js create mode 100644 src/trigger/level.js create mode 100644 src/trigger/parser.js diff --git a/package.json b/package.json index 3548dfd..dd428d0 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "mysql": "^2.11.1", "repl": "^0.1.3", "swig": "^1.4.2", - "uuid": "^2.0.2" + "uuid": "^2.0.2", + "node-ffprobe": "^1.2.2" }, "repository": { "type": "git", diff --git a/src/lib.js b/src/lib.js new file mode 100644 index 0000000..d7137d6 --- /dev/null +++ b/src/lib.js @@ -0,0 +1,164 @@ +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 uuid = require('uuid'); +var crypto = require('crypto'); +var path = require('path'); + +var bot, sql, cfg; +var debug = true; + +module.exports = Lib; +function Lib(tbot, tsql, tcfg) { + this.bot = bot = tbot; + this.sql = sql = tsql; + this.cfg = cfg = tcfg; + this.admins = []; +} + +Lib.prototype.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]); + }); + }); +}; +Lib.prototype.getUserlevel = (e, tmp_channel, cb) => { + Lib.prototype.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 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) { + checkRepost(url, (cbcr) => { + if(cbcr) { + var file = fs.createWriteStream(dest+"."+cfg.allowedMimes[response.headers['content-type']]); + response.pipe(file); + file.on('finish', () => { + file.close(); + probe(dest+"."+cfg.allowedMimes[response.headers['content-type']], (err, probeData) => { + if(probeData.streams[0].height !== undefined || probeData.streams[0].width !== undefined) { + if(probeData.streams[0].height <= cfg.minRes || probeData.streams[0].width <= cfg.minRes) + cb({'status':false, 'msg':'f0ck! your shitpost is too small ('+probeData.streams[0].width+' x '+probeData.streams[0].height+'), min '+cfg.minRes+' x '+cfg.minRes+' required', 'type':1}); + 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']]}}); + } + 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']]}}); + }); + }); + 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)) { + exec('ffmpegthumbnailer -i'+e.dest+' -s256 -o'+thumbnail, (error) => { + if(error) { + log('failed thumbnail for '+e.id+' ('+e.mime+') 1'); + 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) + log('failed thumbnail for '+e.id+' ('+e.mime+') 2'); + else + log("generated thumbnail for "+e.id+" ("+e.mime+")"); + }); + } + }); + } + }); + }); +}; +Lib.prototype.log = (msg) => { + if(debug) + bot.send("#f0ck", msg, 'n0xy'); +}; \ No newline at end of file diff --git a/src/main.js b/src/main.js index f1abb7f..3f17486 100644 --- a/src/main.js +++ b/src/main.js @@ -4,24 +4,24 @@ var http = require('http'); var https = require('https'); var mysql = require('mysql'); var bot = require('coffea')(); -var uuid = require('uuid'); var crypto = require('crypto'); var path = require('path'); var exec = require('child_process').exec; var probe = require('node-ffprobe'); +var Lib = require('./lib.js'); var swig = require('swig'); + var templates = {}; var debug = false; -var upload = true; -var admins = []; + var sql; var haDC = () => { sql = mysql.createConnection(cfg.mysql); sql.connect((err) => { - if(err) setTimeout(haDC,2000); + if(err) setTimeout(haDC, 2000); }); sql.on('error', (err) => { if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC(); @@ -29,6 +29,8 @@ var haDC = () => { }; haDC(); +var lib = new Lib(bot, sql, cfg); + cfg.server.forEach((e,i,a) => { bot.add({ "name": e.name, @@ -44,254 +46,21 @@ cfg.server.forEach((e,i,a) => { console.log("Server "+e.name+" wurde geladen"); }); -var log = (msg) => { - if(debug) - bot.send("#f0ck", msg, 'n0xy'); -}; - bot.on('motd', (e) => { console.log("motd von "+e.network+" erhalten"); + bot.join("#kbot-dev"); // tmp bot.write('MODE f0ck +B', e.network, (c)=>{}); // Botflag }); bot.on('message', (e) => { var orig = e.message; - if(orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi) && e.channel.getName() == "#f0ck" && upload) { // shitpostcatcher - if(!orig.match(/\!ignore$/)) { - var tmp = orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links - tmp.forEach((entry,i,a) => { - var tmpdest = uuid.v1().split('-')[0]; - dl(entry, "./b/"+tmpdest, (cb) => { // download item - if(cb.status === true) { - getUser(e.user.getNick(), e.network, (cbgu) => { - getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { - checkRepostCheckSum(cbcs, (cbcrcs) => { - if(cbcrcs) { - sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [ - entry, - "./b/"+tmpdest+"."+cb.infos.ext, - cb.infos.mime, - cb.infos.size, - cbcs, - cbgu['nick'], - e.channel.getName(), - e.network, - Math.floor(new Date() / 1000), - 0 - ]).on('result', (result) => { - 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']+")"); - }).on('error', (msg) => { - e.reply(msg); - }); - } - else { - fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost - e.reply("repost motherf0cker"); - } - }); - }); - }); - } - else - if(cb.type == 1) - e.reply(cb.msg); - }); - }); + trigger.trigger.forEach((entry,i,a) => { + if(orig.match(new RegExp(entry.call, "gi"))) { + entry.func(e); } - } - else if(orig.match(/^\.user/)) { // (debug) get userinfos - getUser(e.user.getNick(), e.network, (cbgu) => { - e.reply(cbgu); - }); - } - else if(orig.match(/^\!reload tpl$/)) { - getTpls(); - e.reply("templates reloaded"); - } - else if(orig.match(/^\!toggle debug$/)) { - if(debug) { - debug = false; - e.reply("debugmessages deactivated"); - } - else { - debug = true; - e.reply("debugmessages activated"); - } - } - else if(orig.match(/^\!toggle catcher$/)) { - if(upload) { - upload = false; - e.reply("catcher deactivated"); - } - else { - upload = true; - e.reply("catcher activated"); - } - } - else if(orig.match(/^\!load user$/)) { - loadUser((cb) => { - //e.reply(admins); - }); - } - else if(orig.match(/^\!level$/)) { - var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; - getUserlevel(e, tmp_channel, (cb) => { - e.reply("level from "+e.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")"); - }); - } - else if(orig.match(/^\!del (\d+)$/i)) { - var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; - getUserlevel(e, tmp_channel, (cb) => { - if(cb.level >= 100) { - 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"); - }); - } - else if(orig.match(/^\!thumbs$/i)) { - var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; - getUserlevel(e, tmp_channel, (cb) => { - if(cb.level >= 100) { - //exec('rm ./t/*.png', (error) => { - e.reply("generating Thumbnails..."); - generateThumbs(); - //}); - } - else - e.reply("no permission"); - }); - } + }); }); -var 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]); - }); - }); -}; - -var getUserlevel = (e, tmp_channel, cb) => { - 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) - }); - }); -}; - -var loadUser = (cb) => { - admins = []; - sql.query("select * from `f0ck`.`user`", (err, rows, fields) => { - rows.forEach((e,i,a) => { - 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); - });; -}; - -var 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) { - checkRepost(url, (cbcr) => { - if(cbcr) { - var file = fs.createWriteStream(dest+"."+cfg.allowedMimes[response.headers['content-type']]); - response.pipe(file); - file.on('finish', () => { - file.close(); - probe(dest+"."+cfg.allowedMimes[response.headers['content-type']], (err, probeData) => { - if(probeData.streams[0].height !== undefined || probeData.streams[0].width !== undefined) { - if(probeData.streams[0].height <= cfg.minRes || probeData.streams[0].width <= cfg.minRes) - cb({'status':false, 'msg':'f0ck! your shitpost is too small ('+probeData.streams[0].width+' x '+probeData.streams[0].height+'), min '+cfg.minRes+' x '+cfg.minRes+' required', 'type':1}); - 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']]}}); - } - 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']]}}); - }); - }); - 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}); - }); -}; - -var 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); - }); -}; -var 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); - }); -}; - -var 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]; -}; - -var 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')); - }); -}; - - // Webserver http.createServer((req, res) => { if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) { @@ -488,36 +257,34 @@ var getTpls = () => { }; }; -var 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)) { - exec('ffmpegthumbnailer -i'+e.dest+' -s256 -o'+thumbnail, (error) => { - if(error) { - log('failed thumbnail for '+e.id+' ('+e.mime+') 1'); - //fs.unlink(thumbnail); - 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) { - log('failed thumbnail for '+e.id+' ('+e.mime+') 2'); - //fs.unlink(thumbnail); - } - else { - log("generated thumbnail for "+e.id+" ("+e.mime+")"); - } - }); - } - }); - } - }); - }); -}; - -loadUser((cb)=>{ +lib.loadUser((cb)=>{ console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"); }); -getTpls(); \ No newline at end of file +getTpls(); + +var trigger = { + trigger: [], + add: (args) => { + trigger.trigger.push({ + call: args.call, + func: args.func, + desc: args.desc, + level: args.level + }); + } +}; + +var loadPlugins = function() { + var files = fs.readdirSync(__dirname+'/trigger/'); + files.forEach(function (file) { + if(file.substr(-3, 3) === '.js') { + console.log('Loading plugin', file); + require(__dirname+'/trigger/' + file)(bot, trigger, lib); + } + }); +}; +var use = function(fn) { + fn(this); + return this; +}; +loadPlugins(); \ No newline at end of file diff --git a/src/trigger/del.js b/src/trigger/del.js index c6dfdc6..86406a7 100644 --- a/src/trigger/del.js +++ b/src/trigger/del.js @@ -1,22 +1,21 @@ -module.exports = (bot) => { - var del = (e, args) => { - var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; - getUserlevel(e, e.user.getNick(), tmp_channel, (cb) => { - if(cb.level >= 100) { - 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"); - }); - }; - - bot.addCmd({ - name: 'spec', +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'delete', + call: '^\!del (\d+)$', level: 100, - func: spec, - desc: 'delete f0rk' + func: (e) => { + var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; + lib.getUserlevel(e, e.user.getNick(), tmp_channel, (cb) => { + if(cb.level >= 100) { + 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' }); }; \ No newline at end of file diff --git a/src/trigger/level.js b/src/trigger/level.js new file mode 100644 index 0000000..af573ce --- /dev/null +++ b/src/trigger/level.js @@ -0,0 +1,14 @@ +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'level', + call: '^\!level$', + level: 0, + func: (e) => { + var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; + lib.getUserlevel(e, tmp_channel, (cb) => { + e.reply("level from "+e.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")"); + }); + }, + desc: 'get Userlevel' + }); +}; \ No newline at end of file diff --git a/src/trigger/parser.js b/src/trigger/parser.js new file mode 100644 index 0000000..3ff8cd9 --- /dev/null +++ b/src/trigger/parser.js @@ -0,0 +1,55 @@ +var fs = require('fs-extra'); +var uuid = require('uuid'); + +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'parser', + call: 'https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?', + level: 0, + func: (e) => { + if(!e.message.match(/\!ignore$/)) { + var tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links + tmp.forEach((entry,i,a) => { + var tmpdest = uuid.v1().split('-')[0]; + lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item + if(cb.status === true) { + lib.getUser(e.user.getNick(), e.network, (cbgu) => { + lib.getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { + lib.checkRepostCheckSum(cbcs, (cbcrcs) => { + if(cbcrcs) { + lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [ + entry, + "./b/"+tmpdest+"."+cb.infos.ext, + cb.infos.mime, + cb.infos.size, + cbcs, + cbgu['nick'], + e.channel.getName(), + e.network, + Math.floor(new Date() / 1000), + 0 + ]).on('result', (result) => { + 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']+")"); + }).on('error', (msg) => { + e.reply(msg); + }); + } + else { + fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost + e.reply("repost motherf0cker"); + } + }); + }); + }); + } + else + if(cb.type == 1) + e.reply(cb.msg); + }); + }); + } + }, + desc: 'muh' + }); +}; \ No newline at end of file From 9e29f0c8390a289e0d22ed4717f7474aac48a7d1 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 15:50:30 +0000 Subject: [PATCH 02/24] buggsen gefiggst --- package.json | 3 +++ src/lib.js | 61 ++++++++++++++++++++++++++----------------- src/lib.js_tmp | 56 +++++++++++++++++++++++++++++++++++++++ src/main.js | 26 +++++++++++------- src/trigger/del.js | 15 +++-------- src/trigger/level.js | 5 ++-- src/trigger/parser.js | 9 ++++--- src/trigger/user.js | 14 ++++++++++ 8 files changed, 137 insertions(+), 52 deletions(-) create mode 100644 src/lib.js_tmp create mode 100644 src/trigger/user.js diff --git a/package.json b/package.json index dd428d0..c3ad23f 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,9 @@ "name": "keinBotv2", "version": "0.1.0", "main": "src/main.js", + "scripts": { + "start": "node src/main.js" + }, "dependencies": { "coffea": "^0.4.24", "fs-extra": "^0.30.0", diff --git a/src/lib.js b/src/lib.js index d7137d6..17225de 100644 --- a/src/lib.js +++ b/src/lib.js @@ -18,29 +18,42 @@ function Lib(tbot, tsql, tcfg) { this.admins = []; } -Lib.prototype.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]); +Lib.prototype.getUser = (e, cbgu) => { + var u = e.user.getNick(); + var n = e.network; + //console.log(e.user); + if(!e.user.hasOwnProperty('hostname')) { + 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]); + }); }); - }); + } + 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.getUser(e.user.getNick(), e.network, (cbgu) => { +Lib.prototype.getUserlevel = (e, cb) => { + Lib.prototype.getUser(e, (cbgu) => { 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; if(host in this.admins) 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']); if(cfg.allowedMimes.hasOwnProperty(response.headers['content-type'])) { if(response.headers['content-length'] <= cfg.maxFileSize) { - checkRepost(url, (cbcr) => { + Lib.prototype.checkRepost(url, (cbcr) => { if(cbcr) { var file = fs.createWriteStream(dest+"."+cfg.allowedMimes[response.headers['content-type']]); response.pipe(file); @@ -142,15 +155,15 @@ Lib.prototype.generateThumbs = () => { if(!fs.existsSync(thumbnail)) { exec('ffmpegthumbnailer -i'+e.dest+' -s256 -o'+thumbnail, (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 } else { exec('convert '+thumbnail+' -resize "128x128^" -gravity center -crop 128x128+0+0 +repage '+thumbnail, (error) => { if(error) - log('failed thumbnail for '+e.id+' ('+e.mime+') 2'); + Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 2'); else - log("generated thumbnail for "+e.id+" ("+e.mime+")"); + Lib.prototype.log("generated thumbnail for "+e.id+" ("+e.mime+")"); }); } }); diff --git a/src/lib.js_tmp b/src/lib.js_tmp new file mode 100644 index 0000000..f92c69b --- /dev/null +++ b/src/lib.js_tmp @@ -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); + }); + } + } +}; \ No newline at end of file diff --git a/src/main.js b/src/main.js index 3f17486..e5018a9 100644 --- a/src/main.js +++ b/src/main.js @@ -9,15 +9,12 @@ var path = require('path'); var exec = require('child_process').exec; var probe = require('node-ffprobe'); var Lib = require('./lib.js'); - var swig = require('swig'); - var templates = {}; var debug = false; - - var sql; + var haDC = () => { sql = mysql.createConnection(cfg.mysql); sql.connect((err) => { @@ -48,21 +45,29 @@ cfg.server.forEach((e,i,a) => { bot.on('motd', (e) => { console.log("motd von "+e.network+" erhalten"); - bot.join("#kbot-dev"); // tmp - bot.write('MODE f0ck +B', e.network, (c)=>{}); // Botflag + //bot.join("#kbot-dev"); // tmp + bot.write('MODE f0ckyou +B', e.network, (c)=>{}); // Botflag }); bot.on('message', (e) => { - var orig = e.message; trigger.trigger.forEach((entry,i,a) => { - if(orig.match(new RegExp(entry.call, "gi"))) { - entry.func(e); + if(e.message.match(entry.call)) { + 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 http.createServer((req, res) => { + //console.log(req.connection.remoteAddress); if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) { var filePath = '.' + req.url; var url = req.url.split("/")[1]; @@ -135,7 +140,7 @@ http.createServer((req, res) => { data.src = e.src; data.dest = e.dest; data.mime = e.mime; - data.size = formatSize(e.size); + data.size = lib.formatSize(e.size); data.userchannel = e.userchannel; data.usernetwork = e.usernetwork; @@ -266,6 +271,7 @@ var trigger = { trigger: [], add: (args) => { trigger.trigger.push({ + name: args.name, call: args.call, func: args.func, desc: args.desc, diff --git a/src/trigger/del.js b/src/trigger/del.js index 86406a7..82d22d0 100644 --- a/src/trigger/del.js +++ b/src/trigger/del.js @@ -1,19 +1,12 @@ module.exports = (bot, trigger, lib) => { trigger.add({ name: 'delete', - call: '^\!del (\d+)$', + call: new RegExp('^\\!del (\\d+)$', 'i'), level: 100, func: (e) => { - var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; - lib.getUserlevel(e, e.user.getNick(), tmp_channel, (cb) => { - if(cb.level >= 100) { - 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"); + var id = e.message.split(' ')[1]; + lib.sql.query("delete from `f0ck`.`items` where `id` = ?", id).on('end', () => { + e.reply("f0ck "+id+" deleted"); }); }, desc: 'delete f0ck' diff --git a/src/trigger/level.js b/src/trigger/level.js index af573ce..cf0766b 100644 --- a/src/trigger/level.js +++ b/src/trigger/level.js @@ -1,11 +1,10 @@ module.exports = (bot, trigger, lib) => { trigger.add({ name: 'level', - call: '^\!level$', + call: new RegExp('^\\!level$', 'i'), level: 0, func: (e) => { - var tmp_channel = bot.getChannel(e.channel.getName(), e.network).names; - lib.getUserlevel(e, tmp_channel, (cb) => { + lib.getUserlevel(e, (cb) => { e.reply("level from "+e.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")"); }); }, diff --git a/src/trigger/parser.js b/src/trigger/parser.js index 3ff8cd9..56a8ede 100644 --- a/src/trigger/parser.js +++ b/src/trigger/parser.js @@ -1,10 +1,11 @@ var fs = require('fs-extra'); var uuid = require('uuid'); +var path = require('path'); module.exports = (bot, trigger, lib) => { trigger.add({ name: 'parser', - call: 'https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?', + call: new RegExp('https?:\\/\\/[\\w-]+(\\.[\\w-]+)+\\.?(:\\d+)?(\\/\\S*)?', 'gi'), level: 0, func: (e) => { if(!e.message.match(/\!ignore$/)) { @@ -13,7 +14,7 @@ module.exports = (bot, trigger, lib) => { var tmpdest = uuid.v1().split('-')[0]; lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item 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.checkRepostCheckSum(cbcs, (cbcrcs) => { if(cbcrcs) { @@ -29,8 +30,8 @@ module.exports = (bot, trigger, lib) => { Math.floor(new Date() / 1000), 0 ]).on('result', (result) => { - 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']+")"); + lib.generateThumbs(); + 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) => { e.reply(msg); }); diff --git a/src/trigger/user.js b/src/trigger/user.js new file mode 100644 index 0000000..c891f16 --- /dev/null +++ b/src/trigger/user.js @@ -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' + }); +}; \ No newline at end of file From 984e5a9d24c555eee9e357a8d3e804c432059e4a Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 15:52:05 +0000 Subject: [PATCH 03/24] meh --- src/lib.js_tmp | 56 -------------------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 src/lib.js_tmp diff --git a/src/lib.js_tmp b/src/lib.js_tmp deleted file mode 100644 index f92c69b..0000000 --- a/src/lib.js_tmp +++ /dev/null @@ -1,56 +0,0 @@ -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); - }); - } - } -}; \ No newline at end of file From f1ce2ebc759486e07fa76dba30860f908025797c Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 17:30:35 +0000 Subject: [PATCH 04/24] exported webserver --- package.json | 2 +- src/lib.js | 3 - src/main.js | 219 ++------------------------------------------------ src/websrv.js | 205 ++++++++++++++++++++++++++++++++++++++++++++++ start.sh | 2 +- 5 files changed, 212 insertions(+), 219 deletions(-) create mode 100644 src/websrv.js diff --git a/package.json b/package.json index c3ad23f..2241e14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "keinBotv2", - "version": "0.1.0", + "version": "0.1.1", "main": "src/main.js", "scripts": { "start": "node src/main.js" diff --git a/src/lib.js b/src/lib.js index 17225de..9e4ad4a 100644 --- a/src/lib.js +++ b/src/lib.js @@ -3,9 +3,7 @@ var http = require('http'); var https = require('https'); var exec = require('child_process').exec; var probe = require('node-ffprobe'); -var uuid = require('uuid'); var crypto = require('crypto'); -var path = require('path'); var bot, sql, cfg; var debug = true; @@ -21,7 +19,6 @@ function Lib(tbot, tsql, tcfg) { Lib.prototype.getUser = (e, cbgu) => { var u = e.user.getNick(); var n = e.network; - //console.log(e.user); if(!e.user.hasOwnProperty('hostname')) { bot.write('WHOIS '+u, n, () => { bot.once('data', (err, msg) => { diff --git a/src/main.js b/src/main.js index e5018a9..b1da6e9 100644 --- a/src/main.js +++ b/src/main.js @@ -1,18 +1,10 @@ var cfg = require('../../cfg.json'); var fs = require('fs-extra'); -var http = require('http'); -var https = require('https'); var mysql = require('mysql'); var bot = require('coffea')(); -var crypto = require('crypto'); -var path = require('path'); -var exec = require('child_process').exec; -var probe = require('node-ffprobe'); var Lib = require('./lib.js'); -var swig = require('swig'); +var Websrv = require('./websrv.js'); -var templates = {}; -var debug = false; var sql; var haDC = () => { @@ -27,6 +19,7 @@ var haDC = () => { haDC(); var lib = new Lib(bot, sql, cfg); +var websrv = new Websrv(bot, sql, cfg); cfg.server.forEach((e,i,a) => { bot.add({ @@ -45,19 +38,14 @@ cfg.server.forEach((e,i,a) => { bot.on('motd', (e) => { console.log("motd von "+e.network+" erhalten"); - //bot.join("#kbot-dev"); // tmp - bot.write('MODE f0ckyou +B', e.network, (c)=>{}); // Botflag }); bot.on('message', (e) => { trigger.trigger.forEach((entry,i,a) => { if(e.message.match(entry.call)) { lib.getUserlevel(e, (cb) => { - if(cb.level >= entry.level) { - //setTimeout(()=>{ - entry.func(e); - //}, 100); - } + if(cb.level >= entry.level) + entry.func(e); else e.reply('no permission, min level '+entry.level+' required'); }); @@ -65,207 +53,10 @@ bot.on('message', (e) => { }); }); -// Webserver -http.createServer((req, res) => { - //console.log(req.connection.remoteAddress); - if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) { - var filePath = '.' + req.url; - var url = req.url.split("/")[1]; - if(filePath == './') - filePath = './index.html'; - var extname = String(path.extname(filePath)).toLowerCase(); - var contentType = 'text/html'; - var mimeTypes = { - '.html': 'text/html', - '.js': 'text/javascript', - '.css': 'text/css', - '.png': 'image/png', - '.jpg': 'image/jpg', - '.gif': 'image/gif', - '.mp3': 'audio/mpeg', - '.mp4': 'video/mp4', - '.webm': 'video/webm', - '.css': 'text/css', - '.ogg': 'audio/ogg' - }; - if(filePath == "./index.html") { // mainpage - var tpl = swig.compile(templates.index); - var data = { items: [] }; - sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc", (err, rows, fields) => { - rows.forEach((e,i,a) => { - data.items.push({ "id": e.id, "mime": e.mime }); - }); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(tpl(data), 'utf-8'); - }); - } - else if(Number.isInteger(parseInt(url))) { // itempage - 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) => { - var tpl = swig.compile(templates.item); - var data = { - id: '', - username: '', - item: '', - src: '', - dest: '', - mime: '', - size: '', - userchannel: '', - usernetwork: '', - next: null, - prev: null - }; - if(rows[0].length) { - var e = rows[0][0]; - switch(e.mime) { - case "image/png": - case "image/jpeg": - case "image/gif": - data.item = 'image'; - break; - case "video/webm": - case "video/mp4": - data.item = 'video'; - break; - case "audio/mpeg": - case "audio/ogg": - data.item = 'audio'; - break; - } - data.id = e.id; - data.username = e.username; - data.src = e.src; - data.dest = e.dest; - data.mime = e.mime; - data.size = lib.formatSize(e.size); - data.userchannel = e.userchannel; - data.usernetwork = e.usernetwork; - - if(rows[1].length) - data.next = rows[1][0].id; - if(rows[2].length) - data.prev = rows[2][0].id; - } - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(tpl(data), 'utf-8'); - }); - } - else if(filePath == "./random") { - 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 - }); - res.end(); - }); - } - else if(filePath == "./how") { - var tpl = swig.compile(templates.how); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(tpl(), 'utf-8'); - } - else if(filePath == "./contact") { - var tpl = swig.compile(templates.contact); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(tpl(), 'utf-8'); - } - else if(filePath == "./scripts") { - var tpl = swig.compile(templates.scripts); - res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end(tpl(), 'utf-8'); - } - else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file - contentType = mimeTypes[extname]; - switch(contentType) { - case "video/webm": - case "video/mp4": - case "audio/mpeg": - case "audio/ogg": - var start = 0; - var end = 0; - var range = req.headers['range']; - var stat = fs.statSync(filePath); - if(range != null) { - start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-'))); - end = parseInt(range.slice(range.indexOf('-')+1, range.length)); - } - if(isNaN(end) || end == 0) end = stat.size-1; - if(start > end) return; - res.writeHead(206, { - 'Connection':'close', - 'Content-Type':contentType, - 'Content-Length':end - start, - 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, - 'Transfer-Encoding':'chunked' - }); - var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end}); - stream.pipe(res); - break; - default: - fs.readFile(filePath, (error, content) => { - if(error) { - if(error.code == 'ENOENT') { - res.writeHead(200, { 'Content-Type': contentType }); - res.end('404 - f0ck you', 'utf-8'); - } - else { - res.writeHead(500); - res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); - res.end(); - } - } - else { - res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' }); - res.end(content, 'utf-8'); - } - }); - break; - } - fs.readFile(filePath, (error, content) => { - if(error) { - if(error.code == 'ENOENT') { - res.writeHead(200, { 'Content-Type': contentType }); - res.end('404 - f0ck you', 'utf-8'); - } - else { - res.writeHead(500); - res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); - res.end(); - } - } - else { - - } - }); - } - else { // errorpage - res.writeHead(404); - res.end('404 - f0ck you', 'utf-8'); - } - } - else { - res.writeHead(403); - res.end('403 - forbidden'); - } -}).listen(cfg.webserver.port); - -var getTpls = () => { - templates = { - "index": fs.readFileSync("./s/index.tpl.html", "utf-8"), - "item": fs.readFileSync("./s/item.tpl.html", "utf-8"), - "how": fs.readFileSync("./s/how.tpl.html", "utf-8"), - "contact": fs.readFileSync("./s/contact.tpl.html", "utf-8"), - "scripts": fs.readFileSync("./s/scripts.tpl.html", "utf-8") - }; -}; - lib.loadUser((cb)=>{ console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"); }); -getTpls(); +websrv.getTpls(); var trigger = { trigger: [], diff --git a/src/websrv.js b/src/websrv.js new file mode 100644 index 0000000..ca2f0a8 --- /dev/null +++ b/src/websrv.js @@ -0,0 +1,205 @@ +var fs = require('fs-extra'); +var http = require('http'); +var path = require('path'); +var swig = require('swig'); + +var templates = {}; +var bot, sql, cfg; + +module.exports = Websrv; +function Websrv(tbot, tsql, tcfg) { + this.bot = bot = tbot; + this.sql = sql = tsql; + this.cfg = cfg = tcfg; + + http.createServer((req, res) => { + if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) { + var filePath = '.' + req.url; + var url = req.url.split("/")[1]; + if(filePath == './') + filePath = './index.html'; + var extname = String(path.extname(filePath)).toLowerCase(); + var contentType = 'text/html'; + var mimeTypes = { + '.html': 'text/html', + '.js': 'text/javascript', + '.css': 'text/css', + '.png': 'image/png', + '.jpg': 'image/jpg', + '.gif': 'image/gif', + '.mp3': 'audio/mpeg', + '.mp4': 'video/mp4', + '.webm': 'video/webm', + '.css': 'text/css', + '.ogg': 'audio/ogg' + }; + if(filePath == "./index.html") { // mainpage + var tpl = swig.compile(templates.index); + var data = { items: [] }; + sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc", (err, rows, fields) => { + rows.forEach((e,i,a) => { + data.items.push({ "id": e.id, "mime": e.mime }); + }); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(tpl(data), 'utf-8'); + }); + } + else if(Number.isInteger(parseInt(url))) { // itempage + 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) => { + var tpl = swig.compile(templates.item); + var data = { + id: '', + username: '', + item: '', + src: '', + dest: '', + mime: '', + size: '', + userchannel: '', + usernetwork: '', + next: null, + prev: null + }; + if(rows[0].length) { + var e = rows[0][0]; + switch(e.mime) { + case "image/png": + case "image/jpeg": + case "image/gif": + data.item = 'image'; + break; + case "video/webm": + case "video/mp4": + data.item = 'video'; + break; + case "audio/mpeg": + case "audio/ogg": + data.item = 'audio'; + break; + } + data.id = e.id; + data.username = e.username; + data.src = e.src; + data.dest = e.dest; + data.mime = e.mime; + data.size = lib.formatSize(e.size); + data.userchannel = e.userchannel; + data.usernetwork = e.usernetwork; + if(rows[1].length) + data.next = rows[1][0].id; + if(rows[2].length) + data.prev = rows[2][0].id; + } + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(tpl(data), 'utf-8'); + }); + } + else if(filePath == "./random") { + 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 + }); + res.end(); + }); + } + else if(filePath == "./how") { + var tpl = swig.compile(templates.how); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(tpl(), 'utf-8'); + } + else if(filePath == "./contact") { + var tpl = swig.compile(templates.contact); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(tpl(), 'utf-8'); + } + else if(filePath == "./scripts") { + var tpl = swig.compile(templates.scripts); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(tpl(), 'utf-8'); + } + else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file + contentType = mimeTypes[extname]; + switch(contentType) { + case "video/webm": + case "video/mp4": + case "audio/mpeg": + case "audio/ogg": + var start = 0; + var end = 0; + var range = req.headers['range']; + var stat = fs.statSync(filePath); + if(range != null) { + start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-'))); + end = parseInt(range.slice(range.indexOf('-')+1, range.length)); + } + if(isNaN(end) || end == 0) end = stat.size-1; + if(start > end) return; + res.writeHead(206, { + 'Connection':'close', + 'Content-Type':contentType, + 'Content-Length':end - start, + 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, + 'Transfer-Encoding':'chunked' + }); + var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end}); + stream.pipe(res); + break; + default: + fs.readFile(filePath, (error, content) => { + if(error) { + if(error.code == 'ENOENT') { + res.writeHead(200, { 'Content-Type': contentType }); + res.end('404 - f0ck you', 'utf-8'); + } + else { + res.writeHead(500); + res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); + res.end(); + } + } + else { + res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length, 'Cache-Control': 'max-age=2592000, public' }); + res.end(content, 'utf-8'); + } + }); + break; + } + fs.readFile(filePath, (error, content) => { + if(error) { + if(error.code == 'ENOENT') { + res.writeHead(200, { 'Content-Type': contentType }); + res.end('404 - f0ck you', 'utf-8'); + } + else { + res.writeHead(500); + res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); + res.end(); + } + } + }); + } + else { // errorpage + res.writeHead(404); + res.end('404 - f0ck you', 'utf-8'); + } + } + else { + res.writeHead(403); + res.end('403 - forbidden'); + } + }).listen(cfg.webserver.port); +} + +Websrv.prototype.getTpls = () => { + templates = { + "index": fs.readFileSync("./s/index.tpl.html", "utf-8"), + "item": fs.readFileSync("./s/item.tpl.html", "utf-8"), + "how": fs.readFileSync("./s/how.tpl.html", "utf-8"), + "contact": fs.readFileSync("./s/contact.tpl.html", "utf-8"), + "scripts": fs.readFileSync("./s/scripts.tpl.html", "utf-8") + }; +}; \ No newline at end of file diff --git a/start.sh b/start.sh index 1a5a613..2731ae6 100755 --- a/start.sh +++ b/start.sh @@ -1,2 +1,2 @@ #!/bin/bash -tmux new -d -s bot 'node src/main.js'; +tmux new -d -s bot 'npm start'; From eb1aa8edaa1dc6158a62fd4a8780a8c52cad5974 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 17:36:16 +0000 Subject: [PATCH 05/24] buggsenfiggs --- src/main.js | 2 +- src/trigger/parser.js | 78 ++++++++++++++++++++++--------------------- src/websrv.js | 5 +-- 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/main.js b/src/main.js index b1da6e9..2f3f5dc 100644 --- a/src/main.js +++ b/src/main.js @@ -19,7 +19,7 @@ var haDC = () => { haDC(); var lib = new Lib(bot, sql, cfg); -var websrv = new Websrv(bot, sql, cfg); +var websrv = new Websrv(bot, sql, cfg, lib); cfg.server.forEach((e,i,a) => { bot.add({ diff --git a/src/trigger/parser.js b/src/trigger/parser.js index 56a8ede..cc1de80 100644 --- a/src/trigger/parser.js +++ b/src/trigger/parser.js @@ -8,47 +8,49 @@ module.exports = (bot, trigger, lib) => { call: new RegExp('https?:\\/\\/[\\w-]+(\\.[\\w-]+)+\\.?(:\\d+)?(\\/\\S*)?', 'gi'), level: 0, func: (e) => { - if(!e.message.match(/\!ignore$/)) { - var tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links - tmp.forEach((entry,i,a) => { - var tmpdest = uuid.v1().split('-')[0]; - lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item - if(cb.status === true) { - lib.getUser(e, (cbgu) => { - lib.getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { - lib.checkRepostCheckSum(cbcs, (cbcrcs) => { - if(cbcrcs) { - lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [ - entry, - "./b/"+tmpdest+"."+cb.infos.ext, - cb.infos.mime, - cb.infos.size, - cbcs, - cbgu['nick'], - e.channel.getName(), - e.network, - Math.floor(new Date() / 1000), - 0 - ]).on('result', (result) => { - lib.generateThumbs(); - 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) => { - e.reply(msg); - }); - } - else { - fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost - e.reply("repost motherf0cker"); - } + if(e.channel.getName() != '#f0ck') { + if(!e.message.match(/\!ignore$/)) { + var tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links + tmp.forEach((entry,i,a) => { + var tmpdest = uuid.v1().split('-')[0]; + lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item + if(cb.status === true) { + lib.getUser(e, (cbgu) => { + lib.getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { + lib.checkRepostCheckSum(cbcs, (cbcrcs) => { + if(cbcrcs) { + lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [ + entry, + "./b/"+tmpdest+"."+cb.infos.ext, + cb.infos.mime, + cb.infos.size, + cbcs, + cbgu['nick'], + e.channel.getName(), + e.network, + Math.floor(new Date() / 1000), + 0 + ]).on('result', (result) => { + lib.generateThumbs(); + 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) => { + e.reply(msg); + }); + } + else { + fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost + e.reply("repost motherf0cker"); + } + }); }); }); - }); - } - else - if(cb.type == 1) - e.reply(cb.msg); + } + else + if(cb.type == 1) + e.reply(cb.msg); + }); }); - }); + } } }, desc: 'muh' diff --git a/src/websrv.js b/src/websrv.js index ca2f0a8..4264b6a 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -4,13 +4,14 @@ var path = require('path'); var swig = require('swig'); var templates = {}; -var bot, sql, cfg; +var bot, sql, cfg, lib; module.exports = Websrv; -function Websrv(tbot, tsql, tcfg) { +function Websrv(tbot, tsql, tcfg, tlib) { this.bot = bot = tbot; this.sql = sql = tsql; this.cfg = cfg = tcfg; + this.lib = lib = tlib; http.createServer((req, res) => { if(cfg.wlip.hasOwnProperty(req.connection.remoteAddress)) { From 680ef78f073eed8b893ce6ac50a83962c6d7cef5 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 17:37:28 +0000 Subject: [PATCH 06/24] damn --- src/trigger/parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trigger/parser.js b/src/trigger/parser.js index cc1de80..64a1027 100644 --- a/src/trigger/parser.js +++ b/src/trigger/parser.js @@ -8,7 +8,7 @@ module.exports = (bot, trigger, lib) => { call: new RegExp('https?:\\/\\/[\\w-]+(\\.[\\w-]+)+\\.?(:\\d+)?(\\/\\S*)?', 'gi'), level: 0, func: (e) => { - if(e.channel.getName() != '#f0ck') { + if(e.channel.getName() == '#f0ck') { if(!e.message.match(/\!ignore$/)) { var tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links tmp.forEach((entry,i,a) => { From 2f4bfd8269ae46b098dd40b296797852ce4dcb65 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 18:35:31 +0000 Subject: [PATCH 07/24] +api, lol --- src/websrv.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/websrv.js b/src/websrv.js index 4264b6a..d22f3f2 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -183,6 +183,44 @@ function Websrv(tbot, tsql, tcfg, tlib) { } }); } + else if(filePath.match(/^\.\/api/i)) { // api + var url = filePath.split('/'); + if(url[2] === undefined) { // Mainpage + res.writeHead(200, { 'Content-Type': contentType }); + res.end('kommt noch'); + } + else if(Number.isInteger(parseInt(url[2]))) { // Item + 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) => { + var data; + if(rows[0].length) { + var e = rows[0][0]; + data = { + id: e.id, + username: e.username, + src: e.src, + dest: e.dest, + mime: e.mime, + size: e.size, + userchannel: e.userchannel, + usernetwork: e.usernetwork, + next: null, + prev: null + }; + if(rows[1].length) + data.next = rows[1][0].id; + if(rows[2].length) + data.prev = rows[2][0].id; + } + else + data = { error: 'nope' }; + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(data), 'utf-8'); + }); + } + } else { // errorpage res.writeHead(404); res.end('404 - f0ck you', 'utf-8'); From e02e3e60ebede2f1e78695feca8b2c7927f2337a Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 19:22:51 +0000 Subject: [PATCH 08/24] Update cfg.json --- cfg.json | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/cfg.json b/cfg.json index e69de29..5662fe0 100644 --- a/cfg.json +++ b/cfg.json @@ -0,0 +1,58 @@ +{ + "server": [ + { + "name": "Server 1", + "host": "", + "port": "", + "ssl": "1", + "ssl_allow_invalid": "1", + "pass": "", + "nick": "", + "username": "", + "realname": "" + }, + { + "name": "Server 2", + "host": "", + "port": "", + "ssl": "1", + "ssl_allow_invalid": "1", + "pass": "", + "nick": "", + "username": "", + "realname": "" + } + ], + "mysql": { + "host": "localhost", + "user": "", + "password": "", + "database": "", + "multipleStatements": true + }, + "allowedMimes": { + "image/png": "png", + "video/webm": "webm", + "image/gif": "gif", + "image/jpeg": "jpg", + "video/mp4": "mp4", + "audio/mpeg": "mp3", + "audio/ogg": "ogg" + }, + "minRes": 256, + "maxFileSize": 31457280, + "webserver": { + "port": 1488 + }, + "level": { + "~": 80, + "&": 70, + "@": 60, + "%": 50, + "+": 10, + "": 0 + }, + "wlip": { + "::ffff:1.0.0.5":"w0bm" + } +} From 7cf82cb7f76d32a9923455332b207e084785b30e Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 19:25:50 +0000 Subject: [PATCH 09/24] test > match --- src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index 2f3f5dc..d8cb950 100644 --- a/src/main.js +++ b/src/main.js @@ -42,7 +42,7 @@ bot.on('motd', (e) => { bot.on('message', (e) => { trigger.trigger.forEach((entry,i,a) => { - if(e.message.match(entry.call)) { + if(entry.call.test(e.message)) { lib.getUserlevel(e, (cb) => { if(cb.level >= entry.level) entry.func(e); From bbdebb0673c3c5ef68bfb407c7b55ac04afbd925 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 23 Aug 2016 19:28:12 +0000 Subject: [PATCH 10/24] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index e69de29..f3a94e6 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,8 @@ +how to install: + +1. install ffmpeg +2. git clone +3. npm i +4. edit config (WIP) +5. npm start (or ./start.sh) +6. gtfo \ No newline at end of file From dc3cf386701311557e6841be817aa1fc832d61ec Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 07:42:42 +0000 Subject: [PATCH 11/24] +ytdl-trigger --- package.json | 3 +- src/trigger/ytdl.js | 80 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/trigger/ytdl.js diff --git a/package.json b/package.json index 2241e14..a7a80be 100644 --- a/package.json +++ b/package.json @@ -9,10 +9,11 @@ "coffea": "^0.4.24", "fs-extra": "^0.30.0", "mysql": "^2.11.1", + "node-ffprobe": "^1.2.2", "repl": "^0.1.3", "swig": "^1.4.2", "uuid": "^2.0.2", - "node-ffprobe": "^1.2.2" + "ytdl-core": "^0.7.17" }, "repository": { "type": "git", diff --git a/src/trigger/ytdl.js b/src/trigger/ytdl.js new file mode 100644 index 0000000..ce49321 --- /dev/null +++ b/src/trigger/ytdl.js @@ -0,0 +1,80 @@ +var fs = require('fs-extra'); +var ytdl = require('ytdl-core'); +var uuid = require('uuid'); +var probe = require('node-ffprobe'); + +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'ytdl', + call: new RegExp('(https?://)?(www\\.)?(yotu\\.be/|youtube\\.com/)?((.+/)?(watch(\\?v=|.+&v=))?(v=)?)([\\w_-]{11})(&.+)?', 'gi'), + level: 0, + func: (e) => { + if(e.channel.getName() == '#kbot-dev') { + if(!e.message.match(/\!ignore$/)) { + var tmp = e.message.match(/https?:\/\/(www\.)?(yotu\.be\/|youtube\.com\/)?((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi); // get links + tmp.forEach((entry,i,a) => { + var dl = true; + var tmpdest = uuid.v1().split('-')[0]; + lib.checkRepost(entry, (cbcr) => { + if(cbcr) { + var dat = fs.createWriteStream('./b/'+tmpdest+'.webm'); + ytdl(entry, { filter: (format) => { return format.container === 'webm'; } }) + .on('response', (res) => { + if(res.headers['content-length'] > lib.cfg.maxFileSize) { + res.destroy(); + dl = false; + e.reply('f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(lib.cfg.maxFileSize)+' allowed'); + } + }) + .pipe( dat ); + dat.on('finish', () => { + if(dl) { + dat.close(); + //probe("./b/"+tmpdest+".webm", (err, probeData) => { + // var size = probeData.size; + var stat = fs.statSync("./b/"+tmpdest+".webm"); + lib.getUser(e, (cbgu) => { + lib.getCheckSum("./b/"+tmpdest+".webm", (cbcs) => { + lib.checkRepostCheckSum(cbcs, (cbcrcs) => { + if(cbcrcs) { + lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [ + entry, + "./b/"+tmpdest+".webm", + "video/webm", + stat.size, + cbcs, + cbgu['nick'], + e.channel.getName(), + e.network, + Math.floor(new Date() / 1000), + 0 + ]).on('result', (result) => { + lib.generateThumbs(); + e.reply("https://f0ck.me/"+result.insertId+" - "+entry+" (video/webm, ~"+lib.formatSize(stat.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")"); + }).on('error', (msg) => { + e.reply(msg); + }); + } + else { + fs.unlink("./b/"+tmpdest+".webm"); // delete repost + e.reply("repost motherf0cker"); + } + }); + }); + }); + //}); + } + else + fs.unlink('./b/'+tmpdest+'.webm'); + }); + } + else + e.reply('repost motherf0cker'); + }); + }); + } + } + }, + desc: 'download youtubevideos' + }); +}; \ No newline at end of file From c7a48d4fae303b5ca9b240e0cbe7cde6f46615db Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 07:46:25 +0000 Subject: [PATCH 12/24] modified: src/trigger/ytdl.js --- src/trigger/ytdl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trigger/ytdl.js b/src/trigger/ytdl.js index ce49321..2f97a21 100644 --- a/src/trigger/ytdl.js +++ b/src/trigger/ytdl.js @@ -9,7 +9,7 @@ module.exports = (bot, trigger, lib) => { call: new RegExp('(https?://)?(www\\.)?(yotu\\.be/|youtube\\.com/)?((.+/)?(watch(\\?v=|.+&v=))?(v=)?)([\\w_-]{11})(&.+)?', 'gi'), level: 0, func: (e) => { - if(e.channel.getName() == '#kbot-dev') { + if(e.channel.getName() == '#f0ck') { if(!e.message.match(/\!ignore$/)) { var tmp = e.message.match(/https?:\/\/(www\.)?(yotu\.be\/|youtube\.com\/)?((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi); // get links tmp.forEach((entry,i,a) => { From 91a2edcce7be6ff789d4a8e01b82b1b83fe452c6 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 07:49:17 +0000 Subject: [PATCH 13/24] bugfix --- src/trigger/ytdl.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/trigger/ytdl.js b/src/trigger/ytdl.js index 2f97a21..83fcb6a 100644 --- a/src/trigger/ytdl.js +++ b/src/trigger/ytdl.js @@ -18,15 +18,23 @@ module.exports = (bot, trigger, lib) => { lib.checkRepost(entry, (cbcr) => { if(cbcr) { var dat = fs.createWriteStream('./b/'+tmpdest+'.webm'); - ytdl(entry, { filter: (format) => { return format.container === 'webm'; } }) - .on('response', (res) => { - if(res.headers['content-length'] > lib.cfg.maxFileSize) { - res.destroy(); - dl = false; - e.reply('f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(lib.cfg.maxFileSize)+' allowed'); - } - }) - .pipe( dat ); + try { + ytdl(entry, { filter: (format) => { return format.container === 'webm'; } }) + .on('response', (res) => { + if(res.headers['content-length'] > lib.cfg.maxFileSize) { + res.destroy(); + dl = false; + e.reply('f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(lib.cfg.maxFileSize)+' allowed'); + } + }) + .pipe( dat ); + } + catch(ex) { + dat.close(); + fs.unlink('./b/'+tmpdest+'.webm'); + e.reply("noep"); + dl = false; + } dat.on('finish', () => { if(dl) { dat.close(); From b34e63ce0ed289fbf0c0c759c2abf0ee069d093e Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 07:51:26 +0000 Subject: [PATCH 14/24] bugfix --- src/trigger/ytdl.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/trigger/ytdl.js b/src/trigger/ytdl.js index 83fcb6a..bd5ec52 100644 --- a/src/trigger/ytdl.js +++ b/src/trigger/ytdl.js @@ -18,8 +18,15 @@ module.exports = (bot, trigger, lib) => { lib.checkRepost(entry, (cbcr) => { if(cbcr) { var dat = fs.createWriteStream('./b/'+tmpdest+'.webm'); - try { - ytdl(entry, { filter: (format) => { return format.container === 'webm'; } }) + ytdl(entry, { filter: (format) => { + try { + return format.container === 'webm'; + } + catch(ex) { + dl = false; + e.reply("noep D:"); + } + } }) .on('response', (res) => { if(res.headers['content-length'] > lib.cfg.maxFileSize) { res.destroy(); @@ -28,13 +35,6 @@ module.exports = (bot, trigger, lib) => { } }) .pipe( dat ); - } - catch(ex) { - dat.close(); - fs.unlink('./b/'+tmpdest+'.webm'); - e.reply("noep"); - dl = false; - } dat.on('finish', () => { if(dl) { dat.close(); From 334ef5bb2ab53fe8a766a3b437fc637555504582 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 07:55:40 +0000 Subject: [PATCH 15/24] bugfix --- src/main.js | 2 +- src/trigger/del.js | 1 + src/trigger/level.js | 1 + src/trigger/parser.js | 1 + src/trigger/user.js | 1 + src/trigger/ytdl.js | 11 ++--------- 6 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main.js b/src/main.js index d8cb950..a256ad0 100644 --- a/src/main.js +++ b/src/main.js @@ -42,7 +42,7 @@ bot.on('motd', (e) => { bot.on('message', (e) => { trigger.trigger.forEach((entry,i,a) => { - if(entry.call.test(e.message)) { + if(entry.call.test(e.message) && entry.active == 1) { lib.getUserlevel(e, (cb) => { if(cb.level >= entry.level) entry.func(e); diff --git a/src/trigger/del.js b/src/trigger/del.js index 82d22d0..e3d4ba4 100644 --- a/src/trigger/del.js +++ b/src/trigger/del.js @@ -3,6 +3,7 @@ module.exports = (bot, trigger, lib) => { name: 'delete', call: new RegExp('^\\!del (\\d+)$', 'i'), level: 100, + active: 1, func: (e) => { var id = e.message.split(' ')[1]; lib.sql.query("delete from `f0ck`.`items` where `id` = ?", id).on('end', () => { diff --git a/src/trigger/level.js b/src/trigger/level.js index cf0766b..ab0d103 100644 --- a/src/trigger/level.js +++ b/src/trigger/level.js @@ -3,6 +3,7 @@ module.exports = (bot, trigger, lib) => { name: 'level', call: new RegExp('^\\!level$', 'i'), level: 0, + active: 1, func: (e) => { lib.getUserlevel(e, (cb) => { e.reply("level from "+e.user.getNick()+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")"); diff --git a/src/trigger/parser.js b/src/trigger/parser.js index 64a1027..ca77527 100644 --- a/src/trigger/parser.js +++ b/src/trigger/parser.js @@ -7,6 +7,7 @@ module.exports = (bot, trigger, lib) => { name: 'parser', call: new RegExp('https?:\\/\\/[\\w-]+(\\.[\\w-]+)+\\.?(:\\d+)?(\\/\\S*)?', 'gi'), level: 0, + active: 1, func: (e) => { if(e.channel.getName() == '#f0ck') { if(!e.message.match(/\!ignore$/)) { diff --git a/src/trigger/user.js b/src/trigger/user.js index c891f16..688c3e0 100644 --- a/src/trigger/user.js +++ b/src/trigger/user.js @@ -3,6 +3,7 @@ module.exports = (bot, trigger, lib) => { name: 'user', call: new RegExp('^\\!user$', 'i'), level: 0, + active: 1, func: (e) => { //e.reply(e.user); lib.getUser(e, (cbgu) => { diff --git a/src/trigger/ytdl.js b/src/trigger/ytdl.js index bd5ec52..f1419a3 100644 --- a/src/trigger/ytdl.js +++ b/src/trigger/ytdl.js @@ -8,6 +8,7 @@ module.exports = (bot, trigger, lib) => { name: 'ytdl', call: new RegExp('(https?://)?(www\\.)?(yotu\\.be/|youtube\\.com/)?((.+/)?(watch(\\?v=|.+&v=))?(v=)?)([\\w_-]{11})(&.+)?', 'gi'), level: 0, + active: 0, func: (e) => { if(e.channel.getName() == '#f0ck') { if(!e.message.match(/\!ignore$/)) { @@ -18,15 +19,7 @@ module.exports = (bot, trigger, lib) => { lib.checkRepost(entry, (cbcr) => { if(cbcr) { var dat = fs.createWriteStream('./b/'+tmpdest+'.webm'); - ytdl(entry, { filter: (format) => { - try { - return format.container === 'webm'; - } - catch(ex) { - dl = false; - e.reply("noep D:"); - } - } }) + ytdl(entry, { filter: (format) => { return format.container === 'webm'; } }) .on('response', (res) => { if(res.headers['content-length'] > lib.cfg.maxFileSize) { res.destroy(); From 152bd3fe5fd5bf1d775abaaf3b0a6a7e051677cc Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 08:34:30 +0000 Subject: [PATCH 16/24] api index --- src/websrv.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/websrv.js b/src/websrv.js index d22f3f2..b539bdd 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -186,8 +186,18 @@ function Websrv(tbot, tsql, tcfg, tlib) { else if(filePath.match(/^\.\/api/i)) { // api var url = filePath.split('/'); if(url[2] === undefined) { // Mainpage - res.writeHead(200, { 'Content-Type': contentType }); - res.end('kommt noch'); + var query = "select * from `f0ck`.`items`"; + sql.query(query, (err, rows, fields) => { + var items = []; + rows.forEach((e,i,a) => { + items.push({ + 'id': e.id, + 'mime': e.mime + }); + }); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(JSON.stringify(items), 'utf-8'); + }); } else if(Number.isInteger(parseInt(url[2]))) { // Item var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item From 32310b0d5875c8b8f6c5ac232e466871da4d9b68 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 10:35:08 +0000 Subject: [PATCH 17/24] ups. --- src/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.js b/src/main.js index a256ad0..c244ce5 100644 --- a/src/main.js +++ b/src/main.js @@ -66,7 +66,8 @@ var trigger = { call: args.call, func: args.func, desc: args.desc, - level: args.level + level: args.level, + active: args.active }); } }; From 8e79085735ee4594cccf0d9c93d0a05dd7f9620d Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 10:37:16 +0000 Subject: [PATCH 18/24] Update main.js From be4fe8d240748b8aa6b878fd9fdd1319ce0dbe29 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 12:18:51 +0000 Subject: [PATCH 19/24] security fix --- package.json | 1 + src/lib.js | 22 ++++++++++++++-------- src/main.js | 3 ++- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index a7a80be..c3603e4 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dependencies": { "coffea": "^0.4.24", "fs-extra": "^0.30.0", + "mime": "^1.3.4", "mysql": "^2.11.1", "node-ffprobe": "^1.2.2", "repl": "^0.1.3", diff --git a/src/lib.js b/src/lib.js index 9e4ad4a..4059dfb 100644 --- a/src/lib.js +++ b/src/lib.js @@ -4,6 +4,7 @@ var https = require('https'); var exec = require('child_process').exec; var probe = require('node-ffprobe'); var crypto = require('crypto'); +var Mime = require('mime'); var bot, sql, cfg; var debug = true; @@ -114,16 +115,21 @@ Lib.prototype.dl = (url, dest, cb) => { response.pipe(file); file.on('finish', () => { file.close(); - probe(dest+"."+cfg.allowedMimes[response.headers['content-type']], (err, probeData) => { - if(probeData.streams[0].height !== undefined || probeData.streams[0].width !== undefined) { - if(probeData.streams[0].height <= cfg.minRes || probeData.streams[0].width <= cfg.minRes) - cb({'status':false, 'msg':'f0ck! your shitpost is too small ('+probeData.streams[0].width+' x '+probeData.streams[0].height+'), min '+cfg.minRes+' x '+cfg.minRes+' required', 'type':1}); + 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) { + if(probeData.streams[0].height <= cfg.minRes || probeData.streams[0].width <= cfg.minRes) + cb({'status':false, 'msg':'f0ck! your shitpost is too small ('+probeData.streams[0].width+' x '+probeData.streams[0].height+'), min '+cfg.minRes+' x '+cfg.minRes+' required', 'type':1}); + 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']]}}); + } 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']]}}); - } - 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']]}}); - }); + }); + } + else + cb({'status':false, 'msg':'lol, go f0ck yourself', 'type':1}); }); file.on('error', (err) => { fs.unlink(dest+"."+cfg.allowedMimes[response.headers['content-type']]); diff --git a/src/main.js b/src/main.js index a256ad0..c244ce5 100644 --- a/src/main.js +++ b/src/main.js @@ -66,7 +66,8 @@ var trigger = { call: args.call, func: args.func, desc: args.desc, - level: args.level + level: args.level, + active: args.active }); } }; From ff9ac383ef392821fe806bb6edfa4d22e593dc4f Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 19:15:14 +0200 Subject: [PATCH 20/24] timingproblems fixed --- src/lib.js | 44 +++++++++++++++++++++++--------------------- src/trigger/debug.js | 19 +++++++++++++++++++ src/trigger/thumb.js | 21 +++++++++++++++++++++ 3 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 src/trigger/debug.js create mode 100644 src/trigger/thumb.js diff --git a/src/lib.js b/src/lib.js index 4059dfb..16c7d42 100644 --- a/src/lib.js +++ b/src/lib.js @@ -7,13 +7,13 @@ var crypto = require('crypto'); var Mime = require('mime'); var bot, sql, cfg; -var debug = true; module.exports = Lib; function Lib(tbot, tsql, tcfg) { this.bot = bot = tbot; this.sql = sql = tsql; this.cfg = cfg = tcfg; + this.debug = true; this.admins = []; } @@ -21,23 +21,25 @@ Lib.prototype.getUser = (e, cbgu) => { var u = e.user.getNick(); var n = e.network; if(!e.user.hasOwnProperty('hostname')) { - 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]); + 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]); + }); }); - }); + }, 100); } else { cbgu({ @@ -119,8 +121,8 @@ Lib.prototype.dl = (url, dest, cb) => { 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) { - if(probeData.streams[0].height <= cfg.minRes || probeData.streams[0].width <= cfg.minRes) - cb({'status':false, 'msg':'f0ck! your shitpost is too small ('+probeData.streams[0].width+' x '+probeData.streams[0].height+'), min '+cfg.minRes+' x '+cfg.minRes+' required', 'type':1}); + if(probeData.streams[0].height + probeData.streams[0].width <= cfg.minRes) + cb({'status':false, 'msg':'f0ck! your shitpost is too small', 'type':1}); 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']]}}); } @@ -156,7 +158,7 @@ Lib.prototype.generateThumbs = () => { rows.forEach((e,i,a) => { var thumbnail = outdir+e.id+'.png'; if(!fs.existsSync(thumbnail)) { - exec('ffmpegthumbnailer -i'+e.dest+' -s256 -o'+thumbnail, (error) => { + exec('ffmpegthumbnailer -i'+e.dest+' -s1024 -o'+thumbnail, (error) => { if(error) { Lib.prototype.log('failed thumbnail for '+e.id+' ('+e.mime+') 1'); fs.copySync('./s/mp3.png', thumbnail); // copy standardthumbnail @@ -175,6 +177,6 @@ Lib.prototype.generateThumbs = () => { }); }; Lib.prototype.log = (msg) => { - if(debug) + if(this.debug) bot.send("#f0ck", msg, 'n0xy'); }; \ No newline at end of file diff --git a/src/trigger/debug.js b/src/trigger/debug.js new file mode 100644 index 0000000..ed04c64 --- /dev/null +++ b/src/trigger/debug.js @@ -0,0 +1,19 @@ +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'debug', + call: new RegExp('^\\!debug$', 'i'), + level: 100, + active: 1, + func: (e) => { + if(lib.debug) { + lib.debug = false; + e.reply('debugmode deactivated'); + } + else { + lib.debug = true; + e.reply('debugmode activated'); + } + }, + desc: 'toggle debug' + }); +}; \ No newline at end of file diff --git a/src/trigger/thumb.js b/src/trigger/thumb.js new file mode 100644 index 0000000..60f8b75 --- /dev/null +++ b/src/trigger/thumb.js @@ -0,0 +1,21 @@ +var fs = require('fs-extra'); + +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'thumbnailer', + call: new RegExp('^\\!thumb (\\d+)$', 'i'), + level: 100, + active: 1, + func: (e) => { + var id = e.message.split(' ')[1]; + if(Number.isInteger(parseInt(id))) { + fs.unlink('./t/'+id+'.png', () => { + //e.reply('debug: Thumbnail gelöscht'); + lib.generateThumbs(); + //e.reply('debug: sollte Thumbnail generiert haben'); + }); + } + }, + desc: 'regenerate thumbnail' + }); +}; \ No newline at end of file From b71e20933d0248712eab2792ae78c5cacfee9230 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 21:36:18 +0200 Subject: [PATCH 21/24] modified: src/lib.js modified: src/main.js modified: src/trigger/debug.js modified: src/trigger/ytdl.js --- src/lib.js | 16 +++++++++++++--- src/main.js | 12 ++++++++---- src/trigger/debug.js | 9 +-------- src/trigger/ytdl.js | 6 +++++- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/lib.js b/src/lib.js index 16c7d42..61c6a5c 100644 --- a/src/lib.js +++ b/src/lib.js @@ -6,14 +6,14 @@ var probe = require('node-ffprobe'); var crypto = require('crypto'); var Mime = require('mime'); -var bot, sql, cfg; +var bot, sql, cfg, debug; module.exports = Lib; function Lib(tbot, tsql, tcfg) { this.bot = bot = tbot; this.sql = sql = tsql; this.cfg = cfg = tcfg; - this.debug = true; + this.debug = debug = true; this.admins = []; } @@ -177,6 +177,16 @@ Lib.prototype.generateThumbs = () => { }); }; Lib.prototype.log = (msg) => { - if(this.debug) + if(debug) bot.send("#f0ck", msg, 'n0xy'); +}; +Lib.prototype.toggleDebug = () => { + if(debug) { + debug = false; + return 'debugmode deactivated'; + } + else { + debug = true; + return 'debugmode activated'; + } }; \ No newline at end of file diff --git a/src/main.js b/src/main.js index c244ce5..f203ef3 100644 --- a/src/main.js +++ b/src/main.js @@ -43,11 +43,15 @@ bot.on('motd', (e) => { bot.on('message', (e) => { trigger.trigger.forEach((entry,i,a) => { if(entry.call.test(e.message) && entry.active == 1) { + lib.log('trigger: '+entry.name); lib.getUserlevel(e, (cb) => { - if(cb.level >= entry.level) - entry.func(e); - else - e.reply('no permission, min level '+entry.level+' required'); + setTimeout(()=>{ + if(cb.level >= entry.level) { + entry.func(e); + } + else + e.reply('no permission, min level '+entry.level+' required'); + }, 200); }); } }); diff --git a/src/trigger/debug.js b/src/trigger/debug.js index ed04c64..061821f 100644 --- a/src/trigger/debug.js +++ b/src/trigger/debug.js @@ -5,14 +5,7 @@ module.exports = (bot, trigger, lib) => { level: 100, active: 1, func: (e) => { - if(lib.debug) { - lib.debug = false; - e.reply('debugmode deactivated'); - } - else { - lib.debug = true; - e.reply('debugmode activated'); - } + e.reply(lib.toggleDebug()); }, desc: 'toggle debug' }); diff --git a/src/trigger/ytdl.js b/src/trigger/ytdl.js index f1419a3..1c0b08d 100644 --- a/src/trigger/ytdl.js +++ b/src/trigger/ytdl.js @@ -19,7 +19,8 @@ module.exports = (bot, trigger, lib) => { lib.checkRepost(entry, (cbcr) => { if(cbcr) { var dat = fs.createWriteStream('./b/'+tmpdest+'.webm'); - ytdl(entry, { filter: (format) => { return format.container === 'webm'; } }) + //ytdl(entry, { filter: (format) => { return format.container === 'webm'; } }) + ytdl(entry) .on('response', (res) => { if(res.headers['content-length'] > lib.cfg.maxFileSize) { res.destroy(); @@ -27,6 +28,9 @@ module.exports = (bot, trigger, lib) => { e.reply('f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(lib.cfg.maxFileSize)+' allowed'); } }) + .on('error', (err) => { + //e.reply(err); + }) .pipe( dat ); dat.on('finish', () => { if(dl) { From 649e4339aa3d8701d0763fc5b3b56b562af1cfb4 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 22:02:20 +0200 Subject: [PATCH 22/24] small bugfix --- src/main.js | 9 ++++++--- src/trigger/debug.js | 2 +- src/trigger/del.js | 2 +- src/trigger/level.js | 2 +- src/trigger/parser.js | 2 +- src/trigger/thumb.js | 2 +- src/trigger/user.js | 2 +- src/trigger/ytdl.js | 2 +- 8 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main.js b/src/main.js index f203ef3..250b4da 100644 --- a/src/main.js +++ b/src/main.js @@ -42,16 +42,18 @@ bot.on('motd', (e) => { bot.on('message', (e) => { trigger.trigger.forEach((entry,i,a) => { - if(entry.call.test(e.message) && entry.active == 1) { + //if(entry.call.test(e.message) && entry.active == 1) { + //if(e.message.match(new RegExp(entry.call, entry.flags)) && entry.active == 1) { + if(e.message.match(entry.call) && entry.active == 1) { lib.log('trigger: '+entry.name); lib.getUserlevel(e, (cb) => { - setTimeout(()=>{ + //setTimeout(()=>{ if(cb.level >= entry.level) { entry.func(e); } else e.reply('no permission, min level '+entry.level+' required'); - }, 200); + //}, 200); }); } }); @@ -68,6 +70,7 @@ var trigger = { trigger.trigger.push({ name: args.name, call: args.call, + //flags: args.flags, func: args.func, desc: args.desc, level: args.level, diff --git a/src/trigger/debug.js b/src/trigger/debug.js index 061821f..51f3ba1 100644 --- a/src/trigger/debug.js +++ b/src/trigger/debug.js @@ -1,7 +1,7 @@ module.exports = (bot, trigger, lib) => { trigger.add({ name: 'debug', - call: new RegExp('^\\!debug$', 'i'), + call: /^\!debug$/i, level: 100, active: 1, func: (e) => { diff --git a/src/trigger/del.js b/src/trigger/del.js index e3d4ba4..e1d4f61 100644 --- a/src/trigger/del.js +++ b/src/trigger/del.js @@ -1,7 +1,7 @@ module.exports = (bot, trigger, lib) => { trigger.add({ name: 'delete', - call: new RegExp('^\\!del (\\d+)$', 'i'), + call: /^\!del (\d+)$/i, level: 100, active: 1, func: (e) => { diff --git a/src/trigger/level.js b/src/trigger/level.js index ab0d103..f4eb0d0 100644 --- a/src/trigger/level.js +++ b/src/trigger/level.js @@ -1,7 +1,7 @@ module.exports = (bot, trigger, lib) => { trigger.add({ name: 'level', - call: new RegExp('^\\!level$', 'i'), + call: /^\!level$/i, level: 0, active: 1, func: (e) => { diff --git a/src/trigger/parser.js b/src/trigger/parser.js index ca77527..b86294d 100644 --- a/src/trigger/parser.js +++ b/src/trigger/parser.js @@ -5,7 +5,7 @@ var path = require('path'); module.exports = (bot, trigger, lib) => { trigger.add({ name: 'parser', - call: new RegExp('https?:\\/\\/[\\w-]+(\\.[\\w-]+)+\\.?(:\\d+)?(\\/\\S*)?', 'gi'), + call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi, level: 0, active: 1, func: (e) => { diff --git a/src/trigger/thumb.js b/src/trigger/thumb.js index 60f8b75..f759058 100644 --- a/src/trigger/thumb.js +++ b/src/trigger/thumb.js @@ -3,7 +3,7 @@ module.exports = (bot, trigger, lib) => { trigger.add({ name: 'thumbnailer', - call: new RegExp('^\\!thumb (\\d+)$', 'i'), + call: /^\!thumb (\d+)$/i, level: 100, active: 1, func: (e) => { diff --git a/src/trigger/user.js b/src/trigger/user.js index 688c3e0..14726cd 100644 --- a/src/trigger/user.js +++ b/src/trigger/user.js @@ -1,7 +1,7 @@ module.exports = (bot, trigger, lib) => { trigger.add({ name: 'user', - call: new RegExp('^\\!user$', 'i'), + call: /^\!user$/i, level: 0, active: 1, func: (e) => { diff --git a/src/trigger/ytdl.js b/src/trigger/ytdl.js index 1c0b08d..da12ec2 100644 --- a/src/trigger/ytdl.js +++ b/src/trigger/ytdl.js @@ -6,7 +6,7 @@ var probe = require('node-ffprobe'); module.exports = (bot, trigger, lib) => { trigger.add({ name: 'ytdl', - call: new RegExp('(https?://)?(www\\.)?(yotu\\.be/|youtube\\.com/)?((.+/)?(watch(\\?v=|.+&v=))?(v=)?)([\\w_-]{11})(&.+)?', 'gi'), + call: /https?:\/\/(www\.)?(yotu\.be\/|youtube\.com\/)?((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi, level: 0, active: 0, func: (e) => { From 2fbffc0574ee1a364c78e4050a6fafea9895a840 Mon Sep 17 00:00:00 2001 From: Flummi Date: Wed, 24 Aug 2016 22:21:02 +0200 Subject: [PATCH 23/24] muh --- src/lib.js | 2 +- src/trigger/parser.js | 70 ++++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/lib.js b/src/lib.js index 61c6a5c..b5ebbed 100644 --- a/src/lib.js +++ b/src/lib.js @@ -13,7 +13,7 @@ function Lib(tbot, tsql, tcfg) { this.bot = bot = tbot; this.sql = sql = tsql; this.cfg = cfg = tcfg; - this.debug = debug = true; + this.debug = debug = false; this.admins = []; } diff --git a/src/trigger/parser.js b/src/trigger/parser.js index b86294d..2f859dd 100644 --- a/src/trigger/parser.js +++ b/src/trigger/parser.js @@ -14,42 +14,44 @@ module.exports = (bot, trigger, lib) => { var tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links tmp.forEach((entry,i,a) => { var tmpdest = uuid.v1().split('-')[0]; - lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item - if(cb.status === true) { - lib.getUser(e, (cbgu) => { - lib.getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { - lib.checkRepostCheckSum(cbcs, (cbcrcs) => { - if(cbcrcs) { - lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [ - entry, - "./b/"+tmpdest+"."+cb.infos.ext, - cb.infos.mime, - cb.infos.size, - cbcs, - cbgu['nick'], - e.channel.getName(), - e.network, - Math.floor(new Date() / 1000), - 0 - ]).on('result', (result) => { - lib.generateThumbs(); - 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) => { - e.reply(msg); - }); - } - else { - fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost - e.reply("repost motherf0cker"); - } + if(!entry.match(/f0ck\.me/i)) { + lib.dl(entry, "./b/"+tmpdest, (cb) => { // download item + if(cb.status === true) { + lib.getUser(e, (cbgu) => { + lib.getCheckSum("./b/"+tmpdest+"."+cb.infos.ext, (cbcs) => { + lib.checkRepostCheckSum(cbcs, (cbcrcs) => { + if(cbcrcs) { + lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [ + entry, + "./b/"+tmpdest+"."+cb.infos.ext, + cb.infos.mime, + cb.infos.size, + cbcs, + cbgu['nick'], + e.channel.getName(), + e.network, + Math.floor(new Date() / 1000), + 0 + ]).on('result', (result) => { + lib.generateThumbs(); + 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) => { + e.reply(msg); + }); + } + else { + fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost + e.reply("repost motherf0cker"); + } + }); }); }); - }); - } - else - if(cb.type == 1) - e.reply(cb.msg); - }); + } + else + if(cb.type == 1) + e.reply(cb.msg); + }); + } }); } } From 68249b6af239af3114e0afe70564491240947097 Mon Sep 17 00:00:00 2001 From: Flummi Date: Thu, 25 Aug 2016 00:28:40 +0200 Subject: [PATCH 24/24] modified: package.json modified: src/main.js new file: src/trigger/stats.js --- package.json | 1 + src/main.js | 14 ++++---------- src/trigger/stats.js | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 src/trigger/stats.js diff --git a/package.json b/package.json index c3603e4..b86c3ee 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ }, "dependencies": { "coffea": "^0.4.24", + "du": "^0.1.0", "fs-extra": "^0.30.0", "mime": "^1.3.4", "mysql": "^2.11.1", diff --git a/src/main.js b/src/main.js index 250b4da..85ad053 100644 --- a/src/main.js +++ b/src/main.js @@ -42,18 +42,13 @@ bot.on('motd', (e) => { bot.on('message', (e) => { trigger.trigger.forEach((entry,i,a) => { - //if(entry.call.test(e.message) && entry.active == 1) { - //if(e.message.match(new RegExp(entry.call, entry.flags)) && entry.active == 1) { if(e.message.match(entry.call) && entry.active == 1) { lib.log('trigger: '+entry.name); lib.getUserlevel(e, (cb) => { - //setTimeout(()=>{ - if(cb.level >= entry.level) { - entry.func(e); - } - else - e.reply('no permission, min level '+entry.level+' required'); - //}, 200); + if(cb.level >= entry.level) + entry.func(e); + else + e.reply('no permission, min level '+entry.level+' required'); }); } }); @@ -70,7 +65,6 @@ var trigger = { trigger.trigger.push({ name: args.name, call: args.call, - //flags: args.flags, func: args.func, desc: args.desc, level: args.level, diff --git a/src/trigger/stats.js b/src/trigger/stats.js new file mode 100644 index 0000000..9652d72 --- /dev/null +++ b/src/trigger/stats.js @@ -0,0 +1,20 @@ +var du = require('du'); + +module.exports = (bot, trigger, lib) => { + trigger.add({ + name: 'stats', + call: /^\!stats$/i, + level: 10, + active: 1, + func: (e) => { + lib.sql.query("select count(`id`) as anzahl from `f0ck`.`items`", (err, rows, fields) => { + var ret = "f0cked "+String(rows[0].anzahl); + du('./b/', function (err, size) { + ret += " f0cks ("+lib.formatSize(size)+")"; + e.reply(ret); + }) + }); + }, + desc: 'stats' + }); +}; \ No newline at end of file