From c2292c50210df227417cf16c8e7eaeef33dd40dc Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Thu, 17 Jul 2025 15:17:37 +0200 Subject: Allow sending message by statusbot using common --- modules/statusbot/statusbot.js | 45 +++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'modules/statusbot') diff --git a/modules/statusbot/statusbot.js b/modules/statusbot/statusbot.js index 94894a9..bf365bd 100644 --- a/modules/statusbot/statusbot.js +++ b/modules/statusbot/statusbot.js @@ -279,6 +279,32 @@ function matrixSendMsgLogin(text, cb) { }); } +// Calls cb with http status code +function handleMessage(sender, text, cb) { + if (typeof sender != "string" || typeof text != "string" || sender.indexOf("\n") != -1) { + cb(400); + return; + } + + ratelimit.submit(rlcb => { + ptimeout(5000, + cb2 => matrixSendMsgLogin(`[${sender}] ${text}`, cb2), + (finished, success) => { + if (!finished) { + logFailure(`Timed out on message: [${sender}] ${text}`, () => {}); + cb(504); // gateway timeout + } else if (!success) { + logFailure(`Unsuccessful for message: [${sender}] ${text}`, () => {}); + cb(503); // service unavailable + } else cb(200); + rlcb(); + } + ); + }); +} + +cmn.statusbot._handler = handleMessage; + module.exports = function(app, io, _moddir) { moddir = _moddir; @@ -297,24 +323,7 @@ module.exports = function(app, io, _moddir) { gstate = state; app.post("/statusbot", bodyParser.json(), cmn.authgen(accounts), (req, res) => { - if (typeof req.body.sender != "string" || typeof req.body.text != "string") { - return res.sendStatus(400); - } - ratelimit.submit(rlcb => { - ptimeout(5000, - cb => matrixSendMsgLogin(`[${req.body.sender}] ${req.body.text}`, cb), - (finished, success) => { - if (!finished) { - res.sendStatus(504); // gateway timeout - logFailure(`Timed out on message: [${req.body.sender}] ${req.body.text}`, () => {}); - } else if (!success) { - res.sendStatus(503); // service unavailable - logFailure(`Unsuccessful for message: [${req.body.sender}] ${req.body.text}`, () => {}); - } else res.sendStatus(200); - rlcb(); - } - ); - }); + handleMessage(req.body.sender, req.body.text, status => res.sendStatus(status)); }); }); }; -- cgit v1.2.3-70-g09d2