ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 6767
Committed: Fri Nov 13 18:46:50 2015 UTC (10 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 12982 byte(s)
Log Message:
- Implement ircv3.2 invite-notify client capability support

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

Properties

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