2 * IRC - Internet Relay Chat, ircd/m_cap.c
3 * Copyright (C) 2004 Kevin L. Mitchell <klmitch@mit.edu>
5 * See file AUTHORS in IRC package for additional names of
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 1, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * @brief Capability negotiation commands
31 #include "ircd_chattr.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
44 typedef int (*bqcmp)(const void *, const void *);
46 static struct capabilities {
53 #define _CAP(cap, flags, name) \
54 { CAP_ ## cap, #cap, (flags), (name), sizeof(name) - 1 }
59 #define CAPAB_LIST_LEN (sizeof(capab_list) / sizeof(struct capabilities))
62 capab_sort(const struct capabilities *cap1, const struct capabilities *cap2)
64 return ircd_strcmp(cap1->name, cap2->name);
68 capab_search(const char *key, const struct capabilities *cap)
70 const char *rb = cap->name;
71 while (ToLower(*key) == ToLower(*rb)) /* walk equivalent part of strings */
72 if (!*key++) /* hit the end, all right... */
74 else /* OK, let's move on... */
77 /* If the character they differ on happens to be a space, and it happens
78 * to be the same length as the capability name, then we've found a
79 * match; otherwise, return the difference of the two.
81 return (IsSpace(*key) && !*rb) ? 0 : (ToLower(*key) - ToLower(*rb));
84 static struct capabilities *
85 find_cap(const char **caplist_p, int *neg_p)
87 static int inited = 0;
88 const char *caplist = *caplist_p;
89 struct capabilities *cap = 0;
91 *neg_p = 0; /* clear negative flag... */
93 if (!inited) { /* First, let's sort the array... */
94 qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities),
96 inited++; /* remember that we've done this step... */
99 /* Next, find first non-whitespace character... */
100 while (*caplist && IsSpace(*caplist))
103 /* We are now at the beginning of an element of the list; is it negative? */
104 if (*caplist == '-') {
105 caplist++; /* yes; step past the flag... */
106 *neg_p = 1; /* remember that it is negative... */
109 /* OK, now see if we can look up the capability... */
111 if (!(cap = (struct capabilities *)bsearch(caplist, capab_list,
113 sizeof(struct capabilities),
114 (bqcmp)capab_search))) {
115 /* Couldn't find the capability; advance to first whitespace character */
116 while (*caplist && !IsSpace(*caplist))
119 caplist += cap->namelen; /* advance to end of capability name */
122 assert(caplist != *caplist_p || !*caplist); /* we *must* advance */
124 /* move ahead in capability list string--or zero pointer if we hit end */
125 *caplist_p = *caplist ? caplist : 0;
127 return cap; /* and return the capability (if any) */
130 /** Send a CAP \a subcmd list of capability changes to \a sptr.
131 * If more than one line is necessary, each line before the last has
132 * an added "*" parameter before that line's capability list.
133 * @param[in] sptr Client receiving capability list.
134 * @param[in] set Capabilities to show as set (with ack and sticky modifiers).
135 * @param[in] rem Capabalities to show as removed (with no other modifier).
136 * @param[in] subcmd Name of capability subcommand.
139 send_caplist(struct Client *sptr, const struct CapSet *set,
140 const struct CapSet *rem, const char *subcmd)
142 char capbuf[BUFSIZE] = "", pfx[16];
144 int i, loc, len, flags, pfx_len;
146 /* set up the buffer for the final LS message... */
147 mb = msgq_make(sptr, "%:#C " MSG_CAP " %s :", &me, subcmd);
149 for (i = 0, loc = 0; i < CAPAB_LIST_LEN; i++) {
150 flags = capab_list[i].flags;
151 /* This is a little bit subtle, but just involves applying de
152 * Morgan's laws to the obvious check: We must display the
153 * capability if (and only if) it is set in \a rem or \a set, or
154 * if both are null and the capability is hidden.
156 if (!(rem && CapHas(rem, capab_list[i].cap))
157 && !(set && CapHas(set, capab_list[i].cap))
158 && (rem || set || (flags & CAPFL_HIDDEN)))
161 /* Build the prefix (space separator and any modifiers needed). */
164 pfx[pfx_len++] = ' ';
165 if (rem && CapHas(rem, capab_list[i].cap))
166 pfx[pfx_len++] = '-';
168 if (flags & CAPFL_PROTO)
169 pfx[pfx_len++] = '~';
170 if (flags & CAPFL_STICKY)
171 pfx[pfx_len++] = '=';
175 len = capab_list[i].namelen + pfx_len; /* how much we'd add... */
176 if (msgq_bufleft(mb) < loc + len + 2) { /* would add too much; must flush */
177 sendcmdto_one(&me, CMD_CAP, sptr, "%s * :%s", subcmd, capbuf);
178 capbuf[(loc = 0)] = '\0'; /* re-terminate the buffer... */
181 loc += ircd_snprintf(0, capbuf + loc, sizeof(capbuf) - loc, "%s%s",
182 pfx, capab_list[i].name);
185 msgq_append(0, mb, "%s", capbuf); /* append capabilities to the final cmd */
186 send_buffer(sptr, mb, 0); /* send them out... */
187 msgq_clean(mb); /* and release the buffer */
189 return 0; /* convenience return */
193 cap_ls(struct Client *sptr, const char *caplist)
195 if (IsUnknown(sptr)) /* registration hasn't completed; suspend it... */
196 cli_unreg(sptr) |= CLIREG_CAP;
198 return send_caplist(sptr, 0, 0, "LS"); /* send list of capabilities */
202 cap_req(struct Client *sptr, const char *caplist)
204 const char *cl = caplist;
205 struct capabilities *cap;
206 struct CapSet set, rem;
207 struct CapSet cs = *cli_capab(sptr); /* capability set */
208 struct CapSet as = *cli_active(sptr); /* active set */
211 if (IsUnknown(sptr)) /* registration hasn't completed; suspend it... */
212 cli_unreg(sptr) |= CLIREG_CAP;
214 memset(&set, 0, sizeof(set));
215 memset(&rem, 0, sizeof(rem));
216 while (cl) { /* walk through the capabilities list... */
217 if (!(cap = find_cap(&cl, &neg)) /* look up capability... */
218 || (!neg && (cap->flags & CAPFL_PROHIBIT)) /* is it prohibited? */
219 || (neg && (cap->flags & CAPFL_STICKY))) { /* is it sticky? */
220 sendcmdto_one(&me, CMD_CAP, sptr, "NAK :%s", caplist);
221 return 0; /* can't complete requested op... */
224 if (neg) { /* set or clear the capability... */
225 CapSet(&rem, cap->cap);
226 CapClr(&set, cap->cap);
227 CapClr(&cs, cap->cap);
228 if (!(cap->flags & CAPFL_PROTO))
229 CapClr(&as, cap->cap);
231 CapClr(&rem, cap->cap);
232 CapSet(&set, cap->cap);
233 CapSet(&cs, cap->cap);
234 if (!(cap->flags & CAPFL_PROTO))
235 CapSet(&as, cap->cap);
239 /* Notify client of accepted changes and copy over results. */
240 send_caplist(sptr, &set, &rem, "ACK");
241 *cli_capab(sptr) = cs;
242 *cli_active(sptr) = as;
248 cap_ack(struct Client *sptr, const char *caplist)
250 const char *cl = caplist;
251 struct capabilities *cap;
254 /* Coming from the client, this generally indicates that the client
255 * is using a new backwards-incompatible protocol feature. As such,
256 * it does not require further response from the server.
258 while (cl) { /* walk through the capabilities list... */
259 if (!(cap = find_cap(&cl, &neg)) || /* look up capability... */
260 (neg ? HasCap(sptr, cap->cap) : !HasCap(sptr, cap->cap))) /* uh... */
263 if (neg) /* set or clear the active capability... */
264 CapClr(cli_active(sptr), cap->cap);
266 CapSet(cli_active(sptr), cap->cap);
273 cap_clear(struct Client *sptr, const char *caplist)
275 struct CapSet cleared;
276 struct capabilities *cap;
279 /* XXX: If we ever add a capab list sorted by capab value, it would
280 * be good cache-wise to use it here. */
281 memset(&cleared, 0, sizeof(cleared));
282 for (ii = 0; ii < CAPAB_LIST_LEN; ++ii) {
283 cap = &capab_list[ii];
284 /* Only clear active non-sticky capabilities. */
285 if (!HasCap(sptr, cap->cap) || (cap->flags & CAPFL_STICKY))
287 CapSet(&cleared, cap->cap);
288 CapClr(cli_capab(sptr), cap->cap);
289 if (!(cap->flags & CAPFL_PROTO))
290 CapClr(cli_active(sptr), cap->cap);
292 send_caplist(sptr, 0, &cleared, "ACK");
298 cap_end(struct Client *sptr, const char *caplist)
300 if (!IsUnknown(sptr)) /* registration has completed... */
301 return 0; /* so just ignore the message... */
303 cli_unreg(sptr) &= ~CLIREG_CAP; /* capability negotiation is now done... */
305 if (!cli_unreg(sptr)) /* if client is now done... */
306 return register_user(sptr, sptr, cli_name(sptr), cli_user(sptr)->username);
308 return 0; /* Can't do registration yet... */
312 cap_list(struct Client *sptr, const char *caplist)
314 /* Send the list of the client's capabilities */
315 return send_caplist(sptr, cli_capab(sptr), 0, "LIST");
318 static struct subcmd {
320 int (*proc)(struct Client *sptr, const char *caplist);
323 { "CLEAR", cap_clear },
325 { "LIST", cap_list },
332 subcmd_search(const char *cmd, const struct subcmd *elem)
334 return ircd_strcmp(cmd, elem->cmd);
337 /** Handle a capability request or response from a client.
338 * @param[in] cptr Client that sent us the message.
339 * @param[in] sptr Original source of message.
340 * @param[in] parc Number of arguments.
341 * @param[in] parv Argument vector.
342 * @see \ref m_functions
345 m_cap(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
347 char *subcmd, *caplist = 0;
350 if (parc < 2) /* a subcommand is required */
353 if (parc > 2) /* a capability list was provided */
356 /* find the subcommand handler */
357 if (!(cmd = (struct subcmd *)bsearch(subcmd, cmdlist,
358 sizeof(cmdlist) / sizeof(struct subcmd),
359 sizeof(struct subcmd),
360 (bqcmp)subcmd_search)))
361 return send_reply(sptr, ERR_UNKNOWNCAPCMD, subcmd);
363 /* then execute it... */
364 return cmd->proc ? (cmd->proc)(sptr, caplist) : 0;