f0ckv1/src/trigger/parser.js

209 lines
8.4 KiB
JavaScript
Raw Normal View History

2016-10-11 21:12:06 +00:00
var fs = require('fs-extra');
var uuid = require('uuid');
var path = require('path');
var cloudscraper = require('cloudscraper');
var readChunk = require('read-chunk');
var fileType = require('file-type');
var request = require('request');
var ytdl = require('ytdl-core');
var Readable = require('stream').Readable;
module.exports = (lib) => {
lib.trigger.add({
name: 'parser',
call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi,
level: 0,
active: 1,
func: (e) => {
if(e.channel.getName() == '#f0ck' || e.channel.getName() == '#kbot-dev') {
if(!e.message.match(/\!ignore/)) {
var tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
tmp.forEach((entry,i,a) => {
if(!entry.match(/f0ck\.me/i)) {
getLink(entry, (cb) => {
if(cb.success === true) {
fs.move(cb.file, cb.file + '.' + cb.info.ext, (err) => {
if(!err) {
lib.bot.whois(e.user.getNick(), e.network, (err, cbgu) => {
lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`,`thumb`) values (?,?,?,?,?,?,?,?,?,?,?)", [
entry,
cb.file + '.' + cb.info.ext,
cb.info.mime,
cb.size,
cb.checksum,
cbgu['nick'],
e.channel.getName(),
e.network,
Math.floor(new Date() / 1000),
0,
(cb.info.thumb !== null)?cb.info.thumb:''
]).on('result', (result) => {
lib.generateThumbs();
2016-11-07 01:02:02 +00:00
e.reply(lib.cfg.main.url+"/"+result.insertId+" - "+cb.info.title+" ("+cb.info.mime+", ~"+lib.formatSize(cb.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")");
2016-10-11 21:12:06 +00:00
}).on('error', (msg) => {
e.reply(msg);
});
});
}
});
}
2016-11-02 16:16:06 +00:00
else {
2016-11-02 16:20:52 +00:00
fs.stat('./b/' + cb.file, (err, stat) => {
if(cb.msg !== '')
2016-11-02 16:16:06 +00:00
e.reply(cb.msg);
2016-11-02 17:17:56 +00:00
if(!err && stat.isFile())
fs.unlinkSync('./b/' + cb.file);
2016-11-02 16:20:52 +00:00
});
2016-11-02 16:16:06 +00:00
}
2016-10-11 21:12:06 +00:00
});
}
});
}
}
},
desc: 'muh'
});
var getLink = (url, cb) => {
var yt = /https?:\/\/(www\.)?youtu(\.be\/|be\.com\/)((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi;
var sc = /https?:\/\/(www\.)?(soundcloud\.com|snd\.sc)(\/\S*)(\/\S*)/gi;
lib.checkRepost(url, (cbcr) => {
2016-11-02 16:16:06 +00:00
var tmpdest = uuid.v1().split('-')[0];
2016-10-11 21:12:06 +00:00
if(cbcr === true) {
var dat = fs.createWriteStream('./b/' + tmpdest);
var info;
if(url.match(yt)) { // ytdl
ytdl.getInfo(url, (err, inf) => {
if(!err) {
var title = inf.title;
var iurl = inf.iurl;
try {
ytdl.downloadFromInfo(inf, { filter: (format) => { return format.container === 'webm'; } })
.on('response', (res) => {
2016-11-07 01:02:02 +00:00
if(res.headers['content-length'] > lib.cfg.main.maxFileSize) {
2016-10-11 21:12:06 +00:00
res.destroy();
2016-11-02 16:49:40 +00:00
dat.end();
2016-11-07 01:02:02 +00:00
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(lib.cfg.main.maxFileSize)+' allowed' });
2016-10-11 21:12:06 +00:00
}
else {
info = {
type: 'youtube',
title: title,
mime: 'video/webm',
ext: 'webm',
thumb: iurl
};
}
})
.on('error', (err) => {
2016-11-02 16:49:40 +00:00
dat.end();
cb({ success: false, file: tmpdest, msg: err.message });
2016-10-11 21:12:06 +00:00
})
.pipe(dat);
}
catch(ex) {
2016-11-02 16:49:40 +00:00
dat.end();
2016-11-02 16:16:06 +00:00
cb({ success: false, file: tmpdest, msg: ex.message });
2016-10-11 21:12:06 +00:00
}
}
});
}
else if(url.match(sc)) { // scdl
2016-11-07 01:02:02 +00:00
request('https://api.soundcloud.com/resolve.json?client_id=' + lib.cfg.main.scclientid + '&url=' + url, (err, res, body) => {
2016-10-11 21:12:06 +00:00
if(!err && res.statusCode === 200) {
var data = JSON.parse(body);
2016-11-07 01:02:02 +00:00
request(data.stream_url + ((data.stream_url.indexOf('?') === -1)?'?':'&') + 'client_id=' + lib.cfg.main.scclientid)
2016-10-11 21:12:06 +00:00
.pipe(dat);
info = {
type: 'soundcloud',
title: data.title,
mime: 'audio/mpeg',
ext: 'mp3',
thumb: (data.artwork_url !== null)?data.artwork_url.replace('large.jpg', 't300x300.jpg'):null
};
}
else {
2016-11-02 16:49:40 +00:00
dat.end();
2016-11-02 16:16:06 +00:00
cb({ success: false, file: tmpdest, msg: 'f0ck sc-api' });
2016-10-11 21:12:06 +00:00
}
});
}
else { // various
cloudscraper.request({
method: 'GET',
url: url,
encoding: null,
},
(err, res, data) => {
if(!err) {
var type = res.headers['content-type'];
2016-10-26 13:46:23 +00:00
lib.log('MimeType: '+type);
2016-10-11 21:12:06 +00:00
var length = res.headers['content-length'];
2016-11-07 01:02:02 +00:00
if(lib.cfg.main.allowedMimes.hasOwnProperty(type)) {
if(data.length <= lib.cfg.main.maxFileSize) {
2016-10-11 21:12:06 +00:00
var s = new Readable
s.push(data);
s.push(null);
s.pipe(dat);
info = {
type: 'other',
title: path.parse(url).base,
mime: type,
2016-11-07 01:02:02 +00:00
ext: lib.cfg.main.allowedMimes[type],
2016-10-11 21:12:06 +00:00
thumb: null
};
}
else {
2016-11-02 16:49:40 +00:00
dat.end();
2016-11-07 01:02:02 +00:00
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(data.length)+'), max '+lib.formatSize(lib.cfg.main.maxFileSize)+' allowed' });
2016-10-11 21:12:06 +00:00
}
}
else {
2016-11-02 16:49:40 +00:00
dat.end();
2016-11-02 16:16:06 +00:00
cb({ success: false, file: tmpdest, msg: '' });
2016-10-11 21:12:06 +00:00
}
}
else {
2016-11-02 16:49:40 +00:00
dat.end();
2016-11-02 16:16:06 +00:00
cb({ success: false, file: tmpdest, msg: err });
2016-10-11 21:12:06 +00:00
}
});
}
dat
.on('finish', () => {
var size = dat.bytesWritten;
2016-11-02 16:49:40 +00:00
dat.end();
2016-11-07 01:02:02 +00:00
if(size > lib.cfg.main.maxFileSize)
cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(size)+'), max '+lib.formatSize(lib.cfg.main.maxFileSize)+' allowed' });
2016-10-11 21:12:06 +00:00
else {
2016-11-02 16:39:01 +00:00
fs.stat('./b/' + tmpdest, (err, stat) => {
2016-11-02 17:12:43 +00:00
if(!err && stat.isFile() && stat.size > 300) {
2016-11-02 16:59:40 +00:00
lib.log('Datei '+tmpdest+' existiert');
2016-11-02 16:34:27 +00:00
lib.getCheckSum('./b/' + tmpdest, (cbcs) => {
lib.checkRepostCheckSum(cbcs, (cbcrcs) => {
if(cbcrcs === true) {
var mime = fileType(readChunk.sync('./b/' + tmpdest, 0, 262));
2016-11-07 01:02:02 +00:00
if(lib.cfg.main.allowedMimes.hasOwnProperty(mime.mime) || info.type === 'soundcloud')
2016-11-02 16:34:27 +00:00
cb({ success: true, info: info, size: size, file: './b/' + tmpdest, checksum: cbcs });
else
2016-11-07 01:41:17 +00:00
cb({ success: false, file: tmpdest, msg: 'lol, go f0ck yourself ('+JSON.stringify(mime)+')' });
2016-11-02 16:34:27 +00:00
}
else
2016-11-07 01:02:02 +00:00
cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+lib.cfg.main.url+'/'+cbcrcs });
2016-11-02 16:34:27 +00:00
});
});
}
2016-10-11 21:12:06 +00:00
});
}
})
.on('error', (err) => {
2016-11-02 16:16:06 +00:00
cb({ success: false, file: tmpdest, msg: err });
2016-10-11 21:12:06 +00:00
});
}
else
2016-11-07 01:02:02 +00:00
cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+lib.cfg.main.url+'/'+cbcr });
2016-10-11 21:12:06 +00:00
});
};
2016-08-23 11:23:25 +00:00
};