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