]> jfr.im git - solanum.git/blame - modules/m_cap.c
wsockd: conn_t.stream is not needed
[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{
6b23b98a
AC
155 char buf[BUFSIZE] = { 0 };
156 char capbuf[BUFSIZE] = { 0 };
212380e3 157 int buflen = 0;
e23126c8 158 int mlen;
32df5e96 159 struct CapabilityEntry *entry;
4177311e 160 rb_dictionary_iter iter;
212380e3 161
ccb75e91 162 mlen = snprintf(buf, sizeof buf, ":%s CAP %s %s",
55abcbb2
KB
163 me.name,
164 EmptyString(source_p->name) ? "*" : source_p->name,
212380e3
AC
165 subcmd);
166
212380e3
AC
167 /* shortcut, nothing to do */
168 if(flags == -1)
169 {
170 sendto_one(source_p, "%s :", buf);
171 return;
172 }
173
56f84ded 174 RB_DICTIONARY_FOREACH(entry, &iter, cli_capindex->cap_dict)
212380e3 175 {
ddf62b10
AC
176 size_t caplen = 0;
177 struct ClientCapability *clicap = entry->ownerdata;
178 const char *data = NULL;
179
ceabbbbf
MU
180 if(flags && !IsCapableEntry(source_p, entry))
181 continue;
212380e3 182
38ffccf8 183 if (!clicap_visible(source_p, entry))
193d4db3 184 continue;
bbce62d2 185
ddf62b10
AC
186 caplen = strlen(entry->cap);
187 if (!flags && (source_p->flags & FLAGS_CLICAP_DATA) && clicap != NULL && clicap->data != NULL)
38ffccf8 188 data = clicap->data(source_p);
ddf62b10
AC
189
190 if (data != NULL)
191 caplen += strlen(data) + 1;
192
212380e3 193 /* \r\n\0, possible "-~=", space, " *" */
ccb75e91 194 if(buflen + mlen >= BUFSIZE - 10)
212380e3
AC
195 {
196 /* remove our trailing space -- if buflen == mlen
197 * here, we didnt even succeed in adding one.
198 */
ccb75e91 199 capbuf[buflen] = '\0';
212380e3
AC
200
201 sendto_one(source_p, "%s * :%s", buf, capbuf);
6b23b98a 202
212380e3 203 buflen = mlen;
6b23b98a 204 memset(capbuf, 0, sizeof capbuf);
212380e3
AC
205 }
206
ceabbbbf
MU
207 buflen = rb_snprintf_append(capbuf, sizeof capbuf, "%s%s%s ",
208 entry->cap, data != NULL ? "=" : "", data != NULL ? data : "");
212380e3
AC
209 }
210
211 /* remove trailing space */
ccb75e91 212 capbuf[buflen] = '\0';
212380e3
AC
213
214 sendto_one(source_p, "%s :%s", buf, capbuf);
215}
216
217static void
218cap_ack(struct Client *source_p, const char *arg)
219{
32df5e96 220 struct CapabilityEntry *cap;
212380e3
AC
221 int capadd = 0, capdel = 0;
222 int finished = 0, negate;
223
224 if(EmptyString(arg))
225 return;
226
227 for(cap = clicap_find(arg, &negate, &finished); cap;
228 cap = clicap_find(NULL, &negate, &finished))
229 {
230 /* sent an ACK for something they havent REQd */
32df5e96 231 if(!IsCapableEntry(source_p, cap))
212380e3
AC
232 continue;
233
234 if(negate)
235 {
236 /* dont let them ack something sticky off */
32df5e96 237 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
212380e3
AC
238 continue;
239
32df5e96 240 capdel |= (1 << cap->value);
212380e3
AC
241 }
242 else
32df5e96 243 capadd |= (1 << cap->value);
212380e3
AC
244 }
245
246 source_p->localClient->caps |= capadd;
247 source_p->localClient->caps &= ~capdel;
248}
249
212380e3
AC
250static void
251cap_end(struct Client *source_p, const char *arg)
252{
253 if(IsRegistered(source_p))
254 return;
255
095328a7 256 source_p->flags &= ~FLAGS_CLICAP;
212380e3 257
1fb3b1e1 258 if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER)
212380e3 259 {
21251822 260 register_local_user(source_p, source_p);
212380e3
AC
261 }
262}
263
264static void
265cap_list(struct Client *source_p, const char *arg)
266{
267 /* list of what theyre currently using */
268 clicap_generate(source_p, "LIST",
ceabbbbf 269 source_p->localClient->caps ? source_p->localClient->caps : -1);
212380e3
AC
270}
271
272static void
273cap_ls(struct Client *source_p, const char *arg)
274{
275 if(!IsRegistered(source_p))
095328a7 276 source_p->flags |= FLAGS_CLICAP;
212380e3 277
ddf62b10 278 if (arg != NULL && !strcmp(arg, "302"))
ba316ed5 279 {
ddf62b10 280 source_p->flags |= FLAGS_CLICAP_DATA;
ba316ed5
AC
281 source_p->localClient->caps |= CLICAP_CAP_NOTIFY;
282 }
ddf62b10 283
212380e3 284 /* list of what we support */
ceabbbbf 285 clicap_generate(source_p, "LS", 0);
212380e3
AC
286}
287
288static void
289cap_req(struct Client *source_p, const char *arg)
290{
291 char buf[BUFSIZE];
292 char pbuf[2][BUFSIZE];
32df5e96 293 struct CapabilityEntry *cap;
212380e3
AC
294 int buflen, plen;
295 int i = 0;
296 int capadd = 0, capdel = 0;
297 int finished = 0, negate;
298
299 if(!IsRegistered(source_p))
095328a7 300 source_p->flags |= FLAGS_CLICAP;
212380e3
AC
301
302 if(EmptyString(arg))
303 return;
304
5203cba5 305 buflen = snprintf(buf, sizeof(buf), ":%s CAP %s ACK",
212380e3
AC
306 me.name, EmptyString(source_p->name) ? "*" : source_p->name);
307
308 pbuf[0][0] = '\0';
309 plen = 0;
310
311 for(cap = clicap_find(arg, &negate, &finished); cap;
312 cap = clicap_find(NULL, &negate, &finished))
313 {
32df5e96
AC
314 size_t namelen = strlen(cap->cap);
315
212380e3
AC
316 /* filled the first array, but cant send it in case the
317 * request fails. one REQ should never fill more than two
318 * buffers --fl
319 */
32df5e96 320 if(buflen + plen + namelen + 6 >= BUFSIZE)
212380e3
AC
321 {
322 pbuf[1][0] = '\0';
323 plen = 0;
324 i = 1;
325 }
326
327 if(negate)
328 {
32df5e96 329 if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY))
212380e3
AC
330 {
331 finished = 0;
332 break;
333 }
334
335 strcat(pbuf[i], "-");
336 plen++;
337
32df5e96 338 capdel |= (1 << cap->value);
212380e3
AC
339 }
340 else
341 {
38ffccf8 342 if (!clicap_visible(source_p, cap))
bbce62d2 343 {
193d4db3
AC
344 finished = 0;
345 break;
bbce62d2
MT
346 }
347
32df5e96 348 capadd |= (1 << cap->value);
212380e3
AC
349 }
350
32df5e96
AC
351 /* XXX this probably should exclude REQACK'd caps from capadd/capdel, but keep old behaviour for now */
352 if(HasCapabilityFlag(cap, CLICAP_FLAGS_REQACK))
212380e3
AC
353 {
354 strcat(pbuf[i], "~");
355 plen++;
356 }
357
32df5e96 358 strcat(pbuf[i], cap->cap);
d855e13e
VL
359 if (!finished) {
360 strcat(pbuf[i], " ");
32df5e96 361 plen += (namelen + 1);
d855e13e 362 }
212380e3
AC
363 }
364
365 if(!finished)
366 {
367 sendto_one(source_p, ":%s CAP %s NAK :%s",
368 me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg);
369 return;
370 }
371
372 if(i)
373 {
374 sendto_one(source_p, "%s * :%s", buf, pbuf[0]);
375 sendto_one(source_p, "%s :%s", buf, pbuf[1]);
376 }
377 else
378 sendto_one(source_p, "%s :%s", buf, pbuf[0]);
379
380 source_p->localClient->caps |= capadd;
381 source_p->localClient->caps &= ~capdel;
382}
383
384static struct clicap_cmd
385{
386 const char *cmd;
387 void (*func)(struct Client *source_p, const char *arg);
388} clicap_cmdlist[] = {
389 /* This list *MUST* be in alphabetical order */
390 { "ACK", cap_ack },
212380e3
AC
391 { "END", cap_end },
392 { "LIST", cap_list },
393 { "LS", cap_ls },
394 { "REQ", cap_req },
395};
396
397static int
398clicap_cmd_search(const char *command, struct clicap_cmd *entry)
399{
400 return irccmp(command, entry->cmd);
401}
402
3c7d6fcc 403static void
428ca87b 404m_cap(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
405{
406 struct clicap_cmd *cmd;
407
408 if(!(cmd = bsearch(parv[1], clicap_cmdlist,
409 sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd),
410 sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search)))
411 {
412 sendto_one(source_p, form_str(ERR_INVALIDCAPCMD),
48a038f4
JT
413 me.name, EmptyString(source_p->name) ? "*" : source_p->name,
414 parv[1]);
3c7d6fcc 415 return;
212380e3
AC
416 }
417
418 (cmd->func)(source_p, parv[2]);
212380e3 419}