modified: src/main.js

This commit is contained in:
Flummi 2016-08-14 02:55:56 +02:00
parent 0b220ced8f
commit 71bff2ef5b

View File

@ -6,6 +6,10 @@ var mysql = require('mysql');
var bot = require('coffea')();
const path = require('path');
var allowed = [
'image/png', 'video/webm'
];
cfg.server.forEach((e,i,a) => {
bot.add({
"name": e.name,
@ -33,13 +37,20 @@ bot.on('message', (e) => {
var tmp = orig.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
tmp.forEach((entry,i,a) => {
dl(entry, "./b/"+path.parse(entry).base, (cb) => { // download item
e.reply(cb);
if(cb.status === true) {
var tmp = getUser(e.user.getNick(), e.network);
setTimeout(()=>{
e.reply(cb.msg+" for "+tmp['nick']+" ("+tmp['username']+"@"+tmp['hostname']+")");
}, 1600);
}
else
e.reply(cb.msg);
});
});
}
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); }, 1500);
}
}
});
@ -56,19 +67,24 @@ var dl = (url, dest, cb) => {
var file = fs.createWriteStream(dest);
var request = (url.match(/^https/)?https:http).get(url, (response) => {
try {
if(allowed.indexOf(response.headers['content-type']) != -1) {
response.pipe(file);
file.on('finish', () => {
file.close();
cb('downloaded '+dest);
cb({'status':true, 'msg':'downloaded '+dest});
});
file.on('error', (err) => {
fs.unlink(dest);
file.close();
cb(err.message);
cb({'status':false, 'msg':err.message});
});
}
else {
cb({'status':false, 'msg':'f0ck you'});
}
}
catch(ex) {
e.reply(ex);
}
});
}
};