ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 1832
Committed: Fri Apr 19 19:16:09 2013 UTC (12 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 12252 byte(s)
Log Message:
- Made all numeric defines use the actual string instead of the numeric value
  which allows to use gcc's printf format attribute
- Remove current message locale implementation

File Contents

# User Rev Content
1 michael 503 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     *
4     * Copyright (C) 2004 Kevin L. Mitchell <klmitch@mit.edu>
5 michael 1309 * Copyright (C) 2006-2012 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     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22     * $Id$
23     */
24    
25     /** @file
26     * @brief Capability negotiation commands
27     * @version $Id$
28     */
29    
30     #include "stdinc.h"
31     #include "client.h"
32     #include "hash.h"
33     #include "ircd.h"
34     #include "numeric.h"
35 michael 1309 #include "conf.h"
36 michael 503 #include "s_user.h"
37     #include "s_serv.h"
38     #include "send.h"
39     #include "parse.h"
40     #include "modules.h"
41     #include "packet.h"
42     #include "irc_string.h"
43    
44    
45     #define CAPFL_HIDDEN 0x0001 /**< Do not advertize this capability */
46     #define CAPFL_PROHIBIT 0x0002 /**< Client may not set this capability */
47     #define CAPFL_PROTO 0x0004 /**< Cap must be acknowledged by client */
48     #define CAPFL_STICKY 0x0008 /**< Cap may not be cleared once set */
49    
50     typedef int (*bqcmp)(const void *, const void *);
51    
52     static struct capabilities
53     {
54     unsigned int cap;
55     unsigned int flags;
56     const char *name;
57     size_t namelen;
58     } capab_list[] = {
59     #define _CAP(cap, flags, name) \
60     { (cap), (flags), (name), sizeof(name) - 1 }
61 michael 1734 _CAP(CAP_MULTI_PREFIX, 0, "multi-prefix"),
62     _CAP(CAP_AWAY_NOTIFY, 0, "away-notify")
63 michael 503 #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 michael 536 return strcasecmp(cap1->name, cap2->name);
72 michael 503 }
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 michael 872 /*
86     * If the character they differ on happens to be a space, and it happens
87 michael 503 * 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     static int inited = 0;
97     const char *caplist = *caplist_p;
98 michael 872 struct capabilities *cap = NULL;
99 michael 503
100     *neg_p = 0; /* clear negative flag... */
101    
102     if (!inited)
103     {
104     /* First, let's sort the array... */
105     qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort);
106     ++inited;
107     }
108    
109     /* Next, find first non-whitespace character... */
110     while (*caplist && IsSpace(*caplist))
111     ++caplist;
112    
113     /* We are now at the beginning of an element of the list; is it negative? */
114 michael 872 if (*caplist == '-')
115     {
116 michael 503 ++caplist; /* yes; step past the flag... */
117     *neg_p = 1; /* remember that it is negative... */
118     }
119    
120     /* OK, now see if we can look up the capability... */
121 michael 872 if (*caplist)
122     {
123 michael 503 if (!(cap = bsearch(caplist, capab_list, CAPAB_LIST_LEN,
124     sizeof(struct capabilities),
125     (bqcmp)capab_search)))
126     {
127     /* Couldn't find the capability; advance to first whitespace character */
128     while (*caplist && !IsSpace(*caplist))
129     ++caplist;
130     }
131     else
132     caplist += cap->namelen; /* advance to end of capability name */
133     }
134    
135     assert(caplist != *caplist_p || !*caplist); /* we *must* advance */
136    
137     /* move ahead in capability list string--or zero pointer if we hit end */
138     *caplist_p = *caplist ? caplist : 0;
139    
140     return cap; /* and return the capability (if any) */
141     }
142    
143 michael 1230 /** Send a CAP \a subcmd list of capability changes to \a source_p.
144 michael 503 * If more than one line is necessary, each line before the last has
145     * an added "*" parameter before that line's capability list.
146 michael 1230 * @param[in] source_p Client receiving capability list.
147 michael 503 * @param[in] set Capabilities to show as set (with ack and sticky modifiers).
148     * @param[in] rem Capabalities to show as removed (with no other modifier).
149     * @param[in] subcmd Name of capability subcommand.
150     */
151     static int
152 michael 1230 send_caplist(struct Client *source_p, unsigned int set,
153 michael 503 unsigned int rem, const char *subcmd)
154     {
155     char capbuf[IRCD_BUFSIZE] = "", pfx[16];
156     char cmdbuf[IRCD_BUFSIZE] = "";
157 michael 575 unsigned int i, loc, len, flags, pfx_len, clen;
158 michael 503
159     /* set up the buffer for the final LS message... */
160     clen = snprintf(cmdbuf, sizeof(capbuf), ":%s CAP %s %s ", me.name,
161 michael 1230 source_p->name[0] ? source_p->name : "*", subcmd);
162 michael 503
163     for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i)
164     {
165     flags = capab_list[i].flags;
166    
167     /*
168     * This is a little bit subtle, but just involves applying de
169     * Morgan's laws to the obvious check: We must display the
170     * capability if (and only if) it is set in \a rem or \a set, or
171     * if both are null and the capability is hidden.
172     */
173     if (!(rem && (rem & capab_list[i].cap)) &&
174     !(set && (set & capab_list[i].cap)) &&
175     (rem || set || (flags & CAPFL_HIDDEN)))
176     continue;
177    
178     /* Build the prefix (space separator and any modifiers needed). */
179     pfx_len = 0;
180    
181     if (loc)
182     pfx[pfx_len++] = ' ';
183     if (rem && (rem & capab_list[i].cap))
184     pfx[pfx_len++] = '-';
185     else
186     {
187     if (flags & CAPFL_PROTO)
188     pfx[pfx_len++] = '~';
189     if (flags & CAPFL_STICKY)
190     pfx[pfx_len++] = '=';
191     }
192    
193     pfx[pfx_len] = '\0';
194    
195     len = capab_list[i].namelen + pfx_len; /* how much we'd add... */
196    
197     if (sizeof(capbuf) < (clen + loc + len + 15))
198     {
199     /* would add too much; must flush */
200 michael 1230 sendto_one(source_p, "%s* :%s", cmdbuf, capbuf);
201 michael 503 capbuf[(loc = 0)] = '\0'; /* re-terminate the buffer... */
202     }
203    
204     loc += snprintf(capbuf + loc, sizeof(capbuf) - loc,
205     "%s%s", pfx, capab_list[i].name);
206     }
207    
208 michael 1230 sendto_one(source_p, "%s:%s", cmdbuf, capbuf);
209 michael 503
210     return 0; /* convenience return */
211     }
212    
213     static int
214 michael 1230 cap_ls(struct Client *source_p, const char *caplist)
215 michael 503 {
216 michael 1230 if (IsUnknown(source_p)) /* registration hasn't completed; suspend it... */
217     source_p->localClient->registration |= REG_NEED_CAP;
218 michael 503
219 michael 1230 return send_caplist(source_p, 0, 0, "LS"); /* send list of capabilities */
220 michael 503 }
221    
222     static int
223 michael 1230 cap_req(struct Client *source_p, const char *caplist)
224 michael 503 {
225     const char *cl = caplist;
226 michael 872 struct capabilities *cap = NULL;
227 michael 503 unsigned int set = 0, rem = 0;
228 michael 1230 unsigned int cs = source_p->localClient->cap_client; /* capability set */
229     unsigned int as = source_p->localClient->cap_active; /* active set */
230 michael 872 int neg = 0;
231 michael 503
232 michael 1230 if (IsUnknown(source_p)) /* registration hasn't completed; suspend it... */
233     source_p->localClient->registration |= REG_NEED_CAP;
234 michael 503
235     while (cl) { /* walk through the capabilities list... */
236     if (!(cap = find_cap(&cl, &neg)) /* look up capability... */
237     || (!neg && (cap->flags & CAPFL_PROHIBIT)) /* is it prohibited? */
238     || (neg && (cap->flags & CAPFL_STICKY))) { /* is it sticky? */
239 michael 1230 sendto_one(source_p, ":%s CAP %s NAK :%s", me.name,
240     source_p->name[0] ? source_p->name : "*", caplist);
241 michael 503 return 0; /* can't complete requested op... */
242     }
243    
244     if (neg)
245     {
246     /* set or clear the capability... */
247     rem |= cap->cap;
248     set &= ~cap->cap;
249     cs &= ~cap->cap;
250    
251     if (!(cap->flags & CAPFL_PROTO))
252     as &= ~cap->cap;
253     }
254     else
255     {
256     rem &= ~cap->cap;
257     set |= cap->cap;
258     cs |= cap->cap;
259    
260     if (!(cap->flags & CAPFL_PROTO))
261     as |= cap->cap;
262     }
263     }
264    
265     /* Notify client of accepted changes and copy over results. */
266 michael 1230 send_caplist(source_p, set, rem, "ACK");
267 michael 503
268 michael 1230 source_p->localClient->cap_client = cs;
269     source_p->localClient->cap_active = as;
270 michael 503
271     return 0;
272     }
273    
274     static int
275 michael 1230 cap_ack(struct Client *source_p, const char *caplist)
276 michael 503 {
277     const char *cl = caplist;
278     struct capabilities *cap = NULL;
279 michael 872 int neg = 0;
280 michael 503
281     /*
282     * Coming from the client, this generally indicates that the client
283     * is using a new backwards-incompatible protocol feature. As such,
284     * it does not require further response from the server.
285     */
286     while (cl)
287     {
288     /* walk through the capabilities list... */
289     if (!(cap = find_cap(&cl, &neg)) || /* look up capability... */
290 michael 1230 (neg ? (source_p->localClient->cap_active & cap->cap) :
291     !(source_p->localClient->cap_active & cap->cap))) /* uh... */
292 michael 503 continue;
293    
294     if (neg) /* set or clear the active capability... */
295 michael 1230 source_p->localClient->cap_active &= ~cap->cap;
296 michael 503 else
297 michael 1230 source_p->localClient->cap_active |= cap->cap;
298 michael 503 }
299    
300     return 0;
301     }
302    
303     static int
304 michael 1230 cap_clear(struct Client *source_p, const char *caplist)
305 michael 503 {
306 michael 872 struct capabilities *cap = NULL;
307 michael 503 unsigned int ii;
308     unsigned int cleared = 0;
309    
310     for (ii = 0; ii < CAPAB_LIST_LEN; ++ii)
311     {
312     cap = &capab_list[ii];
313    
314     /* Only clear active non-sticky capabilities. */
315 michael 1230 if (!(source_p->localClient->cap_active & cap->cap) || (cap->flags & CAPFL_STICKY))
316 michael 503 continue;
317    
318     cleared |= cap->cap;
319 michael 1230 source_p->localClient->cap_client &= ~cap->cap;
320 michael 503
321     if (!(cap->flags & CAPFL_PROTO))
322 michael 1230 source_p->localClient->cap_active &= ~cap->cap;
323 michael 503 }
324    
325 michael 1230 return send_caplist(source_p, 0, cleared, "ACK");
326 michael 503 }
327    
328     static int
329 michael 1230 cap_end(struct Client *source_p, const char *caplist)
330 michael 503 {
331 michael 1230 if (!IsUnknown(source_p)) /* registration has completed... */
332 michael 503 return 0; /* so just ignore the message... */
333    
334     /* capability negotiation is now done... */
335 michael 1230 source_p->localClient->registration &= ~REG_NEED_CAP;
336 michael 503
337     /* if client is now done... */
338 michael 1230 if (!source_p->localClient->registration)
339 michael 503 {
340 michael 1230 register_local_user(source_p);
341 michael 503 return 0;
342     }
343    
344     return 0; /* Can't do registration yet... */
345     }
346    
347     static int
348 michael 1230 cap_list(struct Client *source_p, const char *caplist)
349 michael 503 {
350     /* Send the list of the client's capabilities */
351 michael 1230 return send_caplist(source_p, source_p->localClient->cap_client, 0, "LIST");
352 michael 503 }
353    
354     static struct subcmd
355     {
356 michael 575 const char *cmd;
357 michael 1230 int (*proc)(struct Client *, const char *);
358 michael 503 } cmdlist[] = {
359     { "ACK", cap_ack },
360     { "CLEAR", cap_clear },
361     { "END", cap_end },
362     { "LIST", cap_list },
363     { "LS", cap_ls },
364 michael 575 { "NAK", NULL },
365 michael 503 { "REQ", cap_req }
366     };
367    
368     static int
369     subcmd_search(const char *cmd, const struct subcmd *elem)
370     {
371 michael 536 return strcasecmp(cmd, elem->cmd);
372 michael 503 }
373    
374     /** Handle a capability request or response from a client.
375 michael 1230 * \param client_p Client that sent us the message.
376     * \param source_p Original source of message.
377     * \param parc Number of arguments.
378     * \param parv Argument vector.
379 michael 503 */
380     static void
381 michael 1230 m_cap(struct Client *client_p, struct Client *source_p, int parc, char *parv[])
382 michael 503 {
383 michael 1222 const char *subcmd = NULL, *caplist = NULL;
384 michael 503 struct subcmd *cmd = NULL;
385    
386 michael 1178 if (EmptyString(parv[1])) /* a subcommand is required */
387 michael 503 return;
388    
389     subcmd = parv[1];
390    
391     if (parc > 2) /* a capability list was provided */
392     caplist = parv[2];
393    
394     /* find the subcommand handler */
395     if (!(cmd = bsearch(subcmd, cmdlist,
396     sizeof(cmdlist) / sizeof(struct subcmd),
397     sizeof(struct subcmd), (bqcmp)subcmd_search)))
398     {
399 michael 1832 sendto_one(source_p, ERR_INVALIDCAPCMD, me.name,
400 michael 1230 source_p->name[0] ? source_p->name : "*", subcmd);
401 michael 503 return;
402     }
403    
404     /* then execute it... */
405     if (cmd->proc)
406 michael 1230 (cmd->proc)(source_p, caplist);
407 michael 503 }
408 michael 1230
409     static struct Message cap_msgtab = {
410     "CAP", 0, 0, 2, MAXPARA, MFLG_SLOW, 0,
411     { m_cap, m_cap, m_ignore, m_ignore, m_cap, m_ignore }
412     };
413    
414     static void
415     module_init(void)
416     {
417     mod_add_cmd(&cap_msgtab);
418     }
419    
420     static void
421     module_exit(void)
422     {
423     mod_del_cmd(&cap_msgtab);
424     }
425    
426     struct module module_entry = {
427     .node = { NULL, NULL, NULL },
428     .name = NULL,
429     .version = "$Revision$",
430     .handle = NULL,
431     .modinit = module_init,
432     .modexit = module_exit,
433     .flags = 0
434     };

Properties

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