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