optimized dlfunc

This commit is contained in:
Flummi 2016-08-14 01:40:14 +02:00
parent b026e89ff8
commit 959ba2a84d

View File

@ -1,8 +1,9 @@
var cfg = require('../../cfg.json'); var cfg = require('../../cfg.json');
var fs = require('fs'); var fs = require('fs');
var request = require('request'); var https = require("https");
var mysql = require('mysql'); var mysql = require('mysql');
var bot = require('coffea')(); var bot = require('coffea')();
const path = require('path');
cfg.server.forEach((e,i,a) => { cfg.server.forEach((e,i,a) => {
bot.add({ bot.add({
@ -22,62 +23,46 @@ 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.write('MODE f0ck +B', e.network, (c)=>{}); // Botflag bot.write('MODE f0ck +B', e.network, (c)=>{}); // Botflag
//if(g_server[e.network].channels.length > 0) {
// bot.join( JSON.parse( g_server[e.network].channels ) );
//}
}); });
bot.on('message', (e) => { bot.on('message', (e) => {
var orig = e.message; var orig = e.message;
if(e.channel.getName().toString() == "#f0ck") { // (debug) only in channel #f0ck
if(e.channel.getName().toString() == "#f0ck") { if(orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) { // shitpostcatcher
var tmp = orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
if(orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi)) {
var tmp = orig.match(/(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi);
tmp.forEach((entry,i,a) => { tmp.forEach((entry,i,a) => {
dl(entry, "./b/test.png", (cb) => { dl(entry, "./b/"+path.parse(entry).base, (cb) => { // download item
e.reply(cb); e.reply(cb);
}); });
}); });
} }
else if(orig.match(/^\.test/)) { else if(orig.match(/^\.user/)) { // (debug) get userinfos
var tmp = getUser(e.user.getNick(), e.network); var tmp = getUser(e.user.getNick(), e.network);
setTimeout(()=>{ e.reply(tmp); }, 2000);
setTimeout(()=>{
e.reply(tmp);
}, 2000);
} }
} }
}); });
var getUser = (u, n) => { var getUser = (u, n) => {
bot.whois(u, n, (fn)=>{ }); bot.whois(u, n, (fn)=>{ }); // send whois
bot.getUser(u, n);
var start = Date.now(); var start = Date.now();
while (Date.now() < start + 3000) {} while(Date.now() < start + 2000) {} // block script for catch whois
return bot.getUser(u, n); return bot.getUser(u, n);
}; };
var dl = (url, dest, cb) => { var dl = (url, dest, cb) => {
var file = fs.createWriteStream(dest); var file = fs.createWriteStream(dest);
var sendReq = request.get(url); var request = https.get(url, (response) => {
sendReq.on('response', (response) => { response.pipe(file);
if(response.statusCode !== 200) return cb('Response status was ' + response.statusCode);
});
sendReq.on('error', (err) => {
fs.unlink(dest);
if(cb) return cb(err.message);
});
sendReq.pipe(file);
file.on('finish', () => { file.on('finish', () => {
file.close(cb); file.close();
cb('downloaded '+dest);
}); });
file.on('error', (err) => { file.on('error', (err) => {
fs.unlink(dest); fs.unlink(dest);
if(cb) return cb(err.message); file.close();
cb(err.message);
}); });
}; });
}