From 3817b190a6ed7ead13075763f1ffd6578ef8d71c Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Sat, 16 Jan 2021 20:53:46 +0800 Subject: [PATCH] forwarder: implement matrix -> irc --- forwarder.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/forwarder.js b/forwarder.js index 16c9a26..5b975b4 100644 --- a/forwarder.js +++ b/forwarder.js @@ -19,6 +19,7 @@ module.exports = class Forwarder { console.log("Starting relay"); this.clientIRC.on("registered", this.onReconnect.bind(this)); this.clientIRC.on("message", this.onIRCMessage.bind(this)); + this.clientMatrix.on("Room.timeline", this.onMatrixMessage.bind(this)); } onReconnect() { @@ -52,4 +53,46 @@ module.exports = class Forwarder { this.clientMatrix.sendMessage(this.mappingI2M[event.target], content); } + + onMatrixMessage(event, room, toStartOfTimeline) { + if (toStartOfTimeline) { + return; // Ignore pagniation + } + + if (!this.mappingM2I[room.roomId]) { + return; // Unmapped room + } + + if (event.sender.userId == this.clientMatrix.getUserId()) { + return; // Prevent loop + } + + let content = event.getContent(); + console.log(content); + let msgTxt = null; + switch (event.getType()) { + case "m.sticker": + msgTxt = `${content.body} ${this.clientMatrix.mxcUrlToHttp(content.url)}`; + break; + case "m.room.message": + switch (content.msgtype) { + case "m.image": + msgTxt = `${content.body} ${this.clientMatrix.mxcUrlToHttp(content.url)}`; + break; + default: + msgTxt = content.body; + break; + } + break; + } + + if (msgTxt != null) { + if (content.msgtype == "m.emote") { + // Special format for emote + this.clientIRC.say(this.mappingM2I[room.roomId], `* ${event.sender.name} ${msgTxt}`); + } else { + this.clientIRC.say(this.mappingM2I[room.roomId], `[${event.sender.name}] ${msgTxt}`); + } + } + } } \ No newline at end of file