ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 3347
Committed: Sun Apr 20 14:03:06 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 12525 byte(s)
Log Message:
- Moved files:
  s_user.c -> user.c
  s_misc.c -> misc.c
  s_serv.c -> server.c

File Contents

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

Properties

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