]>
jfr.im git - solanum.git/blob - modules/m_cap.c
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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
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.
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.
47 typedef int (*bqcmp
)(const void *, const void *);
49 static int m_cap(struct MsgBuf
*, struct Client
*, struct Client
*, int, const char **);
51 struct Message cap_msgtab
= {
53 {{m_cap
, 2}, {m_cap
, 2}, mg_ignore
, mg_ignore
, mg_ignore
, {m_cap
, 2}}
56 mapi_clist_av1 cap_clist
[] = { &cap_msgtab
, NULL
};
57 DECLARE_MODULE_AV1(cap
, NULL
, NULL
, cap_clist
, NULL
, NULL
, "$Revision: 676 $");
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)
63 * Used iteratively over a buffer, extracts individual cap tokens.
65 * Inputs: buffer to start iterating over (NULL to iterate over existing buf)
66 * int pointer to whether the cap token is negated
67 * int pointer to whether we finish with success
68 * Ouputs: Cap entry if found, NULL otherwise.
70 static struct CapabilityEntry
*
71 clicap_find(const char *data
, int *negate
, int *finished
)
73 static char buf
[BUFSIZE
];
75 struct CapabilityEntry
*cap
;
82 rb_strlcpy(buf
, data
, sizeof(buf
));
89 /* skip any whitespace */
90 while(*p
&& IsSpace(*p
))
104 /* someone sent a '-' without a parameter.. */
109 if((s
= strchr(p
, ' ')))
112 if((cap
= capability_find(cli_capindex
, p
)) != NULL
)
124 * Generates a list of capabilities.
126 * Inputs: client to send to, subcmd to send,
127 * flags to match against: 0 to do none, -1 if client has no flags,
128 * int to whether we are doing CAP CLEAR
132 clicap_generate(struct Client
*source_p
, const char *subcmd
, int flags
, int clear
)
135 char capbuf
[BUFSIZE
];
139 struct CapabilityEntry
*entry
;
140 struct DictionaryIter iter
;
142 mlen
= sprintf(buf
, ":%s CAP %s %s",
144 EmptyString(source_p
->name
) ? "*" : source_p
->name
,
150 /* shortcut, nothing to do */
153 sendto_one(source_p
, "%s :", buf
);
157 DICTIONARY_FOREACH(entry
, &iter
, cli_capindex
->cap_dict
)
161 if(!IsCapableEntry(source_p
, entry
))
163 /* they are capable of this, check sticky */
164 else if(clear
&& HasCapabilityFlag(entry
, CLICAP_FLAGS_STICKY
))
168 if ((1 << entry
->value
) == CLICAP_SASL
)
170 struct Client
*agent_p
= NULL
;
172 if (!ConfigFileEntry
.sasl_service
)
175 agent_p
= find_named_client(ConfigFileEntry
.sasl_service
);
176 if (agent_p
== NULL
|| !IsService(agent_p
))
180 /* \r\n\0, possible "-~=", space, " *" */
181 if(buflen
+ strlen(entry
->cap
) >= BUFSIZE
- 10)
183 /* remove our trailing space -- if buflen == mlen
184 * here, we didnt even succeed in adding one.
191 sendto_one(source_p
, "%s * :%s", buf
, capbuf
);
202 curlen
= sprintf(p
, "%s ", entry
->cap
);
207 /* remove trailing space */
213 sendto_one(source_p
, "%s :%s", buf
, capbuf
);
217 cap_ack(struct Client
*source_p
, const char *arg
)
219 struct CapabilityEntry
*cap
;
220 int capadd
= 0, capdel
= 0;
221 int finished
= 0, negate
;
226 for(cap
= clicap_find(arg
, &negate
, &finished
); cap
;
227 cap
= clicap_find(NULL
, &negate
, &finished
))
229 /* sent an ACK for something they havent REQd */
230 if(!IsCapableEntry(source_p
, cap
))
235 /* dont let them ack something sticky off */
236 if(HasCapabilityFlag(cap
, CLICAP_FLAGS_STICKY
))
239 capdel
|= (1 << cap
->value
);
242 capadd
|= (1 << cap
->value
);
245 source_p
->localClient
->caps
|= capadd
;
246 source_p
->localClient
->caps
&= ~capdel
;
250 cap_clear(struct Client
*source_p
, const char *arg
)
252 clicap_generate(source_p
, "ACK",
253 source_p
->localClient
->caps
? source_p
->localClient
->caps
: -1, 1);
255 source_p
->localClient
->caps
= 0;
259 cap_end(struct Client
*source_p
, const char *arg
)
261 if(IsRegistered(source_p
))
264 source_p
->flags
&= ~FLAGS_CLICAP
;
266 if(source_p
->name
[0] && source_p
->flags
& FLAGS_SENTUSER
)
268 register_local_user(source_p
, source_p
);
273 cap_list(struct Client
*source_p
, const char *arg
)
275 /* list of what theyre currently using */
276 clicap_generate(source_p
, "LIST",
277 source_p
->localClient
->caps
? source_p
->localClient
->caps
: -1, 0);
281 cap_ls(struct Client
*source_p
, const char *arg
)
283 if(!IsRegistered(source_p
))
284 source_p
->flags
|= FLAGS_CLICAP
;
286 /* list of what we support */
287 clicap_generate(source_p
, "LS", 0, 0);
291 cap_req(struct Client
*source_p
, const char *arg
)
294 char pbuf
[2][BUFSIZE
];
295 struct CapabilityEntry
*cap
;
298 int capadd
= 0, capdel
= 0;
299 int finished
= 0, negate
;
301 if(!IsRegistered(source_p
))
302 source_p
->flags
|= FLAGS_CLICAP
;
307 buflen
= snprintf(buf
, sizeof(buf
), ":%s CAP %s ACK",
308 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
);
313 for(cap
= clicap_find(arg
, &negate
, &finished
); cap
;
314 cap
= clicap_find(NULL
, &negate
, &finished
))
316 size_t namelen
= strlen(cap
->cap
);
318 /* filled the first array, but cant send it in case the
319 * request fails. one REQ should never fill more than two
322 if(buflen
+ plen
+ namelen
+ 6 >= BUFSIZE
)
331 if(HasCapabilityFlag(cap
, CLICAP_FLAGS_STICKY
))
337 strcat(pbuf
[i
], "-");
340 capdel
|= (1 << cap
->value
);
344 if ((1 << cap
->value
) == CLICAP_SASL
)
346 struct Client
*agent_p
= NULL
;
348 if (!ConfigFileEntry
.sasl_service
)
354 agent_p
= find_named_client(ConfigFileEntry
.sasl_service
);
355 if (agent_p
== NULL
|| !IsService(agent_p
))
362 capadd
|= (1 << cap
->value
);
365 /* XXX this probably should exclude REQACK'd caps from capadd/capdel, but keep old behaviour for now */
366 if(HasCapabilityFlag(cap
, CLICAP_FLAGS_REQACK
))
368 strcat(pbuf
[i
], "~");
372 strcat(pbuf
[i
], cap
->cap
);
374 strcat(pbuf
[i
], " ");
375 plen
+= (namelen
+ 1);
381 sendto_one(source_p
, ":%s CAP %s NAK :%s",
382 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, arg
);
388 sendto_one(source_p
, "%s * :%s", buf
, pbuf
[0]);
389 sendto_one(source_p
, "%s :%s", buf
, pbuf
[1]);
392 sendto_one(source_p
, "%s :%s", buf
, pbuf
[0]);
394 source_p
->localClient
->caps
|= capadd
;
395 source_p
->localClient
->caps
&= ~capdel
;
398 static struct clicap_cmd
401 void (*func
)(struct Client
*source_p
, const char *arg
);
402 } clicap_cmdlist
[] = {
403 /* This list *MUST* be in alphabetical order */
405 { "CLEAR", cap_clear
},
407 { "LIST", cap_list
},
413 clicap_cmd_search(const char *command
, struct clicap_cmd
*entry
)
415 return irccmp(command
, entry
->cmd
);
419 m_cap(struct MsgBuf
*msgbuf_p
, struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
421 struct clicap_cmd
*cmd
;
423 if(!(cmd
= bsearch(parv
[1], clicap_cmdlist
,
424 sizeof(clicap_cmdlist
) / sizeof(struct clicap_cmd
),
425 sizeof(struct clicap_cmd
), (bqcmp
) clicap_cmd_search
)))
427 sendto_one(source_p
, form_str(ERR_INVALIDCAPCMD
),
428 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
,
433 (cmd
->func
)(source_p
, parv
[2]);