]> jfr.im git - solanum.git/blob - modules/m_cap.c
m_cap: Fix CAP LS generation
[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 static const char cap_desc[] = "Provides the commands used for client capability negotiation";
48
49 typedef int (*bqcmp)(const void *, const void *);
50
51 static void m_cap(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
52
53 struct Message cap_msgtab = {
54 "CAP", 0, 0, 0, 0,
55 {{m_cap, 2}, {m_cap, 2}, mg_ignore, mg_ignore, mg_ignore, {m_cap, 2}}
56 };
57
58 mapi_clist_av1 cap_clist[] = { &cap_msgtab, NULL };
59
60 DECLARE_MODULE_AV2(cap, NULL, NULL, cap_clist, NULL, NULL, NULL, NULL, cap_desc);
61
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)
64
65 static inline int
66 clicap_visible(struct Client *client_p, const struct CapabilityEntry *cap)
67 {
68 struct ClientCapability *clicap;
69
70 /* orphaned caps shouldn't be visible */
71 if (cap->flags & CAP_ORPHANED)
72 return 0;
73
74 if (cap->ownerdata == NULL)
75 return 1;
76
77 clicap = cap->ownerdata;
78 if (clicap->visible == NULL)
79 return 1;
80
81 return clicap->visible(client_p);
82 }
83
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 */
92 static struct CapabilityEntry *
93 clicap_find(const char *data, int *negate, int *finished)
94 {
95 static char buf[BUFSIZE];
96 static char *p;
97 struct CapabilityEntry *cap;
98 char *s;
99
100 *negate = 0;
101
102 if(data)
103 {
104 rb_strlcpy(buf, data, sizeof(buf));
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
134 if((cap = capability_find(cli_capindex, p)) != NULL)
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,
149 * flags to match against: 0 to do none, -1 if client has no flags
150 * Outputs: None
151 */
152 static void
153 clicap_generate(struct Client *source_p, const char *subcmd, int flags)
154 {
155 char buf[BUFSIZE] = { 0 };
156 char capbuf[BUFSIZE] = { 0 };
157 int capbuflen = 0;
158 int buflen;
159 struct CapabilityEntry *entry;
160 rb_dictionary_iter iter;
161
162 buflen = snprintf(buf, sizeof buf, ":%s CAP %s %s",
163 me.name,
164 EmptyString(source_p->name) ? "*" : source_p->name,
165 subcmd);
166
167 /* shortcut, nothing to do */
168 if(flags == -1)
169 {
170 sendto_one(source_p, "%s :", buf);
171 return;
172 }
173
174 RB_DICTIONARY_FOREACH(entry, &iter, cli_capindex->cap_dict)
175 {
176 size_t caplen = 0;
177 struct ClientCapability *clicap = entry->ownerdata;
178 const char *data = NULL;
179
180 if(flags && !IsCapableEntry(source_p, entry))
181 continue;
182
183 if (!clicap_visible(source_p, entry))
184 continue;
185
186 caplen = strlen(entry->cap);
187 if (!flags && (source_p->flags & FLAGS_CLICAP_DATA) && clicap != NULL && clicap->data != NULL)
188 data = clicap->data(source_p);
189
190 if (data != NULL)
191 caplen += strlen(data) + 1 /* "=" between cap and data */;
192
193 /* "<buf> * :<capbuf>-~=<cap><=><data> " */
194 if(buflen + 4 + capbuflen + 3 + caplen + 1 > DATALEN)
195 {
196 /* remove our trailing space -- if capbuflen == 0
197 * here, we didnt even succeed in adding one.
198 */
199 capbuf[capbuflen] = '\0';
200
201 sendto_one(source_p, "%s * :%s", buf, capbuf);
202
203 memset(capbuf, 0, sizeof capbuf);
204 }
205
206 capbuflen = rb_snprintf_append(capbuf, sizeof capbuf, "%s%s%s ",
207 entry->cap, data != NULL ? "=" : "", data != NULL ? data : "");
208 }
209
210 /* remove trailing space */
211 capbuf[capbuflen] = '\0';
212
213 sendto_one(source_p, "%s :%s", buf, capbuf);
214 }
215
216 static void
217 cap_ack(struct Client *source_p, const char *arg)
218 {
219 struct CapabilityEntry *cap;
220 int capadd = 0, capdel = 0;
221 int finished = 0, negate;
222
223 if(EmptyString(arg))
224 return;
225
226 for(cap = clicap_find(arg, &negate, &finished); cap;
227 cap = clicap_find(NULL, &negate, &finished))
228 {
229 /* sent an ACK for something they havent REQd */
230 if(!IsCapableEntry(source_p, cap))
231 continue;
232
233 if(negate)
234 {
235 /* dont let them ack something sticky off */
236 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
237 continue;
238
239 capdel |= (1 << cap->value);
240 }
241 else
242 capadd |= (1 << cap->value);
243 }
244
245 source_p->localClient->caps |= capadd;
246 source_p->localClient->caps &= ~capdel;
247 }
248
249 static void
250 cap_end(struct Client *source_p, const char *arg)
251 {
252 if(IsRegistered(source_p))
253 return;
254
255 source_p->flags &= ~FLAGS_CLICAP;
256
257 if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER)
258 {
259 register_local_user(source_p, source_p);
260 }
261 }
262
263 static void
264 cap_list(struct Client *source_p, const char *arg)
265 {
266 /* list of what theyre currently using */
267 clicap_generate(source_p, "LIST",
268 source_p->localClient->caps ? source_p->localClient->caps : -1);
269 }
270
271 static void
272 cap_ls(struct Client *source_p, const char *arg)
273 {
274 if(!IsRegistered(source_p))
275 source_p->flags |= FLAGS_CLICAP;
276
277 if (arg != NULL && !strcmp(arg, "302"))
278 {
279 source_p->flags |= FLAGS_CLICAP_DATA;
280 source_p->localClient->caps |= CLICAP_CAP_NOTIFY;
281 }
282
283 /* list of what we support */
284 clicap_generate(source_p, "LS", 0);
285 }
286
287 static void
288 cap_req(struct Client *source_p, const char *arg)
289 {
290 char buf[BUFSIZE];
291 char pbuf[2][BUFSIZE];
292 struct CapabilityEntry *cap;
293 int buflen, plen;
294 int i = 0;
295 int capadd = 0, capdel = 0;
296 int finished = 0, negate;
297
298 if(!IsRegistered(source_p))
299 source_p->flags |= FLAGS_CLICAP;
300
301 if(EmptyString(arg))
302 return;
303
304 buflen = snprintf(buf, sizeof(buf), ":%s CAP %s ACK",
305 me.name, EmptyString(source_p->name) ? "*" : source_p->name);
306
307 pbuf[0][0] = '\0';
308 plen = 0;
309
310 for(cap = clicap_find(arg, &negate, &finished); cap;
311 cap = clicap_find(NULL, &negate, &finished))
312 {
313 size_t namelen = strlen(cap->cap);
314
315 /* filled the first array, but cant send it in case the
316 * request fails. one REQ should never fill more than two
317 * buffers --fl
318 */
319 if(buflen + plen + namelen + 6 >= BUFSIZE)
320 {
321 pbuf[1][0] = '\0';
322 plen = 0;
323 i = 1;
324 }
325
326 if(negate)
327 {
328 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
329 {
330 finished = 0;
331 break;
332 }
333
334 strcat(pbuf[i], "-");
335 plen++;
336
337 capdel |= (1 << cap->value);
338 }
339 else
340 {
341 if (!clicap_visible(source_p, cap))
342 {
343 finished = 0;
344 break;
345 }
346
347 capadd |= (1 << cap->value);
348 }
349
350 /* XXX this probably should exclude REQACK'd caps from capadd/capdel, but keep old behaviour for now */
351 if(HasCapabilityFlag(cap, CLICAP_FLAGS_REQACK))
352 {
353 strcat(pbuf[i], "~");
354 plen++;
355 }
356
357 strcat(pbuf[i], cap->cap);
358 if (!finished) {
359 strcat(pbuf[i], " ");
360 plen += (namelen + 1);
361 }
362 }
363
364 if(!finished)
365 {
366 sendto_one(source_p, ":%s CAP %s NAK :%s",
367 me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg);
368 return;
369 }
370
371 if(i)
372 {
373 sendto_one(source_p, "%s * :%s", buf, pbuf[0]);
374 sendto_one(source_p, "%s :%s", buf, pbuf[1]);
375 }
376 else
377 sendto_one(source_p, "%s :%s", buf, pbuf[0]);
378
379 source_p->localClient->caps |= capadd;
380 source_p->localClient->caps &= ~capdel;
381 }
382
383 static struct clicap_cmd
384 {
385 const char *cmd;
386 void (*func)(struct Client *source_p, const char *arg);
387 } clicap_cmdlist[] = {
388 /* This list *MUST* be in alphabetical order */
389 { "ACK", cap_ack },
390 { "END", cap_end },
391 { "LIST", cap_list },
392 { "LS", cap_ls },
393 { "REQ", cap_req },
394 };
395
396 static int
397 clicap_cmd_search(const char *command, struct clicap_cmd *entry)
398 {
399 return irccmp(command, entry->cmd);
400 }
401
402 static void
403 m_cap(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
404 {
405 struct clicap_cmd *cmd;
406
407 if(!(cmd = bsearch(parv[1], clicap_cmdlist,
408 sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd),
409 sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search)))
410 {
411 sendto_one(source_p, form_str(ERR_INVALIDCAPCMD),
412 me.name, EmptyString(source_p->name) ? "*" : source_p->name,
413 parv[1]);
414 return;
415 }
416
417 (cmd->func)(source_p, parv[2]);
418 }