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