GDBrowser/api/messages/getMessages.js

46 lines
1.7 KiB
JavaScript
Raw Normal View History

const request = require('request')
const XOR = require('../../classes/XOR.js');
const xor = new XOR();
module.exports = async (app, req, res, api) => {
if (req.body.count) return app.run.countMessages(app, req, res)
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({
accountID: req.body.accountID,
gjp: xor.encrypt(req.body.password, 37526),
page: req.body.page || 0,
getSent: req.query.sent ? 1 : 0
})
2020-11-01 15:29:32 -05:00
request.post(app.endpoint + 'getGJMessages20.php', params, async function (err, resp, body) {
2021-01-11 16:00:21 -05:00
if (err || body == '-1' || body == '-2' || !body) 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()} ago.`)
else app.trackSuccess()
let messages = body.split("|").map(msg => app.parseResponse(msg))
let messageArray = []
messages.forEach(x => {
let msg = {}
msg.id = x[1];
msg.playerID = x[3]
msg.accountID = x[2]
msg.author = x[6]
2019-12-31 18:51:08 -05:00
msg.subject = Buffer.from(x[4], "base64").toString().replace(/^Re: ☆/, "Re: ")
msg.date = x[7] + app.config.timestampSuffix
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)
msg.browserColor = true
}
messageArray.push(msg)
})
return res.status(200).send(messageArray)
})
}