modified: src/main.js

This commit is contained in:
Flummi 2016-08-14 20:08:31 +02:00
parent 97bf115120
commit 6b5089f19b

View File

@ -7,18 +7,6 @@ var bot = require('coffea')();
var uuid = require('uuid'); var uuid = require('uuid');
const path = require('path'); const path = require('path');
var allowed = [
'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;
var haDC = () => { var haDC = () => {
sql = mysql.createConnection(cfg.mysql); sql = mysql.createConnection(cfg.mysql);
@ -56,29 +44,27 @@ 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) => {
var tmpdest = uuid.v1().split('-')[0]+path.parse(entry).ext; var tmpdest = uuid.v1().split('-')[0];
dl(entry, "./b/"+tmpdest, (cb) => { // download item 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/"+tmpdest, "./b/"+tmpdest+"."+cb.infos.ext,
tmpuser['nick'], tmpuser['nick'],
e.channel.getName(), e.channel.getName(),
e.network, e.network,
Math.floor(new Date() / 1000), Math.floor(new Date() / 1000),
0 0
]).on('result', (result) => { ]).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']+")"); 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) if(cb.type == 1)
e.reply(cb.msg); e.reply(cb.msg);
}
}); });
}); });
} }
@ -86,7 +72,7 @@ bot.on('message', (e) => {
var tmp = getUser(e.user.getNick(), e.network); var tmp = getUser(e.user.getNick(), e.network);
setTimeout(()=>{ e.reply(tmp); }, 1500); setTimeout(()=>{ e.reply(tmp); }, 1500);
} }
else if(orig.match(/^\.del/) && e.channel.getName().toString() == "#f0ck") { else if(orig.match(/^\.del/) && e.channel.getName() == "#f0ck") {
sql.query("truncate table `f0ck`.`items`") sql.query("truncate table `f0ck`.`items`")
.on('end', () => { .on('end', () => {
e.reply("database truncated"); e.reply("database truncated");
@ -94,10 +80,6 @@ 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) => {
@ -109,15 +91,16 @@ var getUser = (u, n) => {
var dl = (url, dest, cb) => { var dl = (url, dest, cb) => {
var request = (url.match(/^https/)?https:http).get(url, (response) => { // type:1=post,type:2=stfu var request = (url.match(/^https/)?https:http).get(url, (response) => { // type:1=post,type:2=stfu
if(allowed.indexOf(response.headers['content-type']) != -1) { if(cfg.allowedMimes.hasOwnProperty(response.headers['content-type'])) {
if(response.headers['content-length'] <= maxFileSize) { console.log(cfg.allowedMimes[response.headers['content-type']]);
if(response.headers['content-length'] <= cfg.maxFileSize) {
checkRepost(url, (cbcr) => { checkRepost(url, (cbcr) => {
if(cbcr) { if(cbcr) {
var file = fs.createWriteStream(dest); var file = fs.createWriteStream(dest+"."+cfg.allowedMimes[response.headers['content-type']]);
response.pipe(file); response.pipe(file);
file.on('finish', () => { file.on('finish', () => {
file.close(); file.close();
cb({'status':true, 'msg':'downloaded '+dest, 'type':1, 'infos':{'mime':response.headers['content-type'], 'size':response.headers['content-length']}}); cb({'status':true, 'msg':'downloaded '+dest, 'type':1, 'infos':{'mime':response.headers['content-type'], 'size':response.headers['content-length'], 'ext':cfg.allowedMimes[response.headers['content-type']]}});
}); });
file.on('error', (err) => { file.on('error', (err) => {
fs.unlink(dest); fs.unlink(dest);
@ -125,18 +108,15 @@ var dl = (url, dest, cb) => {
cb({'status':false, 'msg':err.message, 'type':1}); cb({'status':false, 'msg':err.message, 'type':1});
}); });
} }
else { else
cb({'status':false, 'msg':'repost motherf0cker', 'type':1}); cb({'status':false, 'msg':'repost motherf0cker', 'type':1});
}
}); });
} }
else { else
cb({'status':false, 'msg':'f0ck! your file is too big (~'+formatSize(response.headers['content-length'])+'), max '+formatSize(maxFileSize)+' allowed', 'type':1}); cb({'status':false, 'msg':'f0ck! your file is too big (~'+formatSize(response.headers['content-length'])+'), max '+formatSize(cfg.maxFileSize)+' allowed', 'type':1});
}
} }
else { else
cb({'status':false, 'msg':'f0ck you', 'type':2}); cb({'status':false, 'msg':'f0ck you', 'type':2});
}
}).on('error', (msg) => { }).on('error', (msg) => {
cb({'status':false, 'msg':msg, 'type':2}); cb({'status':false, 'msg':msg, 'type':2});
}); });