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