ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/branches/ircd-hybrid-newconf/modules/m_cap.c
Revision: 1027
Committed: Sun Nov 8 13:01:13 2009 UTC (16 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 11910 byte(s)
Log Message:
- Move old 7.3 sources to branches/ircd-hybrid-newconf

File Contents

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

Properties

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