ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/modules/m_cap.c
Revision: 4565
Committed: Sun Aug 24 10:27:40 2014 UTC (11 years ago) by michael
Content type: text/x-csrc
File size: 12553 byte(s)
Log Message:
- Update GPL 2 license headers

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., 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 "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 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, unsigned int set,
145 unsigned int rem, const char *subcmd)
146 {
147 char capbuf[IRCD_BUFSIZE] = "", pfx[16];
148 char cmdbuf[IRCD_BUFSIZE] = "";
149 unsigned int i, loc, len, flags, pfx_len, clen;
150
151 /* Set up the buffer for the final LS message... */
152 clen = snprintf(cmdbuf, sizeof(capbuf), ":%s CAP %s %s ", me.name,
153 source_p->name[0] ? source_p->name : "*", subcmd);
154
155 for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i)
156 {
157 flags = capab_list[i].flags;
158
159 /*
160 * This is a little bit subtle, but just involves applying de
161 * Morgan's laws to the obvious check: We must display the
162 * capability if (and only if) it is set in \a rem or \a set, or
163 * if both are null and the capability is hidden.
164 */
165 if (!(rem && (rem & capab_list[i].cap)) &&
166 !(set && (set & capab_list[i].cap)) &&
167 (rem || set || (flags & CAPFL_HIDDEN)))
168 continue;
169
170 /* Build the prefix (space separator and any modifiers needed). */
171 pfx_len = 0;
172
173 if (loc)
174 pfx[pfx_len++] = ' ';
175 if (rem && (rem & capab_list[i].cap))
176 pfx[pfx_len++] = '-';
177 else
178 {
179 if (flags & CAPFL_PROTO)
180 pfx[pfx_len++] = '~';
181 if (flags & CAPFL_STICKY)
182 pfx[pfx_len++] = '=';
183 }
184
185 pfx[pfx_len] = '\0';
186
187 len = capab_list[i].namelen + pfx_len; /* How much we'd add... */
188
189 if (sizeof(capbuf) < (clen + loc + len + 15))
190 {
191 /* Would add too much; must flush */
192 sendto_one(source_p, "%s* :%s", cmdbuf, capbuf);
193 capbuf[(loc = 0)] = '\0'; /* Re-terminate the buffer... */
194 }
195
196 loc += snprintf(capbuf + loc, sizeof(capbuf) - loc,
197 "%s%s", pfx, capab_list[i].name);
198 }
199
200 sendto_one(source_p, "%s:%s", cmdbuf, capbuf);
201
202 return 0; /* Convenience return */
203 }
204
205 static int
206 cap_ls(struct Client *source_p, const char *caplist)
207 {
208 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
209 source_p->localClient->registration |= REG_NEED_CAP;
210
211 return send_caplist(source_p, 0, 0, "LS"); /* Send list of capabilities */
212 }
213
214 static int
215 cap_req(struct Client *source_p, const char *caplist)
216 {
217 const char *cl = caplist;
218 struct capabilities *cap = NULL;
219 unsigned int set = 0, rem = 0;
220 unsigned int cs = source_p->localClient->cap_client; /* capability set */
221 unsigned int as = source_p->localClient->cap_active; /* active set */
222 int neg = 0;
223
224 if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */
225 source_p->localClient->registration |= REG_NEED_CAP;
226
227 while (cl) { /* Walk through the capabilities list... */
228 if (!(cap = find_cap(&cl, &neg)) /* Look up capability... */
229 || (!neg && (cap->flags & CAPFL_PROHIBIT)) /* Is it prohibited? */
230 || (neg && (cap->flags & CAPFL_STICKY))) { /* Is it sticky? */
231 sendto_one(source_p, ":%s CAP %s NAK :%s", me.name,
232 source_p->name[0] ? source_p->name : "*", caplist);
233 return 0; /* Can't complete requested op... */
234 }
235
236 if (neg)
237 {
238 /* Set or clear the capability... */
239 rem |= cap->cap;
240 set &= ~cap->cap;
241 cs &= ~cap->cap;
242
243 if (!(cap->flags & CAPFL_PROTO))
244 as &= ~cap->cap;
245 }
246 else
247 {
248 rem &= ~cap->cap;
249 set |= cap->cap;
250 cs |= cap->cap;
251
252 if (!(cap->flags & CAPFL_PROTO))
253 as |= cap->cap;
254 }
255 }
256
257 /* Notify client of accepted changes and copy over results. */
258 send_caplist(source_p, set, rem, "ACK");
259
260 source_p->localClient->cap_client = cs;
261 source_p->localClient->cap_active = as;
262
263 return 0;
264 }
265
266 static int
267 cap_ack(struct Client *source_p, const char *caplist)
268 {
269 const char *cl = caplist;
270 struct capabilities *cap = NULL;
271 int neg = 0;
272
273 /*
274 * Coming from the client, this generally indicates that the client
275 * is using a new backwards-incompatible protocol feature. As such,
276 * it does not require further response from the server.
277 */
278 while (cl)
279 {
280 /* Walk through the capabilities list... */
281 if (!(cap = find_cap(&cl, &neg)) || /* Look up capability... */
282 (neg ? (source_p->localClient->cap_active & cap->cap) :
283 !(source_p->localClient->cap_active & cap->cap))) /* uh... */
284 continue;
285
286 if (neg) /* Set or clear the active capability... */
287 source_p->localClient->cap_active &= ~cap->cap;
288 else
289 source_p->localClient->cap_active |= cap->cap;
290 }
291
292 return 0;
293 }
294
295 static int
296 cap_clear(struct Client *source_p, const char *caplist)
297 {
298 struct capabilities *cap = NULL;
299 unsigned int cleared = 0;
300
301 for (unsigned int ii = 0; ii < CAPAB_LIST_LEN; ++ii)
302 {
303 cap = &capab_list[ii];
304
305 /* Only clear active non-sticky capabilities. */
306 if (!(source_p->localClient->cap_active & cap->cap) || (cap->flags & CAPFL_STICKY))
307 continue;
308
309 cleared |= cap->cap;
310 source_p->localClient->cap_client &= ~cap->cap;
311
312 if (!(cap->flags & CAPFL_PROTO))
313 source_p->localClient->cap_active &= ~cap->cap;
314 }
315
316 return send_caplist(source_p, 0, cleared, "ACK");
317 }
318
319 static int
320 cap_end(struct Client *source_p, const char *caplist)
321 {
322 if (!IsUnknown(source_p)) /* Registration has completed... */
323 return 0; /* So just ignore the message... */
324
325 /* Capability negotiation is now done... */
326 source_p->localClient->registration &= ~REG_NEED_CAP;
327
328 /* If client is now done... */
329 if (!source_p->localClient->registration)
330 {
331 register_local_user(source_p);
332 return 0;
333 }
334
335 return 0; /* Can't do registration yet... */
336 }
337
338 static int
339 cap_list(struct Client *source_p, const char *caplist)
340 {
341 /* Send the list of the client's capabilities */
342 return send_caplist(source_p, source_p->localClient->cap_client, 0, "LIST");
343 }
344
345 static struct subcmd
346 {
347 const char *cmd;
348 int (*proc)(struct Client *, const char *);
349 } cmdlist[] = {
350 { "ACK", cap_ack },
351 { "CLEAR", cap_clear },
352 { "END", cap_end },
353 { "LIST", cap_list },
354 { "LS", cap_ls },
355 { "NAK", NULL },
356 { "REQ", cap_req }
357 };
358
359 static int
360 subcmd_search(const char *cmd, const struct subcmd *elem)
361 {
362 return strcasecmp(cmd, elem->cmd);
363 }
364
365 /*! \brief CAP command handler
366 *
367 * \param source_p Pointer to allocated Client struct from which the message
368 * originally comes from. This can be a local or remote client.
369 * \param parc Integer holding the number of supplied arguments.
370 * \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL
371 * pointers.
372 * \note Valid arguments for this command are:
373 * - parv[0] = command
374 * - parv[1] = CAP subcommand
375 * - parv[2] = space-separated list of capabilities
376 */
377 static int
378 m_cap(struct Client *source_p, int parc, char *parv[])
379 {
380 const char *subcmd = NULL, *caplist = NULL;
381 struct subcmd *cmd = NULL;
382
383 if (EmptyString(parv[1])) /* A subcommand is required */
384 return 0;
385
386 subcmd = parv[1];
387
388 if (parc > 2) /* A capability list was provided */
389 caplist = parv[2];
390
391 /* Find the subcommand handler */
392 if (!(cmd = bsearch(subcmd, cmdlist,
393 sizeof(cmdlist) / sizeof(struct subcmd),
394 sizeof(struct subcmd), (bqcmp)subcmd_search)))
395 {
396 sendto_one_numeric(source_p, &me, ERR_INVALIDCAPCMD, subcmd);
397 return 0;
398 }
399
400 /* Then execute it... */
401 if (cmd->proc)
402 (cmd->proc)(source_p, caplist);
403 return 0;
404 }
405
406 static struct Message cap_msgtab =
407 {
408 "CAP", NULL, 0, 0, 2, MAXPARA, MFLG_SLOW, 0,
409 { m_cap, m_cap, m_ignore, m_ignore, m_cap, m_ignore }
410 };
411
412 static void
413 module_init(void)
414 {
415 /* First, let's sort the array */
416 qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort);
417 mod_add_cmd(&cap_msgtab);
418 }
419
420 static void
421 module_exit(void)
422 {
423 mod_del_cmd(&cap_msgtab);
424 }
425
426 struct module module_entry =
427 {
428 .node = { NULL, NULL, NULL },
429 .name = NULL,
430 .version = "$Revision$",
431 .handle = NULL,
432 .modinit = module_init,
433 .modexit = module_exit,
434 .flags = 0
435 };

Properties

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