forwarder: handle reconnect gracefully

This commit is contained in:
Peter Cai 2021-01-16 20:23:42 +08:00
parent d1b3f5057a
commit acea0007ae
2 changed files with 15 additions and 8 deletions

View File

@ -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

View File

@ -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) => {