]>
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
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * 1.Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2.Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3.The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $Id: m_cap.c 676 2006-02-03 20:05:09Z gxti $
46 typedef int (*bqcmp
)(const void *, const void *);
48 static int m_cap(struct Client
*, struct Client
*, int, const char **);
49 static int modinit(void);
51 struct Message cap_msgtab
= {
52 "CAP", 0, 0, 0, MFLG_SLOW
,
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
, modinit
, NULL
, cap_clist
, NULL
, NULL
, "$Revision: 676 $");
59 #define _CLICAP(name, capserv, capclient, caprequired, flags) \
60 { (name), (capserv), (capclient), (caprequired), (flags), sizeof(name) - 1 }
62 #define CLICAP_FLAGS_STICKY 0x001
67 int cap_serv
; /* for altering s->c */
68 int cap_cli
; /* for altering c->s */
69 int cap_required_serv
; /* required dependency cap */
73 _CLICAP("multi-prefix", CLICAP_MULTI_PREFIX
, 0, 0, 0),
74 _CLICAP("sasl", CLICAP_SASL
, 0, 0, CLICAP_FLAGS_STICKY
),
75 _CLICAP("account-notify", CLICAP_ACCOUNT_NOTIFY
, 0, 0, 0),
76 _CLICAP("extended-join", CLICAP_EXTENDED_JOIN
, 0, 0, 0),
77 _CLICAP("away-notify", CLICAP_AWAY_NOTIFY
, 0, 0, 0),
78 _CLICAP("tls", CLICAP_TLS
, 0, 0, 0),
79 _CLICAP("userhost-in-names", CLICAP_USERHOST_IN_NAMES
, 0, 0, 0),
82 #define CLICAP_LIST_LEN (sizeof(clicap_list) / sizeof(struct clicap))
84 static int clicap_sort(struct clicap
*, struct clicap
*);
89 qsort(clicap_list
, CLICAP_LIST_LEN
, sizeof(struct clicap
),
95 clicap_sort(struct clicap
*one
, struct clicap
*two
)
97 return irccmp(one
->name
, two
->name
);
101 clicap_compare(const char *name
, struct clicap
*cap
)
103 return irccmp(name
, cap
->name
);
107 * Used iteratively over a buffer, extracts individual cap tokens.
109 * Inputs: buffer to start iterating over (NULL to iterate over existing buf)
110 * int pointer to whether the cap token is negated
111 * int pointer to whether we finish with success
112 * Ouputs: Cap entry if found, NULL otherwise.
114 static struct clicap
*
115 clicap_find(const char *data
, int *negate
, int *finished
)
117 static char buf
[BUFSIZE
];
126 rb_strlcpy(buf
, data
, sizeof(buf
));
133 /* skip any whitespace */
134 while(*p
&& IsSpace(*p
))
148 /* someone sent a '-' without a parameter.. */
153 if((s
= strchr(p
, ' ')))
156 if((cap
= bsearch(p
, clicap_list
, CLICAP_LIST_LEN
,
157 sizeof(struct clicap
), (bqcmp
) clicap_compare
)))
169 * Generates a list of capabilities.
171 * Inputs: client to send to, subcmd to send,
172 * flags to match against: 0 to do none, -1 if client has no flags,
173 * int to whether we are doing CAP CLEAR
177 clicap_generate(struct Client
*source_p
, const char *subcmd
, int flags
, int clear
)
180 char capbuf
[BUFSIZE
];
186 mlen
= rb_sprintf(buf
, ":%s CAP %s %s",
188 EmptyString(source_p
->name
) ? "*" : source_p
->name
,
194 /* shortcut, nothing to do */
197 sendto_one(source_p
, "%s :", buf
);
201 for(i
= 0; i
< CLICAP_LIST_LEN
; i
++)
205 if(!IsCapable(source_p
, clicap_list
[i
].cap_serv
))
207 /* they are capable of this, check sticky */
208 else if(clear
&& clicap_list
[i
].flags
& CLICAP_FLAGS_STICKY
)
212 /* \r\n\0, possible "-~=", space, " *" */
213 if(buflen
+ clicap_list
[i
].namelen
>= BUFSIZE
- 10)
215 /* remove our trailing space -- if buflen == mlen
216 * here, we didnt even succeed in adding one.
223 sendto_one(source_p
, "%s * :%s", buf
, capbuf
);
233 /* needs a client ack */
234 if(clicap_list
[i
].cap_cli
&&
235 IsCapable(source_p
, clicap_list
[i
].cap_cli
))
243 if(clicap_list
[i
].flags
& CLICAP_FLAGS_STICKY
)
249 /* if we're doing an LS, then we only send this if
252 if(clicap_list
[i
].cap_cli
&&
253 (!flags
|| !IsCapable(source_p
, clicap_list
[i
].cap_cli
)))
260 curlen
= rb_sprintf(p
, "%s ", clicap_list
[i
].name
);
265 /* remove trailing space */
271 sendto_one(source_p
, "%s :%s", buf
, capbuf
);
275 cap_ack(struct Client
*source_p
, const char *arg
)
278 int capadd
= 0, capdel
= 0;
279 int finished
= 0, negate
;
284 for(cap
= clicap_find(arg
, &negate
, &finished
); cap
;
285 cap
= clicap_find(NULL
, &negate
, &finished
))
287 /* sent an ACK for something they havent REQd */
288 if(!IsCapable(source_p
, cap
->cap_serv
))
293 /* dont let them ack something sticky off */
294 if(cap
->flags
& CLICAP_FLAGS_STICKY
)
297 capdel
|= cap
->cap_cli
;
300 capadd
|= cap
->cap_cli
;
303 source_p
->localClient
->caps
|= capadd
;
304 source_p
->localClient
->caps
&= ~capdel
;
308 cap_clear(struct Client
*source_p
, const char *arg
)
310 clicap_generate(source_p
, "ACK",
311 source_p
->localClient
->caps
? source_p
->localClient
->caps
: -1, 1);
313 /* XXX - sticky capabs */
315 source_p
->localClient
->caps
= source_p
->localClient
->caps
& CLICAP_STICKY
;
317 source_p
->localClient
->caps
= 0;
322 cap_end(struct Client
*source_p
, const char *arg
)
324 if(IsRegistered(source_p
))
327 source_p
->flags
&= ~FLAGS_CLICAP
;
329 if(source_p
->name
[0] && source_p
->flags
& FLAGS_SENTUSER
)
332 rb_strlcpy(buf
, source_p
->username
, sizeof(buf
));
333 register_local_user(source_p
, source_p
, buf
);
338 cap_list(struct Client
*source_p
, const char *arg
)
340 /* list of what theyre currently using */
341 clicap_generate(source_p
, "LIST",
342 source_p
->localClient
->caps
? source_p
->localClient
->caps
: -1, 0);
346 cap_ls(struct Client
*source_p
, const char *arg
)
348 if(!IsRegistered(source_p
))
349 source_p
->flags
|= FLAGS_CLICAP
;
351 /* list of what we support */
352 clicap_generate(source_p
, "LS", 0, 0);
356 cap_req(struct Client
*source_p
, const char *arg
)
359 char pbuf
[2][BUFSIZE
];
363 int capadd
= 0, capdel
= 0;
364 int finished
= 0, negate
;
366 if(!IsRegistered(source_p
))
367 source_p
->flags
|= FLAGS_CLICAP
;
372 buflen
= rb_snprintf(buf
, sizeof(buf
), ":%s CAP %s ACK",
373 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
);
378 for(cap
= clicap_find(arg
, &negate
, &finished
); cap
;
379 cap
= clicap_find(NULL
, &negate
, &finished
))
381 /* filled the first array, but cant send it in case the
382 * request fails. one REQ should never fill more than two
385 if(buflen
+ plen
+ cap
->namelen
+ 6 >= BUFSIZE
)
394 if(cap
->flags
& CLICAP_FLAGS_STICKY
)
400 strcat(pbuf
[i
], "-");
403 capdel
|= cap
->cap_serv
;
407 if(cap
->cap_required_serv
&& !((capadd
& cap
->cap_required_serv
) == cap
->cap_required_serv
|| IsCapable(source_p
, cap
->cap_required_serv
)))
413 if(cap
->flags
& CLICAP_FLAGS_STICKY
)
415 strcat(pbuf
[i
], "=");
419 capadd
|= cap
->cap_serv
;
424 strcat(pbuf
[i
], "~");
428 strcat(pbuf
[i
], cap
->name
);
429 strcat(pbuf
[i
], " ");
430 plen
+= (cap
->namelen
+ 1);
435 sendto_one(source_p
, ":%s CAP %s NAK :%s",
436 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
, arg
);
442 sendto_one(source_p
, "%s * :%s", buf
, pbuf
[0]);
443 sendto_one(source_p
, "%s :%s", buf
, pbuf
[1]);
446 sendto_one(source_p
, "%s :%s", buf
, pbuf
[0]);
448 source_p
->localClient
->caps
|= capadd
;
449 source_p
->localClient
->caps
&= ~capdel
;
452 static struct clicap_cmd
455 void (*func
)(struct Client
*source_p
, const char *arg
);
456 } clicap_cmdlist
[] = {
457 /* This list *MUST* be in alphabetical order */
459 { "CLEAR", cap_clear
},
461 { "LIST", cap_list
},
467 clicap_cmd_search(const char *command
, struct clicap_cmd
*entry
)
469 return irccmp(command
, entry
->cmd
);
473 m_cap(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
475 struct clicap_cmd
*cmd
;
477 if(!(cmd
= bsearch(parv
[1], clicap_cmdlist
,
478 sizeof(clicap_cmdlist
) / sizeof(struct clicap_cmd
),
479 sizeof(struct clicap_cmd
), (bqcmp
) clicap_cmd_search
)))
481 sendto_one(source_p
, form_str(ERR_INVALIDCAPCMD
),
482 me
.name
, EmptyString(source_p
->name
) ? "*" : source_p
->name
,
487 (cmd
->func
)(source_p
, parv
[2]);