]> jfr.im git - irc/freenode/solanum.git/blame - modules/m_cap.c
m_cap: refactor clicap_generate to use multiline
[irc/freenode/solanum.git] / modules / m_cap.c
CommitLineData
212380e3 1/* modules/m_cap.c
55abcbb2 2 *
212380e3
WP
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
WP
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
WP
30 */
31
32#include "stdinc.h"
212380e3
WP
33#include "class.h"
34#include "client.h"
4562c604 35#include "match.h"
212380e3
WP
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
WP
49typedef int (*bqcmp)(const void *, const void *);
50
3c7d6fcc 51static void m_cap(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
WP
52
53struct Message cap_msgtab = {
7baa37a9 54 "CAP", 0, 0, 0, 0,
212380e3
WP
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
WP
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
WP
67{
68 struct ClientCapability *clicap;
69
455d2750
WP
70 /* orphaned caps shouldn't be visible */
71 if (cap->flags & CAP_ORPHANED)
72 return 0;
73
193d4db3
WP
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
WP
82}
83
212380e3
WP
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
WP
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
WP
98 char *s;
99
100 *negate = 0;
101
102 if(data)
103 {
f427c8b0 104 rb_strlcpy(buf, data, sizeof(buf));
212380e3
WP
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
WP
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
WP
150 * Outputs: None
151 */
152static void
ceabbbbf 153clicap_generate(struct Client *source_p, const char *subcmd, int flags)
212380e3 154{
56c85304
DF
155 const char *str_cont = "* :";
156 const char *str_final = ":";
32df5e96 157 struct CapabilityEntry *entry;
4177311e 158 rb_dictionary_iter iter;
56c85304
DF
159 bool multiline_ret;
160 enum multiline_item_result multiline_item_ret;
212380e3 161
56c85304 162 multiline_ret = send_multiline_init(source_p, " ", ":%s CAP %s %s %s",
55abcbb2
KB
163 me.name,
164 EmptyString(source_p->name) ? "*" : source_p->name,
56c85304
DF
165 subcmd,
166 (source_p->flags & FLAGS_CLICAP_DATA) ? str_cont : str_final);
212380e3 167
212380e3 168 /* shortcut, nothing to do */
56c85304
DF
169 if (flags == -1 || !multiline_ret) {
170 sendto_one(source_p, ":%s CAP %s %s %s",
171 me.name,
172 EmptyString(source_p->name) ? "*" : source_p->name,
173 subcmd,
174 str_final);
212380e3
WP
175 return;
176 }
177
738b5d29 178 for (int pass = 0; pass < 2; pass++)
3fb264ef 179 RB_DICTIONARY_FOREACH(entry, &iter, cli_capindex->cap_dict) {
ddf62b10
WP
180 struct ClientCapability *clicap = entry->ownerdata;
181 const char *data = NULL;
182
738b5d29
EK
183 if (pass == 0 && !HasCapabilityFlag(entry, CLICAP_FLAGS_PRIORITY))
184 continue;
185 else if (pass == 1 && HasCapabilityFlag(entry, CLICAP_FLAGS_PRIORITY))
186 continue;
187
6ac21a70
EK
188 if (!IsCapableEntry(source_p, entry) && ConfigFileEntry.hidden_caps != NULL)
189 {
190 size_t i;
191 for (i = 0; ConfigFileEntry.hidden_caps[i] != NULL; i++)
192 {
193 if (!rb_strcasecmp(entry->cap, ConfigFileEntry.hidden_caps[i]))
194 break;
195 }
196 if (ConfigFileEntry.hidden_caps[i] != NULL)
197 continue;
198 }
199
3fb264ef 200 if (flags && !IsCapableEntry(source_p, entry))
ceabbbbf 201 continue;
212380e3 202
38ffccf8 203 if (!clicap_visible(source_p, entry))
193d4db3 204 continue;
bbce62d2 205
56c85304
DF
206 if (source_p->flags & FLAGS_CLICAP_DATA) {
207 if (!flags && clicap != NULL && clicap->data != NULL)
208 data = clicap->data(source_p);
ddf62b10 209
56c85304 210 multiline_item_ret = send_multiline_item(source_p, "%s%s%s",
3fb264ef 211 entry->cap,
56c85304
DF
212 data != NULL ? "=" : "",
213 data != NULL ? data : "");
214
215 if (multiline_item_ret == MULTILINE_FAILURE)
216 return;
217 } else {
218 multiline_item_ret = send_multiline_item(source_p, "%s", entry->cap);
219
220 if (multiline_item_ret != MULTILINE_SUCCESS) {
221 send_multiline_reset();
222 return;
3fb264ef 223 }
212380e3 224 }
212380e3
WP
225 }
226
56c85304
DF
227 send_multiline_fini(source_p, ":%s CAP %s %s %s",
228 me.name,
229 EmptyString(source_p->name) ? "*" : source_p->name,
230 subcmd,
231 str_final);
212380e3
WP
232}
233
234static void
235cap_ack(struct Client *source_p, const char *arg)
236{
32df5e96 237 struct CapabilityEntry *cap;
212380e3
WP
238 int capadd = 0, capdel = 0;
239 int finished = 0, negate;
240
241 if(EmptyString(arg))
242 return;
243
244 for(cap = clicap_find(arg, &negate, &finished); cap;
245 cap = clicap_find(NULL, &negate, &finished))
246 {
247 /* sent an ACK for something they havent REQd */
32df5e96 248 if(!IsCapableEntry(source_p, cap))
212380e3
WP
249 continue;
250
251 if(negate)
252 {
253 /* dont let them ack something sticky off */
32df5e96 254 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
212380e3
WP
255 continue;
256
32df5e96 257 capdel |= (1 << cap->value);
212380e3
WP
258 }
259 else
32df5e96 260 capadd |= (1 << cap->value);
212380e3
WP
261 }
262
263 source_p->localClient->caps |= capadd;
264 source_p->localClient->caps &= ~capdel;
265}
266
212380e3
WP
267static void
268cap_end(struct Client *source_p, const char *arg)
269{
270 if(IsRegistered(source_p))
271 return;
272
095328a7 273 source_p->flags &= ~FLAGS_CLICAP;
212380e3 274
1fb3b1e1 275 if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER)
212380e3 276 {
21251822 277 register_local_user(source_p, source_p);
212380e3
WP
278 }
279}
280
281static void
282cap_list(struct Client *source_p, const char *arg)
283{
284 /* list of what theyre currently using */
285 clicap_generate(source_p, "LIST",
ceabbbbf 286 source_p->localClient->caps ? source_p->localClient->caps : -1);
212380e3
WP
287}
288
289static void
290cap_ls(struct Client *source_p, const char *arg)
291{
3fb264ef
SA
292 int caps_version = 301;
293
212380e3 294 if(!IsRegistered(source_p))
095328a7 295 source_p->flags |= FLAGS_CLICAP;
212380e3 296
3fb264ef
SA
297 if (!EmptyString(arg)) {
298 caps_version = atoi(arg);
299 }
300
301 if (caps_version >= 302) {
ddf62b10 302 source_p->flags |= FLAGS_CLICAP_DATA;
ba316ed5
WP
303 source_p->localClient->caps |= CLICAP_CAP_NOTIFY;
304 }
ddf62b10 305
212380e3 306 /* list of what we support */
ceabbbbf 307 clicap_generate(source_p, "LS", 0);
212380e3
WP
308}
309
310static void
311cap_req(struct Client *source_p, const char *arg)
312{
5d727361 313 char ack_buf[DATALEN+1];
32df5e96 314 struct CapabilityEntry *cap;
212380e3
WP
315 int capadd = 0, capdel = 0;
316 int finished = 0, negate;
5d727361 317 int ret;
71f10f83 318 hook_data_cap_change hdata;
212380e3
WP
319
320 if(!IsRegistered(source_p))
095328a7 321 source_p->flags |= FLAGS_CLICAP;
212380e3
WP
322
323 if(EmptyString(arg))
324 return;
325
5d727361
DF
326 ret = snprintf(ack_buf, sizeof ack_buf, ":%s CAP %s ACK :%s",
327 me.name, EmptyString(source_p->name)? "*" : source_p->name, arg);
212380e3 328
5d727361
DF
329 if (ret < 0 || ret > DATALEN)
330 {
331 sendto_one(source_p, ":%s CAP %s NAK :%s",
332 me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg);
333 return;
334 }
212380e3
WP
335
336 for(cap = clicap_find(arg, &negate, &finished); cap;
337 cap = clicap_find(NULL, &negate, &finished))
338 {
212380e3
WP
339 if(negate)
340 {
32df5e96 341 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
212380e3
WP
342 {
343 finished = 0;
344 break;
345 }
346
32df5e96 347 capdel |= (1 << cap->value);
212380e3
WP
348 }
349 else
350 {
38ffccf8 351 if (!clicap_visible(source_p, cap))
bbce62d2 352 {
193d4db3
WP
353 finished = 0;
354 break;
bbce62d2
MT
355 }
356
32df5e96 357 capadd |= (1 << cap->value);
212380e3
WP
358 }
359
212380e3
WP
360 }
361
362 if(!finished)
363 {
364 sendto_one(source_p, ":%s CAP %s NAK :%s",
365 me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg);
366 return;
367 }
368
5d727361 369 sendto_one(source_p, "%s", ack_buf);
212380e3 370
71f10f83
EK
371 hdata.client = source_p;
372 hdata.oldcaps = source_p->localClient->caps;
373 hdata.add = capadd;
374 hdata.del = capdel;
375
212380e3
WP
376 source_p->localClient->caps |= capadd;
377 source_p->localClient->caps &= ~capdel;
71f10f83
EK
378
379 call_hook(h_cap_change, &hdata);
212380e3
WP
380}
381
382static struct clicap_cmd
383{
384 const char *cmd;
385 void (*func)(struct Client *source_p, const char *arg);
386} clicap_cmdlist[] = {
387 /* This list *MUST* be in alphabetical order */
388 { "ACK", cap_ack },
212380e3
WP
389 { "END", cap_end },
390 { "LIST", cap_list },
391 { "LS", cap_ls },
392 { "REQ", cap_req },
393};
394
395static int
396clicap_cmd_search(const char *command, struct clicap_cmd *entry)
397{
398 return irccmp(command, entry->cmd);
399}
400
3c7d6fcc 401static void
428ca87b 402m_cap(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
WP
403{
404 struct clicap_cmd *cmd;
405
406 if(!(cmd = bsearch(parv[1], clicap_cmdlist,
407 sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd),
408 sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search)))
409 {
410 sendto_one(source_p, form_str(ERR_INVALIDCAPCMD),
48a038f4
JT
411 me.name, EmptyString(source_p->name) ? "*" : source_p->name,
412 parv[1]);
3c7d6fcc 413 return;
212380e3
WP
414 }
415
416 (cmd->func)(source_p, parv[2]);
212380e3 417}