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 ;
2016-12-30 18:17:39 +00:00
module . exports = ( lib ) => {
2016-10-11 21:12:06 +00:00
lib . trigger . add ( {
name : 'parser' ,
call : /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi ,
level : 0 ,
2016-11-19 06:46:08 +00:00
active : 0 ,
2016-12-30 18:17:39 +00:00
func : ( e , userlevel ) => {
2016-11-25 12:35:45 +00:00
if ( e . channel . getName ( ) === lib . cfg . main . debugchannel || e . message . match ( /(!|-)f0ck/i ) ) {
if ( ! e . message . match ( /(!|-)ignore/ ) ) {
2016-10-11 21:12:06 +00:00
var tmp = e . message . match ( /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi ) ; // get links
tmp . forEach ( ( entry , i , a ) => {
2016-11-27 12:49:49 +00:00
if ( ! entry . match ( /f0ck\.me/i ) && ! entry . match ( /\.onion/i ) ) {
2017-03-15 10:12:51 +00:00
getLink ( entry , ( ( e . message . match ( /(!|-)force/i ) && userlevel >= 100 ) ? true : false ) , userlevel , ( cb ) => {
2016-10-11 21:12:06 +00:00
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-12-22 03:20:26 +00:00
e . reply ( lib . cfg . main . url + "/" + result . insertId + " - " + cb . info . title + " (" + cb . info . mime + ", ~" + lib . formatSize ( cb . size ) + ") by " + 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
} ) ;
}
} ) ;
2016-11-25 12:23:48 +00:00
}
2016-10-11 21:12:06 +00:00
}
} ,
desc : 'muh'
} ) ;
2017-03-15 10:12:51 +00:00
var getLink = ( url , force , userlevel , cb ) => {
2016-10-11 21:12:06 +00:00
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 {
2017-03-15 10:12:51 +00:00
dlformat = { filter : ( format ) => { return format . container === 'webm' ; } } ;
2017-01-11 00:19:49 +00:00
ytdl . downloadFromInfo ( inf , dlformat )
2016-10-11 21:12:06 +00:00
. on ( 'response' , ( res ) => {
2017-03-15 10:12:51 +00:00
if ( ( res . headers [ 'content-length' ] <= lib . cfg . main . maxFileSize ) || force || ( userlevel >= 70 && res . headers [ 'content-length' ] >= ( lib . cfg . main . maxFileSize * 2 ) ) ) {
2016-10-11 21:12:06 +00:00
info = {
type : 'youtube' ,
title : title ,
2017-03-15 10:18:53 +00:00
mime : 'video/webm' ,
2017-03-15 10:12:51 +00:00
ext : 'webm' ,
2016-10-11 21:12:06 +00:00
thumb : iurl
} ;
}
2016-12-30 17:59:01 +00:00
else {
res . destroy ( ) ;
dat . end ( ) ;
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
} )
. 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 ) ) {
2017-03-15 10:12:51 +00:00
if ( ( data . length <= lib . cfg . main . maxFileSize ) || force || ( userlevel >= 70 && res . headers [ 'content-length' ] >= ( lib . cfg . main . maxFileSize * 2 ) ) ) {
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 ( ) ;
2017-03-15 10:12:51 +00:00
if ( ( size <= lib . cfg . main . maxFileSize ) || force || ( userlevel >= 70 && res . headers [ 'content-length' ] >= ( lib . cfg . main . maxFileSize * 2 ) ) ) {
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:50:17 +00:00
info . ext = mime . ext ;
info . mime = mime . mime ;
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:50:17 +00:00
cb ( { success : false , file : tmpdest , msg : 'lol, go f0ck yourself (' + mime . 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
} ) ;
}
2016-12-30 17:59:01 +00:00
else {
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
} )
. 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
} ;