Skip to content

Commit 8023e9c

Browse files
authored
Merge pull request #209 from kiwiirc/cap-notify
Implement cap-notify
2 parents a96c42a + f43d8f9 commit 8023e9c

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

src/commands/handlers/registration.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ var handlers = {
115115

116116
// Which capabilities we want to enable
117117
var want = [
118+
'cap-notify',
118119
'batch',
119120
'multi-prefix',
120121
'message-tags',
@@ -160,7 +161,7 @@ var handlers = {
160161
if (request_caps.length > 0) {
161162
handler.network.cap.negotiating = true;
162163
handler.connection.write('CAP REQ :' + request_caps.join(' '));
163-
} else {
164+
} else if(handler.network.cap.negotiating) {
164165
handler.connection.write('CAP END');
165166
handler.network.cap.negotiating = false;
166167
}
@@ -169,21 +170,23 @@ var handlers = {
169170
case 'ACK':
170171
if (capabilities.length > 0) {
171172
// Update list of enabled capabilities
172-
handler.network.cap.enabled = capabilities;
173+
handler.network.cap.enabled = _.uniq(handler.network.cap.enabled.concat(capabilities));
174+
173175
// Update list of capabilities we would like to have but that aren't enabled
174176
handler.network.cap.requested = _.difference(
175177
handler.network.cap.requested,
176178
capabilities
177179
);
178180
}
179-
if (handler.network.cap.enabled.length > 0) {
181+
if (handler.network.cap.negotiating) {
180182
if (handler.network.cap.isEnabled('sasl')) {
181183
if (handler.connection.options.sasl_mechanism === 'AUTHCOOKIE') {
182184
handler.connection.write('AUTHENTICATE AUTHCOOKIE');
183185
} else {
184186
handler.connection.write('AUTHENTICATE PLAIN');
185187
}
186-
} else {
188+
} else if (handler.network.cap.requested.length === 0) {
189+
// If all of our requested CAPs have been handled, end CAP negotiation
187190
handler.connection.write('CAP END');
188191
handler.network.cap.negotiating = false;
189192
}
@@ -196,7 +199,9 @@ var handlers = {
196199
capabilities
197200
);
198201
}
199-
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) {
200205
handler.connection.write('CAP END');
201206
handler.network.cap.negotiating = false;
202207
}
@@ -205,10 +210,27 @@ var handlers = {
205210
// should we do anything here?
206211
break;
207212
case 'NEW':
208-
// Not supported yet
213+
// Request any new CAPs that we want but haven't already enabled
214+
request_caps = [];
215+
for (let i = 0; i < capabilities.length; 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);
223+
}
224+
}
225+
226+
handler.connection.write('CAP REQ :' + request_caps.join(' '));
209227
break;
210228
case 'DEL':
211-
// Not supported yet
229+
// Update list of enabled capabilities
230+
handler.network.cap.enabled = _.difference(
231+
handler.network.cap.enabled,
232+
capabilities
233+
);
212234
break;
213235
}
214236
},
@@ -231,7 +253,7 @@ var handlers = {
231253
} else {
232254
handler.connection.write('AUTHENTICATE +');
233255
}
234-
} else {
256+
} else if (handler.network.cap.negotiating) {
235257
handler.connection.write('CAP END');
236258
handler.network.cap.negotiating = false;
237259
}
@@ -297,14 +319,18 @@ var handlers = {
297319
},
298320

299321
ERR_SASLNOTAUTHORISED: function(command, handler) {
300-
handler.connection.write('CAP END');
301-
handler.network.cap.negotiating = false;
322+
if (handler.network.cap.negotiating) {
323+
handler.connection.write('CAP END');
324+
handler.network.cap.negotiating = false;
325+
}
302326
},
303327

304328

305329
ERR_SASLABORTED: function(command, handler) {
306-
handler.connection.write('CAP END');
307-
handler.network.cap.negotiating = false;
330+
if (handler.network.cap.negotiating) {
331+
handler.connection.write('CAP END');
332+
handler.network.cap.negotiating = false;
333+
}
308334
},
309335

310336

0 commit comments

Comments
 (0)