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