From acea0007ae3c563e5a09e113557ae80b7de6444a Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Sat, 16 Jan 2021 20:23:42 +0800 Subject: [PATCH] forwarder: handle reconnect gracefully --- forwarder.js | 15 +++++++++++++++ index.js | 8 -------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/forwarder.js b/forwarder.js index c99ca1c..16c9a26 100644 --- a/forwarder.js +++ b/forwarder.js @@ -6,11 +6,26 @@ module.exports = class Forwarder { this.mappingM2I = mappingM2I; } + joinIRCRooms() { + console.log("Joining IRC Rooms..."); + for (const room of Object.keys(this.mappingI2M)) { + this.clientIRC.say("ChanServ", "INVITE " + room); + this.clientIRC.join(room); + } + } + start() { + this.joinIRCRooms(); console.log("Starting relay"); + this.clientIRC.on("registered", this.onReconnect.bind(this)); this.clientIRC.on("message", this.onIRCMessage.bind(this)); } + onReconnect() { + console.log("IRC client reconnected"); + this.joinIRCRooms(); + } + onIRCMessage(event) { if (!event.target) { // Not something we care about diff --git a/index.js b/index.js index 20baa65..c3c1f22 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,6 @@ function main() { console.log("Logging into IRC..."); connectIrc(config, (clientIRC) => { console.log("Successfully logged into IRC!"); - joinIRCRooms(clientIRC, Object.keys(mappingI2M)); console.log("Logging into matrix..."); connectMatrix(config, (clientMatrix) => { console.log("Successfully logged into Matrix!"); @@ -33,13 +32,6 @@ function connectIrc(config, callback) { client.once('registered', () => { callback(client) }); } -function joinIRCRooms(client, rooms) { - for (const room of rooms) { - client.say("ChanServ", "INVITE " + room); - client.join(room); - } -} - function connectMatrix(config, callback) { let client = matrix.createClient(config.matrix); let myCallback = (state) => {