ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 8431
Committed: Tue Mar 27 18:49:15 2018 UTC (7 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 13007 byte(s)
Log Message:
- Stylistic changes

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-2018 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_PROHIBIT = 1 << 1, /**< Client may not set this capability */
43 CAPFL_PROTO = 1 << 2, /**< Cap must be acknowledged by client */
44 CAPFL_STICKY = 1 << 3 /**< Cap may not be cleared once set */
45 };
46
47 typedef int (*bqcmp)(const void *, const void *);
48
49 static struct capabilities
50 {
51 unsigned int cap;
52 unsigned int flags;
53 const char *name;
54 size_t namelen;
55 } capab_list[] = {
56 #define _CAP(cap, flags, name) \
57 { (cap), (flags), (name), sizeof(name) - 1 }
58 _CAP(CAP_UHNAMES, 0, "userhost-in-names"),
59 _CAP(CAP_MULTI_PREFIX, 0, "multi-prefix"),
60 _CAP(CAP_AWAY_NOTIFY, 0, "away-notify"),
61 _CAP(CAP_EXTENDED_JOIN, 0, "extended-join"),
62 _CAP(CAP_ACCOUNT_NOTIFY, 0, "account-notify"),
63 _CAP(CAP_INVITE_NOTIFY, 0, "invite-notify"),
64 _CAP(CAP_CHGHOST, 0, "chghost")
65 #undef _CAP
66 };
67
68 #define CAPAB_LIST_LEN (sizeof(capab_list) / sizeof(struct capabilities))
69
70 static int
71 capab_sort(const struct capabilities *cap1, const struct capabilities *cap2)
72 {
73 return strcasecmp(cap1->name, cap2->name);
74 }
75
76 static int
77 capab_search(const char *key, const struct capabilities *cap)
78 {
79 const char *rb = cap->name;
80
81 while (ToLower(*key) == ToLower(*rb)) /* Walk equivalent part of strings */
82 if (*key++ == '\0') /* Hit the end, all right... */
83 return 0;
84 else /* OK, let's move on... */
85 ++rb;
86
87 /*
88 * If the character they differ on happens to be a space, and it happens
89 * to be the same length as the capability name, then we've found a
90 * match; otherwise, return the difference of the two.
91 */
92 return (IsSpace(*key) && *rb == '\0') ? 0 : (ToLower(*key) - ToLower(*rb));
93 }
94
95 static struct capabilities *
96 find_cap(const char **caplist_p, int *neg_p)
97 {
98 const char *caplist = *caplist_p;
99 struct capabilities *cap = NULL;
100
101 *neg_p = 0; /* Clear negative flag... */
102
103 /* Next, find first non-whitespace character... */
104 while (*caplist && IsSpace(*caplist))
105 ++caplist;
106
107 /* We are now at the beginning of an element of the list; is it negative? */
108 if (*caplist == '-')
109 {
110 ++caplist; /* Yes; step past the flag... */
111 *neg_p = 1; /* Remember that it is negative... */
112 }
113
114 /* OK, now see if we can look up the capability... */
115 if (*caplist)
116 {
117 if (!(cap = bsearch(caplist, capab_list, CAPAB_LIST_LEN,
118 sizeof(struct capabilities),
119 (bqcmp)capab_search)))
120 {
121 /* Couldn't find the capability; advance to first whitespace character */
122 while (*caplist && !IsSpace(*caplist))
123 ++caplist;
124 }
125 else
126 caplist += cap->namelen; /* Advance to end of capability name */
127
128 /* Strip trailing spaces */
129 while (*caplist && IsSpace(*caplist))
130 ++caplist;
131 }
132
133 assert(caplist != *caplist_p || !*caplist); /* We *must* advance */
134
135 /* Move ahead in capability list string--or zero pointer if we hit end */
136 *caplist_p = *caplist ? caplist : NULL;
137
138 return cap; /* And return the capability (if any) */
139 }
140
141 /** Send a CAP \a subcmd list of capability changes to \a source_p.
142 * If more than one line is necessary, each line before the last has
143 * an added "*" parameter before that line's capability list.
144 * @param[in] source_p Client receiving capability list.
145 * @param[in] set Capabilities to show as set (with ack and sticky modifiers).
146 * @param[in] rem Capabalities to show as removed (with no other modifier).
147 * @param[in] subcmd Name of capability subcommand.
148 */
149 static int
150 send_caplist(struct Client *source_p,
151 const unsigned int *const set,
152 const unsigned int *const rem, const char *subcmd)
153 {
154 char capbuf[IRCD_BUFSIZE] = "", pfx[16];
155 char cmdbuf[IRCD_BUFSIZE] = "";
156 unsigned int i, loc, len, pfx_len, clen;
157
158 /* Set up the buffer for the final LS message... */
159 clen = snprintf(cmdbuf, sizeof(capbuf), ":%s CAP %s %s ", me.name,
160 source_p->name[0] ? source_p->name : "*", subcmd);
161
162 for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i)
163 {
164 const struct capabilities *cap = &capab_list[i];
165
166 /*
167 * This is a little bit subtle, but just involves applying de
168 * Morgan's laws to the obvious check: We must display the
169 * capability if (and only if) it is set in \a rem or \a set, or
170 * if both are null and the capability is hidden.
171 */
172 if (!(rem && (*rem & cap->cap)) &&
173 !(set && (*set & cap->cap)) &&
174 (rem || set || (cap->flags & CAPFL_HIDDEN)))
175 continue;
176
177 /* Build the prefix (space separator and any modifiers needed). */
178 pfx_len = 0;
179
180 if (loc)
181 pfx[pfx_len++] = ' ';
182 if (rem && (*rem & cap->cap))
183 pfx[pfx_len++] = '-';
184 else
185 {
186 if (cap->flags & CAPFL_PROTO)
187 pfx[pfx_len++] = '~';
188 if (cap->flags & CAPFL_STICKY)
189 pfx[pfx_len++] = '=';
190 }
191
192 pfx[pfx_len] = '\0';
193
194 len = cap->namelen + pfx_len; /* How much we'd add... */
195
196 if (sizeof(capbuf) < (clen + loc + len + 15))
197 {
198 /* Would add too much; must flush */
199 sendto_one(source_p, "%s* :%s", cmdbuf, capbuf);
200 capbuf[(loc = 0)] = '\0'; /* Re-terminate the buffer... */
201 }
202
203 loc += snprintf(capbuf + loc, sizeof(capbuf) - loc,
204 "%s%s", pfx, cap->name);
205 }
206
207 sendto_one(source_p, "%s:%s", cmdbuf, capbuf);
208
209 return 0; /* Convenience return */
210 }
211
212 static int
213 cap_ls(struct Client *source_p, const char *caplist)
214 {
215 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
216 source_p->connection->registration |= REG_NEED_CAP;
217
218 return send_caplist(source_p, NULL, NULL, "LS"); /* Send list of capabilities */
219 }
220
221 static int
222 cap_req(struct Client *source_p, const char *caplist)
223 {
224 const char *cl = caplist;
225 struct capabilities *cap = NULL;
226 unsigned int set = 0, rem = 0;
227 unsigned int cs = source_p->connection->cap_client; /* capability set */
228 unsigned int as = source_p->connection->cap_active; /* active set */
229 int neg = 0;
230
231 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
232 source_p->connection->registration |= REG_NEED_CAP;
233
234 while (cl) { /* Walk through the capabilities list... */
235 if (!(cap = find_cap(&cl, &neg)) /* Look up capability... */
236 || (!neg && (cap->flags & CAPFL_PROHIBIT)) /* Is it prohibited? */
237 || (neg && (cap->flags & CAPFL_STICKY))) { /* Is it sticky? */
238 sendto_one(source_p, ":%s CAP %s NAK :%s", me.name,
239 source_p->name[0] ? source_p->name : "*", caplist);
240 return 0; /* Can't complete requested op... */
241 }
242
243 if (neg)
244 {
245 /* Set or clear the capability... */
246 rem |= cap->cap;
247 set &= ~cap->cap;
248 cs &= ~cap->cap;
249
250 if (!(cap->flags & CAPFL_PROTO))
251 as &= ~cap->cap;
252 }
253 else
254 {
255 rem &= ~cap->cap;
256 set |= cap->cap;
257 cs |= cap->cap;
258
259 if (!(cap->flags & CAPFL_PROTO))
260 as |= cap->cap;
261 }
262 }
263
264 /* Notify client of accepted changes and copy over results. */
265 send_caplist(source_p, &set, &rem, "ACK");
266
267 source_p->connection->cap_client = cs;
268 source_p->connection->cap_active = as;
269
270 return 0;
271 }
272
273 static int
274 cap_ack(struct Client *source_p, const char *caplist)
275 {
276 const char *cl = caplist;
277 struct capabilities *cap = NULL;
278 int neg = 0;
279
280 /*
281 * Coming from the client, this generally indicates that the client
282 * is using a new backwards-incompatible protocol feature. As such,
283 * it does not require further response from the server.
284 */
285 while (cl)
286 {
287 /* Walk through the capabilities list... */
288 if (!(cap = find_cap(&cl, &neg)) || /* Look up capability... */
289 (neg ? (source_p->connection->cap_client & cap->cap) :
290 !(source_p->connection->cap_client & cap->cap))) /* uh... */
291 continue;
292
293 /* Set or clear the active capability... */
294 if (neg)
295 {
296 if (cap->flags & CAPFL_STICKY)
297 continue; /* but don't clear sticky capabilities */
298
299 source_p->connection->cap_active &= ~cap->cap;
300 }
301 else
302 {
303 if (cap->flags & CAPFL_PROHIBIT)
304 continue; /* and don't set prohibited ones */
305
306 source_p->connection->cap_active |= cap->cap;
307 }
308 }
309
310 return 0;
311 }
312
313 static int
314 cap_clear(struct Client *source_p, const char *caplist)
315 {
316 unsigned int cleared = 0;
317
318 for (unsigned int ii = 0; ii < CAPAB_LIST_LEN; ++ii)
319 {
320 const struct capabilities *cap = &capab_list[ii];
321
322 /* Only clear active non-sticky capabilities. */
323 if (!(source_p->connection->cap_client & cap->cap) || (cap->flags & CAPFL_STICKY))
324 continue;
325
326 cleared |= cap->cap;
327 source_p->connection->cap_client &= ~cap->cap;
328
329 if (!(cap->flags & CAPFL_PROTO))
330 source_p->connection->cap_active &= ~cap->cap;
331 }
332
333 return send_caplist(source_p, NULL, &cleared, "ACK");
334 }
335
336 static int
337 cap_end(struct Client *source_p, const char *caplist)
338 {
339 if (!IsUnknown(source_p)) /* Registration has completed... */
340 return 0; /* So just ignore the message... */
341
342 /* Capability negotiation is now done... */
343 source_p->connection->registration &= ~REG_NEED_CAP;
344
345 /* If client is now done... */
346 if (!source_p->connection->registration)
347 {
348 register_local_user(source_p);
349 return 0;
350 }
351
352 return 0; /* Can't do registration yet... */
353 }
354
355 static int
356 cap_list(struct Client *source_p, const char *caplist)
357 {
358 /* Send the list of the client's capabilities */
359 return send_caplist(source_p, &source_p->connection->cap_client, NULL, "LIST");
360 }
361
362 static struct subcmd
363 {
364 const char *cmd;
365 int (*proc)(struct Client *, const char *);
366 } cmdlist[] = {
367 { "ACK", cap_ack },
368 { "CLEAR", cap_clear },
369 { "END", cap_end },
370 { "LIST", cap_list },
371 { "LS", cap_ls },
372 { "NAK", NULL },
373 { "REQ", cap_req }
374 };
375
376 static int
377 subcmd_search(const char *cmd, const struct subcmd *elem)
378 {
379 return strcasecmp(cmd, elem->cmd);
380 }
381
382 /*! \brief CAP command handler
383 *
384 * \param source_p Pointer to allocated Client struct from which the message
385 * originally comes from. This can be a local or remote client.
386 * \param parc Integer holding the number of supplied arguments.
387 * \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL
388 * pointers.
389 * \note Valid arguments for this command are:
390 * - parv[0] = command
391 * - parv[1] = CAP subcommand
392 * - parv[2] = space-separated list of capabilities
393 */
394 static int
395 m_cap(struct Client *source_p, int parc, char *parv[])
396 {
397 const char *subcmd = NULL, *caplist = NULL;
398 struct subcmd *cmd = NULL;
399
400 if (EmptyString(parv[1])) /* A subcommand is required */
401 return 0;
402
403 subcmd = parv[1];
404
405 if (parc > 2) /* A capability list was provided */
406 caplist = parv[2];
407
408 /* Find the subcommand handler */
409 if (!(cmd = bsearch(subcmd, cmdlist,
410 sizeof(cmdlist) / sizeof(struct subcmd),
411 sizeof(struct subcmd), (bqcmp)subcmd_search)))
412 {
413 sendto_one_numeric(source_p, &me, ERR_INVALIDCAPCMD, subcmd);
414 return 0;
415 }
416
417 /* Then execute it... */
418 if (cmd->proc)
419 (cmd->proc)(source_p, caplist);
420 return 0;
421 }
422
423 static struct Message cap_msgtab =
424 {
425 .cmd = "CAP",
426 .args_min = 2,
427 .args_max = MAXPARA,
428 .handlers[UNREGISTERED_HANDLER] = m_cap,
429 .handlers[CLIENT_HANDLER] = m_cap,
430 .handlers[SERVER_HANDLER] = m_ignore,
431 .handlers[ENCAP_HANDLER] = m_ignore,
432 .handlers[OPER_HANDLER] = m_cap
433 };
434
435 static void
436 module_init(void)
437 {
438 /* First, let's sort the array */
439 qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort);
440 mod_add_cmd(&cap_msgtab);
441 }
442
443 static void
444 module_exit(void)
445 {
446 mod_del_cmd(&cap_msgtab);
447 }
448
449 struct module module_entry =
450 {
451 .version = "$Revision$",
452 .modinit = module_init,
453 .modexit = module_exit,
454 };

Properties

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