Skip to content

Commit f43d8f9

Browse files
committed
CAP negotiation improvements
1 parent 47c5aa5 commit f43d8f9

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/commands/handlers/registration.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ var handlers = {
161161
if (request_caps.length > 0) {
162162
handler.network.cap.negotiating = true;
163163
handler.connection.write('CAP REQ :' + request_caps.join(' '));
164-
} else {
164+
} else if(handler.network.cap.negotiating) {
165165
handler.connection.write('CAP END');
166166
handler.network.cap.negotiating = false;
167167
}
@@ -170,21 +170,23 @@ var handlers = {
170170
case 'ACK':
171171
if (capabilities.length > 0) {
172172
// Update list of enabled capabilities
173-
handler.network.cap.enabled = capabilities;
173+
handler.network.cap.enabled = _.uniq(handler.network.cap.enabled.concat(capabilities));
174+
174175
// Update list of capabilities we would like to have but that aren't enabled
175176
handler.network.cap.requested = _.difference(
176177
handler.network.cap.requested,
177178
capabilities
178179
);
179180
}
180-
if (handler.network.cap.enabled.length > 0) {
181+
if (handler.network.cap.negotiating) {
181182
if (handler.network.cap.isEnabled('sasl')) {
182183
if (handler.connection.options.sasl_mechanism === 'AUTHCOOKIE') {
183184
handler.connection.write('AUTHENTICATE AUTHCOOKIE');
184185
} else {
185186
handler.connection.write('AUTHENTICATE PLAIN');
186187
}
187-
} else {
188+
} else if (handler.network.cap.requested.length === 0) {
189+
// If all of our requested CAPs have been handled, end CAP negotiation
188190
handler.connection.write('CAP END');
189191
handler.network.cap.negotiating = false;
190192
}
@@ -197,7 +199,9 @@ var handlers = {
197199
capabilities
198200
);
199201
}
200-
if (handler.network.cap.requested.length > 0) {
202+
203+
// If all of our requested CAPs have been handled, end CAP negotiation
204+
if (handler.network.cap.negotiating && handler.network.cap.requested.length === 0) {
201205
handler.connection.write('CAP END');
202206
handler.network.cap.negotiating = false;
203207
}
@@ -206,17 +210,25 @@ var handlers = {
206210
// should we do anything here?
207211
break;
208212
case 'NEW':
209-
// Update list of enabled capabilities
213+
// Request any new CAPs that we want but haven't already enabled
214+
request_caps = [];
210215
for (let i = 0; i < capabilities.length; i++) {
211-
if (this.network.cap.enabled.indexOf(capabilities[i]) === -1) {
212-
this.network.cap.enabled.push(capabilities[i]);
216+
let cap = capabilities[i];
217+
if (
218+
want.indexOf(cap) > -1 &&
219+
request_caps.indexOf(cap) === -1 &&
220+
!handler.network.cap.isEnabled(cap)
221+
) {
222+
request_caps.push(cap);
213223
}
214224
}
225+
226+
handler.connection.write('CAP REQ :' + request_caps.join(' '));
215227
break;
216228
case 'DEL':
217229
// Update list of enabled capabilities
218-
this.network.cap.enabled = _.difference(
219-
this.network.cap.enabled,
230+
handler.network.cap.enabled = _.difference(
231+
handler.network.cap.enabled,
220232
capabilities
221233
);
222234
break;
@@ -241,7 +253,7 @@ var handlers = {
241253
} else {
242254
handler.connection.write('AUTHENTICATE +');
243255
}
244-
} else {
256+
} else if (handler.network.cap.negotiating) {
245257
handler.connection.write('CAP END');
246258
handler.network.cap.negotiating = false;
247259
}
@@ -307,14 +319,18 @@ var handlers = {
307319
},
308320

309321
ERR_SASLNOTAUTHORISED: function(command, handler) {
310-
handler.connection.write('CAP END');
311-
handler.network.cap.negotiating = false;
322+
if (handler.network.cap.negotiating) {
323+
handler.connection.write('CAP END');
324+
handler.network.cap.negotiating = false;
325+
}
312326
},
313327

314328

315329
ERR_SASLABORTED: function(command, handler) {
316-
handler.connection.write('CAP END');
317-
handler.network.cap.negotiating = false;
330+
if (handler.network.cap.negotiating) {
331+
handler.connection.write('CAP END');
332+
handler.network.cap.negotiating = false;
333+
}
318334
},
319335

320336

0 commit comments

Comments
 (0)