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