]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* modules/m_cap.c |
55abcbb2 | 2 | * |
212380e3 AC |
3 | * Copyright (C) 2005 Lee Hardy <lee@leeh.co.uk> |
4 | * Copyright (C) 2005 ircd-ratbox development team | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are | |
8 | * met: | |
9 | * | |
10 | * 1.Redistributions of source code must retain the above copyright notice, | |
11 | * this list of conditions and the following disclaimer. | |
12 | * 2.Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3.The name of the author may not be used to endorse or promote products | |
16 | * derived from this software without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
27 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
28 | * POSSIBILITY OF SUCH DAMAGE. | |
29 | * | |
30 | * $Id: m_cap.c 676 2006-02-03 20:05:09Z gxti $ | |
31 | */ | |
32 | ||
33 | #include "stdinc.h" | |
212380e3 AC |
34 | #include "class.h" |
35 | #include "client.h" | |
4562c604 | 36 | #include "match.h" |
212380e3 AC |
37 | #include "ircd.h" |
38 | #include "numeric.h" | |
39 | #include "msg.h" | |
40 | #include "parse.h" | |
41 | #include "modules.h" | |
42 | #include "s_serv.h" | |
43 | #include "s_user.h" | |
77d3d2db | 44 | #include "send.h" |
bbce62d2 | 45 | #include "s_conf.h" |
dafbd7fa | 46 | #include "hash.h" |
212380e3 AC |
47 | |
48 | typedef int (*bqcmp)(const void *, const void *); | |
49 | ||
50 | static int m_cap(struct Client *, struct Client *, int, const char **); | |
51 | static int modinit(void); | |
52 | ||
53 | struct Message cap_msgtab = { | |
54 | "CAP", 0, 0, 0, MFLG_SLOW, | |
55 | {{m_cap, 2}, {m_cap, 2}, mg_ignore, mg_ignore, mg_ignore, {m_cap, 2}} | |
56 | }; | |
57 | ||
58 | mapi_clist_av1 cap_clist[] = { &cap_msgtab, NULL }; | |
59 | DECLARE_MODULE_AV1(cap, modinit, NULL, cap_clist, NULL, NULL, "$Revision: 676 $"); | |
60 | ||
0044d400 AC |
61 | #define _CLICAP(name, capserv, capclient, caprequired, flags) \ |
62 | { (name), (capserv), (capclient), (caprequired), (flags), sizeof(name) - 1 } | |
212380e3 AC |
63 | |
64 | #define CLICAP_FLAGS_STICKY 0x001 | |
65 | ||
66 | static struct clicap | |
67 | { | |
68 | const char *name; | |
69 | int cap_serv; /* for altering s->c */ | |
70 | int cap_cli; /* for altering c->s */ | |
0044d400 | 71 | int cap_required_serv; /* required dependency cap */ |
212380e3 AC |
72 | int flags; |
73 | int namelen; | |
74 | } clicap_list[] = { | |
0044d400 | 75 | _CLICAP("multi-prefix", CLICAP_MULTI_PREFIX, 0, 0, 0), |
8f05076d | 76 | _CLICAP("sasl", CLICAP_SASL, 0, 0, 0), |
0044d400 AC |
77 | _CLICAP("account-notify", CLICAP_ACCOUNT_NOTIFY, 0, 0, 0), |
78 | _CLICAP("extended-join", CLICAP_EXTENDED_JOIN, 0, 0, 0), | |
79 | _CLICAP("away-notify", CLICAP_AWAY_NOTIFY, 0, 0, 0), | |
80 | _CLICAP("tls", CLICAP_TLS, 0, 0, 0), | |
1b54aa5c | 81 | _CLICAP("userhost-in-names", CLICAP_USERHOST_IN_NAMES, 0, 0, 0), |
13de7083 | 82 | _CLICAP("cap-notify", CLICAP_CAP_NOTIFY, 0, 0, 0), |
212380e3 AC |
83 | }; |
84 | ||
85 | #define CLICAP_LIST_LEN (sizeof(clicap_list) / sizeof(struct clicap)) | |
86 | ||
87 | static int clicap_sort(struct clicap *, struct clicap *); | |
88 | ||
89 | static int | |
90 | modinit(void) | |
91 | { | |
92 | qsort(clicap_list, CLICAP_LIST_LEN, sizeof(struct clicap), | |
93 | (bqcmp) clicap_sort); | |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int | |
98 | clicap_sort(struct clicap *one, struct clicap *two) | |
99 | { | |
100 | return irccmp(one->name, two->name); | |
101 | } | |
102 | ||
103 | static int | |
104 | clicap_compare(const char *name, struct clicap *cap) | |
105 | { | |
106 | return irccmp(name, cap->name); | |
107 | } | |
108 | ||
109 | /* clicap_find() | |
110 | * Used iteratively over a buffer, extracts individual cap tokens. | |
111 | * | |
112 | * Inputs: buffer to start iterating over (NULL to iterate over existing buf) | |
113 | * int pointer to whether the cap token is negated | |
114 | * int pointer to whether we finish with success | |
115 | * Ouputs: Cap entry if found, NULL otherwise. | |
116 | */ | |
117 | static struct clicap * | |
118 | clicap_find(const char *data, int *negate, int *finished) | |
119 | { | |
120 | static char buf[BUFSIZE]; | |
121 | static char *p; | |
122 | struct clicap *cap; | |
123 | char *s; | |
124 | ||
125 | *negate = 0; | |
126 | ||
127 | if(data) | |
128 | { | |
f427c8b0 | 129 | rb_strlcpy(buf, data, sizeof(buf)); |
212380e3 AC |
130 | p = buf; |
131 | } | |
132 | ||
133 | if(*finished) | |
134 | return NULL; | |
135 | ||
136 | /* skip any whitespace */ | |
137 | while(*p && IsSpace(*p)) | |
138 | p++; | |
139 | ||
140 | if(EmptyString(p)) | |
141 | { | |
142 | *finished = 1; | |
143 | return NULL; | |
144 | } | |
145 | ||
146 | if(*p == '-') | |
147 | { | |
148 | *negate = 1; | |
149 | p++; | |
150 | ||
151 | /* someone sent a '-' without a parameter.. */ | |
152 | if(*p == '\0') | |
153 | return NULL; | |
154 | } | |
155 | ||
156 | if((s = strchr(p, ' '))) | |
157 | *s++ = '\0'; | |
158 | ||
55abcbb2 | 159 | if((cap = bsearch(p, clicap_list, CLICAP_LIST_LEN, |
212380e3 AC |
160 | sizeof(struct clicap), (bqcmp) clicap_compare))) |
161 | { | |
162 | if(s) | |
163 | p = s; | |
164 | else | |
165 | *finished = 1; | |
166 | } | |
167 | ||
168 | return cap; | |
169 | } | |
170 | ||
171 | /* clicap_generate() | |
172 | * Generates a list of capabilities. | |
173 | * | |
174 | * Inputs: client to send to, subcmd to send, | |
175 | * flags to match against: 0 to do none, -1 if client has no flags, | |
176 | * int to whether we are doing CAP CLEAR | |
177 | * Outputs: None | |
178 | */ | |
179 | static void | |
180 | clicap_generate(struct Client *source_p, const char *subcmd, int flags, int clear) | |
181 | { | |
182 | char buf[BUFSIZE]; | |
183 | char capbuf[BUFSIZE]; | |
184 | char *p; | |
185 | int buflen = 0; | |
186 | int curlen, mlen; | |
34e02db6 | 187 | size_t i; |
212380e3 | 188 | |
7cdb0a09 | 189 | mlen = rb_sprintf(buf, ":%s CAP %s %s", |
55abcbb2 KB |
190 | me.name, |
191 | EmptyString(source_p->name) ? "*" : source_p->name, | |
212380e3 AC |
192 | subcmd); |
193 | ||
194 | p = capbuf; | |
195 | buflen = mlen; | |
196 | ||
197 | /* shortcut, nothing to do */ | |
198 | if(flags == -1) | |
199 | { | |
200 | sendto_one(source_p, "%s :", buf); | |
201 | return; | |
202 | } | |
203 | ||
204 | for(i = 0; i < CLICAP_LIST_LEN; i++) | |
205 | { | |
206 | if(flags) | |
207 | { | |
208 | if(!IsCapable(source_p, clicap_list[i].cap_serv)) | |
209 | continue; | |
210 | /* they are capable of this, check sticky */ | |
211 | else if(clear && clicap_list[i].flags & CLICAP_FLAGS_STICKY) | |
212 | continue; | |
213 | } | |
214 | ||
bbce62d2 MT |
215 | if (clicap_list[i].cap_serv == CLICAP_SASL) |
216 | { | |
217 | struct Client *agent_p = NULL; | |
218 | ||
219 | if (!ConfigFileEntry.sasl_service) | |
220 | continue; | |
221 | ||
222 | agent_p = find_named_client(ConfigFileEntry.sasl_service); | |
223 | if (agent_p == NULL || !IsService(agent_p)) | |
224 | continue; | |
225 | } | |
226 | ||
212380e3 AC |
227 | /* \r\n\0, possible "-~=", space, " *" */ |
228 | if(buflen + clicap_list[i].namelen >= BUFSIZE - 10) | |
229 | { | |
230 | /* remove our trailing space -- if buflen == mlen | |
231 | * here, we didnt even succeed in adding one. | |
232 | */ | |
233 | if(buflen != mlen) | |
234 | *(p - 1) = '\0'; | |
235 | else | |
236 | *p = '\0'; | |
237 | ||
238 | sendto_one(source_p, "%s * :%s", buf, capbuf); | |
239 | p = capbuf; | |
240 | buflen = mlen; | |
241 | } | |
242 | ||
243 | if(clear) | |
244 | { | |
245 | *p++ = '-'; | |
246 | buflen++; | |
247 | ||
248 | /* needs a client ack */ | |
55abcbb2 | 249 | if(clicap_list[i].cap_cli && |
212380e3 AC |
250 | IsCapable(source_p, clicap_list[i].cap_cli)) |
251 | { | |
252 | *p++ = '~'; | |
253 | buflen++; | |
254 | } | |
255 | } | |
256 | else | |
257 | { | |
258 | if(clicap_list[i].flags & CLICAP_FLAGS_STICKY) | |
259 | { | |
260 | *p++ = '='; | |
261 | buflen++; | |
262 | } | |
263 | ||
264 | /* if we're doing an LS, then we only send this if | |
265 | * they havent ack'd | |
266 | */ | |
267 | if(clicap_list[i].cap_cli && | |
268 | (!flags || !IsCapable(source_p, clicap_list[i].cap_cli))) | |
269 | { | |
270 | *p++ = '~'; | |
271 | buflen++; | |
272 | } | |
273 | } | |
274 | ||
7cdb0a09 | 275 | curlen = rb_sprintf(p, "%s ", clicap_list[i].name); |
212380e3 AC |
276 | p += curlen; |
277 | buflen += curlen; | |
278 | } | |
279 | ||
280 | /* remove trailing space */ | |
281 | if(buflen != mlen) | |
282 | *(p - 1) = '\0'; | |
283 | else | |
284 | *p = '\0'; | |
285 | ||
286 | sendto_one(source_p, "%s :%s", buf, capbuf); | |
287 | } | |
288 | ||
289 | static void | |
290 | cap_ack(struct Client *source_p, const char *arg) | |
291 | { | |
292 | struct clicap *cap; | |
293 | int capadd = 0, capdel = 0; | |
294 | int finished = 0, negate; | |
295 | ||
296 | if(EmptyString(arg)) | |
297 | return; | |
298 | ||
299 | for(cap = clicap_find(arg, &negate, &finished); cap; | |
300 | cap = clicap_find(NULL, &negate, &finished)) | |
301 | { | |
302 | /* sent an ACK for something they havent REQd */ | |
303 | if(!IsCapable(source_p, cap->cap_serv)) | |
304 | continue; | |
305 | ||
306 | if(negate) | |
307 | { | |
308 | /* dont let them ack something sticky off */ | |
309 | if(cap->flags & CLICAP_FLAGS_STICKY) | |
310 | continue; | |
311 | ||
312 | capdel |= cap->cap_cli; | |
313 | } | |
314 | else | |
315 | capadd |= cap->cap_cli; | |
316 | } | |
317 | ||
318 | source_p->localClient->caps |= capadd; | |
319 | source_p->localClient->caps &= ~capdel; | |
320 | } | |
321 | ||
322 | static void | |
323 | cap_clear(struct Client *source_p, const char *arg) | |
324 | { | |
55abcbb2 | 325 | clicap_generate(source_p, "ACK", |
212380e3 AC |
326 | source_p->localClient->caps ? source_p->localClient->caps : -1, 1); |
327 | ||
328 | /* XXX - sticky capabs */ | |
329 | #ifdef CLICAP_STICKY | |
330 | source_p->localClient->caps = source_p->localClient->caps & CLICAP_STICKY; | |
331 | #else | |
332 | source_p->localClient->caps = 0; | |
333 | #endif | |
334 | } | |
335 | ||
336 | static void | |
337 | cap_end(struct Client *source_p, const char *arg) | |
338 | { | |
339 | if(IsRegistered(source_p)) | |
340 | return; | |
341 | ||
095328a7 | 342 | source_p->flags &= ~FLAGS_CLICAP; |
212380e3 | 343 | |
1fb3b1e1 | 344 | if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER) |
212380e3 AC |
345 | { |
346 | char buf[USERLEN+1]; | |
f427c8b0 | 347 | rb_strlcpy(buf, source_p->username, sizeof(buf)); |
212380e3 AC |
348 | register_local_user(source_p, source_p, buf); |
349 | } | |
350 | } | |
351 | ||
352 | static void | |
353 | cap_list(struct Client *source_p, const char *arg) | |
354 | { | |
355 | /* list of what theyre currently using */ | |
356 | clicap_generate(source_p, "LIST", | |
357 | source_p->localClient->caps ? source_p->localClient->caps : -1, 0); | |
358 | } | |
359 | ||
360 | static void | |
361 | cap_ls(struct Client *source_p, const char *arg) | |
362 | { | |
363 | if(!IsRegistered(source_p)) | |
095328a7 | 364 | source_p->flags |= FLAGS_CLICAP; |
212380e3 AC |
365 | |
366 | /* list of what we support */ | |
367 | clicap_generate(source_p, "LS", 0, 0); | |
368 | } | |
369 | ||
370 | static void | |
371 | cap_req(struct Client *source_p, const char *arg) | |
372 | { | |
373 | char buf[BUFSIZE]; | |
374 | char pbuf[2][BUFSIZE]; | |
375 | struct clicap *cap; | |
376 | int buflen, plen; | |
377 | int i = 0; | |
378 | int capadd = 0, capdel = 0; | |
379 | int finished = 0, negate; | |
380 | ||
381 | if(!IsRegistered(source_p)) | |
095328a7 | 382 | source_p->flags |= FLAGS_CLICAP; |
212380e3 AC |
383 | |
384 | if(EmptyString(arg)) | |
385 | return; | |
386 | ||
7cdb0a09 | 387 | buflen = rb_snprintf(buf, sizeof(buf), ":%s CAP %s ACK", |
212380e3 AC |
388 | me.name, EmptyString(source_p->name) ? "*" : source_p->name); |
389 | ||
390 | pbuf[0][0] = '\0'; | |
391 | plen = 0; | |
392 | ||
393 | for(cap = clicap_find(arg, &negate, &finished); cap; | |
394 | cap = clicap_find(NULL, &negate, &finished)) | |
395 | { | |
396 | /* filled the first array, but cant send it in case the | |
397 | * request fails. one REQ should never fill more than two | |
398 | * buffers --fl | |
399 | */ | |
400 | if(buflen + plen + cap->namelen + 6 >= BUFSIZE) | |
401 | { | |
402 | pbuf[1][0] = '\0'; | |
403 | plen = 0; | |
404 | i = 1; | |
405 | } | |
406 | ||
407 | if(negate) | |
408 | { | |
409 | if(cap->flags & CLICAP_FLAGS_STICKY) | |
410 | { | |
411 | finished = 0; | |
412 | break; | |
413 | } | |
414 | ||
415 | strcat(pbuf[i], "-"); | |
416 | plen++; | |
417 | ||
418 | capdel |= cap->cap_serv; | |
419 | } | |
420 | else | |
421 | { | |
3a48406b | 422 | if(cap->cap_required_serv && !((capadd & cap->cap_required_serv) == cap->cap_required_serv || IsCapable(source_p, cap->cap_required_serv))) |
0044d400 AC |
423 | { |
424 | finished = 0; | |
425 | break; | |
426 | } | |
427 | ||
bbce62d2 MT |
428 | if (cap->cap_serv == CLICAP_SASL) |
429 | { | |
430 | struct Client *agent_p = NULL; | |
431 | ||
432 | if (!ConfigFileEntry.sasl_service) | |
433 | { | |
434 | finished = 0; | |
435 | break; | |
436 | } | |
437 | ||
438 | agent_p = find_named_client(ConfigFileEntry.sasl_service); | |
439 | if (agent_p == NULL || !IsService(agent_p)) | |
440 | { | |
441 | finished = 0; | |
442 | break; | |
443 | } | |
444 | } | |
445 | ||
212380e3 AC |
446 | if(cap->flags & CLICAP_FLAGS_STICKY) |
447 | { | |
448 | strcat(pbuf[i], "="); | |
449 | plen++; | |
450 | } | |
451 | ||
452 | capadd |= cap->cap_serv; | |
453 | } | |
454 | ||
455 | if(cap->cap_cli) | |
456 | { | |
457 | strcat(pbuf[i], "~"); | |
458 | plen++; | |
459 | } | |
460 | ||
461 | strcat(pbuf[i], cap->name); | |
462 | strcat(pbuf[i], " "); | |
463 | plen += (cap->namelen + 1); | |
464 | } | |
465 | ||
466 | if(!finished) | |
467 | { | |
468 | sendto_one(source_p, ":%s CAP %s NAK :%s", | |
469 | me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg); | |
470 | return; | |
471 | } | |
472 | ||
473 | if(i) | |
474 | { | |
475 | sendto_one(source_p, "%s * :%s", buf, pbuf[0]); | |
476 | sendto_one(source_p, "%s :%s", buf, pbuf[1]); | |
477 | } | |
478 | else | |
479 | sendto_one(source_p, "%s :%s", buf, pbuf[0]); | |
480 | ||
481 | source_p->localClient->caps |= capadd; | |
482 | source_p->localClient->caps &= ~capdel; | |
483 | } | |
484 | ||
485 | static struct clicap_cmd | |
486 | { | |
487 | const char *cmd; | |
488 | void (*func)(struct Client *source_p, const char *arg); | |
489 | } clicap_cmdlist[] = { | |
490 | /* This list *MUST* be in alphabetical order */ | |
491 | { "ACK", cap_ack }, | |
492 | { "CLEAR", cap_clear }, | |
493 | { "END", cap_end }, | |
494 | { "LIST", cap_list }, | |
495 | { "LS", cap_ls }, | |
496 | { "REQ", cap_req }, | |
497 | }; | |
498 | ||
499 | static int | |
500 | clicap_cmd_search(const char *command, struct clicap_cmd *entry) | |
501 | { | |
502 | return irccmp(command, entry->cmd); | |
503 | } | |
504 | ||
505 | static int | |
506 | m_cap(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
507 | { | |
508 | struct clicap_cmd *cmd; | |
509 | ||
510 | if(!(cmd = bsearch(parv[1], clicap_cmdlist, | |
511 | sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd), | |
512 | sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search))) | |
513 | { | |
514 | sendto_one(source_p, form_str(ERR_INVALIDCAPCMD), | |
48a038f4 JT |
515 | me.name, EmptyString(source_p->name) ? "*" : source_p->name, |
516 | parv[1]); | |
212380e3 AC |
517 | return 0; |
518 | } | |
519 | ||
520 | (cmd->func)(source_p, parv[2]); | |
521 | return 0; | |
522 | } |