1 |
adx |
30 |
/* |
2 |
|
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
|
|
* parse.c: The message parser. |
4 |
|
|
* |
5 |
|
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
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 |
knight |
31 |
* $Id$ |
23 |
adx |
30 |
*/ |
24 |
|
|
|
25 |
|
|
#include "stdinc.h" |
26 |
adx |
743 |
#include "conf/conf.h" |
27 |
adx |
30 |
#include "parse.h" |
28 |
|
|
#include "client.h" |
29 |
|
|
#include "channel.h" |
30 |
|
|
#include "handlers.h" |
31 |
|
|
#include "common.h" |
32 |
|
|
#include "hash.h" |
33 |
|
|
#include "ircd.h" |
34 |
|
|
#include "numeric.h" |
35 |
|
|
#include "send.h" |
36 |
|
|
#include "ircd_handler.h" |
37 |
|
|
#include "msg.h" |
38 |
adx |
801 |
#include "user.h" |
39 |
|
|
#include "server.h" |
40 |
adx |
30 |
|
41 |
|
|
/* |
42 |
|
|
* (based on orabidoo's parser code) |
43 |
|
|
* |
44 |
|
|
* This has always just been a trie. Look at volume III of Knuth ACP |
45 |
|
|
* |
46 |
|
|
* |
47 |
|
|
* ok, you start out with an array of pointers, each one corresponds |
48 |
|
|
* to a letter at the current position in the command being examined. |
49 |
|
|
* |
50 |
|
|
* so roughly you have this for matching 'trie' or 'tie' |
51 |
|
|
* |
52 |
|
|
* 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i' |
53 |
|
|
* -> [MessageTree *] -> [MessageTree *] -> 'e' and matches |
54 |
|
|
* |
55 |
michael |
534 |
* 'i' -> [MessageTree *] -> 'e' and matches |
56 |
adx |
30 |
* |
57 |
|
|
* BUGS (Limitations!) |
58 |
|
|
* |
59 |
|
|
* I designed this trie to parse ircd commands. Hence it currently |
60 |
|
|
* casefolds. This is trivial to fix by increasing MAXPTRLEN. |
61 |
|
|
* This trie also "folds" '{' etc. down. This means, the input to this |
62 |
|
|
* trie must be alpha tokens only. This again, is a limitation that |
63 |
|
|
* can be overcome by increasing MAXPTRLEN to include upper/lower case |
64 |
|
|
* at the expense of more memory. At the extreme end, you could make |
65 |
|
|
* MAXPTRLEN 128. |
66 |
|
|
* |
67 |
|
|
* This is also not a patricia trie. On short ircd tokens, this is |
68 |
|
|
* not likely going to matter. |
69 |
|
|
* |
70 |
|
|
* Diane Bruce (Dianora), June 6 2003 |
71 |
|
|
*/ |
72 |
|
|
|
73 |
michael |
534 |
#define MAXPTRLEN 32 |
74 |
adx |
743 |
/* Must be a power of 2, and |
75 |
adx |
30 |
* larger than 26 [a-z]|[A-Z] |
76 |
|
|
* its used to allocate the set |
77 |
|
|
* of pointers at each node of the tree |
78 |
|
|
* There are MAXPTRLEN pointers at each node. |
79 |
|
|
* Obviously, there have to be more pointers |
80 |
|
|
* Than ASCII letters. 32 is a nice number |
81 |
|
|
* since there is then no need to shift |
82 |
|
|
* 'A'/'a' to base 0 index, at the expense |
83 |
|
|
* of a few never used pointers. For a small |
84 |
|
|
* parser like this, this is a good compromise |
85 |
|
|
* and does make it somewhat faster. |
86 |
|
|
* |
87 |
|
|
* - Dianora |
88 |
|
|
*/ |
89 |
|
|
|
90 |
|
|
struct MessageTree |
91 |
|
|
{ |
92 |
|
|
int links; /* Count of all pointers (including msg) at this node |
93 |
michael |
534 |
* used as reference count for deletion of _this_ node. |
94 |
|
|
*/ |
95 |
adx |
30 |
struct Message *msg; |
96 |
|
|
struct MessageTree *pointers[MAXPTRLEN]; |
97 |
|
|
}; |
98 |
|
|
|
99 |
|
|
static struct MessageTree msg_tree; |
100 |
|
|
|
101 |
|
|
/* |
102 |
|
|
* NOTE: parse() should not be called recursively by other functions! |
103 |
|
|
*/ |
104 |
|
|
static char *sender; |
105 |
michael |
534 |
static char *para[IRCD_MAXPARA + 1]; |
106 |
adx |
30 |
static char buffer[1024]; |
107 |
|
|
|
108 |
|
|
static int cancel_clients(struct Client *, struct Client *, char *); |
109 |
|
|
static void remove_unknown(struct Client *, char *, char *); |
110 |
|
|
static void do_numeric(char[], struct Client *, struct Client *, int, char **); |
111 |
|
|
static void handle_command(struct Message *, struct Client *, struct Client *, unsigned int, char **); |
112 |
|
|
static void recurse_report_messages(struct Client *source_p, struct MessageTree *mtree); |
113 |
|
|
static void add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, const char *cmd); |
114 |
|
|
static void del_msg_element(struct MessageTree *mtree_p, const char *cmd); |
115 |
|
|
|
116 |
adx |
743 |
// turn a string into a parc/parv pair |
117 |
adx |
30 |
static inline int |
118 |
michael |
534 |
string_to_array(char *string, char *parv[]) |
119 |
adx |
30 |
{ |
120 |
|
|
char *p; |
121 |
|
|
char *buf = string; |
122 |
|
|
int x = 1; |
123 |
|
|
|
124 |
|
|
parv[x] = NULL; |
125 |
|
|
|
126 |
adx |
743 |
while (*buf == ' ') // skip leading spaces |
127 |
adx |
30 |
buf++; |
128 |
|
|
|
129 |
adx |
743 |
if (*buf == '\0') // ignore all-space args |
130 |
adx |
30 |
return(x); |
131 |
|
|
|
132 |
|
|
do |
133 |
|
|
{ |
134 |
adx |
743 |
if (*buf == ':') // last parameter |
135 |
adx |
30 |
{ |
136 |
|
|
buf++; |
137 |
|
|
parv[x++] = buf; |
138 |
|
|
parv[x] = NULL; |
139 |
|
|
return(x); |
140 |
|
|
} |
141 |
|
|
else |
142 |
|
|
{ |
143 |
|
|
parv[x++] = buf; |
144 |
|
|
parv[x] = NULL; |
145 |
|
|
|
146 |
|
|
if ((p = strchr(buf, ' ')) != NULL) |
147 |
|
|
{ |
148 |
|
|
*p++ = '\0'; |
149 |
|
|
buf = p; |
150 |
|
|
} |
151 |
|
|
else |
152 |
|
|
return(x); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
while (*buf == ' ') |
156 |
|
|
buf++; |
157 |
|
|
|
158 |
|
|
if (*buf == '\0') |
159 |
|
|
return(x); |
160 |
michael |
534 |
} while (x < IRCD_MAXPARA - 1); |
161 |
adx |
30 |
|
162 |
|
|
if (*p == ':') |
163 |
|
|
p++; |
164 |
|
|
|
165 |
|
|
parv[x++] = p; |
166 |
|
|
parv[x] = NULL; |
167 |
|
|
return(x); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
/* |
171 |
|
|
* parse a buffer. |
172 |
|
|
* |
173 |
|
|
* NOTE: parse() should not be called recusively by any other functions! |
174 |
|
|
*/ |
175 |
|
|
void |
176 |
|
|
parse(struct Client *client_p, char *pbuffer, char *bufend) |
177 |
|
|
{ |
178 |
|
|
struct Client *from = client_p; |
179 |
|
|
char *ch; |
180 |
|
|
char *s; |
181 |
|
|
char *numeric = 0; |
182 |
|
|
unsigned int i = 0; |
183 |
|
|
int paramcount; |
184 |
|
|
int mpara = 0; |
185 |
|
|
struct Message *mptr = NULL; |
186 |
|
|
|
187 |
|
|
if (IsDefunct(client_p)) |
188 |
|
|
return; |
189 |
|
|
|
190 |
|
|
assert(client_p->localClient->fd.flags.open); |
191 |
|
|
assert((bufend - pbuffer) < 512); |
192 |
|
|
|
193 |
adx |
743 |
for (ch = pbuffer; *ch == ' '; ch++) // skip spaces |
194 |
|
|
; // null statement |
195 |
adx |
30 |
|
196 |
|
|
para[0] = from->name; |
197 |
|
|
|
198 |
|
|
if (*ch == ':') |
199 |
|
|
{ |
200 |
|
|
ch++; |
201 |
|
|
|
202 |
|
|
/* Copy the prefix to 'sender' assuming it terminates |
203 |
|
|
* with SPACE (or NULL, which is an error, though). |
204 |
|
|
*/ |
205 |
|
|
sender = ch; |
206 |
|
|
|
207 |
|
|
if ((s = strchr(ch, ' ')) != NULL) |
208 |
|
|
{ |
209 |
|
|
*s = '\0'; |
210 |
|
|
s++; |
211 |
|
|
ch = s; |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
if (*sender && IsServer(client_p)) |
215 |
|
|
{ |
216 |
|
|
/* |
217 |
|
|
* XXX it could be useful to know which of these occurs most frequently. |
218 |
|
|
* the ID check should always come first, though, since it is so easy. |
219 |
|
|
*/ |
220 |
|
|
if ((from = find_person(client_p, sender)) == NULL) |
221 |
|
|
{ |
222 |
|
|
from = find_server(sender); |
223 |
|
|
|
224 |
michael |
534 |
if (from == NULL && IsCapable(client_p, CAP_TS6) && |
225 |
|
|
client_p->name[0] == '*' && IsDigit(*sender) && strlen(sender) == 3) |
226 |
|
|
{ |
227 |
adx |
30 |
/* Dirty hack to allow messages from masked SIDs (i.e. the ones |
228 |
|
|
* hidden by fakename="..."). It shouldn't break anything, since |
229 |
|
|
* unknown SIDs don't happen during normal ircd work --adx |
230 |
|
|
*/ |
231 |
michael |
534 |
from = client_p; |
232 |
|
|
} |
233 |
adx |
30 |
} |
234 |
|
|
|
235 |
michael |
534 |
/* |
236 |
|
|
* Hmm! If the client corresponding to the |
237 |
adx |
30 |
* prefix is not found--what is the correct |
238 |
|
|
* action??? Now, I will ignore the message |
239 |
|
|
* (old IRC just let it through as if the |
240 |
|
|
* prefix just wasn't there...) --msa |
241 |
|
|
*/ |
242 |
|
|
if (from == NULL) |
243 |
|
|
{ |
244 |
michael |
515 |
++ServerStats.is_unpf; |
245 |
adx |
30 |
remove_unknown(client_p, sender, pbuffer); |
246 |
|
|
return; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
para[0] = from->name; |
250 |
|
|
|
251 |
|
|
if (from->from != client_p) |
252 |
|
|
{ |
253 |
michael |
515 |
++ServerStats.is_wrdi; |
254 |
adx |
30 |
cancel_clients(client_p, from, pbuffer); |
255 |
|
|
return; |
256 |
|
|
} |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
while (*ch == ' ') |
260 |
michael |
515 |
++ch; |
261 |
adx |
30 |
} |
262 |
|
|
|
263 |
|
|
if (*ch == '\0') |
264 |
|
|
{ |
265 |
michael |
515 |
++ServerStats.is_empt; |
266 |
adx |
30 |
return; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
/* Extract the command code from the packet. Point s to the end |
270 |
|
|
* of the command code and calculate the length using pointer |
271 |
|
|
* arithmetic. Note: only need length for numerics and *all* |
272 |
|
|
* numerics must have parameters and thus a space after the command |
273 |
|
|
* code. -avalon |
274 |
|
|
*/ |
275 |
|
|
|
276 |
adx |
743 |
// EOB is 3 chars long but is not a numeric |
277 |
|
|
if (*(ch + 3) == ' ' && // ok, lets see if its a possible numeric.. |
278 |
adx |
30 |
IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2))) |
279 |
|
|
{ |
280 |
|
|
mptr = NULL; |
281 |
|
|
numeric = ch; |
282 |
michael |
534 |
paramcount = IRCD_MAXPARA; |
283 |
michael |
515 |
++ServerStats.is_num; |
284 |
adx |
743 |
s = ch + 3; // I know this is ' ' from above if |
285 |
|
|
*s++ = '\0'; // blow away the ' ', and point s to next part |
286 |
adx |
30 |
} |
287 |
|
|
else |
288 |
|
|
{ |
289 |
|
|
int ii = 0; |
290 |
|
|
|
291 |
|
|
if ((s = strchr(ch, ' ')) != NULL) |
292 |
|
|
*s++ = '\0'; |
293 |
|
|
|
294 |
|
|
if ((mptr = find_command(ch)) == NULL) |
295 |
|
|
{ |
296 |
|
|
/* Note: Give error message *only* to recognized |
297 |
|
|
* persons. It's a nightmare situation to have |
298 |
|
|
* two programs sending "Unknown command"'s or |
299 |
|
|
* equivalent to each other at full blast.... |
300 |
|
|
* If it has got to person state, it at least |
301 |
|
|
* seems to be well behaving. Perhaps this message |
302 |
|
|
* should never be generated, though... --msa |
303 |
|
|
* Hm, when is the buffer empty -- if a command |
304 |
|
|
* code has been found ?? -Armin |
305 |
|
|
*/ |
306 |
|
|
if (pbuffer[0] != '\0') |
307 |
|
|
{ |
308 |
|
|
if (IsClient(from)) |
309 |
|
|
sendto_one(from, form_str(ERR_UNKNOWNCOMMAND), |
310 |
|
|
me.name, from->name, ch); |
311 |
|
|
} |
312 |
|
|
|
313 |
michael |
515 |
++ServerStats.is_unco; |
314 |
adx |
30 |
return; |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
assert(mptr->cmd != NULL); |
318 |
|
|
|
319 |
|
|
paramcount = mptr->parameters; |
320 |
|
|
mpara = mptr->maxpara; |
321 |
|
|
|
322 |
|
|
ii = bufend - ((s) ? s : ch); |
323 |
|
|
mptr->bytes += ii; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
if (s != NULL) |
327 |
|
|
i = string_to_array(s, para); |
328 |
|
|
else |
329 |
|
|
{ |
330 |
|
|
i = 0; |
331 |
|
|
para[1] = NULL; |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
if (mptr == NULL) |
335 |
|
|
do_numeric(numeric, client_p, from, i, para); |
336 |
|
|
else |
337 |
|
|
handle_command(mptr, client_p, from, i, para); |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
/* handle_command() |
341 |
|
|
* |
342 |
adx |
743 |
* inputs - pointer to message block |
343 |
|
|
* - pointer to client |
344 |
|
|
* - pointer to client message is from |
345 |
|
|
* - count of number of args |
346 |
|
|
* - pointer to argv[] array |
347 |
|
|
* output - -1 if error from server |
348 |
|
|
* side effects - |
349 |
adx |
30 |
*/ |
350 |
|
|
static void |
351 |
|
|
handle_command(struct Message *mptr, struct Client *client_p, |
352 |
michael |
534 |
struct Client *from, unsigned int i, char *hpara[]) |
353 |
adx |
30 |
{ |
354 |
|
|
MessageHandler handler = 0; |
355 |
|
|
|
356 |
|
|
if (IsServer(client_p)) |
357 |
|
|
mptr->rcount++; |
358 |
|
|
|
359 |
|
|
mptr->count++; |
360 |
|
|
|
361 |
|
|
/* New patch to avoid server flooding from unregistered connects |
362 |
|
|
* - Pie-Man 07/27/2000 */ |
363 |
|
|
if (!IsRegistered(client_p)) |
364 |
|
|
{ |
365 |
|
|
/* if its from a possible server connection |
366 |
|
|
* ignore it.. more than likely its a header thats sneaked through |
367 |
|
|
*/ |
368 |
|
|
if ((IsHandshake(client_p) || IsConnecting(client_p) || |
369 |
|
|
IsServer(client_p)) && !(mptr->flags & MFLG_UNREG)) |
370 |
|
|
return; |
371 |
|
|
} |
372 |
|
|
|
373 |
|
|
handler = mptr->handlers[client_p->handler]; |
374 |
|
|
|
375 |
adx |
743 |
// check right amount of params is passed... --is |
376 |
adx |
30 |
if (i < mptr->parameters) |
377 |
|
|
{ |
378 |
|
|
if (!IsServer(client_p)) |
379 |
|
|
{ |
380 |
|
|
sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS), |
381 |
|
|
me.name, EmptyString(hpara[0]) ? "*" : hpara[0], mptr->cmd); |
382 |
|
|
} |
383 |
|
|
else |
384 |
|
|
{ |
385 |
|
|
sendto_realops_flags(UMODE_ALL, L_ALL, |
386 |
|
|
"Dropping server %s due to (invalid) command '%s' " |
387 |
|
|
"with only %d arguments (expecting %d).", |
388 |
|
|
client_p->name, mptr->cmd, i, mptr->parameters); |
389 |
|
|
ilog(L_CRIT, "Insufficient parameters (%d) for command '%s' from %s.", |
390 |
|
|
i, mptr->cmd, client_p->name); |
391 |
|
|
exit_client(client_p, client_p, |
392 |
|
|
"Not enough arguments to server command."); |
393 |
|
|
} |
394 |
|
|
} |
395 |
|
|
else |
396 |
|
|
(*handler)(client_p, from, i, hpara); |
397 |
|
|
} |
398 |
|
|
|
399 |
|
|
/* clear_tree_parse() |
400 |
|
|
* |
401 |
|
|
* inputs - NONE |
402 |
|
|
* output - NONE |
403 |
|
|
* side effects - MUST MUST be called at startup ONCE before |
404 |
|
|
* any other keyword routine is used. |
405 |
|
|
*/ |
406 |
|
|
void |
407 |
|
|
clear_tree_parse(void) |
408 |
|
|
{ |
409 |
|
|
memset(&msg_tree, 0, sizeof(msg_tree)); |
410 |
|
|
} |
411 |
|
|
|
412 |
|
|
/* add_msg_element() |
413 |
|
|
* |
414 |
|
|
* inputs - pointer to MessageTree |
415 |
|
|
* - pointer to Message to add for given command |
416 |
|
|
* - pointer to current portion of command being added |
417 |
|
|
* output - NONE |
418 |
|
|
* side effects - recursively build the Message Tree ;-) |
419 |
|
|
*/ |
420 |
|
|
/* |
421 |
|
|
* How this works. |
422 |
|
|
* |
423 |
|
|
* The code first checks to see if its reached the end of the command |
424 |
|
|
* If so, that struct MessageTree has a msg pointer updated and the links |
425 |
|
|
* count incremented, since a msg pointer is a reference. |
426 |
|
|
* Then the code descends recursively, building the trie. |
427 |
|
|
* If a pointer index inside the struct MessageTree is NULL a new |
428 |
|
|
* child struct MessageTree has to be allocated. |
429 |
|
|
* The links (reference count) is incremented as they are created |
430 |
|
|
* in the parent. |
431 |
|
|
*/ |
432 |
|
|
static void |
433 |
|
|
add_msg_element(struct MessageTree *mtree_p, |
434 |
|
|
struct Message *msg_p, const char *cmd) |
435 |
|
|
{ |
436 |
|
|
struct MessageTree *ntree_p; |
437 |
|
|
|
438 |
|
|
if (*cmd == '\0') |
439 |
|
|
{ |
440 |
|
|
mtree_p->msg = msg_p; |
441 |
adx |
743 |
mtree_p->links++; // Have msg pointer, so up ref count |
442 |
adx |
30 |
} |
443 |
|
|
else |
444 |
|
|
{ |
445 |
|
|
/* *cmd & (MAXPTRLEN-1) |
446 |
|
|
* convert the char pointed to at *cmd from ASCII to an integer |
447 |
|
|
* between 0 and MAXPTRLEN. |
448 |
|
|
* Thus 'A' -> 0x1 'B' -> 0x2 'c' -> 0x3 etc. |
449 |
|
|
*/ |
450 |
|
|
|
451 |
|
|
if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) == NULL) |
452 |
|
|
{ |
453 |
|
|
ntree_p = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree)); |
454 |
|
|
mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = ntree_p; |
455 |
|
|
|
456 |
adx |
743 |
mtree_p->links++; // Have new pointer, so up ref count |
457 |
adx |
30 |
} |
458 |
michael |
534 |
|
459 |
adx |
30 |
add_msg_element(ntree_p, msg_p, cmd+1); |
460 |
|
|
} |
461 |
|
|
} |
462 |
|
|
|
463 |
|
|
/* del_msg_element() |
464 |
|
|
* |
465 |
|
|
* inputs - Pointer to MessageTree to delete from |
466 |
|
|
* - pointer to command name to delete |
467 |
|
|
* output - NONE |
468 |
|
|
* side effects - recursively deletes a token from the Message Tree ;-) |
469 |
|
|
*/ |
470 |
|
|
/* |
471 |
|
|
* How this works. |
472 |
|
|
* |
473 |
|
|
* Well, first off, the code recursively descends into the trie |
474 |
|
|
* until it finds the terminating letter of the command being removed. |
475 |
|
|
* Once it has done that, it marks the msg pointer as NULL then |
476 |
|
|
* reduces the reference count on that allocated struct MessageTree |
477 |
|
|
* since a command counts as a reference. |
478 |
|
|
* |
479 |
|
|
* Then it pops up the recurse stack. As it comes back up the recurse |
480 |
|
|
* The code checks to see if the child now has no pointers or msg |
481 |
|
|
* i.e. the links count has gone to zero. If its no longer used, the |
482 |
|
|
* child struct MessageTree can be deleted. The parent reference |
483 |
|
|
* to this child is then removed and the parents link count goes down. |
484 |
|
|
* Thus, we continue to go back up removing all unused MessageTree(s) |
485 |
|
|
*/ |
486 |
|
|
static void |
487 |
|
|
del_msg_element(struct MessageTree *mtree_p, const char *cmd) |
488 |
|
|
{ |
489 |
|
|
struct MessageTree *ntree_p; |
490 |
|
|
|
491 |
michael |
534 |
/* |
492 |
|
|
* In case this is called for a nonexistent command |
493 |
adx |
30 |
* check that there is a msg pointer here, else links-- goes -ve |
494 |
|
|
* -db |
495 |
|
|
*/ |
496 |
|
|
if ((*cmd == '\0') && (mtree_p->msg != NULL)) |
497 |
|
|
{ |
498 |
|
|
mtree_p->msg = NULL; |
499 |
|
|
mtree_p->links--; |
500 |
|
|
} |
501 |
|
|
else |
502 |
|
|
{ |
503 |
|
|
if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL) |
504 |
|
|
{ |
505 |
|
|
del_msg_element(ntree_p, cmd+1); |
506 |
michael |
534 |
|
507 |
adx |
30 |
if (ntree_p->links == 0) |
508 |
|
|
{ |
509 |
michael |
534 |
mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = NULL; |
510 |
|
|
mtree_p->links--; |
511 |
|
|
MyFree(ntree_p); |
512 |
adx |
30 |
} |
513 |
|
|
} |
514 |
|
|
} |
515 |
|
|
} |
516 |
|
|
|
517 |
|
|
/* msg_tree_parse() |
518 |
|
|
* |
519 |
adx |
743 |
* inputs - Pointer to command to find |
520 |
|
|
* - Pointer to MessageTree root |
521 |
|
|
* output - Find given command returning Message * if found NULL if not |
522 |
|
|
* side effects - none |
523 |
adx |
30 |
*/ |
524 |
|
|
static struct Message * |
525 |
|
|
msg_tree_parse(const char *cmd, struct MessageTree *root) |
526 |
|
|
{ |
527 |
michael |
413 |
struct MessageTree *mtree = root; |
528 |
adx |
30 |
assert(cmd && *cmd); |
529 |
|
|
|
530 |
michael |
413 |
while (IsAlpha(*cmd) && (mtree = mtree->pointers[*cmd & (MAXPTRLEN - 1)])) |
531 |
|
|
if (*++cmd == '\0') |
532 |
|
|
return mtree->msg; |
533 |
adx |
30 |
|
534 |
michael |
413 |
return NULL; |
535 |
adx |
30 |
} |
536 |
|
|
|
537 |
|
|
/* mod_add_cmd() |
538 |
|
|
* |
539 |
|
|
* inputs - pointer to struct Message |
540 |
|
|
* output - none |
541 |
|
|
* side effects - load this one command name |
542 |
|
|
* msg->count msg->bytes is modified in place, in |
543 |
|
|
* modules address space. Might not want to do that... |
544 |
|
|
*/ |
545 |
|
|
void |
546 |
|
|
mod_add_cmd(struct Message *msg) |
547 |
|
|
{ |
548 |
michael |
413 |
struct Message *found_msg = NULL; |
549 |
adx |
30 |
|
550 |
|
|
if (msg == NULL) |
551 |
|
|
return; |
552 |
|
|
|
553 |
adx |
743 |
// someone loaded a module with a bad messagetab |
554 |
adx |
30 |
assert(msg->cmd != NULL); |
555 |
|
|
|
556 |
adx |
743 |
// command already added? |
557 |
adx |
30 |
if ((found_msg = msg_tree_parse(msg->cmd, &msg_tree)) != NULL) |
558 |
|
|
return; |
559 |
|
|
|
560 |
|
|
add_msg_element(&msg_tree, msg, msg->cmd); |
561 |
|
|
msg->count = msg->rcount = msg->bytes = 0; |
562 |
|
|
} |
563 |
|
|
|
564 |
|
|
/* mod_del_cmd() |
565 |
|
|
* |
566 |
adx |
743 |
* inputs - pointer to struct Message |
567 |
|
|
* output - none |
568 |
adx |
30 |
* side effects - unload this one command name |
569 |
|
|
*/ |
570 |
|
|
void |
571 |
|
|
mod_del_cmd(struct Message *msg) |
572 |
|
|
{ |
573 |
|
|
assert(msg != NULL); |
574 |
|
|
|
575 |
|
|
if (msg == NULL) |
576 |
|
|
return; |
577 |
|
|
|
578 |
|
|
del_msg_element(&msg_tree, msg->cmd); |
579 |
|
|
} |
580 |
|
|
|
581 |
|
|
/* find_command() |
582 |
|
|
* |
583 |
adx |
743 |
* inputs - command name |
584 |
|
|
* output - pointer to struct Message |
585 |
adx |
30 |
* side effects - none |
586 |
|
|
*/ |
587 |
|
|
struct Message * |
588 |
|
|
find_command(const char *cmd) |
589 |
|
|
{ |
590 |
michael |
413 |
return msg_tree_parse(cmd, &msg_tree); |
591 |
adx |
30 |
} |
592 |
|
|
|
593 |
|
|
/* report_messages() |
594 |
|
|
* |
595 |
adx |
743 |
* inputs - pointer to client to report to |
596 |
|
|
* output - NONE |
597 |
|
|
* side effects - client is shown list of commands |
598 |
adx |
30 |
*/ |
599 |
|
|
void |
600 |
|
|
report_messages(struct Client *source_p) |
601 |
|
|
{ |
602 |
|
|
struct MessageTree *mtree = &msg_tree; |
603 |
|
|
int i; |
604 |
|
|
|
605 |
|
|
for (i = 0; i < MAXPTRLEN; i++) |
606 |
|
|
{ |
607 |
|
|
if (mtree->pointers[i] != NULL) |
608 |
|
|
recurse_report_messages(source_p, mtree->pointers[i]); |
609 |
|
|
} |
610 |
|
|
} |
611 |
|
|
|
612 |
|
|
static void |
613 |
|
|
recurse_report_messages(struct Client *source_p, struct MessageTree *mtree) |
614 |
|
|
{ |
615 |
|
|
int i; |
616 |
|
|
|
617 |
|
|
if (mtree->msg != NULL) |
618 |
|
|
{ |
619 |
|
|
sendto_one(source_p, form_str(RPL_STATSCOMMANDS), |
620 |
|
|
me.name, source_p->name, mtree->msg->cmd, |
621 |
|
|
mtree->msg->count, mtree->msg->bytes, |
622 |
|
|
mtree->msg->rcount); |
623 |
|
|
} |
624 |
|
|
|
625 |
|
|
for (i = 0; i < MAXPTRLEN; i++) |
626 |
|
|
{ |
627 |
|
|
if (mtree->pointers[i] != NULL) |
628 |
|
|
recurse_report_messages(source_p, mtree->pointers[i]); |
629 |
|
|
} |
630 |
|
|
} |
631 |
|
|
|
632 |
|
|
/* cancel_clients() |
633 |
|
|
* |
634 |
adx |
743 |
* inputs - |
635 |
|
|
* output - |
636 |
|
|
* side effects - |
637 |
adx |
30 |
*/ |
638 |
|
|
static int |
639 |
|
|
cancel_clients(struct Client *client_p, struct Client *source_p, char *cmd) |
640 |
|
|
{ |
641 |
|
|
/* kill all possible points that are causing confusion here, |
642 |
|
|
* I'm not sure I've got this all right... |
643 |
|
|
* - avalon |
644 |
|
|
* |
645 |
|
|
* knowing avalon, probably not. |
646 |
|
|
*/ |
647 |
|
|
|
648 |
|
|
/* with TS, fake prefixes are a common thing, during the |
649 |
|
|
* connect burst when there's a nick collision, and they |
650 |
|
|
* must be ignored rather than killed because one of the |
651 |
|
|
* two is surviving.. so we don't bother sending them to |
652 |
|
|
* all ops everytime, as this could send 'private' stuff |
653 |
|
|
* from lagged clients. we do send the ones that cause |
654 |
|
|
* servers to be dropped though, as well as the ones from |
655 |
|
|
* non-TS servers -orabidoo |
656 |
|
|
*/ |
657 |
|
|
/* Incorrect prefix for a server from some connection. If it is a |
658 |
|
|
* client trying to be annoying, just QUIT them, if it is a server |
659 |
|
|
* then the same deal. |
660 |
|
|
*/ |
661 |
|
|
if (IsServer(source_p) || IsMe(source_p)) |
662 |
|
|
{ |
663 |
|
|
sendto_realops_flags(UMODE_DEBUG, L_ADMIN, "Message for %s[%s] from %s", |
664 |
|
|
source_p->name, source_p->from->name, |
665 |
|
|
get_client_name(client_p, SHOW_IP)); |
666 |
|
|
sendto_realops_flags(UMODE_DEBUG, L_OPER, "Message for %s[%s] from %s", |
667 |
|
|
source_p->name, source_p->from->name, |
668 |
|
|
get_client_name(client_p, MASK_IP)); |
669 |
|
|
sendto_realops_flags(UMODE_DEBUG, L_ALL, |
670 |
|
|
"Not dropping server %s (%s) for Fake Direction", |
671 |
|
|
client_p->name, source_p->name); |
672 |
adx |
743 |
return -1; |
673 |
|
|
// return exit_client(client_p, client_p, &me, "Fake Direction"); |
674 |
adx |
30 |
} |
675 |
|
|
|
676 |
|
|
/* Ok, someone is trying to impose as a client and things are |
677 |
|
|
* confused. If we got the wrong prefix from a server, send out a |
678 |
|
|
* kill, else just exit the lame client. |
679 |
|
|
*/ |
680 |
|
|
/* If the fake prefix is coming from a TS server, discard it |
681 |
|
|
* silently -orabidoo |
682 |
|
|
* |
683 |
|
|
* all servers must be TS these days --is |
684 |
|
|
*/ |
685 |
|
|
sendto_realops_flags(UMODE_DEBUG, L_ADMIN, |
686 |
|
|
"Message for %s[%s@%s!%s] from %s (TS, ignored)", |
687 |
|
|
source_p->name, source_p->username, source_p->host, |
688 |
|
|
source_p->from->name, get_client_name(client_p, SHOW_IP)); |
689 |
|
|
sendto_realops_flags(UMODE_DEBUG, L_OPER, |
690 |
|
|
"Message for %s[%s@%s!%s] from %s (TS, ignored)", |
691 |
|
|
source_p->name, source_p->username, source_p->host, |
692 |
|
|
source_p->from->name, get_client_name(client_p, MASK_IP)); |
693 |
|
|
|
694 |
adx |
743 |
return 0; |
695 |
adx |
30 |
} |
696 |
|
|
|
697 |
|
|
/* remove_unknown() |
698 |
|
|
* |
699 |
adx |
743 |
* inputs - |
700 |
|
|
* output - |
701 |
|
|
* side effects - |
702 |
adx |
30 |
*/ |
703 |
|
|
static void |
704 |
|
|
remove_unknown(struct Client *client_p, char *lsender, char *lbuffer) |
705 |
|
|
{ |
706 |
|
|
/* Do kill if it came from a server because it means there is a ghost |
707 |
|
|
* user on the other server which needs to be removed. -avalon |
708 |
|
|
* Tell opers about this. -Taner |
709 |
|
|
*/ |
710 |
|
|
/* '[0-9]something' is an ID (KILL/SQUIT depending on its length) |
711 |
|
|
* 'nodots' is a nickname (KILL) |
712 |
|
|
* 'no.dot.at.start' is a server (SQUIT) |
713 |
|
|
*/ |
714 |
|
|
if ((IsDigit(*lsender) && strlen(lsender) <= IRC_MAXSID) || |
715 |
|
|
strchr(lsender, '.') != NULL) |
716 |
|
|
{ |
717 |
|
|
sendto_realops_flags(UMODE_DEBUG, L_ADMIN, |
718 |
|
|
"Unknown prefix (%s) from %s, Squitting %s", |
719 |
|
|
lbuffer, get_client_name(client_p, SHOW_IP), lsender); |
720 |
|
|
sendto_realops_flags(UMODE_DEBUG, L_OPER, |
721 |
|
|
"Unknown prefix (%s) from %s, Squitting %s", |
722 |
|
|
lbuffer, client_p->name, lsender); |
723 |
|
|
sendto_one(client_p, ":%s SQUIT %s :(Unknown prefix (%s) from %s)", |
724 |
|
|
me.name, lsender, lbuffer, client_p->name); |
725 |
|
|
} |
726 |
|
|
else |
727 |
|
|
sendto_one(client_p, ":%s KILL %s :%s (Unknown Client)", |
728 |
|
|
me.name, lsender, me.name); |
729 |
|
|
} |
730 |
|
|
|
731 |
|
|
/* |
732 |
|
|
* |
733 |
|
|
* parc number of arguments ('sender' counted as one!) |
734 |
|
|
* parv[0] pointer to 'sender' (may point to empty string) (not used) |
735 |
|
|
* parv[1]..parv[parc-1] |
736 |
|
|
* pointers to additional parameters, this is a NULL |
737 |
|
|
* terminated list (parv[parc] == NULL). |
738 |
|
|
* |
739 |
|
|
* *WARNING* |
740 |
|
|
* Numerics are mostly error reports. If there is something |
741 |
|
|
* wrong with the message, just *DROP* it! Don't even think of |
742 |
|
|
* sending back a neat error message -- big danger of creating |
743 |
|
|
* a ping pong error message... |
744 |
|
|
*/ |
745 |
|
|
static void |
746 |
|
|
do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, |
747 |
|
|
int parc, char *parv[]) |
748 |
|
|
{ |
749 |
|
|
struct Client *target_p; |
750 |
|
|
struct Channel *chptr; |
751 |
adx |
743 |
char *t; // current position within the buffer |
752 |
|
|
int i, tl; // current length of presently being built string in t |
753 |
adx |
30 |
|
754 |
|
|
if (parc < 2 || !IsServer(source_p)) |
755 |
|
|
return; |
756 |
|
|
|
757 |
adx |
743 |
// Remap low number numerics. |
758 |
adx |
30 |
if (numeric[0] == '0') |
759 |
|
|
numeric[0] = '1'; |
760 |
|
|
|
761 |
|
|
/* Prepare the parameter portion of the message into 'buffer'. |
762 |
|
|
* (Because the buffer is twice as large as the message buffer |
763 |
|
|
* for the socket, no overflow can occur here... ...on current |
764 |
|
|
* assumptions--bets are off, if these are changed --msa) |
765 |
|
|
*/ |
766 |
|
|
t = buffer; |
767 |
|
|
for (i = 2; i < (parc - 1); i++) |
768 |
|
|
{ |
769 |
|
|
tl = ircsprintf(t, " %s", parv[i]); |
770 |
|
|
t += tl; |
771 |
|
|
} |
772 |
|
|
|
773 |
|
|
ircsprintf(t," :%s", parv[parc-1]); |
774 |
|
|
|
775 |
|
|
if (((target_p = find_person(client_p, parv[1])) != NULL) || |
776 |
|
|
((target_p = find_server(parv[1])) != NULL)) |
777 |
|
|
{ |
778 |
|
|
if (IsMe(target_p)) |
779 |
|
|
{ |
780 |
|
|
int num; |
781 |
|
|
|
782 |
|
|
/* |
783 |
|
|
* We shouldn't get numerics sent to us, |
784 |
|
|
* any numerics we do get indicate a bug somewhere.. |
785 |
|
|
*/ |
786 |
|
|
/* ugh. this is here because of nick collisions. when two servers |
787 |
|
|
* relink, they burst each other their nicks, then perform collides. |
788 |
|
|
* if there is a nick collision, BOTH servers will kill their own |
789 |
|
|
* nicks, and BOTH will kill the other servers nick, which wont exist, |
790 |
|
|
* because it will have been already killed by the local server. |
791 |
|
|
* |
792 |
|
|
* unfortunately, as we cant guarantee other servers will do the |
793 |
|
|
* "right thing" on a nick collision, we have to keep both kills. |
794 |
|
|
* ergo we need to ignore ERR_NOSUCHNICK. --fl_ |
795 |
|
|
*/ |
796 |
|
|
/* quick comment. This _was_ tried. i.e. assume the other servers |
797 |
|
|
* will do the "right thing" and kill a nick that is colliding. |
798 |
|
|
* unfortunately, it did not work. --Dianora |
799 |
|
|
*/ |
800 |
|
|
|
801 |
|
|
/* Yes, a good compiler would have optimised this, but |
802 |
|
|
* this is probably easier to read. -db |
803 |
|
|
*/ |
804 |
|
|
num = atoi(numeric); |
805 |
|
|
|
806 |
michael |
534 |
if (num != ERR_NOSUCHNICK) |
807 |
adx |
30 |
sendto_realops_flags(UMODE_ALL, L_ADMIN, |
808 |
michael |
534 |
"*** %s(via %s) sent a %s numeric to me: %s", |
809 |
|
|
source_p->name, client_p->name, numeric, buffer); |
810 |
adx |
30 |
return; |
811 |
|
|
} |
812 |
|
|
else if (target_p->from == client_p) |
813 |
|
|
{ |
814 |
|
|
/* This message changed direction (nick collision?) |
815 |
|
|
* ignore it. |
816 |
|
|
*/ |
817 |
|
|
return; |
818 |
|
|
} |
819 |
|
|
|
820 |
adx |
743 |
// csircd will send out unknown umode flag for +a (admin), drop it here. |
821 |
adx |
30 |
if ((atoi(numeric) == ERR_UMODEUNKNOWNFLAG) && MyClient(target_p)) |
822 |
|
|
return; |
823 |
|
|
|
824 |
adx |
743 |
// Fake it for server hiding, if its our client |
825 |
|
|
if (ServerHide.hide_servers && MyClient(target_p) && !IsOper(target_p)) |
826 |
adx |
30 |
sendto_one(target_p, ":%s %s %s%s", me.name, numeric, target_p->name, buffer); |
827 |
adx |
752 |
else |
828 |
|
|
sendto_one(target_p, ":%s %s %s%s", ID_or_name(source_p, target_p->from), |
829 |
|
|
numeric, ID_or_name(target_p, target_p->from), buffer); |
830 |
adx |
30 |
return; |
831 |
|
|
} |
832 |
|
|
else if ((chptr = hash_find_channel(parv[1])) != NULL) |
833 |
|
|
sendto_channel_local(ALL_MEMBERS, NO, chptr, |
834 |
michael |
534 |
":%s %s %s %s", |
835 |
|
|
source_p->name, |
836 |
|
|
numeric, chptr->chname, buffer); |
837 |
adx |
30 |
} |
838 |
|
|
|
839 |
|
|
/* m_not_oper() |
840 |
adx |
743 |
* inputs - |
841 |
|
|
* output - |
842 |
|
|
* side effects - just returns a nastyogram to given user |
843 |
adx |
30 |
*/ |
844 |
|
|
void |
845 |
|
|
m_not_oper(struct Client *client_p, struct Client *source_p, |
846 |
|
|
int parc, char *parv[]) |
847 |
|
|
{ |
848 |
|
|
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
849 |
|
|
me.name, parv[0]); |
850 |
|
|
} |
851 |
|
|
|
852 |
|
|
void |
853 |
|
|
m_unregistered(struct Client *client_p, struct Client *source_p, |
854 |
|
|
int parc, char *parv[]) |
855 |
|
|
{ |
856 |
|
|
/* bit of a hack. |
857 |
|
|
* I don't =really= want to waste a bit in a flag |
858 |
|
|
* number_of_nick_changes is only really valid after the client |
859 |
|
|
* is fully registered.. |
860 |
|
|
*/ |
861 |
|
|
if (client_p->localClient->number_of_nick_changes == 0) |
862 |
|
|
{ |
863 |
|
|
sendto_one(client_p, ":%s %d * %s :Register first.", |
864 |
|
|
me.name, ERR_NOTREGISTERED, parv[0]); |
865 |
|
|
client_p->localClient->number_of_nick_changes++; |
866 |
|
|
} |
867 |
|
|
} |
868 |
|
|
|
869 |
|
|
void |
870 |
|
|
m_registered(struct Client *client_p, struct Client *source_p, |
871 |
|
|
int parc, char *parv[]) |
872 |
|
|
{ |
873 |
|
|
sendto_one(client_p, form_str(ERR_ALREADYREGISTRED), |
874 |
|
|
me.name, parv[0]); |
875 |
|
|
} |
876 |
|
|
|
877 |
|
|
void |
878 |
|
|
m_ignore(struct Client *client_p, struct Client *source_p, |
879 |
|
|
int parc, char *parv[]) |
880 |
|
|
{ |
881 |
|
|
return; |
882 |
|
|
} |
883 |
|
|
|