2016-08-11 10:42:51 +02:00
var cfg = require ( '../../cfg.json' ) ;
2016-08-14 00:16:56 +02:00
var fs = require ( 'fs' ) ;
2016-08-14 02:20:26 +02:00
var http = require ( 'http' ) ;
var https = require ( 'https' ) ;
2016-08-11 14:38:07 +02:00
var mysql = require ( 'mysql' ) ;
2016-08-09 17:36:37 +02:00
var bot = require ( 'coffea' ) ( ) ;
2016-08-14 16:37:56 +02:00
var uuid = require ( 'uuid' ) ;
2016-08-15 09:18:09 +02:00
var crypto = require ( 'crypto' ) ;
2016-08-15 13:04:36 +02:00
var path = require ( 'path' ) ;
2016-08-15 17:25:50 +02:00
var exec = require ( 'child_process' ) . exec ;
var neu = true ;
2016-08-11 10:42:51 +02:00
2016-08-14 03:41:31 +02:00
var sql ;
2016-08-14 16:37:56 +02:00
var haDC = ( ) => {
2016-08-14 03:41:31 +02:00
sql = mysql . createConnection ( cfg . mysql ) ;
sql . connect ( ( err ) => {
if ( err ) setTimeout ( haDC , 2000 ) ;
} ) ;
sql . on ( 'error' , ( err ) => {
if ( err . code === 'PROTOCOL_CONNECTION_LOST' ) haDC ( ) ;
} ) ;
2016-08-14 16:37:56 +02:00
} ;
2016-08-14 03:41:31 +02:00
haDC ( ) ;
2016-08-12 14:12:44 +02:00
cfg . server . forEach ( ( e , i , a ) => {
2016-08-11 10:42:51 +02:00
bot . add ( {
"name" : e . name ,
"host" : e . host ,
"port" : e . port ,
"ssl" : e . ssl ,
"ssl_allow_invalid" : e . ssl _allow _invalid ,
"pass" : e . pass ,
"nick" : e . nick ,
"username" : e . username ,
"realname" : e . realname
} ) ;
2016-08-14 16:37:56 +02:00
console . log ( "Server " + e . name + " wurde geladen" ) ;
2016-08-11 10:42:51 +02:00
} ) ;
bot . on ( 'motd' , ( e ) => {
console . log ( "motd von " + e . network + " erhalten" ) ;
2016-08-12 14:12:44 +02:00
bot . write ( 'MODE f0ck +B' , e . network , ( c ) => { } ) ; // Botflag
2016-08-11 10:42:51 +02:00
} ) ;
bot . on ( 'message' , ( e ) => {
var orig = e . message ;
2016-08-14 14:06:18 +02:00
if ( orig . match ( /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi ) ) { // shitpostcatcher
var tmp = orig . match ( /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi ) ; // get links
tmp . forEach ( ( entry , i , a ) => {
2016-08-14 20:08:31 +02:00
var tmpdest = uuid . v1 ( ) . split ( '-' ) [ 0 ] ;
2016-08-14 16:37:56 +02:00
dl ( entry , "./b/" + tmpdest , ( cb ) => { // download item
2016-08-14 14:06:18 +02:00
if ( cb . status === true ) {
var tmpuser = getUser ( e . user . getNick ( ) , e . network ) ;
2016-08-15 09:18:09 +02:00
getCheckSum ( "./b/" + tmpdest + "." + cb . infos . ext , ( cbcs ) => {
checkRepostCheckSum ( cbcs , ( cbcrcs ) => {
if ( cbcrcs ) {
2016-08-15 17:25:50 +02:00
sql . query ( "insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`thumb`,`active`) values (?,?,?,?,?,?,?,?,?,?,?)" , [
2016-08-15 09:18:09 +02:00
entry ,
"./b/" + tmpdest + "." + cb . infos . ext ,
cb . infos . mime ,
cb . infos . size ,
cbcs ,
tmpuser [ 'nick' ] ,
e . channel . getName ( ) ,
e . network ,
Math . floor ( new Date ( ) / 1000 ) ,
2016-08-15 17:25:50 +02:00
'' ,
2016-08-15 09:18:09 +02:00
0
] ) . on ( 'result' , ( result ) => {
2016-08-15 17:25:50 +02:00
neu = true ;
2016-08-15 09:18:09 +02:00
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 ) => {
e . reply ( msg ) ;
} ) ;
}
2016-08-15 13:04:36 +02:00
else {
fs . unlink ( "./b/" + tmpdest + "." + cb . infos . ext ) ; // delete repost
2016-08-15 09:18:09 +02:00
e . reply ( "repost motherf0cker" ) ;
2016-08-15 13:04:36 +02:00
}
2016-08-15 09:18:09 +02:00
} ) ;
2016-08-14 14:06:18 +02:00
} ) ;
}
2016-08-14 20:08:31 +02:00
else
2016-08-14 16:37:56 +02:00
if ( cb . type == 1 )
e . reply ( cb . msg ) ;
2016-08-14 14:06:18 +02:00
} ) ;
} ) ;
}
else if ( orig . match ( /^\.user/ ) ) { // (debug) get userinfos
var tmp = getUser ( e . user . getNick ( ) , e . network ) ;
setTimeout ( ( ) => { e . reply ( tmp ) ; } , 1500 ) ;
}
2016-08-14 00:16:56 +02:00
} ) ;
var getUser = ( u , n ) => {
2016-08-14 01:40:14 +02:00
bot . whois ( u , n , ( fn ) => { } ) ; // send whois
2016-08-14 00:16:56 +02:00
var start = Date . now ( ) ;
2016-08-14 14:06:18 +02:00
while ( Date . now ( ) < start + 1000 ) { } // block script for catch whois
2016-08-14 00:16:56 +02:00
return bot . getUser ( u , n ) ;
} ;
var dl = ( url , dest , cb ) => {
2016-08-14 16:54:37 +02:00
var request = ( url . match ( /^https/ ) ? https : http ) . get ( url , ( response ) => { // type:1=post,type:2=stfu
2016-08-14 21:27:39 +02:00
console . log ( response . headers [ 'content-type' ] ) ;
2016-08-14 20:08:31 +02:00
if ( cfg . allowedMimes . hasOwnProperty ( response . headers [ 'content-type' ] ) ) {
if ( response . headers [ 'content-length' ] <= cfg . maxFileSize ) {
2016-08-14 16:54:37 +02:00
checkRepost ( url , ( cbcr ) => {
if ( cbcr ) {
2016-08-14 20:08:31 +02:00
var file = fs . createWriteStream ( dest + "." + cfg . allowedMimes [ response . headers [ 'content-type' ] ] ) ;
2016-08-14 16:54:37 +02:00
response . pipe ( file ) ;
file . on ( 'finish' , ( ) => {
file . close ( ) ;
2016-08-14 20:08:31 +02:00
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' ] ] } } ) ;
2016-08-14 16:54:37 +02:00
} ) ;
file . on ( 'error' , ( err ) => {
fs . unlink ( dest ) ;
file . close ( ) ;
cb ( { 'status' : false , 'msg' : err . message , 'type' : 1 } ) ;
} ) ;
}
2016-08-14 20:08:31 +02:00
else
2016-08-14 16:54:37 +02:00
cb ( { 'status' : false , 'msg' : 'repost motherf0cker' , 'type' : 1 } ) ;
} ) ;
2016-08-14 02:57:19 +02:00
}
2016-08-14 20:08:31 +02:00
else
cb ( { 'status' : false , 'msg' : 'f0ck! your file is too big (~' + formatSize ( response . headers [ 'content-length' ] ) + '), max ' + formatSize ( cfg . maxFileSize ) + ' allowed' , 'type' : 1 } ) ;
2016-08-14 02:15:53 +02:00
}
2016-08-14 20:08:31 +02:00
else
2016-08-14 16:54:37 +02:00
cb ( { 'status' : false , 'msg' : 'f0ck you' , 'type' : 2 } ) ;
} ) . on ( 'error' , ( msg ) => {
cb ( { 'status' : false , 'msg' : msg , 'type' : 2 } ) ;
2016-08-14 02:15:53 +02:00
} ) ;
2016-08-14 03:41:31 +02:00
} ;
2016-08-14 04:06:01 +02:00
var checkRepost = ( url , cbcr ) => {
sql . query ( "select count(*) as count from `f0ck`.`items` where `src` = ?" , url , ( err , rows , fields ) => {
cbcr ( ( rows [ 0 ] . count == 0 ) ? true : false ) ;
} ) ;
2016-08-14 16:37:56 +02:00
} ;
2016-08-15 09:18:09 +02:00
var checkRepostCheckSum = ( cs , cbcrcs ) => {
sql . query ( "select count(*) as count from `f0ck`.`items` where `checksum` = ?" , cs , ( err , rows , fields ) => {
cbcrcs ( ( rows [ 0 ] . count == 0 ) ? true : false ) ;
} ) ;
} ;
2016-08-14 16:37:56 +02:00
var formatSize = ( size ) => {
var i = Math . floor ( Math . log ( size ) / Math . log ( 1024 ) ) ;
return ( size / Math . pow ( 1024 , i ) ) . toFixed ( 2 ) * 1 + ' ' + [ 'B' , 'kB' , 'MB' , 'GB' , 'TB' ] [ i ] ;
2016-08-14 20:48:34 +02:00
} ;
2016-08-15 09:18:09 +02:00
var getCheckSum = ( file , cbcs ) => {
var sha256sum = crypto . createHash ( 'sha256' ) ;
var s = fs . ReadStream ( file ) ;
s . on ( 'data' , ( d ) => {
sha256sum . update ( d ) ;
} ) ;
s . on ( 'end' , ( ) => {
var generated _hash = sha256sum . digest ( 'hex' ) ;
cbcs ( generated _hash ) ;
} ) ;
} ;
2016-08-14 20:48:34 +02:00
// Webserver
2016-08-15 13:04:36 +02:00
http . createServer ( ( req , res ) => {
var filePath = '.' + req . url ;
var url = req . url . split ( "/" ) [ 1 ] ;
if ( filePath == './' )
filePath = './index.html' ;
console . log ( 'request ' , filePath ) ;
var extname = String ( path . extname ( filePath ) ) . toLowerCase ( ) ;
var contentType = 'text/html' ;
var mimeTypes = {
'.html' : 'text/html' ,
'.js' : 'text/javascript' ,
'.css' : 'text/css' ,
'.png' : 'image/png' ,
'.jpg' : 'image/jpg' ,
'.gif' : 'image/gif' ,
'.mp3' : 'audio/mpeg' ,
'.mp4' : 'video/mp4' ,
2016-08-15 17:25:50 +02:00
'.webm' : 'video/webm' ,
'.css' : 'text/css'
2016-08-15 13:04:36 +02:00
} ;
if ( filePath == "./index.html" ) { // mainpage
2016-08-14 20:48:34 +02:00
sql . query ( "select * from `f0ck`.`items`" , ( err , rows , fields ) => {
2016-08-15 17:25:50 +02:00
var tmpres = "<!DOCTYPE blah><html><head><title>f0ck me!</title><link rel=\"stylesheet\" type=\"text/css\" href=\"./s/style.css\"></head><body>" ;
2016-08-14 20:48:34 +02:00
rows . forEach ( ( e , i , a ) => {
2016-08-15 17:25:50 +02:00
tmpres += "<a href='./" + e . id + "'><img src='./t/" + e . id + ".png' /></a>\n" ;
2016-08-14 20:48:34 +02:00
} ) ;
2016-08-15 17:25:50 +02:00
tmpres += "</body></html>" ;
res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
2016-08-15 13:04:36 +02:00
res . end ( tmpres , 'utf-8' ) ;
2016-08-14 20:48:34 +02:00
} ) ;
}
2016-08-15 13:04:36 +02:00
else if ( Number . isInteger ( parseInt ( url ) ) ) { // itempage
2016-08-15 11:30:27 +02:00
sql . query ( "select * from `f0ck`.`items` where `id` = ? limit 1" , url , ( err , rows , fields ) => {
2016-08-15 13:04:36 +02:00
var tmpres = "" ;
2016-08-15 11:30:27 +02:00
rows . forEach ( ( e , i , a ) => {
2016-08-15 13:04:36 +02:00
tmpres += "ID: " + e . id + "<br />\n" ;
2016-08-15 17:25:50 +02:00
tmpres += "src: " + e . src + "<br />\n" ;
2016-08-15 13:04:36 +02:00
tmpres += "dest: " + e . dest + "<br />\n" ;
tmpres += "mime: " + e . mime + "<br />\n" ;
2016-08-15 17:25:50 +02:00
tmpres += "size: " + formatSize ( e . size ) + "<br />\n" ;
2016-08-15 13:04:36 +02:00
tmpres += "nick: " + e . username + "<br />\n" ;
tmpres += "channel: " + e . userchannel + "<br />\n" ;
tmpres += "network: " + e . usernetwork + "<br />\n" ;
switch ( e . mime ) {
case "image/png" :
case "image/jpeg" :
case "image/gif" :
tmpres += "<img src='" + e . dest + "' />" ;
break ;
case "video/webm" :
case "video/mp4" :
tmpres += "<video src='" + e . dest + "' autoplay controls loop></video>" ;
break ;
case "audio/mpeg" :
tmpres += "<audio controls src='" + e . dest + "' type='audio/mp3' autoplay></audio>" ;
break ;
}
2016-08-15 11:30:27 +02:00
} ) ;
2016-08-15 13:04:36 +02:00
res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
res . end ( tmpres , 'utf-8' ) ;
} ) ;
}
2016-08-15 17:25:50 +02:00
else if ( filePath . match ( /^\.\/(b|s|t)\/.*/ ) ) { // file
2016-08-15 13:04:36 +02:00
contentType = mimeTypes [ extname ] || 'application/octect-stream' ;
fs . readFile ( filePath , ( error , content ) => {
if ( error ) {
if ( error . code == 'ENOENT' ) {
res . writeHead ( 200 , { 'Content-Type' : contentType } ) ;
res . end ( '404 - file not found' , 'utf-8' ) ;
}
else {
res . writeHead ( 500 ) ;
res . end ( 'Sorry, check with the site admin for error: ' + error . code + ' ..\n' ) ;
res . end ( ) ;
}
}
else {
res . writeHead ( 200 , { 'Content-Type' : contentType , 'Content-Length' : content . length } ) ;
res . end ( content , 'utf-8' ) ;
}
2016-08-15 11:30:27 +02:00
} ) ;
}
2016-08-15 13:04:36 +02:00
else { // errorpage
res . writeHead ( 404 ) ;
res . end ( '404 - not found' , 'utf-8' ) ;
2016-08-14 20:48:34 +02:00
}
2016-08-15 17:25:50 +02:00
} ) . listen ( cfg . webserver . port ) ;
// Thumbnailbackgroundworker
setInterval ( ( ) => { generateThumbs ( ) ; } , 300000 ) ; // 5 minutes
setTimeout ( ( ) => { generateThumbs ( ) ; } , 5000 ) ; // 5 seconds (start)
var generateThumbs = ( ) => {
var outdir = './t/' ;
if ( neu ) {
sql . query ( "select * from `f0ck`.`items` where `thumb` = ''" , ( err , rows , fields ) => {
rows . forEach ( ( e , i , a ) => {
if ( ! fs . existsSync ( outdir + e . id + '.png' ) ) {
exec ( 'ffmpegthumbnailer -i' + e . dest + ' -o' + outdir + e . id + '.png -a' , ( error ) => {
if ( error ) {
//console.log(error);
//bot.send("#f0ck", "failed thumbnail for "+e.id, 'n0xy');
}
else {
//bot.send("#f0ck", "generated thumbnail for "+e.id, 'n0xy');
}
} ) ;
}
} ) ;
} ) ;
}
} ;