]>
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 }; | |
105a4985 | 57 | DECLARE_MODULE_AV2(cap, NULL, NULL, cap_clist, NULL, NULL, NULL, NULL, NULL); |
212380e3 | 58 | |
32df5e96 AC |
59 | #define IsCapableEntry(c, e) IsCapable(c, 1 << (e)->value) |
60 | #define HasCapabilityFlag(c, f) (c->ownerdata != NULL && (((struct ClientCapability *)c->ownerdata)->flags & (f)) == f) | |
212380e3 | 61 | |
193d4db3 | 62 | static inline int |
38ffccf8 | 63 | clicap_visible(struct Client *client_p, const struct CapabilityEntry *cap) |
193d4db3 AC |
64 | { |
65 | struct ClientCapability *clicap; | |
66 | ||
455d2750 AC |
67 | /* orphaned caps shouldn't be visible */ |
68 | if (cap->flags & CAP_ORPHANED) | |
69 | return 0; | |
70 | ||
193d4db3 AC |
71 | if (cap->ownerdata == NULL) |
72 | return 1; | |
73 | ||
74 | clicap = cap->ownerdata; | |
75 | if (clicap->visible == NULL) | |
76 | return 1; | |
77 | ||
38ffccf8 | 78 | return clicap->visible(client_p); |
193d4db3 AC |
79 | } |
80 | ||
212380e3 AC |
81 | /* clicap_find() |
82 | * Used iteratively over a buffer, extracts individual cap tokens. | |
83 | * | |
84 | * Inputs: buffer to start iterating over (NULL to iterate over existing buf) | |
85 | * int pointer to whether the cap token is negated | |
86 | * int pointer to whether we finish with success | |
87 | * Ouputs: Cap entry if found, NULL otherwise. | |
88 | */ | |
32df5e96 | 89 | static struct CapabilityEntry * |
212380e3 AC |
90 | clicap_find(const char *data, int *negate, int *finished) |
91 | { | |
92 | static char buf[BUFSIZE]; | |
93 | static char *p; | |
32df5e96 | 94 | struct CapabilityEntry *cap; |
212380e3 AC |
95 | char *s; |
96 | ||
97 | *negate = 0; | |
98 | ||
99 | if(data) | |
100 | { | |
f427c8b0 | 101 | rb_strlcpy(buf, data, sizeof(buf)); |
212380e3 AC |
102 | p = buf; |
103 | } | |
104 | ||
105 | if(*finished) | |
106 | return NULL; | |
107 | ||
108 | /* skip any whitespace */ | |
109 | while(*p && IsSpace(*p)) | |
110 | p++; | |
111 | ||
112 | if(EmptyString(p)) | |
113 | { | |
114 | *finished = 1; | |
115 | return NULL; | |
116 | } | |
117 | ||
118 | if(*p == '-') | |
119 | { | |
120 | *negate = 1; | |
121 | p++; | |
122 | ||
123 | /* someone sent a '-' without a parameter.. */ | |
124 | if(*p == '\0') | |
125 | return NULL; | |
126 | } | |
127 | ||
128 | if((s = strchr(p, ' '))) | |
129 | *s++ = '\0'; | |
130 | ||
32df5e96 | 131 | if((cap = capability_find(cli_capindex, p)) != NULL) |
212380e3 AC |
132 | { |
133 | if(s) | |
134 | p = s; | |
135 | else | |
136 | *finished = 1; | |
137 | } | |
138 | ||
139 | return cap; | |
140 | } | |
141 | ||
142 | /* clicap_generate() | |
143 | * Generates a list of capabilities. | |
144 | * | |
145 | * Inputs: client to send to, subcmd to send, | |
146 | * flags to match against: 0 to do none, -1 if client has no flags, | |
147 | * int to whether we are doing CAP CLEAR | |
148 | * Outputs: None | |
149 | */ | |
150 | static void | |
151 | clicap_generate(struct Client *source_p, const char *subcmd, int flags, int clear) | |
152 | { | |
6b23b98a AC |
153 | char buf[BUFSIZE] = { 0 }; |
154 | char capbuf[BUFSIZE] = { 0 }; | |
212380e3 AC |
155 | int buflen = 0; |
156 | int curlen, mlen; | |
32df5e96 AC |
157 | struct CapabilityEntry *entry; |
158 | struct DictionaryIter iter; | |
212380e3 | 159 | |
ccb75e91 | 160 | mlen = snprintf(buf, sizeof buf, ":%s CAP %s %s", |
55abcbb2 KB |
161 | me.name, |
162 | EmptyString(source_p->name) ? "*" : source_p->name, | |
212380e3 AC |
163 | subcmd); |
164 | ||
212380e3 AC |
165 | /* shortcut, nothing to do */ |
166 | if(flags == -1) | |
167 | { | |
168 | sendto_one(source_p, "%s :", buf); | |
169 | return; | |
170 | } | |
171 | ||
32df5e96 | 172 | DICTIONARY_FOREACH(entry, &iter, cli_capindex->cap_dict) |
212380e3 | 173 | { |
ddf62b10 AC |
174 | size_t caplen = 0; |
175 | struct ClientCapability *clicap = entry->ownerdata; | |
176 | const char *data = NULL; | |
177 | ||
212380e3 AC |
178 | if(flags) |
179 | { | |
32df5e96 | 180 | if(!IsCapableEntry(source_p, entry)) |
212380e3 AC |
181 | continue; |
182 | /* they are capable of this, check sticky */ | |
32df5e96 | 183 | else if(clear && HasCapabilityFlag(entry, CLICAP_FLAGS_STICKY)) |
212380e3 AC |
184 | continue; |
185 | } | |
186 | ||
38ffccf8 | 187 | if (!clicap_visible(source_p, entry)) |
193d4db3 | 188 | continue; |
bbce62d2 | 189 | |
ddf62b10 AC |
190 | caplen = strlen(entry->cap); |
191 | if (!flags && (source_p->flags & FLAGS_CLICAP_DATA) && clicap != NULL && clicap->data != NULL) | |
38ffccf8 | 192 | data = clicap->data(source_p); |
ddf62b10 AC |
193 | |
194 | if (data != NULL) | |
195 | caplen += strlen(data) + 1; | |
196 | ||
212380e3 | 197 | /* \r\n\0, possible "-~=", space, " *" */ |
ccb75e91 | 198 | if(buflen + mlen >= BUFSIZE - 10) |
212380e3 AC |
199 | { |
200 | /* remove our trailing space -- if buflen == mlen | |
201 | * here, we didnt even succeed in adding one. | |
202 | */ | |
ccb75e91 | 203 | capbuf[buflen] = '\0'; |
212380e3 AC |
204 | |
205 | sendto_one(source_p, "%s * :%s", buf, capbuf); | |
6b23b98a | 206 | |
212380e3 | 207 | buflen = mlen; |
6b23b98a | 208 | memset(capbuf, 0, sizeof capbuf); |
212380e3 AC |
209 | } |
210 | ||
ccb75e91 AC |
211 | buflen = rb_snprintf_append(capbuf, sizeof capbuf, "%s%s%s%s ", |
212 | clear ? "-" : "", entry->cap, data != NULL ? "=" : "", data != NULL ? data : ""); | |
212380e3 AC |
213 | } |
214 | ||
215 | /* remove trailing space */ | |
ccb75e91 | 216 | capbuf[buflen] = '\0'; |
212380e3 AC |
217 | |
218 | sendto_one(source_p, "%s :%s", buf, capbuf); | |
219 | } | |
220 | ||
221 | static void | |
222 | cap_ack(struct Client *source_p, const char *arg) | |
223 | { | |
32df5e96 | 224 | struct CapabilityEntry *cap; |
212380e3 AC |
225 | int capadd = 0, capdel = 0; |
226 | int finished = 0, negate; | |
227 | ||
228 | if(EmptyString(arg)) | |
229 | return; | |
230 | ||
231 | for(cap = clicap_find(arg, &negate, &finished); cap; | |
232 | cap = clicap_find(NULL, &negate, &finished)) | |
233 | { | |
234 | /* sent an ACK for something they havent REQd */ | |
32df5e96 | 235 | if(!IsCapableEntry(source_p, cap)) |
212380e3 AC |
236 | continue; |
237 | ||
238 | if(negate) | |
239 | { | |
240 | /* dont let them ack something sticky off */ | |
32df5e96 | 241 | if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY)) |
212380e3 AC |
242 | continue; |
243 | ||
32df5e96 | 244 | capdel |= (1 << cap->value); |
212380e3 AC |
245 | } |
246 | else | |
32df5e96 | 247 | capadd |= (1 << cap->value); |
212380e3 AC |
248 | } |
249 | ||
250 | source_p->localClient->caps |= capadd; | |
251 | source_p->localClient->caps &= ~capdel; | |
252 | } | |
253 | ||
254 | static void | |
255 | cap_clear(struct Client *source_p, const char *arg) | |
256 | { | |
55abcbb2 | 257 | clicap_generate(source_p, "ACK", |
212380e3 AC |
258 | source_p->localClient->caps ? source_p->localClient->caps : -1, 1); |
259 | ||
212380e3 | 260 | source_p->localClient->caps = 0; |
212380e3 AC |
261 | } |
262 | ||
263 | static void | |
264 | cap_end(struct Client *source_p, const char *arg) | |
265 | { | |
266 | if(IsRegistered(source_p)) | |
267 | return; | |
268 | ||
095328a7 | 269 | source_p->flags &= ~FLAGS_CLICAP; |
212380e3 | 270 | |
1fb3b1e1 | 271 | if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER) |
212380e3 | 272 | { |
21251822 | 273 | register_local_user(source_p, source_p); |
212380e3 AC |
274 | } |
275 | } | |
276 | ||
277 | static void | |
278 | cap_list(struct Client *source_p, const char *arg) | |
279 | { | |
280 | /* list of what theyre currently using */ | |
281 | clicap_generate(source_p, "LIST", | |
282 | source_p->localClient->caps ? source_p->localClient->caps : -1, 0); | |
283 | } | |
284 | ||
285 | static void | |
286 | cap_ls(struct Client *source_p, const char *arg) | |
287 | { | |
288 | if(!IsRegistered(source_p)) | |
095328a7 | 289 | source_p->flags |= FLAGS_CLICAP; |
212380e3 | 290 | |
ddf62b10 | 291 | if (arg != NULL && !strcmp(arg, "302")) |
ba316ed5 | 292 | { |
ddf62b10 | 293 | source_p->flags |= FLAGS_CLICAP_DATA; |
ba316ed5 AC |
294 | source_p->localClient->caps |= CLICAP_CAP_NOTIFY; |
295 | } | |
ddf62b10 | 296 | |
212380e3 AC |
297 | /* list of what we support */ |
298 | clicap_generate(source_p, "LS", 0, 0); | |
299 | } | |
300 | ||
301 | static void | |
302 | cap_req(struct Client *source_p, const char *arg) | |
303 | { | |
304 | char buf[BUFSIZE]; | |
305 | char pbuf[2][BUFSIZE]; | |
32df5e96 | 306 | struct CapabilityEntry *cap; |
212380e3 AC |
307 | int buflen, plen; |
308 | int i = 0; | |
309 | int capadd = 0, capdel = 0; | |
310 | int finished = 0, negate; | |
311 | ||
312 | if(!IsRegistered(source_p)) | |
095328a7 | 313 | source_p->flags |= FLAGS_CLICAP; |
212380e3 AC |
314 | |
315 | if(EmptyString(arg)) | |
316 | return; | |
317 | ||
5203cba5 | 318 | buflen = snprintf(buf, sizeof(buf), ":%s CAP %s ACK", |
212380e3 AC |
319 | me.name, EmptyString(source_p->name) ? "*" : source_p->name); |
320 | ||
321 | pbuf[0][0] = '\0'; | |
322 | plen = 0; | |
323 | ||
324 | for(cap = clicap_find(arg, &negate, &finished); cap; | |
325 | cap = clicap_find(NULL, &negate, &finished)) | |
326 | { | |
32df5e96 AC |
327 | size_t namelen = strlen(cap->cap); |
328 | ||
212380e3 AC |
329 | /* filled the first array, but cant send it in case the |
330 | * request fails. one REQ should never fill more than two | |
331 | * buffers --fl | |
332 | */ | |
32df5e96 | 333 | if(buflen + plen + namelen + 6 >= BUFSIZE) |
212380e3 AC |
334 | { |
335 | pbuf[1][0] = '\0'; | |
336 | plen = 0; | |
337 | i = 1; | |
338 | } | |
339 | ||
340 | if(negate) | |
341 | { | |
32df5e96 | 342 | if(HasCapabilityFlag(cap, CLICAP_FLAGS_STICKY)) |
212380e3 AC |
343 | { |
344 | finished = 0; | |
345 | break; | |
346 | } | |
347 | ||
348 | strcat(pbuf[i], "-"); | |
349 | plen++; | |
350 | ||
32df5e96 | 351 | capdel |= (1 << cap->value); |
212380e3 AC |
352 | } |
353 | else | |
354 | { | |
38ffccf8 | 355 | if (!clicap_visible(source_p, cap)) |
bbce62d2 | 356 | { |
193d4db3 AC |
357 | finished = 0; |
358 | break; | |
bbce62d2 MT |
359 | } |
360 | ||
32df5e96 | 361 | capadd |= (1 << cap->value); |
212380e3 AC |
362 | } |
363 | ||
32df5e96 AC |
364 | /* XXX this probably should exclude REQACK'd caps from capadd/capdel, but keep old behaviour for now */ |
365 | if(HasCapabilityFlag(cap, CLICAP_FLAGS_REQACK)) | |
212380e3 AC |
366 | { |
367 | strcat(pbuf[i], "~"); | |
368 | plen++; | |
369 | } | |
370 | ||
32df5e96 | 371 | strcat(pbuf[i], cap->cap); |
d855e13e VL |
372 | if (!finished) { |
373 | strcat(pbuf[i], " "); | |
32df5e96 | 374 | plen += (namelen + 1); |
d855e13e | 375 | } |
212380e3 AC |
376 | } |
377 | ||
378 | if(!finished) | |
379 | { | |
380 | sendto_one(source_p, ":%s CAP %s NAK :%s", | |
381 | me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg); | |
382 | return; | |
383 | } | |
384 | ||
385 | if(i) | |
386 | { | |
387 | sendto_one(source_p, "%s * :%s", buf, pbuf[0]); | |
388 | sendto_one(source_p, "%s :%s", buf, pbuf[1]); | |
389 | } | |
390 | else | |
391 | sendto_one(source_p, "%s :%s", buf, pbuf[0]); | |
392 | ||
393 | source_p->localClient->caps |= capadd; | |
394 | source_p->localClient->caps &= ~capdel; | |
395 | } | |
396 | ||
397 | static struct clicap_cmd | |
398 | { | |
399 | const char *cmd; | |
400 | void (*func)(struct Client *source_p, const char *arg); | |
401 | } clicap_cmdlist[] = { | |
402 | /* This list *MUST* be in alphabetical order */ | |
403 | { "ACK", cap_ack }, | |
404 | { "CLEAR", cap_clear }, | |
405 | { "END", cap_end }, | |
406 | { "LIST", cap_list }, | |
407 | { "LS", cap_ls }, | |
408 | { "REQ", cap_req }, | |
409 | }; | |
410 | ||
411 | static int | |
412 | clicap_cmd_search(const char *command, struct clicap_cmd *entry) | |
413 | { | |
414 | return irccmp(command, entry->cmd); | |
415 | } | |
416 | ||
417 | static int | |
428ca87b | 418 | m_cap(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
419 | { |
420 | struct clicap_cmd *cmd; | |
421 | ||
422 | if(!(cmd = bsearch(parv[1], clicap_cmdlist, | |
423 | sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd), | |
424 | sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search))) | |
425 | { | |
426 | sendto_one(source_p, form_str(ERR_INVALIDCAPCMD), | |
48a038f4 JT |
427 | me.name, EmptyString(source_p->name) ? "*" : source_p->name, |
428 | parv[1]); | |
212380e3 AC |
429 | return 0; |
430 | } | |
431 | ||
432 | (cmd->func)(source_p, parv[2]); | |
433 | return 0; | |
434 | } |