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; var cfg = require('../../cfg/main.json'); 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(); e.reply(cfg.url+"/"+result.insertId+" - "+cb.info.title+" ("+cb.info.mime+", ~"+lib.formatSize(cb.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")"); }).on('error', (msg) => { e.reply(msg); }); }); } }); } else { fs.stat('./b/' + cb.file, (err, stat) => { if(err === null) fs.unlinkSync('./b/' + cb.file); if(cb.msg !== '') e.reply(cb.msg); }); } }); } }); } } }, 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) => { var tmpdest = uuid.v1().split('-')[0]; 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) => { if(res.headers['content-length'] > cfg.maxFileSize) { res.destroy(); dat.close(); cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(cfg.maxFileSize)+' allowed' }); } else { info = { type: 'youtube', title: title, mime: 'video/webm', ext: 'webm', thumb: iurl }; } }) .on('error', (err) => { dat.close(); cb({ success: false, file: tmpdest, msg: ex }); }) .pipe(dat); } catch(ex) { dat.close(); cb({ success: false, file: tmpdest, msg: ex.message }); } } }); } else if(url.match(sc)) { // scdl request('https://api.soundcloud.com/resolve.json?client_id=' + cfg.scclientid + '&url=' + url, (err, res, body) => { if(!err && res.statusCode === 200) { var data = JSON.parse(body); request(data.stream_url + ((data.stream_url.indexOf('?') === -1)?'?':'&') + 'client_id=' + cfg.scclientid) .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 { dat.close(); cb({ success: false, file: tmpdest, msg: 'f0ck sc-api' }); } }); } else { // various cloudscraper.request({ method: 'GET', url: url, encoding: null, }, (err, res, data) => { if(!err) { var type = res.headers['content-type']; lib.log('MimeType: '+type); var length = res.headers['content-length']; if(cfg.allowedMimes.hasOwnProperty(type)) { if(data.length <= cfg.maxFileSize) { var s = new Readable s.push(data); s.push(null); s.pipe(dat); info = { type: 'other', title: path.parse(url).base, mime: type, ext: cfg.allowedMimes[type], thumb: null }; } else { dat.close(); cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(data.length)+'), max '+lib.formatSize(cfg.maxFileSize)+' allowed' }); } } else { dat.close(); cb({ success: false, file: tmpdest, msg: '' }); } } else { dat.close(); cb({ success: false, file: tmpdest, msg: err }); } }); } dat .on('finish', () => { var size = dat.bytesWritten; dat.close(); if(size > cfg.maxFileSize) cb({ success: false, file: tmpdest, msg: 'f0ck! your file is too big (~'+lib.formatSize(size)+'), max '+lib.formatSize(cfg.maxFileSize)+' allowed' }); else { fs.stat('./b/' + cb.file, (err, stat) => { if(err === null) { lib.getCheckSum('./b/' + tmpdest, (cbcs) => { lib.checkRepostCheckSum(cbcs, (cbcrcs) => { if(cbcrcs === true) { var mime = fileType(readChunk.sync('./b/' + tmpdest, 0, 262)); if(cfg.allowedMimes.hasOwnProperty(mime.mime) || info.type === 'soundcloud') cb({ success: true, info: info, size: size, file: './b/' + tmpdest, checksum: cbcs }); else cb({ success: false, file: tmpdest, msg: 'lol, go f0ck yourself ('+mime+')' }); } else cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+cfg.url+'/'+cbcrcs }); }); }); } }); } }) .on('error', (err) => { cb({ success: false, file: tmpdest, msg: err }); }); } else cb({ success: false, file: tmpdest, msg: 'repost motherf0cker: '+cfg.url+'/'+cbcr }); }); }; };