]>
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), |
aaaf9faf | 76 | _CLICAP("sasl", CLICAP_SASL, 0, 0, CLICAP_FLAGS_STICKY), |
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), |
4a5330bb | 83 | _CLICAP("chghost", CLICAP_CHGHOST, 0, 0, 0), |
212380e3 AC |
84 | }; |
85 | ||
86 | #define CLICAP_LIST_LEN (sizeof(clicap_list) / sizeof(struct clicap)) | |
87 | ||
88 | static int clicap_sort(struct clicap *, struct clicap *); | |
89 | ||
90 | static int | |
91 | modinit(void) | |
92 | { | |
93 | qsort(clicap_list, CLICAP_LIST_LEN, sizeof(struct clicap), | |
94 | (bqcmp) clicap_sort); | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static int | |
99 | clicap_sort(struct clicap *one, struct clicap *two) | |
100 | { | |
101 | return irccmp(one->name, two->name); | |
102 | } | |
103 | ||
104 | static int | |
105 | clicap_compare(const char *name, struct clicap *cap) | |
106 | { | |
107 | return irccmp(name, cap->name); | |
108 | } | |
109 | ||
110 | /* clicap_find() | |
111 | * Used iteratively over a buffer, extracts individual cap tokens. | |
112 | * | |
113 | * Inputs: buffer to start iterating over (NULL to iterate over existing buf) | |
114 | * int pointer to whether the cap token is negated | |
115 | * int pointer to whether we finish with success | |
116 | * Ouputs: Cap entry if found, NULL otherwise. | |
117 | */ | |
118 | static struct clicap * | |
119 | clicap_find(const char *data, int *negate, int *finished) | |
120 | { | |
121 | static char buf[BUFSIZE]; | |
122 | static char *p; | |
123 | struct clicap *cap; | |
124 | char *s; | |
125 | ||
126 | *negate = 0; | |
127 | ||
128 | if(data) | |
129 | { | |
f427c8b0 | 130 | rb_strlcpy(buf, data, sizeof(buf)); |
212380e3 AC |
131 | p = buf; |
132 | } | |
133 | ||
134 | if(*finished) | |
135 | return NULL; | |
136 | ||
137 | /* skip any whitespace */ | |
138 | while(*p && IsSpace(*p)) | |
139 | p++; | |
140 | ||
141 | if(EmptyString(p)) | |
142 | { | |
143 | *finished = 1; | |
144 | return NULL; | |
145 | } | |
146 | ||
147 | if(*p == '-') | |
148 | { | |
149 | *negate = 1; | |
150 | p++; | |
151 | ||
152 | /* someone sent a '-' without a parameter.. */ | |
153 | if(*p == '\0') | |
154 | return NULL; | |
155 | } | |
156 | ||
157 | if((s = strchr(p, ' '))) | |
158 | *s++ = '\0'; | |
159 | ||
55abcbb2 | 160 | if((cap = bsearch(p, clicap_list, CLICAP_LIST_LEN, |
212380e3 AC |
161 | sizeof(struct clicap), (bqcmp) clicap_compare))) |
162 | { | |
163 | if(s) | |
164 | p = s; | |
165 | else | |
166 | *finished = 1; | |
167 | } | |
168 | ||
169 | return cap; | |
170 | } | |
171 | ||
172 | /* clicap_generate() | |
173 | * Generates a list of capabilities. | |
174 | * | |
175 | * Inputs: client to send to, subcmd to send, | |
176 | * flags to match against: 0 to do none, -1 if client has no flags, | |
177 | * int to whether we are doing CAP CLEAR | |
178 | * Outputs: None | |
179 | */ | |
180 | static void | |
181 | clicap_generate(struct Client *source_p, const char *subcmd, int flags, int clear) | |
182 | { | |
183 | char buf[BUFSIZE]; | |
184 | char capbuf[BUFSIZE]; | |
185 | char *p; | |
186 | int buflen = 0; | |
187 | int curlen, mlen; | |
34e02db6 | 188 | size_t i; |
212380e3 | 189 | |
7cdb0a09 | 190 | mlen = rb_sprintf(buf, ":%s CAP %s %s", |
55abcbb2 KB |
191 | me.name, |
192 | EmptyString(source_p->name) ? "*" : source_p->name, | |
212380e3 AC |
193 | subcmd); |
194 | ||
195 | p = capbuf; | |
196 | buflen = mlen; | |
197 | ||
198 | /* shortcut, nothing to do */ | |
199 | if(flags == -1) | |
200 | { | |
201 | sendto_one(source_p, "%s :", buf); | |
202 | return; | |
203 | } | |
204 | ||
205 | for(i = 0; i < CLICAP_LIST_LEN; i++) | |
206 | { | |
207 | if(flags) | |
208 | { | |
209 | if(!IsCapable(source_p, clicap_list[i].cap_serv)) | |
210 | continue; | |
211 | /* they are capable of this, check sticky */ | |
212 | else if(clear && clicap_list[i].flags & CLICAP_FLAGS_STICKY) | |
213 | continue; | |
214 | } | |
215 | ||
bbce62d2 MT |
216 | if (clicap_list[i].cap_serv == CLICAP_SASL) |
217 | { | |
218 | struct Client *agent_p = NULL; | |
219 | ||
220 | if (!ConfigFileEntry.sasl_service) | |
221 | continue; | |
222 | ||
223 | agent_p = find_named_client(ConfigFileEntry.sasl_service); | |
224 | if (agent_p == NULL || !IsService(agent_p)) | |
225 | continue; | |
226 | } | |
227 | ||
212380e3 AC |
228 | /* \r\n\0, possible "-~=", space, " *" */ |
229 | if(buflen + clicap_list[i].namelen >= BUFSIZE - 10) | |
230 | { | |
231 | /* remove our trailing space -- if buflen == mlen | |
232 | * here, we didnt even succeed in adding one. | |
233 | */ | |
234 | if(buflen != mlen) | |
235 | *(p - 1) = '\0'; | |
236 | else | |
237 | *p = '\0'; | |
238 | ||
239 | sendto_one(source_p, "%s * :%s", buf, capbuf); | |
240 | p = capbuf; | |
241 | buflen = mlen; | |
242 | } | |
243 | ||
244 | if(clear) | |
245 | { | |
246 | *p++ = '-'; | |
247 | buflen++; | |
212380e3 AC |
248 | } |
249 | ||
7cdb0a09 | 250 | curlen = rb_sprintf(p, "%s ", clicap_list[i].name); |
212380e3 AC |
251 | p += curlen; |
252 | buflen += curlen; | |
253 | } | |
254 | ||
255 | /* remove trailing space */ | |
256 | if(buflen != mlen) | |
257 | *(p - 1) = '\0'; | |
258 | else | |
259 | *p = '\0'; | |
260 | ||
261 | sendto_one(source_p, "%s :%s", buf, capbuf); | |
262 | } | |
263 | ||
264 | static void | |
265 | cap_ack(struct Client *source_p, const char *arg) | |
266 | { | |
267 | struct clicap *cap; | |
268 | int capadd = 0, capdel = 0; | |
269 | int finished = 0, negate; | |
270 | ||
271 | if(EmptyString(arg)) | |
272 | return; | |
273 | ||
274 | for(cap = clicap_find(arg, &negate, &finished); cap; | |
275 | cap = clicap_find(NULL, &negate, &finished)) | |
276 | { | |
277 | /* sent an ACK for something they havent REQd */ | |
278 | if(!IsCapable(source_p, cap->cap_serv)) | |
279 | continue; | |
280 | ||
281 | if(negate) | |
282 | { | |
283 | /* dont let them ack something sticky off */ | |
284 | if(cap->flags & CLICAP_FLAGS_STICKY) | |
285 | continue; | |
286 | ||
287 | capdel |= cap->cap_cli; | |
288 | } | |
289 | else | |
290 | capadd |= cap->cap_cli; | |
291 | } | |
292 | ||
293 | source_p->localClient->caps |= capadd; | |
294 | source_p->localClient->caps &= ~capdel; | |
295 | } | |
296 | ||
297 | static void | |
298 | cap_clear(struct Client *source_p, const char *arg) | |
299 | { | |
55abcbb2 | 300 | clicap_generate(source_p, "ACK", |
212380e3 AC |
301 | source_p->localClient->caps ? source_p->localClient->caps : -1, 1); |
302 | ||
303 | /* XXX - sticky capabs */ | |
304 | #ifdef CLICAP_STICKY | |
305 | source_p->localClient->caps = source_p->localClient->caps & CLICAP_STICKY; | |
306 | #else | |
307 | source_p->localClient->caps = 0; | |
308 | #endif | |
309 | } | |
310 | ||
311 | static void | |
312 | cap_end(struct Client *source_p, const char *arg) | |
313 | { | |
314 | if(IsRegistered(source_p)) | |
315 | return; | |
316 | ||
095328a7 | 317 | source_p->flags &= ~FLAGS_CLICAP; |
212380e3 | 318 | |
1fb3b1e1 | 319 | if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER) |
212380e3 | 320 | { |
21251822 | 321 | register_local_user(source_p, source_p); |
212380e3 AC |
322 | } |
323 | } | |
324 | ||
325 | static void | |
326 | cap_list(struct Client *source_p, const char *arg) | |
327 | { | |
328 | /* list of what theyre currently using */ | |
329 | clicap_generate(source_p, "LIST", | |
330 | source_p->localClient->caps ? source_p->localClient->caps : -1, 0); | |
331 | } | |
332 | ||
333 | static void | |
334 | cap_ls(struct Client *source_p, const char *arg) | |
335 | { | |
336 | if(!IsRegistered(source_p)) | |
095328a7 | 337 | source_p->flags |= FLAGS_CLICAP; |
212380e3 AC |
338 | |
339 | /* list of what we support */ | |
340 | clicap_generate(source_p, "LS", 0, 0); | |
341 | } | |
342 | ||
343 | static void | |
344 | cap_req(struct Client *source_p, const char *arg) | |
345 | { | |
346 | char buf[BUFSIZE]; | |
347 | char pbuf[2][BUFSIZE]; | |
348 | struct clicap *cap; | |
349 | int buflen, plen; | |
350 | int i = 0; | |
351 | int capadd = 0, capdel = 0; | |
352 | int finished = 0, negate; | |
353 | ||
354 | if(!IsRegistered(source_p)) | |
095328a7 | 355 | source_p->flags |= FLAGS_CLICAP; |
212380e3 AC |
356 | |
357 | if(EmptyString(arg)) | |
358 | return; | |
359 | ||
7cdb0a09 | 360 | buflen = rb_snprintf(buf, sizeof(buf), ":%s CAP %s ACK", |
212380e3 AC |
361 | me.name, EmptyString(source_p->name) ? "*" : source_p->name); |
362 | ||
363 | pbuf[0][0] = '\0'; | |
364 | plen = 0; | |
365 | ||
366 | for(cap = clicap_find(arg, &negate, &finished); cap; | |
367 | cap = clicap_find(NULL, &negate, &finished)) | |
368 | { | |
369 | /* filled the first array, but cant send it in case the | |
370 | * request fails. one REQ should never fill more than two | |
371 | * buffers --fl | |
372 | */ | |
373 | if(buflen + plen + cap->namelen + 6 >= BUFSIZE) | |
374 | { | |
375 | pbuf[1][0] = '\0'; | |
376 | plen = 0; | |
377 | i = 1; | |
378 | } | |
379 | ||
380 | if(negate) | |
381 | { | |
382 | if(cap->flags & CLICAP_FLAGS_STICKY) | |
383 | { | |
384 | finished = 0; | |
385 | break; | |
386 | } | |
387 | ||
388 | strcat(pbuf[i], "-"); | |
389 | plen++; | |
390 | ||
391 | capdel |= cap->cap_serv; | |
392 | } | |
393 | else | |
394 | { | |
3a48406b | 395 | if(cap->cap_required_serv && !((capadd & cap->cap_required_serv) == cap->cap_required_serv || IsCapable(source_p, cap->cap_required_serv))) |
0044d400 AC |
396 | { |
397 | finished = 0; | |
398 | break; | |
399 | } | |
400 | ||
bbce62d2 MT |
401 | if (cap->cap_serv == CLICAP_SASL) |
402 | { | |
403 | struct Client *agent_p = NULL; | |
404 | ||
405 | if (!ConfigFileEntry.sasl_service) | |
406 | { | |
407 | finished = 0; | |
408 | break; | |
409 | } | |
410 | ||
411 | agent_p = find_named_client(ConfigFileEntry.sasl_service); | |
412 | if (agent_p == NULL || !IsService(agent_p)) | |
413 | { | |
414 | finished = 0; | |
415 | break; | |
416 | } | |
417 | } | |
418 | ||
212380e3 AC |
419 | capadd |= cap->cap_serv; |
420 | } | |
421 | ||
422 | if(cap->cap_cli) | |
423 | { | |
424 | strcat(pbuf[i], "~"); | |
425 | plen++; | |
426 | } | |
427 | ||
428 | strcat(pbuf[i], cap->name); | |
429 | strcat(pbuf[i], " "); | |
430 | plen += (cap->namelen + 1); | |
431 | } | |
432 | ||
433 | if(!finished) | |
434 | { | |
435 | sendto_one(source_p, ":%s CAP %s NAK :%s", | |
436 | me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg); | |
437 | return; | |
438 | } | |
439 | ||
440 | if(i) | |
441 | { | |
442 | sendto_one(source_p, "%s * :%s", buf, pbuf[0]); | |
443 | sendto_one(source_p, "%s :%s", buf, pbuf[1]); | |
444 | } | |
445 | else | |
446 | sendto_one(source_p, "%s :%s", buf, pbuf[0]); | |
447 | ||
448 | source_p->localClient->caps |= capadd; | |
449 | source_p->localClient->caps &= ~capdel; | |
450 | } | |
451 | ||
452 | static struct clicap_cmd | |
453 | { | |
454 | const char *cmd; | |
455 | void (*func)(struct Client *source_p, const char *arg); | |
456 | } clicap_cmdlist[] = { | |
457 | /* This list *MUST* be in alphabetical order */ | |
458 | { "ACK", cap_ack }, | |
459 | { "CLEAR", cap_clear }, | |
460 | { "END", cap_end }, | |
461 | { "LIST", cap_list }, | |
462 | { "LS", cap_ls }, | |
463 | { "REQ", cap_req }, | |
464 | }; | |
465 | ||
466 | static int | |
467 | clicap_cmd_search(const char *command, struct clicap_cmd *entry) | |
468 | { | |
469 | return irccmp(command, entry->cmd); | |
470 | } | |
471 | ||
472 | static int | |
473 | m_cap(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
474 | { | |
475 | struct clicap_cmd *cmd; | |
476 | ||
477 | if(!(cmd = bsearch(parv[1], clicap_cmdlist, | |
478 | sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd), | |
479 | sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search))) | |
480 | { | |
481 | sendto_one(source_p, form_str(ERR_INVALIDCAPCMD), | |
48a038f4 JT |
482 | me.name, EmptyString(source_p->name) ? "*" : source_p->name, |
483 | parv[1]); | |
212380e3 AC |
484 | return 0; |
485 | } | |
486 | ||
487 | (cmd->func)(source_p, parv[2]); | |
488 | return 0; | |
489 | } |