]> jfr.im git - solanum.git/blob - modules/m_cap.c
Migrate remaining modules to AV2
[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 * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1.Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2.Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3.The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
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 #include "send.h"
44 #include "s_conf.h"
45 #include "hash.h"
46
47 typedef int (*bqcmp)(const void *, const void *);
48
49 static int m_cap(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
50
51 struct Message cap_msgtab = {
52 "CAP", 0, 0, 0, 0,
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_AV2(cap, NULL, NULL, cap_clist, NULL, NULL, NULL, NULL, NULL);
58
59 #define IsCapableEntry(c, e) IsCapable(c, 1 << (e)->value)
60 #define HasCapabilityFlag(c, f) (c->ownerdata != NULL && (((struct ClientCapability *)c->ownerdata)->flags & (f)) == f)
61
62 static inline int
63 clicap_visible(struct Client *client_p, const struct CapabilityEntry *cap)
64 {
65 struct ClientCapability *clicap;
66
67 /* orphaned caps shouldn't be visible */
68 if (cap->flags & CAP_ORPHANED)
69 return 0;
70
71 if (cap->ownerdata == NULL)
72 return 1;
73
74 clicap = cap->ownerdata;
75 if (clicap->visible == NULL)
76 return 1;
77
78 return clicap->visible(client_p);
79 }
80
81 /* clicap_find()
82 * Used iteratively over a buffer, extracts individual cap tokens.
83 *
84 * Inputs: buffer to start iterating over (NULL to iterate over existing buf)
85 * int pointer to whether the cap token is negated
86 * int pointer to whether we finish with success
87 * Ouputs: Cap entry if found, NULL otherwise.
88 */
89 static struct CapabilityEntry *
90 clicap_find(const char *data, int *negate, int *finished)
91 {
92 static char buf[BUFSIZE];
93 static char *p;
94 struct CapabilityEntry *cap;
95 char *s;
96
97 *negate = 0;
98
99 if(data)
100 {
101 rb_strlcpy(buf, data, sizeof(buf));
102 p = buf;
103 }
104
105 if(*finished)
106 return NULL;
107
108 /* skip any whitespace */
109 while(*p && IsSpace(*p))
110 p++;
111
112 if(EmptyString(p))
113 {
114 *finished = 1;
115 return NULL;
116 }
117
118 if(*p == '-')
119 {
120 *negate = 1;
121 p++;
122
123 /* someone sent a '-' without a parameter.. */
124 if(*p == '\0')
125 return NULL;
126 }
127
128 if((s = strchr(p, ' ')))
129 *s++ = '\0';
130
131 if((cap = capability_find(cli_capindex, p)) != NULL)
132 {
133 if(s)
134 p = s;
135 else
136 *finished = 1;
137 }
138
139 return cap;
140 }
141
142 /* clicap_generate()
143 * Generates a list of capabilities.
144 *
145 * Inputs: client to send to, subcmd to send,
146 * flags to match against: 0 to do none, -1 if client has no flags,
147 * int to whether we are doing CAP CLEAR
148 * Outputs: None
149 */
150 static void
151 clicap_generate(struct Client *source_p, const char *subcmd, int flags, int clear)
152 {
153 char buf[BUFSIZE] = { 0 };
154 char capbuf[BUFSIZE] = { 0 };
155 char *p;
156 int buflen = 0;
157 int curlen, mlen;
158 struct CapabilityEntry *entry;
159 struct DictionaryIter iter;
160
161 mlen = sprintf(buf, ":%s CAP %s %s",
162 me.name,
163 EmptyString(source_p->name) ? "*" : source_p->name,
164 subcmd);
165
166 p = capbuf;
167 buflen = mlen;
168
169 /* shortcut, nothing to do */
170 if(flags == -1)
171 {
172 sendto_one(source_p, "%s :", buf);
173 return;
174 }
175
176 DICTIONARY_FOREACH(entry, &iter, cli_capindex->cap_dict)
177 {
178 size_t caplen = 0;
179 struct ClientCapability *clicap = entry->ownerdata;
180 const char *data = NULL;
181
182 if(flags)
183 {
184 if(!IsCapableEntry(source_p, entry))
185 continue;
186 /* they are capable of this, check sticky */
187 else if(clear && HasCapabilityFlag(entry, CLICAP_FLAGS_STICKY))
188 continue;
189 }
190
191 if (!clicap_visible(source_p, entry))
192 continue;
193
194 caplen = strlen(entry->cap);
195 if (!flags && (source_p->flags & FLAGS_CLICAP_DATA) && clicap != NULL && clicap->data != NULL)
196 data = clicap->data(source_p);
197
198 if (data != NULL)
199 caplen += strlen(data) + 1;
200
201 /* \r\n\0, possible "-~=", space, " *" */
202 if(buflen + caplen >= BUFSIZE - 10)
203 {
204 /* remove our trailing space -- if buflen == mlen
205 * here, we didnt even succeed in adding one.
206 */
207 if(buflen != mlen)
208 *(p - 1) = '\0';
209 else
210 *p = '\0';
211
212 sendto_one(source_p, "%s * :%s", buf, capbuf);
213
214 p = capbuf;
215 buflen = mlen;
216 memset(capbuf, 0, sizeof capbuf);
217 }
218
219 if(clear)
220 {
221 *p++ = '-';
222 buflen++;
223 }
224
225 if (data == NULL)
226 curlen = sprintf(p, "%s ", entry->cap);
227 else
228 curlen = sprintf(p, "%s=%s ", entry->cap, data);
229
230 p += curlen;
231 buflen += curlen;
232 }
233
234 /* remove trailing space */
235 if(buflen != mlen)
236 *(p - 1) = '\0';
237 else
238 *p = '\0';
239
240 sendto_one(source_p, "%s :%s", buf, capbuf);
241 }
242
243 static void
244 cap_ack(struct Client *source_p, const char *arg)
245 {
246 struct CapabilityEntry *cap;
247 int capadd = 0, capdel = 0;
248 int finished = 0, negate;
249
250 if(EmptyString(arg))
251 return;
252
253 for(cap = clicap_find(arg, &negate, &finished); cap;
254 cap = clicap_find(NULL, &negate, &finished))
255 {
256 /* sent an ACK for something they havent REQd */
257 if(!IsCapableEntry(source_p, cap))
258 continue;
259
260 if(negate)
261 {
262 /* dont let them ack something sticky off */
263 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
264 continue;
265
266 capdel |= (1 << cap->value);
267 }
268 else
269 capadd |= (1 << cap->value);
270 }
271
272 source_p->localClient->caps |= capadd;
273 source_p->localClient->caps &= ~capdel;
274 }
275
276 static void
277 cap_clear(struct Client *source_p, const char *arg)
278 {
279 clicap_generate(source_p, "ACK",
280 source_p->localClient->caps ? source_p->localClient->caps : -1, 1);
281
282 source_p->localClient->caps = 0;
283 }
284
285 static void
286 cap_end(struct Client *source_p, const char *arg)
287 {
288 if(IsRegistered(source_p))
289 return;
290
291 source_p->flags &= ~FLAGS_CLICAP;
292
293 if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER)
294 {
295 register_local_user(source_p, source_p);
296 }
297 }
298
299 static void
300 cap_list(struct Client *source_p, const char *arg)
301 {
302 /* list of what theyre currently using */
303 clicap_generate(source_p, "LIST",
304 source_p->localClient->caps ? source_p->localClient->caps : -1, 0);
305 }
306
307 static void
308 cap_ls(struct Client *source_p, const char *arg)
309 {
310 if(!IsRegistered(source_p))
311 source_p->flags |= FLAGS_CLICAP;
312
313 if (arg != NULL && !strcmp(arg, "302"))
314 {
315 source_p->flags |= FLAGS_CLICAP_DATA;
316 source_p->localClient->caps |= CLICAP_CAP_NOTIFY;
317 }
318
319 /* list of what we support */
320 clicap_generate(source_p, "LS", 0, 0);
321 }
322
323 static void
324 cap_req(struct Client *source_p, const char *arg)
325 {
326 char buf[BUFSIZE];
327 char pbuf[2][BUFSIZE];
328 struct CapabilityEntry *cap;
329 int buflen, plen;
330 int i = 0;
331 int capadd = 0, capdel = 0;
332 int finished = 0, negate;
333
334 if(!IsRegistered(source_p))
335 source_p->flags |= FLAGS_CLICAP;
336
337 if(EmptyString(arg))
338 return;
339
340 buflen = snprintf(buf, sizeof(buf), ":%s CAP %s ACK",
341 me.name, EmptyString(source_p->name) ? "*" : source_p->name);
342
343 pbuf[0][0] = '\0';
344 plen = 0;
345
346 for(cap = clicap_find(arg, &negate, &finished); cap;
347 cap = clicap_find(NULL, &negate, &finished))
348 {
349 size_t namelen = strlen(cap->cap);
350
351 /* filled the first array, but cant send it in case the
352 * request fails. one REQ should never fill more than two
353 * buffers --fl
354 */
355 if(buflen + plen + namelen + 6 >= BUFSIZE)
356 {
357 pbuf[1][0] = '\0';
358 plen = 0;
359 i = 1;
360 }
361
362 if(negate)
363 {
364 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
365 {
366 finished = 0;
367 break;
368 }
369
370 strcat(pbuf[i], "-");
371 plen++;
372
373 capdel |= (1 << cap->value);
374 }
375 else
376 {
377 if (!clicap_visible(source_p, cap))
378 {
379 finished = 0;
380 break;
381 }
382
383 capadd |= (1 << cap->value);
384 }
385
386 /* XXX this probably should exclude REQACK'd caps from capadd/capdel, but keep old behaviour for now */
387 if(HasCapabilityFlag(cap, CLICAP_FLAGS_REQACK))
388 {
389 strcat(pbuf[i], "~");
390 plen++;
391 }
392
393 strcat(pbuf[i], cap->cap);
394 if (!finished) {
395 strcat(pbuf[i], " ");
396 plen += (namelen + 1);
397 }
398 }
399
400 if(!finished)
401 {
402 sendto_one(source_p, ":%s CAP %s NAK :%s",
403 me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg);
404 return;
405 }
406
407 if(i)
408 {
409 sendto_one(source_p, "%s * :%s", buf, pbuf[0]);
410 sendto_one(source_p, "%s :%s", buf, pbuf[1]);
411 }
412 else
413 sendto_one(source_p, "%s :%s", buf, pbuf[0]);
414
415 source_p->localClient->caps |= capadd;
416 source_p->localClient->caps &= ~capdel;
417 }
418
419 static struct clicap_cmd
420 {
421 const char *cmd;
422 void (*func)(struct Client *source_p, const char *arg);
423 } clicap_cmdlist[] = {
424 /* This list *MUST* be in alphabetical order */
425 { "ACK", cap_ack },
426 { "CLEAR", cap_clear },
427 { "END", cap_end },
428 { "LIST", cap_list },
429 { "LS", cap_ls },
430 { "REQ", cap_req },
431 };
432
433 static int
434 clicap_cmd_search(const char *command, struct clicap_cmd *entry)
435 {
436 return irccmp(command, entry->cmd);
437 }
438
439 static int
440 m_cap(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
441 {
442 struct clicap_cmd *cmd;
443
444 if(!(cmd = bsearch(parv[1], clicap_cmdlist,
445 sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd),
446 sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search)))
447 {
448 sendto_one(source_p, form_str(ERR_INVALIDCAPCMD),
449 me.name, EmptyString(source_p->name) ? "*" : source_p->name,
450 parv[1]);
451 return 0;
452 }
453
454 (cmd->func)(source_p, parv[2]);
455 return 0;
456 }