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