ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 9799
Committed: Tue Dec 8 20:42:15 2020 UTC (5 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 10441 byte(s)
Log Message:
- Removed Connection::cap_client; renamed Connection::cap_active to Connection::cap

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 2004 Kevin L. Mitchell <klmitch@mit.edu>
5 * Copyright (c) 2006-2020 ircd-hybrid development team
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22
23 /*! \file m_cap.c
24 * \brief Includes required functions for processing the CAP command.
25 * \version $Id$
26 */
27
28 #include "stdinc.h"
29 #include "client.h"
30 #include "ircd.h"
31 #include "numeric.h"
32 #include "user.h"
33 #include "send.h"
34 #include "parse.h"
35 #include "modules.h"
36 #include "irc_string.h"
37
38
39 enum
40 {
41 CAPFL_HIDDEN = 1 << 0, /**< Do not advertize this capability */
42 CAPFL_STICKY = 1 << 1 /**< Cap may not be cleared once set */
43 };
44
45 typedef int (*bqcmp)(const void *, const void *);
46
47 static struct capabilities
48 {
49 unsigned int cap;
50 unsigned int flags;
51 const char *name;
52 size_t namelen;
53 } capab_list[] = {
54 #define _CAP(cap, flags, name) \
55 { (cap), (flags), (name), sizeof(name) - 1 }
56 _CAP(CAP_UHNAMES, 0, "userhost-in-names"),
57 _CAP(CAP_MULTI_PREFIX, 0, "multi-prefix"),
58 _CAP(CAP_AWAY_NOTIFY, 0, "away-notify"),
59 _CAP(CAP_EXTENDED_JOIN, 0, "extended-join"),
60 _CAP(CAP_ACCOUNT_NOTIFY, 0, "account-notify"),
61 _CAP(CAP_INVITE_NOTIFY, 0, "invite-notify"),
62 _CAP(CAP_CHGHOST, 0, "chghost")
63 #undef _CAP
64 };
65
66 #define CAPAB_LIST_LEN (sizeof(capab_list) / sizeof(struct capabilities))
67
68 static int
69 capab_sort(const struct capabilities *cap1, const struct capabilities *cap2)
70 {
71 return strcasecmp(cap1->name, cap2->name);
72 }
73
74 static int
75 capab_search(const char *key, const struct capabilities *cap)
76 {
77 const char *rb = cap->name;
78
79 while (ToLower(*key) == ToLower(*rb)) /* Walk equivalent part of strings */
80 if (*key++ == '\0') /* Hit the end, all right... */
81 return 0;
82 else /* OK, let's move on... */
83 ++rb;
84
85 /*
86 * If the character they differ on happens to be a space, and it happens
87 * to be the same length as the capability name, then we've found a
88 * match; otherwise, return the difference of the two.
89 */
90 return (IsSpace(*key) && *rb == '\0') ? 0 : (ToLower(*key) - ToLower(*rb));
91 }
92
93 static struct capabilities *
94 find_cap(const char **caplist_p, int *neg_p)
95 {
96 const char *caplist = *caplist_p;
97 struct capabilities *cap = NULL;
98
99 *neg_p = 0; /* Clear negative flag... */
100
101 /* Next, find first non-whitespace character... */
102 while (*caplist && IsSpace(*caplist))
103 ++caplist;
104
105 /* We are now at the beginning of an element of the list; is it negative? */
106 if (*caplist == '-')
107 {
108 ++caplist; /* Yes; step past the flag... */
109 *neg_p = 1; /* Remember that it is negative... */
110 }
111
112 /* OK, now see if we can look up the capability... */
113 if (*caplist)
114 {
115 if (!(cap = bsearch(caplist, capab_list, CAPAB_LIST_LEN,
116 sizeof(struct capabilities),
117 (bqcmp)capab_search)))
118 {
119 /* Couldn't find the capability; advance to first whitespace character */
120 while (*caplist && !IsSpace(*caplist))
121 ++caplist;
122 }
123 else
124 caplist += cap->namelen; /* Advance to end of capability name */
125
126 /* Strip trailing spaces */
127 while (*caplist && IsSpace(*caplist))
128 ++caplist;
129 }
130
131 assert(caplist != *caplist_p || !*caplist); /* We *must* advance */
132
133 /* Move ahead in capability list string--or zero pointer if we hit end */
134 *caplist_p = *caplist ? caplist : NULL;
135
136 return cap; /* And return the capability (if any) */
137 }
138
139 /** Send a CAP \a subcmd list of capability changes to \a source_p.
140 * If more than one line is necessary, each line before the last has
141 * an added "*" parameter before that line's capability list.
142 * @param[in] source_p Client receiving capability list.
143 * @param[in] set Capabilities to show as set (with ack and sticky modifiers).
144 * @param[in] rem Capabalities to show as removed (with no other modifier).
145 * @param[in] subcmd Name of capability subcommand.
146 */
147 static void
148 send_caplist(struct Client *source_p,
149 const unsigned int *const set,
150 const unsigned int *const rem, const char *subcmd)
151 {
152 char capbuf[IRCD_BUFSIZE] = "", pfx[4];
153 char cmdbuf[IRCD_BUFSIZE] = "";
154 unsigned int i, loc, len, pfx_len, clen;
155
156 /* Set up the buffer for the final LS message... */
157 clen = snprintf(cmdbuf, sizeof(capbuf), ":%s CAP %s %s ", me.name,
158 source_p->name[0] ? source_p->name : "*", subcmd);
159
160 for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i)
161 {
162 const struct capabilities *cap = &capab_list[i];
163
164 /*
165 * This is a little bit subtle, but just involves applying de
166 * Morgan's laws to the obvious check: We must display the
167 * capability if (and only if) it is set in \a rem or \a set, or
168 * if both are null and the capability is hidden.
169 */
170 if (!(rem && (*rem & cap->cap)) &&
171 !(set && (*set & cap->cap)) &&
172 (rem || set || (cap->flags & CAPFL_HIDDEN)))
173 continue;
174
175 /* Build the prefix (space separator and any modifiers needed). */
176 pfx_len = 0;
177
178 if (loc)
179 pfx[pfx_len++] = ' ';
180 if (rem && (*rem & cap->cap))
181 pfx[pfx_len++] = '-';
182
183 pfx[pfx_len] = '\0';
184
185 len = cap->namelen + pfx_len; /* How much we'd add... */
186
187 if (sizeof(capbuf) < (clen + loc + len + 15))
188 {
189 /* Would add too much; must flush */
190 sendto_one(source_p, "%s* :%s", cmdbuf, capbuf);
191 capbuf[(loc = 0)] = '\0'; /* Re-terminate the buffer... */
192 }
193
194 loc += snprintf(capbuf + loc, sizeof(capbuf) - loc,
195 "%s%s", pfx, cap->name);
196 }
197
198 sendto_one(source_p, "%s:%s", cmdbuf, capbuf);
199 }
200
201 static void
202 cap_ls(struct Client *source_p, const char *arg)
203 {
204 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
205 source_p->connection->registration |= REG_NEED_CAP;
206
207 if (arg && atoi(arg) >= 302)
208 AddFlag(source_p, FLAGS_CAP302);
209
210 send_caplist(source_p, NULL, NULL, "LS"); /* Send list of capabilities */
211 }
212
213 static void
214 cap_req(struct Client *source_p, const char *arg)
215 {
216 unsigned int set = 0, rem = 0;
217 unsigned int cs = source_p->connection->cap; /* Enabled capabilities */
218 int neg = 0;
219
220 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
221 source_p->connection->registration |= REG_NEED_CAP;
222
223 /* Walk through the capabilities list... */
224 for (const char *cl = arg; cl; )
225 {
226 /* Look up capability... */
227 const struct capabilities *cap = find_cap(&cl, &neg);
228 if (cap == NULL ||
229 (neg && (cap->flags & CAPFL_STICKY)))
230 {
231 sendto_one(source_p, ":%s CAP %s NAK :%s", me.name,
232 source_p->name[0] ? source_p->name : "*", arg);
233 return; /* Can't complete requested op... */
234 }
235
236 if (neg)
237 {
238 /* Set or clear the capability... */
239 rem |= cap->cap;
240 set &= ~cap->cap;
241 cs &= ~cap->cap;
242 }
243 else
244 {
245 rem &= ~cap->cap;
246 set |= cap->cap;
247 cs |= cap->cap;
248 }
249 }
250
251 /* Notify client of accepted changes and copy over results. */
252 send_caplist(source_p, &set, &rem, "ACK");
253
254 source_p->connection->cap = cs;
255 }
256
257 static void
258 cap_end(struct Client *source_p, const char *arg)
259 {
260 if (!IsUnknown(source_p)) /* Registration has completed... */
261 return; /* So just ignore the message... */
262
263 /* Capability negotiation is now done... */
264 source_p->connection->registration &= ~REG_NEED_CAP;
265
266 /* If client is now done... */
267 if (source_p->connection->registration == 0)
268 register_local_user(source_p);
269 }
270
271 static void
272 cap_list(struct Client *source_p, const char *arg)
273 {
274 /* Send the list of the client's capabilities */
275 send_caplist(source_p, &source_p->connection->cap, NULL, "LIST");
276 }
277
278 static struct subcmd
279 {
280 const char *cmd;
281 void (*proc)(struct Client *, const char *);
282 } cmdlist[] = {
283 { "END", cap_end },
284 { "LIST", cap_list },
285 { "LS", cap_ls },
286 { "REQ", cap_req }
287 };
288
289 static int
290 subcmd_search(const char *cmd, const struct subcmd *elem)
291 {
292 return strcasecmp(cmd, elem->cmd);
293 }
294
295 /*! \brief CAP command handler
296 *
297 * \param source_p Pointer to allocated Client struct from which the message
298 * originally comes from. This can be a local or remote client.
299 * \param parc Integer holding the number of supplied arguments.
300 * \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL
301 * pointers.
302 * \note Valid arguments for this command are:
303 * - parv[0] = command
304 * - parv[1] = CAP subcommand
305 * - parv[2] = space-separated list of capabilities
306 */
307 static void
308 m_cap(struct Client *source_p, int parc, char *parv[])
309 {
310 const char *subcmd = parv[1], *caplist = parv[2];
311 struct subcmd *cmd = NULL;
312
313 /* Find the subcommand handler */
314 if (!(cmd = bsearch(subcmd, cmdlist,
315 sizeof(cmdlist) / sizeof(struct subcmd),
316 sizeof(struct subcmd), (bqcmp)subcmd_search)))
317 {
318 sendto_one_numeric(source_p, &me, ERR_INVALIDCAPCMD, subcmd);
319 return;
320 }
321
322 /* Then execute it... */
323 if (cmd->proc)
324 (cmd->proc)(source_p, caplist);
325 }
326
327 static struct Message cap_msgtab =
328 {
329 .cmd = "CAP",
330 .handlers[UNREGISTERED_HANDLER] = { .handler = m_cap, .args_min = 2 },
331 .handlers[CLIENT_HANDLER] = { .handler = m_cap, .args_min = 2 },
332 .handlers[SERVER_HANDLER] = { .handler = m_ignore },
333 .handlers[ENCAP_HANDLER] = { .handler = m_ignore },
334 .handlers[OPER_HANDLER] = { .handler = m_cap, .args_min = 2 }
335 };
336
337 static void
338 module_init(void)
339 {
340 /* First, let's sort the array */
341 qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort);
342 mod_add_cmd(&cap_msgtab);
343 }
344
345 static void
346 module_exit(void)
347 {
348 mod_del_cmd(&cap_msgtab);
349 }
350
351 struct module module_entry =
352 {
353 .version = "$Revision$",
354 .modinit = module_init,
355 .modexit = module_exit,
356 };

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision