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