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

Contents of /ircd-hybrid-7.2/modules/m_cap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (show annotations)
Mon May 1 11:41:09 2006 UTC (14 years, 3 months ago) by michael
File MIME type: text/x-chdr
File size: 12008 byte(s)
- Fixed a bunch of compile warnings

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

Properties

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

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