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