ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/modules/m_cap.c
Revision: 1230
Committed: Thu Sep 22 19:41:19 2011 UTC (14 years, 10 months ago) by michael
Content type: text/x-csrc
File size: 12275 byte(s)
Log Message:
- cleanup module loader. Make module api more flexible

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

Properties

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