]> jfr.im git - solanum.git/blame - modules/m_cap.c
Combine stats A output parameters (#35)
[solanum.git] / modules / m_cap.c
CommitLineData
212380e3 1/* modules/m_cap.c
55abcbb2 2 *
212380e3
AC
3 * Copyright (C) 2005 Lee Hardy <lee@leeh.co.uk>
4 * Copyright (C) 2005 ircd-ratbox development team
32df5e96 5 * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
212380e3
AC
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.
212380e3
AC
30 */
31
32#include "stdinc.h"
212380e3
AC
33#include "class.h"
34#include "client.h"
4562c604 35#include "match.h"
212380e3
AC
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"
77d3d2db 43#include "send.h"
bbce62d2 44#include "s_conf.h"
dafbd7fa 45#include "hash.h"
212380e3 46
eeabf33a
EM
47static const char cap_desc[] = "Provides the commands used for client capability negotiation";
48
212380e3
AC
49typedef int (*bqcmp)(const void *, const void *);
50
3c7d6fcc 51static void m_cap(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
52
53struct Message cap_msgtab = {
7baa37a9 54 "CAP", 0, 0, 0, 0,
212380e3
AC
55 {{m_cap, 2}, {m_cap, 2}, mg_ignore, mg_ignore, mg_ignore, {m_cap, 2}}
56};
57
58mapi_clist_av1 cap_clist[] = { &cap_msgtab, NULL };
5544da98 59
5544da98 60DECLARE_MODULE_AV2(cap, NULL, NULL, cap_clist, NULL, NULL, NULL, NULL, cap_desc);
212380e3 61
32df5e96
AC
62#define IsCapableEntry(c, e) IsCapable(c, 1 << (e)->value)
63#define HasCapabilityFlag(c, f) (c->ownerdata != NULL && (((struct ClientCapability *)c->ownerdata)->flags & (f)) == f)
212380e3 64
193d4db3 65static inline int
38ffccf8 66clicap_visible(struct Client *client_p, const struct CapabilityEntry *cap)
193d4db3
AC
67{
68 struct ClientCapability *clicap;
69
455d2750
AC
70 /* orphaned caps shouldn't be visible */
71 if (cap->flags & CAP_ORPHANED)
72 return 0;
73
193d4db3
AC
74 if (cap->ownerdata == NULL)
75 return 1;
76
77 clicap = cap->ownerdata;
78 if (clicap->visible == NULL)
79 return 1;
80
38ffccf8 81 return clicap->visible(client_p);
193d4db3
AC
82}
83
212380e3
AC
84/* clicap_find()
85 * Used iteratively over a buffer, extracts individual cap tokens.
86 *
87 * Inputs: buffer to start iterating over (NULL to iterate over existing buf)
88 * int pointer to whether the cap token is negated
89 * int pointer to whether we finish with success
90 * Ouputs: Cap entry if found, NULL otherwise.
91 */
32df5e96 92static struct CapabilityEntry *
212380e3
AC
93clicap_find(const char *data, int *negate, int *finished)
94{
95 static char buf[BUFSIZE];
96 static char *p;
32df5e96 97 struct CapabilityEntry *cap;
212380e3
AC
98 char *s;
99
100 *negate = 0;
101
102 if(data)
103 {
f427c8b0 104 rb_strlcpy(buf, data, sizeof(buf));
212380e3
AC
105 p = buf;
106 }
107
108 if(*finished)
109 return NULL;
110
111 /* skip any whitespace */
112 while(*p && IsSpace(*p))
113 p++;
114
115 if(EmptyString(p))
116 {
117 *finished = 1;
118 return NULL;
119 }
120
121 if(*p == '-')
122 {
123 *negate = 1;
124 p++;
125
126 /* someone sent a '-' without a parameter.. */
127 if(*p == '\0')
128 return NULL;
129 }
130
131 if((s = strchr(p, ' ')))
132 *s++ = '\0';
133
32df5e96 134 if((cap = capability_find(cli_capindex, p)) != NULL)
212380e3
AC
135 {
136 if(s)
137 p = s;
138 else
139 *finished = 1;
140 }
141
142 return cap;
143}
144
145/* clicap_generate()
146 * Generates a list of capabilities.
147 *
148 * Inputs: client to send to, subcmd to send,
ceabbbbf 149 * flags to match against: 0 to do none, -1 if client has no flags
212380e3
AC
150 * Outputs: None
151 */
152static void
ceabbbbf 153clicap_generate(struct Client *source_p, const char *subcmd, int flags)
212380e3 154{
3fb264ef
SA
155 static char buf_prefix[DATALEN + 1];
156 static char buf_list[DATALEN + 1];
157 const char *str_cont = " * :";
158 const char *str_final = " :";
159 int len_prefix;
160 int max_list;
32df5e96 161 struct CapabilityEntry *entry;
4177311e 162 rb_dictionary_iter iter;
212380e3 163
3fb264ef
SA
164 buf_prefix[0] = '\0';
165 len_prefix = rb_snprintf_try_append(buf_prefix, sizeof(buf_prefix),
166 ":%s CAP %s %s",
55abcbb2
KB
167 me.name,
168 EmptyString(source_p->name) ? "*" : source_p->name,
212380e3
AC
169 subcmd);
170
212380e3 171 /* shortcut, nothing to do */
3fb264ef
SA
172 if (flags == -1 || len_prefix < 0) {
173 sendto_one(source_p, "%s%s", buf_prefix, str_final);
212380e3
AC
174 return;
175 }
176
3fb264ef
SA
177 buf_list[0] = '\0';
178 max_list = sizeof(buf_prefix) - len_prefix - strlen(str_cont);
179
738b5d29 180 for (int pass = 0; pass < 2; pass++)
3fb264ef 181 RB_DICTIONARY_FOREACH(entry, &iter, cli_capindex->cap_dict) {
ddf62b10
AC
182 struct ClientCapability *clicap = entry->ownerdata;
183 const char *data = NULL;
184
738b5d29
EK
185 if (pass == 0 && !HasCapabilityFlag(entry, CLICAP_FLAGS_PRIORITY))
186 continue;
187 else if (pass == 1 && HasCapabilityFlag(entry, CLICAP_FLAGS_PRIORITY))
188 continue;
189
3fb264ef 190 if (flags && !IsCapableEntry(source_p, entry))
ceabbbbf 191 continue;
212380e3 192
38ffccf8 193 if (!clicap_visible(source_p, entry))
193d4db3 194 continue;
bbce62d2 195
ddf62b10 196 if (!flags && (source_p->flags & FLAGS_CLICAP_DATA) && clicap != NULL && clicap->data != NULL)
38ffccf8 197 data = clicap->data(source_p);
ddf62b10 198
3fb264ef
SA
199 for (int attempts = 0; attempts < 2; attempts++) {
200 if (rb_snprintf_try_append(buf_list, max_list, "%s%s%s%s",
201 buf_list[0] == '\0' ? "" : " ", /* space between caps */
202 entry->cap,
203 data != NULL ? "=" : "", /* '=' between cap and data */
204 data != NULL ? data : "") < 0
205 && buf_list[0] != '\0') {
206
207 if (!(source_p->flags & FLAGS_CLICAP_DATA)) {
208 /* the client doesn't support multiple lines */
f3439650 209 continue;
3fb264ef
SA
210 }
211
212 /* doesn't fit in the buffer, output what we have */
213 sendto_one(source_p, "%s%s%s", buf_prefix, str_cont, buf_list);
214
215 /* reset the buffer and go around the loop one more time */
216 buf_list[0] = '\0';
217 } else {
218 break;
219 }
212380e3 220 }
212380e3
AC
221 }
222
3fb264ef 223 sendto_one(source_p, "%s%s%s", buf_prefix, str_final, buf_list);
212380e3
AC
224}
225
226static void
227cap_ack(struct Client *source_p, const char *arg)
228{
32df5e96 229 struct CapabilityEntry *cap;
212380e3
AC
230 int capadd = 0, capdel = 0;
231 int finished = 0, negate;
232
233 if(EmptyString(arg))
234 return;
235
236 for(cap = clicap_find(arg, &negate, &finished); cap;
237 cap = clicap_find(NULL, &negate, &finished))
238 {
239 /* sent an ACK for something they havent REQd */
32df5e96 240 if(!IsCapableEntry(source_p, cap))
212380e3
AC
241 continue;
242
243 if(negate)
244 {
245 /* dont let them ack something sticky off */
32df5e96 246 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
212380e3
AC
247 continue;
248
32df5e96 249 capdel |= (1 << cap->value);
212380e3
AC
250 }
251 else
32df5e96 252 capadd |= (1 << cap->value);
212380e3
AC
253 }
254
255 source_p->localClient->caps |= capadd;
256 source_p->localClient->caps &= ~capdel;
257}
258
212380e3
AC
259static void
260cap_end(struct Client *source_p, const char *arg)
261{
262 if(IsRegistered(source_p))
263 return;
264
095328a7 265 source_p->flags &= ~FLAGS_CLICAP;
212380e3 266
1fb3b1e1 267 if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER)
212380e3 268 {
21251822 269 register_local_user(source_p, source_p);
212380e3
AC
270 }
271}
272
273static void
274cap_list(struct Client *source_p, const char *arg)
275{
276 /* list of what theyre currently using */
277 clicap_generate(source_p, "LIST",
ceabbbbf 278 source_p->localClient->caps ? source_p->localClient->caps : -1);
212380e3
AC
279}
280
281static void
282cap_ls(struct Client *source_p, const char *arg)
283{
3fb264ef
SA
284 int caps_version = 301;
285
212380e3 286 if(!IsRegistered(source_p))
095328a7 287 source_p->flags |= FLAGS_CLICAP;
212380e3 288
3fb264ef
SA
289 if (!EmptyString(arg)) {
290 caps_version = atoi(arg);
291 }
292
293 if (caps_version >= 302) {
ddf62b10 294 source_p->flags |= FLAGS_CLICAP_DATA;
ba316ed5
AC
295 source_p->localClient->caps |= CLICAP_CAP_NOTIFY;
296 }
ddf62b10 297
212380e3 298 /* list of what we support */
ceabbbbf 299 clicap_generate(source_p, "LS", 0);
212380e3
AC
300}
301
302static void
303cap_req(struct Client *source_p, const char *arg)
304{
3fb264ef
SA
305 static char buf_prefix[DATALEN + 1];
306 static char buf_list[2][DATALEN + 1];
307 const char *str_cont = " * :";
308 const char *str_final = " :";
309 int len_prefix;
310 int max_list;
32df5e96 311 struct CapabilityEntry *cap;
212380e3
AC
312 int i = 0;
313 int capadd = 0, capdel = 0;
314 int finished = 0, negate;
71f10f83 315 hook_data_cap_change hdata;
212380e3
AC
316
317 if(!IsRegistered(source_p))
095328a7 318 source_p->flags |= FLAGS_CLICAP;
212380e3
AC
319
320 if(EmptyString(arg))
321 return;
322
3fb264ef
SA
323 buf_prefix[0] = '\0';
324 len_prefix = rb_snprintf_try_append(buf_prefix, sizeof(buf_prefix),
325 ":%s CAP %s ACK",
326 me.name,
327 EmptyString(source_p->name) ? "*" : source_p->name);
212380e3 328
3fb264ef
SA
329 buf_list[0][0] = '\0';
330 buf_list[1][0] = '\0';
331 max_list = sizeof(buf_prefix) - len_prefix - strlen(str_cont);
212380e3
AC
332
333 for(cap = clicap_find(arg, &negate, &finished); cap;
334 cap = clicap_find(NULL, &negate, &finished))
335 {
3fb264ef 336 const char *type;
212380e3
AC
337
338 if(negate)
339 {
32df5e96 340 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
212380e3
AC
341 {
342 finished = 0;
343 break;
344 }
345
3fb264ef 346 type = "-";
32df5e96 347 capdel |= (1 << cap->value);
212380e3
AC
348 }
349 else
350 {
38ffccf8 351 if (!clicap_visible(source_p, cap))
bbce62d2 352 {
193d4db3
AC
353 finished = 0;
354 break;
bbce62d2
MT
355 }
356
3fb264ef 357 type = "";
32df5e96 358 capadd |= (1 << cap->value);
212380e3
AC
359 }
360
3fb264ef
SA
361 for (int attempts = 0; attempts < 2; attempts++) {
362 if (rb_snprintf_try_append(buf_list[i], max_list, "%s%s%s",
363 buf_list[i][0] == '\0' ? "" : " ", /* space between caps */
364 type,
365 cap->cap) < 0
366 && buf_list[i][0] != '\0') {
367
368 if (!(source_p->flags & FLAGS_CLICAP_DATA)) {
369 /* the client doesn't support multiple lines */
370 break;
371 }
372
373 /* doesn't fit in the buffer, move to the next one */
374 if (i < 2) {
375 i++;
376 } else {
377 /* uh-oh */
378 break;
379 }
380
381 /* reset the buffer and go around the loop one more time */
382 buf_list[i][0] = '\0';
383 } else {
384 break;
385 }
d855e13e 386 }
212380e3
AC
387 }
388
389 if(!finished)
390 {
391 sendto_one(source_p, ":%s CAP %s NAK :%s",
392 me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg);
393 return;
394 }
395
3fb264ef
SA
396 if (i) {
397 sendto_one(source_p, "%s%s%s", buf_prefix, str_cont, buf_list[0]);
398 sendto_one(source_p, "%s%s%s", buf_prefix, str_final, buf_list[1]);
399 } else {
400 sendto_one(source_p, "%s%s%s", buf_prefix, str_final, buf_list[0]);
212380e3 401 }
212380e3 402
71f10f83
EK
403 hdata.client = source_p;
404 hdata.oldcaps = source_p->localClient->caps;
405 hdata.add = capadd;
406 hdata.del = capdel;
407
212380e3
AC
408 source_p->localClient->caps |= capadd;
409 source_p->localClient->caps &= ~capdel;
71f10f83
EK
410
411 call_hook(h_cap_change, &hdata);
212380e3
AC
412}
413
414static struct clicap_cmd
415{
416 const char *cmd;
417 void (*func)(struct Client *source_p, const char *arg);
418} clicap_cmdlist[] = {
419 /* This list *MUST* be in alphabetical order */
420 { "ACK", cap_ack },
212380e3
AC
421 { "END", cap_end },
422 { "LIST", cap_list },
423 { "LS", cap_ls },
424 { "REQ", cap_req },
425};
426
427static int
428clicap_cmd_search(const char *command, struct clicap_cmd *entry)
429{
430 return irccmp(command, entry->cmd);
431}
432
3c7d6fcc 433static void
428ca87b 434m_cap(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
435{
436 struct clicap_cmd *cmd;
437
438 if(!(cmd = bsearch(parv[1], clicap_cmdlist,
439 sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd),
440 sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search)))
441 {
442 sendto_one(source_p, form_str(ERR_INVALIDCAPCMD),
48a038f4
JT
443 me.name, EmptyString(source_p->name) ? "*" : source_p->name,
444 parv[1]);
3c7d6fcc 445 return;
212380e3
AC
446 }
447
448 (cmd->func)(source_p, parv[2]);
212380e3 449}