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