modified: src/main.js

This commit is contained in:
Flummi 2016-08-14 16:37:56 +02:00
parent 059b713555
commit 93653c5e53

View File

@ -4,14 +4,23 @@ var http = require('http');
var https = require('https');
var mysql = require('mysql');
var bot = require('coffea')();
var uuid = require('uuid');
const path = require('path');
var allowed = [
'image/png', 'video/webm'
'image/png',
'video/webm',
'image/gif',
'image/jpeg',
'video/mp4',
'audio/mpeg3',
'audio/x-mpeg-3',
'audio/mpeg'
];
var maxFileSize = 10485760; // 10 mb
var sql;
function haDC() {
var haDC = () => {
sql = mysql.createConnection(cfg.mysql);
sql.connect((err) => {
if(err) setTimeout(haDC,2000);
@ -19,7 +28,7 @@ function haDC() {
sql.on('error', (err) => {
if(err.code === 'PROTOCOL_CONNECTION_LOST') haDC();
});
}
};
haDC();
cfg.server.forEach((e,i,a) => {
@ -34,7 +43,7 @@ cfg.server.forEach((e,i,a) => {
"username": e.username,
"realname": e.realname
});
console.log("Server "+e.name+" wurden geladen");
console.log("Server "+e.name+" wurde geladen");
});
bot.on('motd', (e) => {
@ -47,25 +56,29 @@ bot.on('message', (e) => {
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/"+path.parse(entry).base, (cb) => { // download item
var tmpdest = uuid.v1().split('-')[0]+path.parse(entry).ext;
dl(entry, "./b/"+tmpdest, (cb) => { // download item
if(cb.status === true) {
var tmpuser = getUser(e.user.getNick(), e.network);
sql.query("insert into `f0ck`.`items` (`src`,`dest`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?)", [
entry,
"./b/"+path.parse(entry).base,
"./b/"+tmpdest,
tmpuser['nick'],
e.channel.getName(),
e.network,
new Date().getTime(),
0
]).on('end', () => {
e.reply(cb.msg+" for "+tmpuser['nick']+" ("+tmpuser['username']+"@"+tmpuser['hostname']+")");
]).on('result', (result) => {
e.reply("https://f0ck.me/"+result.insertId+" - "+path.parse(entry).base+" ("+cb.infos.mime+", ~"+formatSize(cb.infos.size)+") from "+tmpuser['nick']+" ("+tmpuser['username']+"@"+tmpuser['hostname']+")");
}).on('error', (msg) => {
e.reply(msg);
});
}
else
e.reply(cb.msg);
else {
if(cb.type == 1)
e.reply(cb.msg);
}
});
});
}
@ -81,6 +94,10 @@ bot.on('message', (e) => {
e.reply(msg);
});
}
else if(orig.match(/^\.uuid/)) {
var tmp = uuid.v1().split('-')[0];
e.reply(tmp);
}
});
var getUser = (u, n) => {
@ -91,30 +108,36 @@ var getUser = (u, n) => {
};
var dl = (url, dest, cb) => {
var file = fs.createWriteStream(dest);
var request = (url.match(/^https/)?https:http).get(url, (response) => {
try {
try { // type:1=post,type:2=stfu
console.log(response.headers['content-type']);
if(allowed.indexOf(response.headers['content-type']) != -1) {
checkRepost(url, (cbcr) => {
if(cbcr) {
response.pipe(file);
file.on('finish', () => {
file.close();
cb({'status':true, 'msg':'downloaded '+dest});
});
file.on('error', (err) => {
fs.unlink(dest);
file.close();
cb({'status':false, 'msg':err.message});
});
}
else {
cb({'status':false, 'msg':'repost motherf0cker'});
}
});
if(response.headers['content-length'] <= maxFileSize) {
checkRepost(url, (cbcr) => {
if(cbcr) {
var file = fs.createWriteStream(dest);
response.pipe(file);
file.on('finish', () => {
file.close();
cb({'status':true, 'msg':'downloaded '+dest, 'type':1, 'infos':{'mime':response.headers['content-type'], 'size':response.headers['content-length']}});
});
file.on('error', (err) => {
fs.unlink(dest);
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(maxFileSize)+' allowed', 'type':1});
}
}
else {
cb({'status':false, 'msg':'f0ck you'});
cb({'status':false, 'msg':'f0ck you', 'type':2});
}
}
catch(ex) {
@ -128,3 +151,8 @@ var checkRepost = (url, cbcr) => {
cbcr((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];
};