ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 4934
Committed: Tue Nov 25 21:09:09 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 12584 byte(s)
Log Message:
- Style corrections; constifications

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

Properties

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