/[svn]/ircd-hybrid/modules/m_cap.c
ViewVC logotype

Annotation of /ircd-hybrid/modules/m_cap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 502 - (hide annotations)
Fri Mar 3 19:49:25 2006 UTC (15 years, 3 months ago) by michael
File MIME type: text/x-chdr
File size: 11931 byte(s)
- Implemented CAP command handler based uppon ircu's m_cap()
- Added somewhat outdated draft-mitchell-irc-capabilities-01.txt until
  I get the latest version from kev.
- Added "multi-prefix" cap so clients supporting "multi-prefix"
  may recieve multi prefixed NAMES replies, e.g. @%+nick1 @+nick2 ..
- Fixed "make clean" for src/conf/

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

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Id

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28