forwarder: collapse long messages to pastebin url

master
Peter Cai 2 years ago
parent eba5401fef
commit 856d7c511a
  1. 34
      forwarder.js
  2. 13
      package-lock.json
  3. 1
      package.json

@ -1,4 +1,5 @@
var NickMap = require("./nickmap");
var axios = require("axios");
module.exports = class Forwarder {
constructor(clientIRC, clientMatrix, mappingI2M, mappingM2I) {
@ -80,6 +81,30 @@ module.exports = class Forwarder {
return msg.startsWith("!");
}
shouldUsePastebin(txt) {
return txt.length >= 160 || txt.split("\n").length > 3;
}
async pastebin(txt) {
let resp = await axios.request({
url: "https://fars.ee/",
method: "post",
headers: {
"Content-Type": "application/json"
},
data: {
content: txt,
filename: "message.txt"
}
});
if (resp.status != 200) {
throw resp.statusText;
} else {
return resp.data.url;
}
}
handleMatrixCommand(sender, msg) {
let splitted = msg.split(" ");
let cmd = splitted[0].slice(1);
@ -187,6 +212,15 @@ module.exports = class Forwarder {
}
if (msgTxt != null) {
if (this.shouldUsePastebin(msgTxt)) {
try {
msgTxt = "Long Msg: " + await this.pastebin(msgTxt);
} catch (err) {
console.log(err);
return;
}
}
let name = this.processMatrixName(event.sender.userId, event.sender.name);
if (content.msgtype == "m.emote") {
// Special format for emote

13
package-lock.json generated

@ -56,6 +56,14 @@
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.11.0.tgz",
"integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA=="
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"base-x": {
"version": "3.0.8",
"resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.8.tgz",
@ -165,6 +173,11 @@
"resolved": "https://registry.npmjs.org/fast-text-encoding/-/fast-text-encoding-1.0.3.tgz",
"integrity": "sha512-dtm4QZH9nZtcDt8qJiOH9fcQd1NAgi+K1O2DbE6GG1PPCK/BWfOH3idCTRQ4ImXRUOyopDEgDEnVEE7Y/2Wrig=="
},
"follow-redirects": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
},
"forever-agent": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",

@ -9,6 +9,7 @@
"author": "PeterCxy",
"license": "WTFPL",
"dependencies": {
"axios": "^0.21.1",
"irc-framework": "^4.9.0",
"matrix-js-sdk": "^9.4.1"
}

Loading…
Cancel
Save