summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2025-07-17 15:17:37 +0200
committerTom Smeding <tom@tomsmeding.com>2025-07-17 15:17:37 +0200
commitc2292c50210df227417cf16c8e7eaeef33dd40dc (patch)
tree3d5a8cbf22efdf6ec3c988a98baf5efc9119261e
parent52ec17571d6b8792019f0e98d545eed9fb6881f9 (diff)
Allow sending message by statusbot using common
-rw-r--r--modules/$common.js17
-rw-r--r--modules/statusbot/statusbot.js45
2 files changed, 44 insertions, 18 deletions
diff --git a/modules/$common.js b/modules/$common.js
index 59b9a66..f7ec42a 100644
--- a/modules/$common.js
+++ b/modules/$common.js
@@ -4,13 +4,28 @@ var path=require("path"),
var cwd=process.cwd();
var globalAccounts=require(cwd+"/globalAccounts.json");
+var statusbotObject={
+ // calls cb with http status code
+ "send":function statusbotSend(sender,text,cb){
+ if(statusbotObject._handler==null){
+ console.log("[common.statusbot] No statusbot yet");
+ cb(500);
+ return;
+ }
+ statusbotObject._handler(sender,text,cb);
+ },
+ "_handler":null, // to be set from the statusbot module
+};
+
module.exports={
"serverdir":cwd,
"webfilesdir":cwd+"/web_files",
"persistdir":cwd+"/persist",
+
"simpleHTMLescape":function simpleHTMLescape(str){
return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
},
+
"authgen":function authgen(accounts){ //omit `accounts` to use the globalAccounts list
if(!accounts)accounts=globalAccounts;
return function (req,res,next){
@@ -32,4 +47,6 @@ module.exports={
return unauth(res);
};
},
+
+ "statusbot":statusbotObject,
};
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));
});
});
};