Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / parse.c
1 /*
2  * IRC - Internet Relay Chat, common/parse.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 1, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * $Id$
21  */
22 #include "parse.h"
23 #include "client.h"
24 #include "channel.h"
25 #include "handlers.h"
26 #include "hash.h"
27 #include "ircd.h"
28 #include "ircd_alloc.h"
29 #include "ircd_chattr.h"
30 #include "ircd_reply.h"
31 #include "ircd_string.h"
32 #include "msg.h"
33 #include "numeric.h"
34 #include "numnicks.h"
35 #include "opercmds.h"
36 #include "querycmds.h"
37 #include "res.h"
38 #include "s_bsd.h"
39 #include "s_conf.h"
40 #include "s_debug.h"
41 #include "s_misc.h"
42 #include "s_numeric.h"
43 #include "s_user.h"
44 #include "send.h"
45 #include "struct.h"
46 #include "sys.h"
47 #include "whocmds.h"
48 #include "whowas.h"
49
50 #include <assert.h>
51 #include <string.h>
52 #include <stdlib.h>
53
54
55
56 struct MessageTree {
57   char *final;
58   struct Message *msg;
59   struct MessageTree *pointers[26];
60 };
61
62
63 struct Message msgtab[] = {
64   {
65     MSG_PRIVATE,
66     TOK_PRIVATE,
67     0, MAXPARA, MFLG_SLOW, 0,
68     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
69     { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
70   },
71   {
72     MSG_NICK,
73     TOK_NICK,
74     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
75     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
76     { m_nick, m_nick, ms_nick, m_nick, m_ignore }
77   },
78   {
79     MSG_NOTICE,
80     TOK_NOTICE,
81     0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0,
82     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
83     { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
84   },
85   {
86     MSG_WALLCHOPS,
87     TOK_WALLCHOPS,
88     0, MAXPARA, MFLG_SLOW, 0,
89     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
90     { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
91   },
92   {
93     MSG_CPRIVMSG,
94     TOK_CPRIVMSG,
95     0, MAXPARA, MFLG_SLOW, 0,
96     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
97     { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
98   },
99   {
100     MSG_CNOTICE,
101     TOK_CNOTICE,
102     0, MAXPARA, MFLG_SLOW, 0,
103     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
104     { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
105   },
106   {
107     MSG_JOIN,
108     TOK_JOIN,
109     0, MAXPARA, MFLG_SLOW, 0,
110     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
111     { m_unregistered, m_join, ms_join, m_join, m_ignore }
112   },
113   {
114     MSG_MODE,
115     TOK_MODE,
116     0, MAXPARA, MFLG_SLOW, 0,
117     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
118     { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
119   },
120   {
121     MSG_BURST,
122     TOK_BURST,
123     0, MAXPARA, MFLG_SLOW, 0,
124     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
125     { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
126   },
127   {
128     MSG_CREATE,
129     TOK_CREATE,
130     0, MAXPARA, MFLG_SLOW, 0,
131     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
132     { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
133   },
134   {
135     MSG_DESTRUCT,
136     TOK_DESTRUCT,
137     0, MAXPARA, MFLG_SLOW, 0,
138     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
139     { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
140   },
141   {
142     MSG_QUIT,
143     TOK_QUIT,
144     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
145     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
146     { m_quit, m_quit, ms_quit, m_quit, m_ignore }
147   },
148   {
149     MSG_PART,
150     TOK_PART,
151     0, MAXPARA, MFLG_SLOW, 0,
152     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
153     { m_unregistered, m_part, ms_part, m_part, m_ignore }
154   },
155   {
156     MSG_TOPIC,
157     TOK_TOPIC,
158     0, MAXPARA, MFLG_SLOW, 0,
159     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
160     { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
161   },
162   {
163     MSG_INVITE,
164     TOK_INVITE,
165     0, MAXPARA, MFLG_SLOW, 0,
166     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
167     { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
168   },
169   {
170     MSG_KICK,
171     TOK_KICK,
172     0, MAXPARA, MFLG_SLOW, 0,
173     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
174     { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
175   },
176   {
177     MSG_WALLOPS,
178     TOK_WALLOPS,
179     0, MAXPARA, MFLG_SLOW, 0,
180     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
181     { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
182   },
183   {
184     MSG_DESYNCH,
185     TOK_DESYNCH,
186     0, MAXPARA, MFLG_SLOW, 0,
187     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
188     { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
189   },
190   {
191     MSG_PING,
192     TOK_PING,
193     0, MAXPARA, MFLG_SLOW, 0,
194     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
195     { m_unregistered, m_ping, ms_ping, m_ping, m_ignore }
196   },
197   {
198     MSG_PONG,
199     TOK_PONG,
200     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
201     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
202     { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
203   },
204   {
205     MSG_ERROR,
206     TOK_ERROR,
207     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
208     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
209     { m_ignore, m_ignore, ms_error, m_ignore, m_ignore }
210   },
211   {
212     MSG_KILL,
213     TOK_KILL,
214     0, MAXPARA, MFLG_SLOW, 0,
215     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
216     { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
217   },
218   {
219     MSG_USER,
220     TOK_USER,
221     0, MAXPARA, MFLG_SLOW, 0,
222     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
223     { m_user, m_registered, m_ignore, m_registered, m_ignore }
224   },
225   {
226     MSG_AWAY,
227     TOK_AWAY,
228     0, MAXPARA, MFLG_SLOW, 0,
229     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
230     { m_unregistered, m_away, ms_away, m_away, m_ignore }
231   },
232   {
233     MSG_ISON,
234     TOK_ISON,
235     0, 1, MFLG_SLOW, 0,
236     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
237     { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
238   },
239   {
240     MSG_SERVER,
241     TOK_SERVER,
242     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
243     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
244     { mr_server, m_registered, ms_server, m_registered, m_ignore }
245   },
246   {
247     MSG_SQUIT,
248     TOK_SQUIT,
249     0, MAXPARA, MFLG_SLOW, 0,
250     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
251     { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
252   },
253   {
254     MSG_WHOIS,
255     TOK_WHOIS,
256     0, MAXPARA, MFLG_SLOW, 0,
257     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
258     { m_unregistered, m_whois, m_whois, m_whois, m_ignore }
259   },
260   {
261     MSG_WHO,
262     TOK_WHO,
263     0, MAXPARA, MFLG_SLOW, 0,
264     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
265     { m_unregistered, m_who, m_ignore, m_who, m_ignore }
266   },
267   {
268     MSG_WHOWAS,
269     TOK_WHOWAS,
270     0, MAXPARA, MFLG_SLOW, 0,
271     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
272     { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
273   },
274   {
275     MSG_LIST,
276     TOK_LIST,
277     0, MAXPARA, MFLG_SLOW, 0,
278     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
279     { m_unregistered, m_list, m_ignore, m_list, m_ignore }
280   },
281   {
282     MSG_NAMES,
283     TOK_NAMES,
284     0, MAXPARA, MFLG_SLOW, 0,
285     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
286     { m_unregistered, m_names, ms_names, m_names, m_ignore }
287   },
288   {
289     MSG_USERHOST,
290     TOK_USERHOST,
291     0, 1, MFLG_SLOW, 0,
292     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
293     { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
294   },
295   {
296     MSG_USERIP,
297     TOK_USERIP,
298     0, 1, MFLG_SLOW, 0,
299     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
300     { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
301   },
302   {
303     MSG_TRACE,
304     TOK_TRACE,
305     0, MAXPARA, MFLG_SLOW, 0,
306     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
307     { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
308   },
309   {
310     MSG_PASS,
311     TOK_PASS,
312     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
313     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
314     { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
315   },
316   {
317     MSG_LUSERS,
318     TOK_LUSERS,
319     0, MAXPARA, MFLG_SLOW, 0,
320     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
321     { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
322   },
323   {
324     MSG_TIME,
325     TOK_TIME,
326     0, MAXPARA, MFLG_SLOW, 0,
327     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
328     { m_unregistered, m_time, m_time, m_time, m_ignore }
329   },
330   {
331     MSG_SETTIME,
332     TOK_SETTIME,
333     0, MAXPARA, MFLG_SLOW, 0,
334     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
335     { m_unregistered, m_ignore, ms_settime, mo_settime, m_ignore }
336   },
337   {
338     MSG_RPING,
339     TOK_RPING,
340     0, MAXPARA, MFLG_SLOW, 0,
341     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
342     { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
343   },
344   {
345     MSG_RPONG,
346     TOK_RPONG,
347     0, MAXPARA, MFLG_SLOW, 0,
348     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
349     { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
350   },
351   {
352     MSG_OPER,
353     TOK_OPER,
354     0, MAXPARA, MFLG_SLOW, 0,
355     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
356 #ifndef NOOPER    
357     { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
358 #else
359     { m_unregistered, m_oper, m_ignore, m_ignore, m_ignore }
360 #endif
361   },
362   {
363     MSG_CONNECT,
364     TOK_CONNECT,
365     0, MAXPARA, MFLG_SLOW, 0,
366     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
367     { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
368   },
369   {
370     MSG_MAP,
371     TOK_MAP,
372     0, MAXPARA, MFLG_SLOW, 0,
373     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
374     { m_unregistered, m_map, m_ignore, m_map, m_ignore }
375   },
376   {
377     MSG_VERSION,
378     TOK_VERSION,
379     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
380     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
381     { m_version, m_version, ms_version, m_version, m_ignore }
382   },
383   {
384     MSG_STATS,
385     TOK_STATS,
386     0, MAXPARA, MFLG_SLOW, 0,
387     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
388     { m_unregistered, m_stats, ms_stats, mo_stats, m_ignore }
389   },
390   {
391     MSG_LINKS,
392     TOK_LINKS,
393     0, MAXPARA, MFLG_SLOW, 0,
394     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
395     { m_unregistered, m_links, ms_links, m_links, m_ignore }
396   },
397   {
398     MSG_ADMIN,
399     TOK_ADMIN,
400     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
401     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
402     { m_admin, m_admin, ms_admin, m_admin, m_ignore }
403   },
404   {
405     MSG_HELP,
406     TOK_HELP,
407     0, MAXPARA, MFLG_SLOW, 0,
408     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
409     { m_unregistered, m_help, m_ignore, m_help, m_ignore }
410   },
411   {
412     MSG_INFO,
413     TOK_INFO,
414     0, MAXPARA, MFLG_SLOW, 0,
415     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
416     { m_unregistered, m_info, ms_info, mo_info, m_ignore }
417   },
418   {
419     MSG_MOTD,
420     TOK_MOTD,
421     0, MAXPARA, MFLG_SLOW, 0,
422     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
423     { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
424   },
425   {
426     MSG_CLOSE,
427     TOK_CLOSE,
428     0, MAXPARA, MFLG_SLOW, 0,
429     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
430     { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
431   },
432   {
433     MSG_SILENCE,
434     TOK_SILENCE,
435     0, MAXPARA, MFLG_SLOW, 0,
436     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
437     { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
438   },
439   {
440     MSG_GLINE,
441     TOK_GLINE,
442     0, MAXPARA, MFLG_SLOW, 0,
443     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
444     { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
445   },
446   {
447     MSG_JUPE,
448     TOK_JUPE,
449     0, MAXPARA, MFLG_SLOW, 0,
450     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
451 #ifdef CONFIG_OPERCMDS
452     { m_unregistered, m_jupe, ms_jupe, mo_jupe, m_ignore }
453 #else
454     { m_unregistered, m_jupe, ms_jupe, m_jupe, m_ignore }
455 #endif /* CONFIG_OPERCMDS */
456   },
457   {
458     MSG_OPMODE,
459     TOK_OPMODE,
460     0, MAXPARA, MFLG_SLOW, 0,
461     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
462     { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
463   },
464   {
465     MSG_CLEARMODE,
466     TOK_CLEARMODE,
467     0, MAXPARA, MFLG_SLOW, 0,
468     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
469     { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
470   },
471   {
472     MSG_UPING,
473     TOK_UPING,
474     0, MAXPARA, MFLG_SLOW, 0,
475     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
476     { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
477   },
478   {
479     MSG_END_OF_BURST,
480     TOK_END_OF_BURST,
481     0, MAXPARA, MFLG_SLOW, 0,
482     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
483     { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
484   },
485   {
486     MSG_END_OF_BURST_ACK,
487     TOK_END_OF_BURST_ACK,
488     0, MAXPARA, 1, 0,
489     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
490     { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
491   },
492   {
493     MSG_HASH,
494     TOK_HASH,
495     0, MAXPARA, MFLG_SLOW, 0,
496     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
497     { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
498   },
499   {
500     MSG_DNS,
501     TOK_DNS,
502     0, MAXPARA, MFLG_SLOW, 0,
503     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
504     { m_unregistered, m_dns, m_dns, m_dns, m_ignore }
505   },
506   {
507     MSG_REHASH,
508     TOK_REHASH,
509     0, MAXPARA, MFLG_SLOW, 0,
510     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
511     { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
512   },
513   {
514     MSG_RESTART,
515     TOK_RESTART,
516     0, MAXPARA, MFLG_SLOW, 0,
517     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
518     { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
519   },
520   {
521     MSG_DIE,
522     TOK_DIE,
523     0, MAXPARA, MFLG_SLOW, 0,
524     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
525     { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
526   },
527   {
528     MSG_PROTO,
529     TOK_PROTO,
530     0, MAXPARA, MFLG_SLOW, 0,
531     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
532     { m_proto, m_proto, m_proto, m_proto, m_ignore }
533   },
534   {
535     MSG_SET,
536     TOK_SET,
537     0, MAXPARA, MFLG_SLOW, 0,
538     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
539     { m_unregistered, m_not_oper, m_ignore, mo_set, m_ignore }
540   },
541   {
542     MSG_RESET,
543     TOK_RESET,
544     0, MAXPARA, MFLG_SLOW, 0,
545     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
546     { m_unregistered, m_not_oper, m_ignore, mo_reset, m_ignore }
547   },
548   {
549     MSG_GET,
550     TOK_GET,
551     0, MAXPARA, MFLG_SLOW, 0,
552     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
553     { m_unregistered, m_not_oper, m_ignore, mo_get, m_ignore }
554   },
555   /* This command is an alias for QUIT during the unregistered part of
556    * of the server.  This is because someone jumping via a broken web
557    * proxy will send a 'POST' as their first command - which we will
558    * obviously disconnect them immediately for, stopping people abusing
559    * open gateways
560    */
561   {
562     MSG_POST,
563     TOK_POST,
564     0, MAXPARA, MFLG_SLOW, 0,
565     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
566     { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
567   },  
568   { 0 }
569 }; 
570
571
572 #ifdef GODMODE
573 extern int sdbflag;
574 #endif /* GODMODE */
575
576 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
577
578 /*
579  * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
580  * Adapted to Undernet, adding token support, etc by comstud 10/06/97
581  */
582
583 static struct MessageTree msg_tree_cmd;
584 static struct MessageTree msg_tree_tok;
585
586 /*
587  * Guts of making the token tree...
588  */
589 static struct Message **do_msg_tree_tok(struct MessageTree *mtree, char *prefix,
590     struct Message **mptr)
591 {
592   char newprefix[64];           /* Must be longer than every command name */
593   int c, c2, lp;
594   struct MessageTree *mtree1;
595
596   lp = strlen(prefix);
597   if (!lp || !strncmp((*mptr)->tok, prefix, lp))
598   {
599     if (!mptr[1] || (lp && strncmp(mptr[1]->tok, prefix, lp)))
600     {
601       /* last command in the message struct or last command in this prefix */
602       mtree->final = (*mptr)->tok + lp;
603       mtree->msg = *mptr;
604       for (c = 0; c < 26; ++c)
605         mtree->pointers[c] = NULL;
606       return mptr + 1;
607     }
608     /* command in this prefix */
609     if (0 == ircd_strcmp((*mptr)->tok, prefix))
610     {
611       mtree->final = "";
612       mtree->msg = *mptr++;
613     }
614     else
615       mtree->final = NULL;
616
617     for (c = 'A'; c <= 'Z'; ++c)
618     {
619       if ((*mptr)->tok[lp] == c)
620       {
621         mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
622         mtree1->final = NULL;
623         mtree->pointers[c - 'A'] = mtree1;
624         strcpy(newprefix, prefix);
625         newprefix[lp] = c;
626         newprefix[lp + 1] = '\0';
627         mptr = do_msg_tree_tok(mtree1, newprefix, mptr);
628         if (!*mptr || strncmp((*mptr)->tok, prefix, lp))
629         {
630           for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
631             mtree->pointers[c2] = NULL;
632           return mptr;
633         }
634       }
635       else
636         mtree->pointers[c - 'A'] = NULL;
637     }
638     return mptr;
639   }
640   /*
641    * XXX - should never be here, quick hack, this can be done better
642    */
643   assert(0);
644   exit(1);
645 }
646
647 /*
648  * Guts of making the command tree...
649  */
650 static struct Message *do_msg_tree_cmd(struct MessageTree *mtree, char *prefix,
651     struct Message *mptr)
652 {
653   char newprefix[64];           /* Must be longer than every command name */
654   int c, c2, lp;
655   struct MessageTree *mtree1;
656
657   lp = strlen(prefix);
658   if (!lp || !strncmp(mptr->cmd, prefix, lp))
659   {
660     if (!mptr[1].cmd || (lp && strncmp(mptr[1].cmd, prefix, lp)))
661     {
662       /* last command in the message struct or last command in this prefix */
663       mtree->final = mptr->cmd + lp;
664       mtree->msg = mptr;
665       for (c = 0; c < 26; ++c)
666         mtree->pointers[c] = NULL;
667       return mptr + 1;
668     }
669     /* command in this prefix */
670     if (0 == ircd_strcmp(mptr->cmd, prefix))
671     {
672       mtree->final = "";
673       mtree->msg = mptr++;
674     }
675     else
676       mtree->final = NULL;
677
678     for (c = 'A'; c <= 'Z'; ++c)
679     {
680       if (mptr->cmd[lp] == c)
681       {
682         mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
683         mtree1->final = NULL;
684         mtree->pointers[c - 'A'] = mtree1;
685         strcpy(newprefix, prefix);
686         newprefix[lp] = c;
687         newprefix[lp + 1] = '\0';
688         mptr = do_msg_tree_cmd(mtree1, newprefix, mptr);
689         if (!mptr->cmd || strncmp(mptr->cmd, prefix, lp))
690         {
691           for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
692             mtree->pointers[c2] = NULL;
693           return mptr;
694         }
695       }
696       else
697         mtree->pointers[c - 'A'] = NULL;
698     }
699     return mptr;
700   }
701   /* 
702    * This should never happen
703    */
704   assert(0);
705   exit(1);
706 }
707
708 static int mcmdcmp(const struct Message *m1, const struct Message *m2)
709 {
710   return strcmp(m1->cmd, m2->cmd);
711 }
712
713 static int mtokcmp(const struct Message **m1, const struct Message **m2)
714 {
715   return strcmp((*m1)->tok, (*m2)->tok);
716 }
717
718 /*
719  * Sort the command names.
720  * Create table of pointers into msgtab for tokens.
721  * Create trees for ->cmd and ->tok and free the token pointers.
722  */
723 void initmsgtree(void)
724 {
725   int i;
726   struct Message *msg = msgtab;
727   int ii;
728   struct Message **msgtab_tok;
729   struct Message **msgtok;
730   
731   for (i = 0; msg->cmd; ++i, ++msg)
732     continue;
733   qsort(msgtab, i, sizeof(struct Message),
734       (int (*)(const void *, const void *))mcmdcmp);
735   msgtab_tok = (struct Message **)MyMalloc((i + 1) * sizeof(struct Message *));
736   for (ii = 0; ii < i; ++ii)
737     msgtab_tok[ii] = msgtab + ii;
738   msgtab_tok[i] = NULL;         /* Needed by `do_msg_tree_tok' */
739   qsort(msgtab_tok, i, sizeof(struct Message *),
740       (int (*)(const void *, const void *))mtokcmp);
741   msg = do_msg_tree_cmd(&msg_tree_cmd, "", msgtab);
742   msgtok = do_msg_tree_tok(&msg_tree_tok, "", msgtab_tok);
743   MyFree(msgtab_tok);
744 }
745
746 /*
747  * Generic tree parser which works for both commands and tokens.
748  * Optimized by Run.
749  */
750 static struct Message *msg_tree_parse(char *cmd, struct MessageTree *root)
751 {
752   struct MessageTree *mtree;
753   unsigned char r = (0xdf & (unsigned char)*cmd) - 'A';
754   if (r > 25 || !(mtree = root->pointers[r]))
755     return NULL;
756   for (;;)
757   {
758     r = 0xdf & (unsigned char)*++cmd;
759     if (mtree->final && *mtree->final == r)
760       return mtree->msg;
761     if ((r -= 'A') > 25 || !(mtree = mtree->pointers[r]))
762       return NULL;
763   }
764 }
765
766 /*
767  * This one is identical to the one above, but it is slower because it
768  * makes sure that `cmd' matches the _full_ command, exactly.
769  * This is to avoid confusion with commands like /quake on clients
770  * that send unknown commands directly to the server.
771  */
772 static struct Message *msg_tree_parse_client(char *cmd,
773     struct MessageTree *root)
774 {
775   struct MessageTree *mtree;
776   unsigned char q = (0xdf & (unsigned char)*cmd) - 'A';
777   if (q > 25 || !(mtree = root->pointers[q]))
778     return NULL;
779   for (;;)
780   {
781     q = 0xdf & (unsigned char)*++cmd;
782     if (mtree->final && 0 == ircd_strcmp(mtree->final, cmd))
783       return mtree->msg;
784     if ((q -= 'A') > 25 || !(mtree = mtree->pointers[q]))
785       return NULL;
786   }
787 }
788
789 /*
790  * parse a buffer.
791  *
792  * NOTE: parse_*() should not be called recusively by any other fucntions!
793  */
794 int parse_client(struct Client *cptr, char *buffer, char *bufend)
795 {
796   struct Client*  from = cptr;
797   char*           ch;
798   char*           s;
799   int             i;
800   int             paramcount;
801   int             noprefix = 0;
802   struct Message* mptr;
803   MessageHandler  handler = 0;
804
805   Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
806
807   if (IsDead(cptr))
808     return 0;
809
810   para[0] = from->name;
811   for (ch = buffer; *ch == ' '; ch++);  /* Eat leading spaces */
812   if (*ch == ':')               /* Is any client doing this ? */
813   {
814     for (++ch; *ch && *ch != ' '; ++ch)
815       ; /* Ignore sender prefix from client */
816     while (*ch == ' ')
817       ch++;                     /* Advance to command */
818   }
819   else
820     noprefix = 1;
821   if (*ch == '\0')
822   {
823     ServerStats->is_empt++;
824     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
825         cptr->name, from->name));
826     return (-1);
827   }
828
829   if ((s = strchr(ch, ' ')))
830     *s++ = '\0';
831
832   /*
833    * This is a client/unregistered entity.
834    * Check long command list only.
835    */
836   if (!(mptr = msg_tree_parse_client(ch, &msg_tree_cmd)))
837   {
838     /*
839      * Note: Give error message *only* to recognized
840      * persons. It's a nightmare situation to have
841      * two programs sending "Unknown command"'s or
842      * equivalent to each other at full blast....
843      * If it has got to person state, it at least
844      * seems to be well behaving. Perhaps this message
845      * should never be generated, though...  --msa
846      * Hm, when is the buffer empty -- if a command
847      * code has been found ?? -Armin
848      */
849     if (buffer[0] != '\0')
850     {
851       if (IsUser(from))
852         send_reply(from, ERR_UNKNOWNCOMMAND, ch);
853       Debug((DEBUG_ERROR, "Unknown (%s) from %s",
854             ch, get_client_name(cptr, HIDE_IP)));
855     }
856     ServerStats->is_unco++;
857     return (-1);
858   }
859
860   paramcount = mptr->parameters;
861   i = bufend - ((s) ? s : ch);
862   mptr->bytes += i;
863   if ((mptr->flags & MFLG_SLOW))
864     cptr->since += (2 + i / 120);
865   /*
866    * Allow only 1 msg per 2 seconds
867    * (on average) to prevent dumping.
868    * to keep the response rate up,
869    * bursts of up to 5 msgs are allowed
870    * -SRB
871    */
872
873   /*
874    * Must the following loop really be so devious? On
875    * surface it splits the message to parameters from
876    * blank spaces. But, if paramcount has been reached,
877    * the rest of the message goes into this last parameter
878    * (about same effect as ":" has...) --msa
879    */
880
881   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
882
883   i = 0;
884   if (s)
885   {
886     if (paramcount > MAXPARA)
887       paramcount = MAXPARA;
888     for (;;)
889     {
890       /*
891        * Never "FRANCE " again!! ;-) Clean
892        * out *all* blanks.. --msa
893        */
894       while (*s == ' ')
895         *s++ = '\0';
896
897       if (*s == '\0')
898         break;
899       if (*s == ':')
900       {
901         /*
902          * The rest is single parameter--can
903          * include blanks also.
904          */
905         para[++i] = s + 1;
906         break;
907       }
908       para[++i] = s;
909       if (i >= paramcount)
910         break;
911       for (; *s != ' ' && *s; s++);
912     }
913   }
914   para[++i] = NULL;
915   ++mptr->count;
916 #if 0
917   /*
918    * The "unregistered command check" was ugly and mildly inefficient.
919    * I fixed it. :)  --Shadow
920    */
921   if (!IsUser(cptr) && 0 == (mptr->flags & MFLG_UNREG)) {
922     if (0 == (mptr->flags & MFLG_IGNORE))
923       sendto_one(from, ":%s %d * %s :Register first.", /* XXX DEAD */
924                  me.name, ERR_NOTREGISTERED, ch);
925     return -1;
926   }
927 #endif
928   handler = mptr->handlers[cptr->handler];
929   assert(0 != handler);
930
931 #ifndef IDLE_FROM_MSG
932   if (IsUser(cptr) && handler != m_ping && handler != m_ignore)
933     from->user->last = CurrentTime;
934 #endif
935
936   return (*handler) (cptr, from, i, para);
937 }
938
939 int parse_server(struct Client *cptr, char *buffer, char *bufend)
940 {
941   struct Client*  from = cptr;
942   char*           ch = buffer;
943   char*           s;
944   int             len;
945   int             i;
946   int             numeric = 0;
947   int             paramcount;
948   struct Message* mptr;
949
950   Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
951
952   if (IsDead(cptr))
953     return 0;
954
955   para[0] = from->name;
956
957   /*
958    * A server ALWAYS sends a prefix. When it starts with a ':' it's the
959    * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
960    * nick or server
961    */
962   if (*ch == ':')
963   {
964     /* Let para[0] point to the name of the sender */
965     para[0] = ch + 1;
966     if (!(ch = strchr(ch, ' ')))
967       return -1;
968     *ch++ = '\0';
969
970     /* And let `from' point to its client structure,
971        opps.. a server is _also_ a client --Nem */
972     from = FindClient(para[0]);
973
974     /*
975      * If the client corresponding to the
976      * prefix is not found. We must ignore it,
977      * it is simply a lagged message travelling
978      * upstream a SQUIT that removed the client
979      * --Run
980      */
981     if (!from)
982     {
983       Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
984           para[0], buffer, cptr->name));
985       ++ServerStats->is_unpf;
986       while (*ch == ' ')
987         ch++;
988       /*
989        * However, the only thing that MUST be
990        * allowed to travel upstream against an
991        * squit, is an SQUIT itself (the timestamp
992        * protects us from being used wrong)
993        */
994       if (ch[1] == 'Q')
995       {
996         para[0] = cptr->name;
997         from = cptr;
998       }
999       else
1000         return 0;
1001     }
1002     else if (from->from != cptr)
1003     {
1004       ++ServerStats->is_wrdi;
1005       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1006           buffer, cptr->name));
1007       return 0;
1008     }
1009   }
1010   else if (Protocol(cptr) > 9)  /* Well, not ALWAYS, 2.9 can send no prefix */
1011   {
1012     char numeric_prefix[6];
1013     int  i;
1014     for (i = 0; i < 5; ++i) {
1015       if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i])) {
1016         break;
1017       }
1018     }
1019     numeric_prefix[i] = '\0';
1020     /*
1021      * We got a numeric nick as prefix
1022      * 1 or 2 character prefixes are from servers
1023      * 3 or 5 chars are from clients
1024      */
1025     if (' ' == ch[1] || ' ' == ch[2])
1026       from = FindNServer(numeric_prefix);
1027     else 
1028       from = findNUser(numeric_prefix);
1029
1030     do
1031     {
1032       ++ch;
1033     }
1034     while (*ch != ' ' && *ch);
1035
1036     /*
1037      * If the client corresponding to the
1038      * prefix is not found. We must ignore it,
1039      * it is simply a lagged message travelling
1040      * upstream a SQUIT that removed the client
1041      * --Run
1042      * There turned out to be other reasons that
1043      * a prefix is unknown, needing an upstream
1044      * KILL.  Also, next to an SQUIT we better
1045      * allow a KILL to pass too.
1046      * --Run
1047      */
1048     if (!from)
1049     {
1050       ServerStats->is_unpf++;
1051       while (*ch == ' ')
1052         ch++;
1053       if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1054         /* Only sent a KILL for a nick change */
1055       {
1056         struct Client *server;
1057         /* Kill the unknown numeric prefix upstream if
1058          * it's server still exists: */
1059         if ((server = FindNServer(numeric_prefix)) && server->from == cptr)
1060           sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1061                         numeric_prefix, me.name);
1062       }
1063       /*
1064        * Things that must be allowed to travel
1065        * upstream against an squit:
1066        */
1067       if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1068           (*ch == 'K' && ch[2] == 'L'))
1069         from = cptr;
1070       else
1071         return 0;
1072     }
1073
1074     /* Let para[0] point to the name of the sender */
1075     para[0] = from->name;
1076
1077     if (from->from != cptr)
1078     {
1079       ServerStats->is_wrdi++;
1080       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1081           buffer, cptr->name));
1082       return 0;
1083     }
1084   }
1085
1086   while (*ch == ' ')
1087     ch++;
1088   if (*ch == '\0')
1089   {
1090     ServerStats->is_empt++;
1091     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1092         cptr->name, from->name));
1093     return (-1);
1094   }
1095
1096   /*
1097    * Extract the command code from the packet.   Point s to the end
1098    * of the command code and calculate the length using pointer
1099    * arithmetic.  Note: only need length for numerics and *all*
1100    * numerics must have parameters and thus a space after the command
1101    * code. -avalon
1102    */
1103   s = strchr(ch, ' ');          /* s -> End of the command code */
1104   len = (s) ? (s - ch) : 0;
1105   if (len == 3 && IsDigit(*ch))
1106   {
1107     numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1108     paramcount = 2; /* destination, and the rest of it */
1109     ServerStats->is_num++;
1110     mptr = NULL;                /* Init. to avoid stupid compiler warning :/ */
1111   }
1112   else
1113   {
1114     if (s)
1115       *s++ = '\0';
1116
1117     /* Version      Receive         Send
1118      * 2.9          Long            Long
1119      * 2.10.0       Tkn/Long        Long
1120      * 2.10.10      Tkn/Long        Tkn
1121      * 2.10.20      Tkn             Tkn
1122      *
1123      * Clients/unreg servers always receive/
1124      * send long commands   -record
1125      */
1126
1127     /*
1128      * This is a server. Check the token command list.
1129      * -record!jegelhof@cloud9.net
1130      */
1131     mptr = msg_tree_parse(ch, &msg_tree_tok);
1132
1133 #if 1                           /* for 2.10.0/2.10.10 */
1134     /*
1135      * This code supports 2.9 and 2.10.0 sending long commands.
1136      * It makes more calls to ircd_strcmp() than the above
1137      * so it will be somewhat slower.
1138      */
1139     if (!mptr)
1140       mptr = msg_tree_parse(ch, &msg_tree_cmd);
1141 #endif /* 1 */
1142
1143     if (!mptr)
1144     {
1145       /*
1146        * Note: Give error message *only* to recognized
1147        * persons. It's a nightmare situation to have
1148        * two programs sending "Unknown command"'s or
1149        * equivalent to each other at full blast....
1150        * If it has got to person state, it at least
1151        * seems to be well behaving. Perhaps this message
1152        * should never be generated, though...   --msa
1153        * Hm, when is the buffer empty -- if a command
1154        * code has been found ?? -Armin
1155        */
1156 #ifdef DEBUGMODE
1157       if (buffer[0] != '\0')
1158       {
1159         Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1160               ch, get_client_name(cptr, HIDE_IP)));
1161       }
1162 #endif
1163       ServerStats->is_unco++;
1164       return (-1);
1165     }
1166
1167     paramcount = mptr->parameters;
1168     i = bufend - ((s) ? s : ch);
1169     mptr->bytes += i;
1170   }
1171   /*
1172    * Must the following loop really be so devious? On
1173    * surface it splits the message to parameters from
1174    * blank spaces. But, if paramcount has been reached,
1175    * the rest of the message goes into this last parameter
1176    * (about same effect as ":" has...) --msa
1177    */
1178
1179   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1180
1181   i = 0;
1182   if (s)
1183   {
1184     if (paramcount > MAXPARA)
1185       paramcount = MAXPARA;
1186     for (;;)
1187     {
1188       /*
1189        * Never "FRANCE " again!! ;-) Clean
1190        * out *all* blanks.. --msa
1191        */
1192       while (*s == ' ')
1193         *s++ = '\0';
1194
1195       if (*s == '\0')
1196         break;
1197       if (*s == ':')
1198       {
1199         /*
1200          * The rest is single parameter--can
1201          * include blanks also.
1202          */
1203         if (numeric)
1204           para[++i] = s; /* preserve the colon to make do_numeric happy */
1205         else
1206           para[++i] = s + 1;
1207         break;
1208       }
1209       para[++i] = s;
1210       if (i >= paramcount)
1211         break;
1212       for (; *s != ' ' && *s; s++);
1213     }
1214   }
1215   para[++i] = NULL;
1216   if (numeric)
1217     return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1218   mptr->count++;
1219
1220   return (*mptr->handlers[cptr->handler]) (cptr, from, i, para);
1221 }