From adefc620de822771a5d09c5f943e68fe16825f43 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sun, 21 Nov 2021 13:48:41 +0100 Subject: [PATCH] lib/client: send BOUNCER BIND and CAP END immediately Don't wait for auth to finish. This reduces the number of roundtrips. --- lib/client.js | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/client.js b/lib/client.js index 394caec..2bd574a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -293,15 +293,6 @@ export default class Client extends EventTarget { case irc.RPL_LOGGEDOUT: console.log("Logged out"); break; - case irc.RPL_SASLSUCCESS: - console.log("SASL authentication success"); - if (this.status != Client.Status.REGISTERED) { - if (this.enabledCaps["soju.im/bouncer-networks"] && this.params.bouncerNetwork) { - this.send({ command: "BOUNCER", params: ["BIND", this.params.bouncerNetwork] }); - } - this.send({ command: "CAP", params: ["END"] }); - } - break; case irc.RPL_NAMREPLY: this.pushPendingList("NAMES " + msg.params[2], msg); break; @@ -325,6 +316,9 @@ export default class Client extends EventTarget { case irc.RPL_ENDOFWHO: this.endPendingList("WHO", msg); break; + case irc.RPL_SASLSUCCESS: + console.log("SASL authentication success"); + break; case irc.ERR_NICKLOCKED: case irc.ERR_SASLFAIL: case irc.ERR_SASLTOOLONG: @@ -398,6 +392,9 @@ export default class Client extends EventTarget { } authenticate(mechanism, params) { + if (!this.supportsSASL(mechanism)) { + throw new Error(`${mechanism} authentication not supported by the server`); + } console.log(`Starting SASL ${mechanism} authentication`); this.send({ command: "AUTHENTICATE", params: [mechanism] }); switch (mechanism) { @@ -576,20 +573,28 @@ export default class Client extends EventTarget { case "LS": this.supportsCap = true; this.addAvailableCaps(args[args.length - 1]); - if (args[0] != "*") { - console.log("Available server caps:", this.availableCaps); + if (args[0] == "*") { + break; + } - let capEnd = true; - if ((this.params.saslPlain && this.supportsSASL("PLAIN")) || (this.params.saslExternal && this.supportsSASL("EXTERNAL"))) { - // CAP END is deferred after authentication finishes - capEnd = false; + console.log("Available server caps:", this.availableCaps); + + this.requestCaps(); + + if (this.status !== Client.Status.REGISTERED) { + if (this.availableCaps["sasl"] !== undefined) { + if (this.params.saslPlain) { + this.authenticate("PLAIN", this.params.saslPlain); + } else if (this.params.saslExternal) { + this.authenticate("EXTERNAL"); + } } - this.requestCaps(); - - if (this.status != Client.Status.REGISTERED && capEnd) { - this.send({ command: "CAP", params: ["END"] }); + if (this.availableCaps["soju.im/bouncer-networks"] !== undefined && this.params.bouncerNetwork) { + this.send({ command: "BOUNCER", params: ["BIND", this.params.bouncerNetwork] }); } + + this.send({ command: "CAP", params: ["END"] }); } break; case "NEW": @@ -610,14 +615,6 @@ export default class Client extends EventTarget { args[0].split(" ").forEach((cap) => { cap = cap.toLowerCase(); this.enabledCaps[cap] = true; - - if (cap === "sasl" && this.status !== Client.Status.REGISTERED) { - if (this.params.saslPlain) { - this.authenticate("PLAIN", this.params.saslPlain); - } else if (this.params.saslExternal) { - this.authenticate("EXTERNAL"); - } - } }); break; case "NAK":