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

Contents of /ircd-hybrid/branches/8.2.x/modules/m_cap.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5019 - (show annotations)
Thu Dec 11 14:52:31 2014 UTC (5 years, 7 months ago) by michael
File MIME type: text/x-chdr
File size: 12624 byte(s)
- m_cap.c: fixed some issues which have been introduced when it was ported from ircu

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28