ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 8437
Committed: Thu Mar 29 09:05:34 2018 UTC (8 years, 3 months ago) by michael
Content type: text/x-csrc
File size: 13011 byte(s)
Log Message:
- Stylistic changes

File Contents

# User Rev Content
1 michael 503 /*
2 michael 2820 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 michael 503 *
4 michael 2820 * Copyright (c) 2004 Kevin L. Mitchell <klmitch@mit.edu>
5 michael 8279 * Copyright (c) 2006-2018 ircd-hybrid development team
6 michael 503 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 michael 503 * USA
21     */
22    
23 michael 2820 /*! \file m_cap.c
24     * \brief Includes required functions for processing the CAP command.
25     * \version $Id$
26 michael 503 */
27    
28     #include "stdinc.h"
29     #include "client.h"
30     #include "ircd.h"
31     #include "numeric.h"
32 michael 3347 #include "user.h"
33 michael 503 #include "send.h"
34     #include "parse.h"
35     #include "modules.h"
36     #include "irc_string.h"
37    
38    
39 michael 8431 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 michael 503
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 michael 2345 { (cap), (flags), (name), sizeof(name) - 1 }
58 michael 2910 _CAP(CAP_UHNAMES, 0, "userhost-in-names"),
59 michael 1734 _CAP(CAP_MULTI_PREFIX, 0, "multi-prefix"),
60 michael 4792 _CAP(CAP_AWAY_NOTIFY, 0, "away-notify"),
61 michael 6400 _CAP(CAP_EXTENDED_JOIN, 0, "extended-join"),
62 michael 6767 _CAP(CAP_ACCOUNT_NOTIFY, 0, "account-notify"),
63 michael 6774 _CAP(CAP_INVITE_NOTIFY, 0, "invite-notify"),
64     _CAP(CAP_CHGHOST, 0, "chghost")
65 michael 503 #undef _CAP
66     };
67    
68 michael 2345 #define CAPAB_LIST_LEN (sizeof(capab_list) / sizeof(struct capabilities))
69 michael 503
70     static int
71     capab_sort(const struct capabilities *cap1, const struct capabilities *cap2)
72     {
73 michael 536 return strcasecmp(cap1->name, cap2->name);
74 michael 503 }
75    
76     static int
77     capab_search(const char *key, const struct capabilities *cap)
78     {
79     const char *rb = cap->name;
80    
81 michael 2820 while (ToLower(*key) == ToLower(*rb)) /* Walk equivalent part of strings */
82     if (*key++ == '\0') /* Hit the end, all right... */
83 michael 503 return 0;
84 michael 3875 else /* OK, let's move on... */
85 michael 4934 ++rb;
86 michael 503
87 michael 872 /*
88     * If the character they differ on happens to be a space, and it happens
89 michael 503 * 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 michael 872 struct capabilities *cap = NULL;
100 michael 503
101 michael 2820 *neg_p = 0; /* Clear negative flag... */
102 michael 503
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 michael 872 if (*caplist == '-')
109     {
110 michael 2820 ++caplist; /* Yes; step past the flag... */
111     *neg_p = 1; /* Remember that it is negative... */
112 michael 503 }
113    
114     /* OK, now see if we can look up the capability... */
115 michael 872 if (*caplist)
116     {
117 michael 503 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 michael 2345 ++caplist;
124 michael 503 }
125     else
126 michael 2820 caplist += cap->namelen; /* Advance to end of capability name */
127 michael 3497
128     /* Strip trailing spaces */
129     while (*caplist && IsSpace(*caplist))
130     ++caplist;
131 michael 503 }
132    
133 michael 2820 assert(caplist != *caplist_p || !*caplist); /* We *must* advance */
134 michael 503
135 michael 2820 /* Move ahead in capability list string--or zero pointer if we hit end */
136 michael 3565 *caplist_p = *caplist ? caplist : NULL;
137 michael 503
138 michael 2820 return cap; /* And return the capability (if any) */
139 michael 503 }
140    
141 michael 1230 /** Send a CAP \a subcmd list of capability changes to \a source_p.
142 michael 503 * If more than one line is necessary, each line before the last has
143     * an added "*" parameter before that line's capability list.
144 michael 1230 * @param[in] source_p Client receiving capability list.
145 michael 503 * @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 michael 5018 send_caplist(struct Client *source_p,
151     const unsigned int *const set,
152     const unsigned int *const rem, const char *subcmd)
153 michael 503 {
154     char capbuf[IRCD_BUFSIZE] = "", pfx[16];
155     char cmdbuf[IRCD_BUFSIZE] = "";
156 michael 5021 unsigned int i, loc, len, pfx_len, clen;
157 michael 503
158 michael 2820 /* Set up the buffer for the final LS message... */
159 michael 503 clen = snprintf(cmdbuf, sizeof(capbuf), ":%s CAP %s %s ", me.name,
160 michael 1230 source_p->name[0] ? source_p->name : "*", subcmd);
161 michael 503
162     for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i)
163     {
164 michael 5021 const struct capabilities *cap = &capab_list[i];
165 michael 503
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 michael 5021 if (!(rem && (*rem & cap->cap)) &&
173     !(set && (*set & cap->cap)) &&
174     (rem || set || (cap->flags & CAPFL_HIDDEN)))
175 michael 503 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 michael 5021 if (rem && (*rem & cap->cap))
183 michael 503 pfx[pfx_len++] = '-';
184     else
185     {
186 michael 5021 if (cap->flags & CAPFL_PROTO)
187 michael 503 pfx[pfx_len++] = '~';
188 michael 5021 if (cap->flags & CAPFL_STICKY)
189 michael 503 pfx[pfx_len++] = '=';
190     }
191    
192     pfx[pfx_len] = '\0';
193    
194 michael 5021 len = cap->namelen + pfx_len; /* How much we'd add... */
195 michael 503
196     if (sizeof(capbuf) < (clen + loc + len + 15))
197     {
198 michael 2820 /* Would add too much; must flush */
199 michael 1230 sendto_one(source_p, "%s* :%s", cmdbuf, capbuf);
200 michael 2820 capbuf[(loc = 0)] = '\0'; /* Re-terminate the buffer... */
201 michael 503 }
202    
203     loc += snprintf(capbuf + loc, sizeof(capbuf) - loc,
204 michael 5021 "%s%s", pfx, cap->name);
205 michael 503 }
206    
207 michael 1230 sendto_one(source_p, "%s:%s", cmdbuf, capbuf);
208 michael 503
209 michael 2820 return 0; /* Convenience return */
210 michael 503 }
211    
212     static int
213 michael 1230 cap_ls(struct Client *source_p, const char *caplist)
214 michael 503 {
215 michael 2820 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
216 michael 4588 source_p->connection->registration |= REG_NEED_CAP;
217 michael 503
218 michael 5018 return send_caplist(source_p, NULL, NULL, "LS"); /* Send list of capabilities */
219 michael 503 }
220    
221     static int
222 michael 1230 cap_req(struct Client *source_p, const char *caplist)
223 michael 503 {
224     const char *cl = caplist;
225 michael 872 struct capabilities *cap = NULL;
226 michael 503 unsigned int set = 0, rem = 0;
227 michael 4588 unsigned int cs = source_p->connection->cap_client; /* capability set */
228     unsigned int as = source_p->connection->cap_active; /* active set */
229 michael 872 int neg = 0;
230 michael 503
231 michael 2820 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
232 michael 4588 source_p->connection->registration |= REG_NEED_CAP;
233 michael 503
234 michael 2820 while (cl) { /* Walk through the capabilities list... */
235 michael 3875 if (!(cap = find_cap(&cl, &neg)) /* Look up capability... */
236 michael 2820 || (!neg && (cap->flags & CAPFL_PROHIBIT)) /* Is it prohibited? */
237     || (neg && (cap->flags & CAPFL_STICKY))) { /* Is it sticky? */
238 michael 1230 sendto_one(source_p, ":%s CAP %s NAK :%s", me.name,
239     source_p->name[0] ? source_p->name : "*", caplist);
240 michael 2820 return 0; /* Can't complete requested op... */
241 michael 503 }
242    
243     if (neg)
244     {
245 michael 2820 /* Set or clear the capability... */
246 michael 503 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 michael 5018 send_caplist(source_p, &set, &rem, "ACK");
266 michael 503
267 michael 4588 source_p->connection->cap_client = cs;
268     source_p->connection->cap_active = as;
269 michael 503
270     return 0;
271     }
272    
273     static int
274 michael 1230 cap_ack(struct Client *source_p, const char *caplist)
275 michael 503 {
276     const char *cl = caplist;
277     struct capabilities *cap = NULL;
278 michael 872 int neg = 0;
279 michael 503
280     /*
281     * Coming from the client, this generally indicates that the client
282 michael 2820 * is using a new backwards-incompatible protocol feature. As such,
283 michael 503 * it does not require further response from the server.
284     */
285     while (cl)
286     {
287 michael 2820 /* Walk through the capabilities list... */
288     if (!(cap = find_cap(&cl, &neg)) || /* Look up capability... */
289 michael 5018 (neg ? (source_p->connection->cap_client & cap->cap) :
290     !(source_p->connection->cap_client & cap->cap))) /* uh... */
291 michael 503 continue;
292    
293 michael 6561 /* 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 michael 4588 source_p->connection->cap_active &= ~cap->cap;
300 michael 6561 }
301 michael 503 else
302 michael 6561 {
303     if (cap->flags & CAPFL_PROHIBIT)
304     continue; /* and don't set prohibited ones */
305    
306 michael 4588 source_p->connection->cap_active |= cap->cap;
307 michael 6561 }
308 michael 503 }
309    
310     return 0;
311     }
312    
313     static int
314 michael 1230 cap_clear(struct Client *source_p, const char *caplist)
315 michael 503 {
316     unsigned int cleared = 0;
317    
318 michael 3565 for (unsigned int ii = 0; ii < CAPAB_LIST_LEN; ++ii)
319 michael 503 {
320 michael 7870 const struct capabilities *cap = &capab_list[ii];
321 michael 503
322     /* Only clear active non-sticky capabilities. */
323 michael 5018 if (!(source_p->connection->cap_client & cap->cap) || (cap->flags & CAPFL_STICKY))
324 michael 503 continue;
325    
326     cleared |= cap->cap;
327 michael 4588 source_p->connection->cap_client &= ~cap->cap;
328 michael 503
329     if (!(cap->flags & CAPFL_PROTO))
330 michael 4588 source_p->connection->cap_active &= ~cap->cap;
331 michael 503 }
332    
333 michael 5018 return send_caplist(source_p, NULL, &cleared, "ACK");
334 michael 503 }
335    
336     static int
337 michael 1230 cap_end(struct Client *source_p, const char *caplist)
338 michael 503 {
339 michael 2820 if (!IsUnknown(source_p)) /* Registration has completed... */
340     return 0; /* So just ignore the message... */
341 michael 503
342 michael 2820 /* Capability negotiation is now done... */
343 michael 4588 source_p->connection->registration &= ~REG_NEED_CAP;
344 michael 503
345 michael 2820 /* If client is now done... */
346 michael 8437 if (source_p->connection->registration == 0)
347 michael 503 {
348 michael 1230 register_local_user(source_p);
349 michael 503 return 0;
350     }
351    
352 michael 2345 return 0; /* Can't do registration yet... */
353 michael 503 }
354    
355     static int
356 michael 1230 cap_list(struct Client *source_p, const char *caplist)
357 michael 503 {
358     /* Send the list of the client's capabilities */
359 michael 5018 return send_caplist(source_p, &source_p->connection->cap_client, NULL, "LIST");
360 michael 503 }
361    
362     static struct subcmd
363     {
364 michael 575 const char *cmd;
365 michael 1230 int (*proc)(struct Client *, const char *);
366 michael 503 } cmdlist[] = {
367     { "ACK", cap_ack },
368     { "CLEAR", cap_clear },
369     { "END", cap_end },
370     { "LIST", cap_list },
371     { "LS", cap_ls },
372 michael 575 { "NAK", NULL },
373 michael 503 { "REQ", cap_req }
374     };
375    
376     static int
377     subcmd_search(const char *cmd, const struct subcmd *elem)
378     {
379 michael 536 return strcasecmp(cmd, elem->cmd);
380 michael 503 }
381    
382 michael 3332 /*! \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 michael 503 */
394 michael 2820 static int
395 michael 3156 m_cap(struct Client *source_p, int parc, char *parv[])
396 michael 503 {
397 michael 1222 const char *subcmd = NULL, *caplist = NULL;
398 michael 503 struct subcmd *cmd = NULL;
399    
400 michael 2820 if (EmptyString(parv[1])) /* A subcommand is required */
401     return 0;
402 michael 503
403     subcmd = parv[1];
404    
405 michael 2820 if (parc > 2) /* A capability list was provided */
406 michael 503 caplist = parv[2];
407    
408 michael 2820 /* Find the subcommand handler */
409 michael 503 if (!(cmd = bsearch(subcmd, cmdlist,
410     sizeof(cmdlist) / sizeof(struct subcmd),
411     sizeof(struct subcmd), (bqcmp)subcmd_search)))
412     {
413 michael 3109 sendto_one_numeric(source_p, &me, ERR_INVALIDCAPCMD, subcmd);
414 michael 2820 return 0;
415 michael 503 }
416    
417 michael 2820 /* Then execute it... */
418 michael 503 if (cmd->proc)
419 michael 1230 (cmd->proc)(source_p, caplist);
420 michael 2820 return 0;
421 michael 503 }
422 michael 1230
423 michael 2820 static struct Message cap_msgtab =
424     {
425 michael 5881 .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 michael 1230 };
434    
435     static void
436     module_init(void)
437     {
438 michael 4384 /* First, let's sort the array */
439     qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort);
440 michael 1230 mod_add_cmd(&cap_msgtab);
441     }
442    
443     static void
444     module_exit(void)
445     {
446     mod_del_cmd(&cap_msgtab);
447     }
448    
449 michael 2820 struct module module_entry =
450     {
451 michael 1230 .version = "$Revision$",
452     .modinit = module_init,
453     .modexit = module_exit,
454     };

Properties

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