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 ;
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 ( ) ;
2016-10-20 23:06:06 +00:00
e . reply ( cfg . 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 ) => {
2016-11-02 16:34:27 +00:00
if ( err === null )
2016-11-02 16:20:52 +00:00
fs . unlinkSync ( './b/' + cb . file ) ;
if ( cb . msg !== '' )
2016-11-02 16:16:06 +00:00
e . reply ( cb . msg ) ;
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 ) => {
if ( res . headers [ 'content-length' ] > cfg . maxFileSize ) {
res . destroy ( ) ;
dat . close ( ) ;
2016-11-02 16:16:06 +00:00
cb ( { success : false , file : tmpdest , msg : 'f0ck! your file is too big (~' + lib . formatSize ( res . headers [ 'content-length' ] ) + '), max ' + lib . formatSize ( cfg . 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 ) => {
dat . close ( ) ;
2016-11-02 16:16:06 +00:00
cb ( { success : false , file : tmpdest , msg : ex } ) ;
2016-10-11 21:12:06 +00:00
} )
. pipe ( dat ) ;
}
catch ( ex ) {
2016-11-02 16:16:06 +00:00
dat . close ( ) ;
cb ( { success : false , file : tmpdest , msg : ex . message } ) ;
2016-10-11 21:12:06 +00:00
}
}
} ) ;
}
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 ( ) ;
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' ] ;
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 ( ) ;
2016-11-02 16:16:06 +00:00
cb ( { success : false , file : tmpdest , msg : 'f0ck! your file is too big (~' + lib . formatSize ( data . length ) + '), max ' + lib . formatSize ( cfg . maxFileSize ) + ' allowed' } ) ;
2016-10-11 21:12:06 +00:00
}
}
else {
dat . close ( ) ;
2016-11-02 16:16:06 +00:00
cb ( { success : false , file : tmpdest , msg : '' } ) ;
2016-10-11 21:12:06 +00:00
}
}
else {
dat . close ( ) ;
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 ;
dat . close ( ) ;
if ( size > cfg . maxFileSize )
2016-11-02 16:16:06 +00:00
cb ( { success : false , file : tmpdest , msg : 'f0ck! your file is too big (~' + lib . formatSize ( size ) + '), max ' + lib . formatSize ( cfg . maxFileSize ) + ' allowed' } ) ;
2016-10-11 21:12:06 +00:00
else {
2016-11-02 16:34:27 +00:00
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 } ) ;
} ) ;
} ) ;
}
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-02 16:16:06 +00:00
cb ( { success : false , file : tmpdest , msg : 'repost motherf0cker: ' + cfg . url + '/' + cbcr } ) ;
2016-10-11 21:12:06 +00:00
} ) ;
} ;
2016-08-23 11:23:25 +00:00
} ;