From fade6a8d2ec5d621d761e2f6a716c5e59f4a9770 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Mon, 27 Feb 2023 18:30:33 +0100 Subject: [PATCH] network: add getLobby accessor This documents what we actually want and allows us to shift the logic to the network --- server/client.ts | 4 ++-- server/models/network.ts | 10 +++++++--- server/plugins/irc-events/away.ts | 2 +- server/plugins/irc-events/cap.ts | 2 +- server/plugins/irc-events/connection.ts | 20 ++++++++++---------- server/plugins/irc-events/ctcp.ts | 2 +- server/plugins/irc-events/error.ts | 6 +++--- server/plugins/irc-events/help.ts | 2 +- server/plugins/irc-events/info.ts | 2 +- server/plugins/irc-events/invite.ts | 2 +- server/plugins/irc-events/message.ts | 4 ++-- server/plugins/irc-events/mode.ts | 4 ++-- server/plugins/irc-events/modelist.ts | 2 +- server/plugins/irc-events/motd.ts | 2 +- server/plugins/irc-events/nick.ts | 2 +- server/plugins/irc-events/sasl.ts | 4 ++-- server/plugins/irc-events/unhandled.ts | 2 +- server/plugins/irc-events/welcome.ts | 2 +- server/plugins/irc-events/whois.ts | 2 +- 19 files changed, 40 insertions(+), 36 deletions(-) diff --git a/server/client.ts b/server/client.ts index 826bef13..66a408c0 100644 --- a/server/client.ts +++ b/server/client.ts @@ -321,7 +321,7 @@ class Client { }); // Set network lobby channel id - network.channels[0].id = lobbyChannelId; + network.getLobby().id = lobbyChannelId; client.networks.push(network); client.emit("network", { @@ -344,7 +344,7 @@ class Client { }); if (network.userDisconnected) { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: "You have manually disconnected from this network before, use the /connect command to connect again.", diff --git a/server/models/network.ts b/server/models/network.ts index 188fa9fe..a47757f5 100644 --- a/server/models/network.ts +++ b/server/models/network.ts @@ -208,7 +208,7 @@ class Network { this.proxyEnabled = !!this.proxyEnabled; const error = function (network: Network, text: string) { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ type: MessageType.ERROR, @@ -241,7 +241,7 @@ class Network { if (Config.values.public) { this.name = Config.values.defaults.name; // Sync lobby channel name - this.channels[0].name = Config.values.defaults.name; + this.getLobby().name = Config.values.defaults.name; } this.host = Config.values.defaults.host; @@ -401,7 +401,7 @@ class Network { .filter((command) => command.length > 0); // Sync lobby channel name - this.channels[0].name = this.name; + this.getLobby().name = this.name; if (this.name !== oldNetworkName) { // Send updated network name to all connected clients @@ -651,6 +651,10 @@ class Network { return i > 0 && that.name.toLowerCase() === name; }); } + + getLobby() { + return this.channels[0]; + } } export default Network; diff --git a/server/plugins/irc-events/away.ts b/server/plugins/irc-events/away.ts index 5709101d..7256103f 100644 --- a/server/plugins/irc-events/away.ts +++ b/server/plugins/irc-events/away.ts @@ -19,7 +19,7 @@ export default function (irc, network) { time: data.time, }); - network.channels[0].pushMessage(client, msg, true); + network.getLobby().pushMessage(client, msg, true); return; } diff --git a/server/plugins/irc-events/cap.ts b/server/plugins/irc-events/cap.ts index d98dedce..cb4c6864 100644 --- a/server/plugins/irc-events/cap.ts +++ b/server/plugins/irc-events/cap.ts @@ -42,7 +42,7 @@ export default function (irc, network) { return; } - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: `Server sent a strict transport security policy, reconnecting to ${network.host}:${port}…`, diff --git a/server/plugins/irc-events/connection.ts b/server/plugins/irc-events/connection.ts index dcb3be77..300db36b 100644 --- a/server/plugins/irc-events/connection.ts +++ b/server/plugins/irc-events/connection.ts @@ -11,7 +11,7 @@ import {ChanType, ChanState} from "../../models/chan"; export default function (irc, network) { const client = this; - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: "Network created, connecting to " + network.host + ":" + network.port + "...", @@ -21,7 +21,7 @@ export default function (irc, network) { irc.on("registered", function () { if (network.irc.network.cap.enabled.length > 0) { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: "Enabled capabilities: " + network.irc.network.cap.enabled.join(", "), @@ -44,7 +44,7 @@ export default function (irc, network) { network.commands.forEach((cmd) => { setTimeout(function () { client.input({ - target: network.channels[0].id, + target: network.getLobby().id, text: cmd, }); }, delay); @@ -69,7 +69,7 @@ export default function (irc, network) { network.serverOptions.PREFIX.update(irc.network.options.PREFIX); } - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: "Connected to the network.", @@ -81,7 +81,7 @@ export default function (irc, network) { }); irc.on("close", function () { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: "Disconnected from the network, and will not reconnect. Use /connect to reconnect again.", @@ -114,7 +114,7 @@ export default function (irc, network) { }); if (error) { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ type: MessageType.ERROR, @@ -151,7 +151,7 @@ export default function (irc, network) { if (Config.values.debug.raw) { irc.on("raw", function (message) { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ self: !message.from_server, @@ -164,7 +164,7 @@ export default function (irc, network) { } irc.on("socket error", function (err) { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ type: MessageType.ERROR, @@ -175,7 +175,7 @@ export default function (irc, network) { }); irc.on("reconnecting", function (data) { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: `Disconnected from the network. Reconnecting in ${Math.round( @@ -187,7 +187,7 @@ export default function (irc, network) { }); irc.on("ping timeout", function () { - network.channels[0].pushMessage( + network.getLobby().pushMessage( client, new Msg({ text: "Ping timeout, disconnecting…", diff --git a/server/plugins/irc-events/ctcp.ts b/server/plugins/irc-events/ctcp.ts index 2e6e3508..db5e1806 100644 --- a/server/plugins/irc-events/ctcp.ts +++ b/server/plugins/irc-events/ctcp.ts @@ -17,7 +17,7 @@ const ctcpResponses = { export default function (irc, network) { const client = this; - const lobby = network.channels[0]; + const lobby = network.getLobby(); irc.on("ctcp response", function (data) { const shouldIgnore = network.ignoreList.some(function (entry) { diff --git a/server/plugins/irc-events/error.ts b/server/plugins/irc-events/error.ts index 58ef9ead..b6890db0 100644 --- a/server/plugins/irc-events/error.ts +++ b/server/plugins/irc-events/error.ts @@ -17,7 +17,7 @@ export default function (irc, network) { command: data.command, }); - let target = network.channels[0]; + let target = network.getLobby(); // If this error is channel specific and a channel // with this name exists, put this error in that channel @@ -46,7 +46,7 @@ export default function (irc, network) { network.keepNick = irc.user.nick; } - const lobby = network.channels[0]; + const lobby = network.getLobby(); const msg = new Msg({ type: MessageType.ERROR, text: message, @@ -74,7 +74,7 @@ export default function (irc, network) { }); irc.on("nick invalid", function (data) { - const lobby = network.channels[0]; + const lobby = network.getLobby(); const msg = new Msg({ type: MessageType.ERROR, text: data.nick + ": " + (data.reason || "Nickname is invalid."), diff --git a/server/plugins/irc-events/help.ts b/server/plugins/irc-events/help.ts index 2f0b790c..9bcaa3f6 100644 --- a/server/plugins/irc-events/help.ts +++ b/server/plugins/irc-events/help.ts @@ -5,7 +5,7 @@ export default function (irc, network) { const client = this; irc.on("help", function (data) { - const lobby = network.channels[0]; + const lobby = network.getLobby(); if (data.help) { const msg = new Msg({ diff --git a/server/plugins/irc-events/info.ts b/server/plugins/irc-events/info.ts index 9bfa47e8..8e0f3359 100644 --- a/server/plugins/irc-events/info.ts +++ b/server/plugins/irc-events/info.ts @@ -5,7 +5,7 @@ export default function (irc, network) { const client = this; irc.on("info", function (data) { - const lobby = network.channels[0]; + const lobby = network.getLobby(); if (data.info) { const msg = new Msg({ diff --git a/server/plugins/irc-events/invite.ts b/server/plugins/irc-events/invite.ts index 64e04793..f6dc6524 100644 --- a/server/plugins/irc-events/invite.ts +++ b/server/plugins/irc-events/invite.ts @@ -9,7 +9,7 @@ export default function (irc, network) { let chan = network.getChannel(data.channel); if (typeof chan === "undefined") { - chan = network.channels[0]; + chan = network.getLobby(); } const invitedYou = data.invited === irc.user.nick; diff --git a/server/plugins/irc-events/message.ts b/server/plugins/irc-events/message.ts index 682d3b8a..393f247d 100644 --- a/server/plugins/irc-events/message.ts +++ b/server/plugins/irc-events/message.ts @@ -75,7 +75,7 @@ export default function (irc, network) { !network.getChannel(data.target) || network.getChannel(data.target)?.type !== ChanType.CHANNEL) ) { - chan = network.channels[0]; + chan = network.getLobby(); from = chan.getUser(data.nick); } else { if (shouldIgnore) { @@ -95,7 +95,7 @@ export default function (irc, network) { // Send notices that are not targeted at us into the server window if (data.type === MessageType.NOTICE) { showInActive = true; - chan = network.channels[0]; + chan = network.getLobby(); } else { chan = client.createChannel({ type: ChanType.QUERY, diff --git a/server/plugins/irc-events/mode.ts b/server/plugins/irc-events/mode.ts index ec5bf558..b1f65845 100644 --- a/server/plugins/irc-events/mode.ts +++ b/server/plugins/irc-events/mode.ts @@ -41,7 +41,7 @@ export default function (irc, network) { }); irc.on("user info", function (data) { - const serverChan = network.channels[0]; + const serverChan = network.getLobby(); const msg = new Msg({ type: MessageType.MODE_USER, @@ -56,7 +56,7 @@ export default function (irc, network) { let targetChan; if (data.target === irc.user.nick) { - targetChan = network.channels[0]; + targetChan = network.getLobby(); } else { targetChan = network.getChannel(data.target); diff --git a/server/plugins/irc-events/modelist.ts b/server/plugins/irc-events/modelist.ts index 7de437b4..2c429bf5 100644 --- a/server/plugins/irc-events/modelist.ts +++ b/server/plugins/irc-events/modelist.ts @@ -47,7 +47,7 @@ export default function (irc, network) { // Send error to lobby if we receive empty list for a channel we're not in if (typeof chan === "undefined") { msg.showInActive = true; - chan = network.channels[0]; + chan = network.getLobby(); } chan.pushMessage(client, msg, true); diff --git a/server/plugins/irc-events/motd.ts b/server/plugins/irc-events/motd.ts index 2f138975..cffce33b 100644 --- a/server/plugins/irc-events/motd.ts +++ b/server/plugins/irc-events/motd.ts @@ -6,7 +6,7 @@ export default function (irc, network) { const client = this; irc.on("motd", function (data) { - const lobby = network.channels[0]; + const lobby = network.getLobby(); if (data.motd) { const msg = new Msg({ diff --git a/server/plugins/irc-events/nick.ts b/server/plugins/irc-events/nick.ts index 34e89f5a..51b3f81f 100644 --- a/server/plugins/irc-events/nick.ts +++ b/server/plugins/irc-events/nick.ts @@ -11,7 +11,7 @@ export default function (irc, network) { if (self) { network.setNick(data.new_nick); - const lobby = network.channels[0]; + const lobby = network.getLobby(); const msg = new Msg({ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions text: `You're now known as ${data.new_nick}`, diff --git a/server/plugins/irc-events/sasl.ts b/server/plugins/irc-events/sasl.ts index 132d13a6..b486cace 100644 --- a/server/plugins/irc-events/sasl.ts +++ b/server/plugins/irc-events/sasl.ts @@ -6,7 +6,7 @@ export default function (irc, network) { const client = this; irc.on("loggedin", (data) => { - const lobby = network.channels[0]; + const lobby = network.getLobby(); const msg = new Msg({ type: MessageType.LOGIN, @@ -17,7 +17,7 @@ export default function (irc, network) { }); irc.on("loggedout", () => { - const lobby = network.channels[0]; + const lobby = network.getLobby(); const msg = new Msg({ type: MessageType.LOGOUT, diff --git a/server/plugins/irc-events/unhandled.ts b/server/plugins/irc-events/unhandled.ts index da7d7634..6a4997f4 100644 --- a/server/plugins/irc-events/unhandled.ts +++ b/server/plugins/irc-events/unhandled.ts @@ -6,7 +6,7 @@ export default function (irc, network) { const client = this; irc.on("unknown command", function (command) { - let target = network.channels[0]; + let target = network.getLobby(); // Do not display users own name if (command.params.length > 0 && command.params[0] === network.irc.user.nick) { diff --git a/server/plugins/irc-events/welcome.ts b/server/plugins/irc-events/welcome.ts index 279fe4cf..039fac08 100644 --- a/server/plugins/irc-events/welcome.ts +++ b/server/plugins/irc-events/welcome.ts @@ -8,7 +8,7 @@ export default function (irc, network) { irc.on("registered", function (data) { network.setNick(data.nick); - const lobby = network.channels[0]; + const lobby = network.getLobby(); const msg = new Msg({ text: "You're now known as " + data.nick, }); diff --git a/server/plugins/irc-events/whois.ts b/server/plugins/irc-events/whois.ts index 9c62e7fb..67fdf3ff 100644 --- a/server/plugins/irc-events/whois.ts +++ b/server/plugins/irc-events/whois.ts @@ -20,7 +20,7 @@ export default function (irc, network) { if (typeof chan === "undefined") { // Do not create new windows for errors as they may contain illegal characters if (data.error) { - chan = network.channels[0]; + chan = network.getLobby(); } else { chan = client.createChannel({ type: ChanType.QUERY,