ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/modules/m_cap.c
Revision: 590
Committed: Thu May 11 11:35:07 2006 UTC (17 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 11932 byte(s)
Log Message:
- Fixed compile warnings in send_caplist()

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

Properties

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