diff --git a/lib/client.js b/lib/client.js index 3582a8b..dc8e3a4 100644 --- a/lib/client.js +++ b/lib/client.js @@ -132,6 +132,28 @@ export default class Client extends EventTarget { }); } + supportsSASL(mech) { + var saslCap = this.availableCaps["sasl"]; + if (saslCap === undefined) { + return false; + } + return saslCap.split(",").includes(mech); + } + + requestCaps(extra) { + var reqCaps = extra || []; + + permanentCaps.forEach((cap) => { + if (this.availableCaps[cap] !== undefined && !this.enabledCaps[cap]) { + reqCaps.push(cap); + } + }); + + if (reqCaps.length > 0) { + this.send({ command: "CAP", params: ["REQ", reqCaps.join(" ")] }); + } + } + handleCap(msg) { var subCmd = msg.params[1]; var args = msg.params.slice(2); @@ -142,29 +164,14 @@ export default class Client extends EventTarget { console.log("Available server caps:", this.availableCaps); var reqCaps = []; - - var saslCap = this.availableCaps["sasl"]; - var supportsSaslPlain = (saslCap !== undefined); - if (saslCap.length > 0) { - supportsSaslPlain = saslCap.split(",").includes("PLAIN"); - } - var capEnd = true; - if (this.params.saslPlain && supportsSaslPlain) { + if (this.params.saslPlain && this.supportsSASL("PLAIN")) { // CAP END is deferred after authentication finishes reqCaps.push("sasl"); capEnd = false; } - permanentCaps.forEach((cap) => { - if (this.availableCaps[cap] !== undefined) { - reqCaps.push(cap); - } - }); - - if (reqCaps.length > 0) { - this.send({ command: "CAP", params: ["REQ", reqCaps.join(" ")] }); - } + this.requestCaps(reqCaps); if (!this.registered && capEnd) { this.send({ command: "CAP", params: ["END"] }); @@ -174,7 +181,7 @@ export default class Client extends EventTarget { case "NEW": this.addAvailableCaps(args[0]); console.log("Server added available caps:", args[0]); - // TODO: request caps + this.requestCaps(); break; case "DEL": args[0].split(" ").forEach((cap) => {