network: add getLobby accessor

This documents what we actually want and allows us to shift the
logic to the network
This commit is contained in:
Reto Brunner 2023-02-27 18:30:33 +01:00
parent c2e7390127
commit fade6a8d2e
19 changed files with 40 additions and 36 deletions

View file

@ -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.",

View file

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

View file

@ -19,7 +19,7 @@ export default <IrcEventHandler>function (irc, network) {
time: data.time,
});
network.channels[0].pushMessage(client, msg, true);
network.getLobby().pushMessage(client, msg, true);
return;
}

View file

@ -42,7 +42,7 @@ export default <IrcEventHandler>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}`,

View file

@ -11,7 +11,7 @@ import {ChanType, ChanState} from "../../models/chan";
export default <IrcEventHandler>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 <IrcEventHandler>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 <IrcEventHandler>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 <IrcEventHandler>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 <IrcEventHandler>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 <IrcEventHandler>function (irc, network) {
});
if (error) {
network.channels[0].pushMessage(
network.getLobby().pushMessage(
client,
new Msg({
type: MessageType.ERROR,
@ -151,7 +151,7 @@ export default <IrcEventHandler>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 <IrcEventHandler>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 <IrcEventHandler>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 <IrcEventHandler>function (irc, network) {
});
irc.on("ping timeout", function () {
network.channels[0].pushMessage(
network.getLobby().pushMessage(
client,
new Msg({
text: "Ping timeout, disconnecting…",

View file

@ -17,7 +17,7 @@ const ctcpResponses = {
export default <IrcEventHandler>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) {

View file

@ -17,7 +17,7 @@ export default <IrcEventHandler>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 <IrcEventHandler>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 <IrcEventHandler>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."),

View file

@ -5,7 +5,7 @@ export default <IrcEventHandler>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({

View file

@ -5,7 +5,7 @@ export default <IrcEventHandler>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({

View file

@ -9,7 +9,7 @@ export default <IrcEventHandler>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;

View file

@ -75,7 +75,7 @@ export default <IrcEventHandler>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 <IrcEventHandler>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,

View file

@ -41,7 +41,7 @@ export default <IrcEventHandler>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 <IrcEventHandler>function (irc, network) {
let targetChan;
if (data.target === irc.user.nick) {
targetChan = network.channels[0];
targetChan = network.getLobby();
} else {
targetChan = network.getChannel(data.target);

View file

@ -47,7 +47,7 @@ export default <IrcEventHandler>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);

View file

@ -6,7 +6,7 @@ export default <IrcEventHandler>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({

View file

@ -11,7 +11,7 @@ export default <IrcEventHandler>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}`,

View file

@ -6,7 +6,7 @@ export default <IrcEventHandler>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 <IrcEventHandler>function (irc, network) {
});
irc.on("loggedout", () => {
const lobby = network.channels[0];
const lobby = network.getLobby();
const msg = new Msg({
type: MessageType.LOGOUT,

View file

@ -6,7 +6,7 @@ export default <IrcEventHandler>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) {

View file

@ -8,7 +8,7 @@ export default <IrcEventHandler>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,
});

View file

@ -20,7 +20,7 @@ export default <IrcEventHandler>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,