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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 504 - (show annotations)
Fri Mar 3 20:11:54 2006 UTC (15 years, 2 months ago) by michael
File MIME type: text/x-chdr
File size: 11950 byte(s)
- Misc changes regarding CAP API documentation
- Removed forgotten debugging code in ircd.c

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_stats.h"
38 #include "s_user.h"
39 #include "s_serv.h"
40 #include "send.h"
41 #include "channel.h"
42 #include "msg.h"
43 #include "parse.h"
44 #include "conf/modules.h"
45 #include "common.h"
46 #include "packet.h"
47
48 static void m_cap(struct Client *, struct Client *, int, char *[]);
49
50 struct Message cap_msgtab = {
51 "CAP", 0, 0, 2, 0, MFLG_SLOW, 0,
52 { m_cap, m_cap, m_ignore, m_ignore, m_cap, m_ignore }
53 };
54
55 INIT_MODULE(m_cap, "$Revision$")
56 {
57 mod_add_cmd(&cap_msgtab);
58 }
59
60 CLEANUP_MODULE
61 {
62 mod_del_cmd(&cap_msgtab);
63 }
64
65 #define CAPFL_HIDDEN 0x0001 /**< Do not advertize this capability */
66 #define CAPFL_PROHIBIT 0x0002 /**< Client may not set this capability */
67 #define CAPFL_PROTO 0x0004 /**< Cap must be acknowledged by client */
68 #define CAPFL_STICKY 0x0008 /**< Cap may not be cleared once set */
69
70 typedef int (*bqcmp)(const void *, const void *);
71
72 static struct capabilities
73 {
74 unsigned int cap;
75 unsigned int flags;
76 const char *name;
77 size_t namelen;
78 } capab_list[] = {
79 #define _CAP(cap, flags, name) \
80 { (cap), (flags), (name), sizeof(name) - 1 }
81 _CAP(CAP_MULTI_PREFIX, 0, "multi-prefix")
82 #undef _CAP
83 };
84
85 #define CAPAB_LIST_LEN (sizeof(capab_list) / sizeof(struct capabilities))
86
87 static int
88 capab_sort(const struct capabilities *cap1, const struct capabilities *cap2)
89 {
90 return strcasecmp(cap1->name, cap2->name);
91 }
92
93 static int
94 capab_search(const char *key, const struct capabilities *cap)
95 {
96 const char *rb = cap->name;
97
98 while (ToLower(*key) == ToLower(*rb)) /* walk equivalent part of strings */
99 if (*key++ == '\0') /* hit the end, all right... */
100 return 0;
101 else /* OK, let's move on... */
102 rb++;
103
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 {
123 /* First, let's sort the array... */
124 qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort);
125 inited = 1;
126 }
127
128 /* Next, find first non-whitespace character... */
129 while (*caplist && IsSpace(*caplist))
130 ++caplist;
131
132 /* We are now at the beginning of an element of the list; is it negative? */
133 if (*caplist == '-') {
134 ++caplist; /* yes; step past the flag... */
135 *neg_p = 1; /* remember that it is negative... */
136 }
137
138 /* OK, now see if we can look up the capability... */
139 if (*caplist) {
140 if (!(cap = bsearch(caplist, capab_list, CAPAB_LIST_LEN,
141 sizeof(struct capabilities),
142 (bqcmp)capab_search)))
143 {
144 /* Couldn't find the capability; advance to first whitespace character */
145 while (*caplist && !IsSpace(*caplist))
146 ++caplist;
147 }
148 else
149 caplist += cap->namelen; /* advance to end of capability name */
150 }
151
152 assert(caplist != *caplist_p || !*caplist); /* we *must* advance */
153
154 /* move ahead in capability list string--or zero pointer if we hit end */
155 *caplist_p = *caplist ? caplist : 0;
156
157 return cap; /* and return the capability (if any) */
158 }
159
160 /** Send a CAP \a subcmd list of capability changes to \a sptr.
161 * If more than one line is necessary, each line before the last has
162 * an added "*" parameter before that line's capability list.
163 * \param sptr Client receiving capability list.
164 * \param set Capabilities to show as set (with ack and sticky modifiers).
165 * \param rem Capabalities to show as removed (with no other modifier).
166 * \param subcmd Name of capability subcommand.
167 */
168 static int
169 send_caplist(struct Client *sptr, unsigned int set,
170 unsigned int rem, const char *subcmd)
171 {
172 char capbuf[IRCD_BUFSIZE] = "", pfx[16];
173 char cmdbuf[IRCD_BUFSIZE] = "";
174 int i, loc, len, flags, pfx_len, clen;
175
176 /* set up the buffer for the final LS message... */
177 clen = snprintf(cmdbuf, sizeof(cmdbuf), ":%s CAP %s %s ", me.name,
178 sptr->name[0] ? sptr->name : "*", subcmd);
179
180 for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i)
181 {
182 flags = capab_list[i].flags;
183
184 /*
185 * This is a little bit subtle, but just involves applying de
186 * Morgan's laws to the obvious check: We must display the
187 * capability if (and only if) it is set in \a rem or \a set, or
188 * if both are null and the capability is hidden.
189 */
190 if (!(rem && (rem & capab_list[i].cap)) &&
191 !(set && (set & capab_list[i].cap)) &&
192 (rem || set || (flags & CAPFL_HIDDEN)))
193 continue;
194
195 /* Build the prefix (space separator and any modifiers needed). */
196 pfx_len = 0;
197
198 if (loc)
199 pfx[pfx_len++] = ' ';
200 if (rem && (rem & capab_list[i].cap))
201 pfx[pfx_len++] = '-';
202 else
203 {
204 if (flags & CAPFL_PROTO)
205 pfx[pfx_len++] = '~';
206 if (flags & CAPFL_STICKY)
207 pfx[pfx_len++] = '=';
208 }
209
210 pfx[pfx_len] = '\0';
211
212 len = capab_list[i].namelen + pfx_len; /* how much we'd add... */
213
214 if (sizeof(capbuf) < (clen + loc + len + 15))
215 {
216 /* would add too much; must flush */
217 sendto_one(sptr, "%s* :%s", cmdbuf, capbuf);
218 capbuf[(loc = 0)] = '\0'; /* re-terminate the buffer... */
219 }
220
221 loc += snprintf(capbuf + loc, sizeof(capbuf) - loc,
222 "%s%s", pfx, capab_list[i].name);
223 }
224
225 sendto_one(sptr, "%s:%s", cmdbuf, capbuf);
226
227 return 0; /* convenience return */
228 }
229
230 static int
231 cap_ls(struct Client *sptr, const char *caplist)
232 {
233 if (IsUnknown(sptr))
234 /* registration hasn't completed; suspend it... */
235 sptr->localClient->registration |= REG_NEED_CAP;
236
237 return send_caplist(sptr, 0, 0, "LS"); /* send list of capabilities */
238 }
239
240 static int
241 cap_req(struct Client *sptr, const char *caplist)
242 {
243 const char *cl = caplist;
244 struct capabilities *cap;
245 unsigned int set = 0, rem = 0;
246 unsigned int cs = sptr->localClient->cap_client; /* capability set */
247 unsigned int as = sptr->localClient->cap_active; /* active set */
248 int neg;
249
250 if (IsUnknown(sptr)) /* registration hasn't completed; suspend it... */
251 sptr->localClient->registration |= REG_NEED_CAP;
252
253 while (cl)
254 {
255 /* walk through the capabilities list... */
256 if (!(cap = find_cap(&cl, &neg)) /* look up capability... */
257 || (!neg && (cap->flags & CAPFL_PROHIBIT)) /* is it prohibited? */
258 || (neg && (cap->flags & CAPFL_STICKY))) /* is it sticky? */
259 {
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, 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 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", 0 },
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