2021-12-07 11:06:33 -08:00
module . exports = async ( app , req , res ) => {
if ( req . method !== 'POST' ) return res . status ( 405 ) . send ( "Method not allowed." )
2019-12-25 16:20:32 -05:00
2019-12-29 18:59:29 -05:00
if ( req . body . count ) return app . run . countMessages ( app , req , res )
2019-12-25 16:20:32 -05:00
if ( ! req . body . accountID ) return res . status ( 400 ) . send ( "No account ID provided!" )
if ( ! req . body . password ) return res . status ( 400 ) . send ( "No password provided!" )
2020-11-01 15:29:32 -05:00
let params = req . gdParams ( {
2019-12-25 16:20:32 -05:00
accountID : req . body . accountID ,
2021-01-18 21:54:18 -05:00
gjp : app . xor . encrypt ( req . body . password , 37526 ) ,
2019-12-25 16:20:32 -05:00
page : req . body . page || 0 ,
2019-12-29 18:59:29 -05:00
getSent : req . query . sent ? 1 : 0
2020-09-29 21:42:18 -04:00
} )
2019-12-25 16:20:32 -05:00
2021-01-18 21:54:18 -05:00
req . gdRequest ( 'getGJMessages20' , params , function ( err , resp , body ) {
2019-12-25 16:20:32 -05:00
2021-08-18 20:10:35 -04:00
if ( err ) return res . status ( 400 ) . send ( ` Error fetching messages! Messages get blocked a lot so try again later, or make sure your username and password are entered correctly. Last worked: ${ app . timeSince ( req . id ) } ago. ` )
2021-01-18 21:54:18 -05:00
else app . trackSuccess ( req . id )
2019-12-25 16:20:32 -05:00
let messages = body . split ( "|" ) . map ( msg => app . parseResponse ( msg ) )
let messageArray = [ ]
messages . forEach ( x => {
let msg = { }
msg . id = x [ 1 ] ;
2019-12-29 18:59:29 -05:00
msg . playerID = x [ 3 ]
msg . accountID = x [ 2 ]
2019-12-25 16:20:32 -05:00
msg . author = x [ 6 ]
2019-12-31 18:51:08 -05:00
msg . subject = Buffer . from ( x [ 4 ] , "base64" ) . toString ( ) . replace ( /^Re: ☆/ , "Re: " )
2021-01-19 00:56:21 -05:00
msg . date = x [ 7 ] + req . timestampSuffix
2019-12-25 16:20:32 -05:00
msg . unread = x [ 8 ] != "1"
2019-12-31 18:51:08 -05:00
if ( msg . subject . endsWith ( "☆" ) || msg . subject . startsWith ( "☆" ) ) {
if ( msg . subject . endsWith ( "☆" ) ) msg . subject = msg . subject . slice ( 0 , - 1 )
else msg . subject = msg . subject . slice ( 1 )
2019-12-29 18:59:29 -05:00
msg . browserColor = true
}
2019-12-25 16:20:32 -05:00
2021-01-21 17:15:31 -05:00
app . userCache ( req . id , msg . accountID , msg . playerID , msg . author )
2019-12-25 16:20:32 -05:00
messageArray . push ( msg )
} )
return res . status ( 200 ) . send ( messageArray )
} )
}