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