Author: Isomer <isomer@coders.net>
[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_string.h"
31 #include "map.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_ignore, 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     { mr_error, 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     { m_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     { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
357   },
358   {
359     MSG_CONNECT,
360     TOK_CONNECT,
361     0, MAXPARA, MFLG_SLOW, 0,
362     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
363     { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
364   },
365   {
366     MSG_MAP,
367     TOK_MAP,
368     0, MAXPARA, MFLG_SLOW, 0,
369     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
370     { m_unregistered, m_map, m_ignore, m_map, m_ignore }
371   },
372   {
373     MSG_VERSION,
374     TOK_VERSION,
375     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
376     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
377     { m_version, m_version, ms_version, m_version, m_ignore }
378   },
379   {
380     MSG_STATS,
381     TOK_STATS,
382     0, MAXPARA, MFLG_SLOW, 0,
383     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
384     { m_unregistered, m_stats, ms_stats, mo_stats, m_ignore }
385   },
386   {
387     MSG_LINKS,
388     TOK_LINKS,
389     0, MAXPARA, MFLG_SLOW, 0,
390     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
391     { m_unregistered, m_links, ms_links, m_links, m_ignore }
392   },
393   {
394     MSG_ADMIN,
395     TOK_ADMIN,
396     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
397     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
398     { m_admin, m_admin, ms_admin, m_admin, m_ignore }
399   },
400   {
401     MSG_HELP,
402     TOK_HELP,
403     0, MAXPARA, MFLG_SLOW, 0,
404     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
405     { m_unregistered, m_help, m_ignore, m_help, m_ignore }
406   },
407   {
408     MSG_INFO,
409     TOK_INFO,
410     0, MAXPARA, MFLG_SLOW, 0,
411     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
412     { m_unregistered, m_info, ms_info, mo_info, m_ignore }
413   },
414   {
415     MSG_MOTD,
416     TOK_MOTD,
417     0, MAXPARA, MFLG_SLOW, 0,
418     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
419     { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
420   },
421   {
422     MSG_CLOSE,
423     TOK_CLOSE,
424     0, MAXPARA, MFLG_SLOW, 0,
425     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
426     { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
427   },
428   {
429     MSG_SILENCE,
430     TOK_SILENCE,
431     0, MAXPARA, MFLG_SLOW, 0,
432     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
433     { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
434   },
435   {
436     MSG_GLINE,
437     TOK_GLINE,
438     0, MAXPARA, MFLG_SLOW, 0,
439     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
440     { m_unregistered, m_not_oper, ms_gline, mo_gline, m_ignore }
441   },
442   {
443     MSG_JUPE,
444     TOK_JUPE,
445     0, MAXPARA, MFLG_SLOW, 0,
446     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
447     { m_unregistered, m_jupe, ms_jupe, mo_jupe, m_ignore }
448   },
449   {
450     MSG_OPMODE,
451     TOK_OPMODE,
452     0, MAXPARA, MFLG_SLOW, 0,
453     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
454     { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
455   },
456   {
457     MSG_CLEARMODE,
458     TOK_CLEARMODE,
459     0, MAXPARA, MFLG_SLOW, 0,
460     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
461     { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
462   },
463   {
464     MSG_UPING,
465     TOK_UPING,
466     0, MAXPARA, MFLG_SLOW, 0,
467     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
468     { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
469   },
470   {
471     MSG_END_OF_BURST,
472     TOK_END_OF_BURST,
473     0, MAXPARA, MFLG_SLOW, 0,
474     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
475     { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
476   },
477   {
478     MSG_END_OF_BURST_ACK,
479     TOK_END_OF_BURST_ACK,
480     0, MAXPARA, 1, 0,
481     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
482     { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
483   },
484   {
485     MSG_HASH,
486     TOK_HASH,
487     0, MAXPARA, MFLG_SLOW, 0,
488     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
489     { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
490   },
491   {
492     MSG_DNS,
493     TOK_DNS,
494     0, MAXPARA, MFLG_SLOW, 0,
495     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
496     { m_unregistered, m_dns, m_dns, m_dns, m_ignore }
497   },
498   {
499     MSG_REHASH,
500     TOK_REHASH,
501     0, MAXPARA, MFLG_SLOW, 0,
502     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
503     { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
504   },
505   {
506     MSG_RESTART,
507     TOK_RESTART,
508     0, MAXPARA, MFLG_SLOW, 0,
509     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
510     { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
511   },
512   {
513     MSG_DIE,
514     TOK_DIE,
515     0, MAXPARA, MFLG_SLOW, 0,
516     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
517     { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
518   },
519   {
520     MSG_PROTO,
521     TOK_PROTO,
522     0, MAXPARA, MFLG_SLOW, 0,
523     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
524     { m_proto, m_proto, m_proto, m_proto, m_ignore }
525   },
526   { 0 }
527 }; 
528
529
530 #ifdef GODMODE
531 extern int sdbflag;
532 #endif /* GODMODE */
533
534 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
535
536 /*
537  * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
538  * Adapted to Undernet, adding token support, etc by comstud 10/06/97
539  */
540
541 static struct MessageTree msg_tree_cmd;
542 static struct MessageTree msg_tree_tok;
543
544 /*
545  * Guts of making the token tree...
546  */
547 static struct Message **do_msg_tree_tok(struct MessageTree *mtree, char *prefix,
548     struct Message **mptr)
549 {
550   char newprefix[64];           /* Must be longer than every command name */
551   int c, c2, lp;
552   struct MessageTree *mtree1;
553
554   lp = strlen(prefix);
555   if (!lp || !strncmp((*mptr)->tok, prefix, lp))
556   {
557     if (!mptr[1] || (lp && strncmp(mptr[1]->tok, prefix, lp)))
558     {
559       /* last command in the message struct or last command in this prefix */
560       mtree->final = (*mptr)->tok + lp;
561       mtree->msg = *mptr;
562       for (c = 0; c < 26; ++c)
563         mtree->pointers[c] = NULL;
564       return mptr + 1;
565     }
566     /* command in this prefix */
567     if (0 == ircd_strcmp((*mptr)->tok, prefix))
568     {
569       mtree->final = "";
570       mtree->msg = *mptr++;
571     }
572     else
573       mtree->final = NULL;
574
575     for (c = 'A'; c <= 'Z'; ++c)
576     {
577       if ((*mptr)->tok[lp] == c)
578       {
579         mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
580         mtree1->final = NULL;
581         mtree->pointers[c - 'A'] = mtree1;
582         strcpy(newprefix, prefix);
583         newprefix[lp] = c;
584         newprefix[lp + 1] = '\0';
585         mptr = do_msg_tree_tok(mtree1, newprefix, mptr);
586         if (!*mptr || strncmp((*mptr)->tok, prefix, lp))
587         {
588           for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
589             mtree->pointers[c2] = NULL;
590           return mptr;
591         }
592       }
593       else
594         mtree->pointers[c - 'A'] = NULL;
595     }
596     return mptr;
597   }
598   /*
599    * XXX - should never be here, quick hack, this can be done better
600    */
601   assert(0);
602   exit(1);
603 }
604
605 /*
606  * Guts of making the command tree...
607  */
608 static struct Message *do_msg_tree_cmd(struct MessageTree *mtree, char *prefix,
609     struct Message *mptr)
610 {
611   char newprefix[64];           /* Must be longer than every command name */
612   int c, c2, lp;
613   struct MessageTree *mtree1;
614
615   lp = strlen(prefix);
616   if (!lp || !strncmp(mptr->cmd, prefix, lp))
617   {
618     if (!mptr[1].cmd || (lp && strncmp(mptr[1].cmd, prefix, lp)))
619     {
620       /* last command in the message struct or last command in this prefix */
621       mtree->final = mptr->cmd + lp;
622       mtree->msg = mptr;
623       for (c = 0; c < 26; ++c)
624         mtree->pointers[c] = NULL;
625       return mptr + 1;
626     }
627     /* command in this prefix */
628     if (0 == ircd_strcmp(mptr->cmd, prefix))
629     {
630       mtree->final = "";
631       mtree->msg = mptr++;
632     }
633     else
634       mtree->final = NULL;
635
636     for (c = 'A'; c <= 'Z'; ++c)
637     {
638       if (mptr->cmd[lp] == c)
639       {
640         mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
641         mtree1->final = NULL;
642         mtree->pointers[c - 'A'] = mtree1;
643         strcpy(newprefix, prefix);
644         newprefix[lp] = c;
645         newprefix[lp + 1] = '\0';
646         mptr = do_msg_tree_cmd(mtree1, newprefix, mptr);
647         if (!mptr->cmd || strncmp(mptr->cmd, prefix, lp))
648         {
649           for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
650             mtree->pointers[c2] = NULL;
651           return mptr;
652         }
653       }
654       else
655         mtree->pointers[c - 'A'] = NULL;
656     }
657     return mptr;
658   }
659   /* 
660    * This should never happen
661    */
662   assert(0);
663   exit(1);
664 }
665
666 static int mcmdcmp(const struct Message *m1, const struct Message *m2)
667 {
668   return strcmp(m1->cmd, m2->cmd);
669 }
670
671 static int mtokcmp(const struct Message **m1, const struct Message **m2)
672 {
673   return strcmp((*m1)->tok, (*m2)->tok);
674 }
675
676 /*
677  * Sort the command names.
678  * Create table of pointers into msgtab for tokens.
679  * Create trees for ->cmd and ->tok and free the token pointers.
680  */
681 void initmsgtree(void)
682 {
683   int i;
684   struct Message *msg = msgtab;
685   int ii;
686   struct Message **msgtab_tok;
687   struct Message **msgtok;
688
689   for (i = 0; msg->cmd; ++i, ++msg)
690     continue;
691   qsort(msgtab, i, sizeof(struct Message),
692       (int (*)(const void *, const void *))mcmdcmp);
693   msgtab_tok = (struct Message **)MyMalloc((i + 1) * sizeof(struct Message *));
694   for (ii = 0; ii < i; ++ii)
695     msgtab_tok[ii] = msgtab + ii;
696   msgtab_tok[i] = NULL;         /* Needed by `do_msg_tree_tok' */
697   qsort(msgtab_tok, i, sizeof(struct Message *),
698       (int (*)(const void *, const void *))mtokcmp);
699   msg = do_msg_tree_cmd(&msg_tree_cmd, "", msgtab);
700   msgtok = do_msg_tree_tok(&msg_tree_tok, "", msgtab_tok);
701   MyFree(msgtab_tok);
702 }
703
704 /*
705  * Generic tree parser which works for both commands and tokens.
706  * Optimized by Run.
707  */
708 static struct Message *msg_tree_parse(char *cmd, struct MessageTree *root)
709 {
710   struct MessageTree *mtree;
711   unsigned char r = (0xdf & (unsigned char)*cmd) - 'A';
712   if (r > 25 || !(mtree = root->pointers[r]))
713     return NULL;
714   for (;;)
715   {
716     r = 0xdf & (unsigned char)*++cmd;
717     if (mtree->final && *mtree->final == r)
718       return mtree->msg;
719     if ((r -= 'A') > 25 || !(mtree = mtree->pointers[r]))
720       return NULL;
721   }
722 }
723
724 /*
725  * This one is identical to the one above, but it is slower because it
726  * makes sure that `cmd' matches the _full_ command, exactly.
727  * This is to avoid confusion with commands like /quake on clients
728  * that send unknown commands directly to the server.
729  */
730 static struct Message *msg_tree_parse_client(char *cmd,
731     struct MessageTree *root)
732 {
733   struct MessageTree *mtree;
734   unsigned char q = (0xdf & (unsigned char)*cmd) - 'A';
735   if (q > 25 || !(mtree = root->pointers[q]))
736     return NULL;
737   for (;;)
738   {
739     q = 0xdf & (unsigned char)*++cmd;
740     if (mtree->final && 0 == ircd_strcmp(mtree->final, cmd))
741       return mtree->msg;
742     if ((q -= 'A') > 25 || !(mtree = mtree->pointers[q]))
743       return NULL;
744   }
745 }
746
747 /*
748  * parse a buffer.
749  *
750  * NOTE: parse_*() should not be called recusively by any other fucntions!
751  */
752 int parse_client(struct Client *cptr, char *buffer, char *bufend)
753 {
754   struct Client*  from = cptr;
755   char*           ch;
756   char*           s;
757   int             i;
758   int             paramcount;
759   int             noprefix = 0;
760   struct Message* mptr;
761   MessageHandler  handler = 0;
762
763   Debug((DEBUG_DEBUG, "Parsing: %s", buffer));
764
765   if (IsDead(cptr))
766     return 0;
767
768   para[0] = from->name;
769   for (ch = buffer; *ch == ' '; ch++);  /* Eat leading spaces */
770   if (*ch == ':')               /* Is any client doing this ? */
771   {
772     for (++ch; *ch && *ch != ' '; ++ch)
773       ; /* Ignore sender prefix from client */
774     while (*ch == ' ')
775       ch++;                     /* Advance to command */
776   }
777   else
778     noprefix = 1;
779   if (*ch == '\0')
780   {
781     ServerStats->is_empt++;
782     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
783         cptr->name, from->name));
784     return (-1);
785   }
786
787   if ((s = strchr(ch, ' ')))
788     *s++ = '\0';
789
790   /*
791    * This is a client/unregistered entity.
792    * Check long command list only.
793    */
794   if (!(mptr = msg_tree_parse_client(ch, &msg_tree_cmd)))
795   {
796     /*
797      * Note: Give error message *only* to recognized
798      * persons. It's a nightmare situation to have
799      * two programs sending "Unknown command"'s or
800      * equivalent to each other at full blast....
801      * If it has got to person state, it at least
802      * seems to be well behaving. Perhaps this message
803      * should never be generated, though...  --msa
804      * Hm, when is the buffer empty -- if a command
805      * code has been found ?? -Armin
806      */
807     if (buffer[0] != '\0')
808     {
809       if (IsUser(from))
810         sendto_one(from, ":%s %d %s %s :Unknown command",
811             me.name, ERR_UNKNOWNCOMMAND, from->name, ch);
812       Debug((DEBUG_ERROR, "Unknown (%s) from %s",
813             ch, get_client_name(cptr, HIDE_IP)));
814     }
815     ServerStats->is_unco++;
816     return (-1);
817   }
818
819   paramcount = mptr->parameters;
820   i = bufend - ((s) ? s : ch);
821   mptr->bytes += i;
822   if ((mptr->flags & MFLG_SLOW))
823     cptr->since += (2 + i / 120);
824   /*
825    * Allow only 1 msg per 2 seconds
826    * (on average) to prevent dumping.
827    * to keep the response rate up,
828    * bursts of up to 5 msgs are allowed
829    * -SRB
830    */
831
832   /*
833    * Must the following loop really be so devious? On
834    * surface it splits the message to parameters from
835    * blank spaces. But, if paramcount has been reached,
836    * the rest of the message goes into this last parameter
837    * (about same effect as ":" has...) --msa
838    */
839
840   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
841
842   i = 0;
843   if (s)
844   {
845     if (paramcount > MAXPARA)
846       paramcount = MAXPARA;
847     for (;;)
848     {
849       /*
850        * Never "FRANCE " again!! ;-) Clean
851        * out *all* blanks.. --msa
852        */
853       while (*s == ' ')
854         *s++ = '\0';
855
856       if (*s == '\0')
857         break;
858       if (*s == ':')
859       {
860         /*
861          * The rest is single parameter--can
862          * include blanks also.
863          */
864         para[++i] = s + 1;
865         break;
866       }
867       para[++i] = s;
868       if (i >= paramcount)
869         break;
870       for (; *s != ' ' && *s; s++);
871     }
872   }
873   para[++i] = NULL;
874   ++mptr->count;
875 #if 0
876   /*
877    * The "unregistered command check" was ugly and mildly inefficient.
878    * I fixed it. :)  --Shadow
879    */
880   if (!IsUser(cptr) && 0 == (mptr->flags & MFLG_UNREG)) {
881     if (0 == (mptr->flags & MFLG_IGNORE))
882       sendto_one(from, ":%s %d * %s :Register first.",
883                  me.name, ERR_NOTREGISTERED, ch);
884     return -1;
885   }
886 #endif
887   handler = mptr->handlers[cptr->handler];
888   assert(0 != handler);
889
890 #ifndef IDLE_FROM_MSG
891   if (IsUser(cptr) && handler != m_ping && handler != m_ignore)
892     from->user->last = CurrentTime;
893 #endif
894
895   return (*handler) (cptr, from, i, para);
896 }
897
898 int parse_server(struct Client *cptr, char *buffer, char *bufend)
899 {
900   struct Client*  from = cptr;
901   char*           ch = buffer;
902   char*           s;
903   int             len;
904   int             i;
905   int             numeric = 0;
906   int             paramcount;
907   struct Message* mptr;
908
909   Debug((DEBUG_DEBUG, "Parsing: %s", buffer));
910
911 #ifdef GODMODE
912   len = strlen(buffer);
913   sdbflag = 1;
914   if (len > 402)
915   {
916     char c = buffer[200];
917     buffer[200] = 0;
918     sendto_ops("RCV:%-8.8s(%.4d): \"%s...%s\"",
919         cptr->name, len, buffer, &buffer[len - 200]);
920     buffer[200] = c;
921   }
922   else
923     sendto_ops("RCV:%-8.8s(%.4d): \"%s\"", cptr->name, len, buffer);
924   sdbflag = 0;
925 #endif /* GODMODE */
926
927   if (IsDead(cptr))
928     return 0;
929
930   para[0] = from->name;
931
932   /*
933    * A server ALWAYS sends a prefix. When it starts with a ':' it's the
934    * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
935    * nick or server
936    */
937   if (*ch == ':')
938   {
939     /* Let para[0] point to the name of the sender */
940     para[0] = ch + 1;
941     if (!(ch = strchr(ch, ' ')))
942       return -1;
943     *ch++ = '\0';
944
945     /* And let `from' point to its client structure,
946        opps.. a server is _also_ a client --Nem */
947     from = FindClient(para[0]);
948
949     /*
950      * If the client corresponding to the
951      * prefix is not found. We must ignore it,
952      * it is simply a lagged message travelling
953      * upstream a SQUIT that removed the client
954      * --Run
955      */
956     if (!from)
957     {
958       Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
959           para[0], buffer, cptr->name));
960       ++ServerStats->is_unpf;
961       while (*ch == ' ')
962         ch++;
963       /*
964        * However, the only thing that MUST be
965        * allowed to travel upstream against an
966        * squit, is an SQUIT itself (the timestamp
967        * protects us from being used wrong)
968        */
969       if (ch[1] == 'Q')
970       {
971         para[0] = cptr->name;
972         from = cptr;
973       }
974       else
975         return 0;
976     }
977     else if (from->from != cptr)
978     {
979       ++ServerStats->is_wrdi;
980       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
981           buffer, cptr->name));
982       return 0;
983     }
984   }
985   else if (Protocol(cptr) > 9)  /* Well, not ALWAYS, 2.9 can send no prefix */
986   {
987     char numeric_prefix[6];
988     int  i;
989     for (i = 0; i < 5; ++i) {
990       if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i])) {
991         break;
992       }
993     }
994     numeric_prefix[i] = '\0';
995     /*
996      * We got a numeric nick as prefix
997      * 1 or 2 character prefixes are from servers
998      * 3 or 5 chars are from clients
999      */
1000     if (' ' == ch[1] || ' ' == ch[2])
1001       from = FindNServer(numeric_prefix);
1002     else 
1003       from = findNUser(numeric_prefix);
1004
1005     do
1006     {
1007       ++ch;
1008     }
1009     while (*ch != ' ' && *ch);
1010
1011     /*
1012      * If the client corresponding to the
1013      * prefix is not found. We must ignore it,
1014      * it is simply a lagged message travelling
1015      * upstream a SQUIT that removed the client
1016      * --Run
1017      * There turned out to be other reasons that
1018      * a prefix is unknown, needing an upstream
1019      * KILL.  Also, next to an SQUIT we better
1020      * allow a KILL to pass too.
1021      * --Run
1022      */
1023     if (!from)
1024     {
1025       ServerStats->is_unpf++;
1026       while (*ch == ' ')
1027         ch++;
1028       if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1029         /* Only sent a KILL for a nick change */
1030       {
1031         struct Client *server;
1032         /* Kill the unknown numeric prefix upstream if
1033          * it's server still exists: */
1034         if ((server = FindNServer(numeric_prefix)) && server->from == cptr)
1035           sendto_one(cptr, "%s KILL %s :%s (Unknown numeric nick)",
1036                      NumServ(&me), numeric_prefix, me.name);
1037       }
1038       /*
1039        * Things that must be allowed to travel
1040        * upstream against an squit:
1041        */
1042       if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1043           (*ch == 'K' && ch[2] == 'L'))
1044         from = cptr;
1045       else
1046         return 0;
1047     }
1048
1049     /* Let para[0] point to the name of the sender */
1050     para[0] = from->name;
1051
1052     if (from->from != cptr)
1053     {
1054       ServerStats->is_wrdi++;
1055       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1056           buffer, cptr->name));
1057       return 0;
1058     }
1059   }
1060
1061   while (*ch == ' ')
1062     ch++;
1063   if (*ch == '\0')
1064   {
1065     ServerStats->is_empt++;
1066     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1067         cptr->name, from->name));
1068     return (-1);
1069   }
1070
1071   /*
1072    * Extract the command code from the packet.   Point s to the end
1073    * of the command code and calculate the length using pointer
1074    * arithmetic.  Note: only need length for numerics and *all*
1075    * numerics must have parameters and thus a space after the command
1076    * code. -avalon
1077    */
1078   s = strchr(ch, ' ');          /* s -> End of the command code */
1079   len = (s) ? (s - ch) : 0;
1080   if (len == 3 && IsDigit(*ch))
1081   {
1082     numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1083     paramcount = MAXPARA;
1084     ServerStats->is_num++;
1085     mptr = NULL;                /* Init. to avoid stupid compiler warning :/ */
1086   }
1087   else
1088   {
1089     if (s)
1090       *s++ = '\0';
1091
1092     /* Version      Receive         Send
1093      * 2.9          Long            Long
1094      * 2.10.0       Tkn/Long        Long
1095      * 2.10.10      Tkn/Long        Tkn
1096      * 2.10.20      Tkn             Tkn
1097      *
1098      * Clients/unreg servers always receive/
1099      * send long commands   -record
1100      */
1101
1102     /*
1103      * This is a server. Check the token command list.
1104      * -record!jegelhof@cloud9.net
1105      */
1106     mptr = msg_tree_parse(ch, &msg_tree_tok);
1107
1108 #if 1                           /* for 2.10.0/2.10.10 */
1109     /*
1110      * This code supports 2.9 and 2.10.0 sending long commands.
1111      * It makes more calls to ircd_strcmp() than the above
1112      * so it will be somewhat slower.
1113      */
1114     if (!mptr)
1115       mptr = msg_tree_parse(ch, &msg_tree_cmd);
1116 #endif /* 1 */
1117
1118     if (!mptr)
1119     {
1120       /*
1121        * Note: Give error message *only* to recognized
1122        * persons. It's a nightmare situation to have
1123        * two programs sending "Unknown command"'s or
1124        * equivalent to each other at full blast....
1125        * If it has got to person state, it at least
1126        * seems to be well behaving. Perhaps this message
1127        * should never be generated, though...   --msa
1128        * Hm, when is the buffer empty -- if a command
1129        * code has been found ?? -Armin
1130        */
1131 #ifdef DEBUGMODE
1132       if (buffer[0] != '\0')
1133       {
1134         Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1135               ch, get_client_name(cptr, HIDE_IP)));
1136       }
1137 #endif
1138       ServerStats->is_unco++;
1139       return (-1);
1140     }
1141
1142     paramcount = mptr->parameters;
1143     i = bufend - ((s) ? s : ch);
1144     mptr->bytes += i;
1145   }
1146   /*
1147    * Must the following loop really be so devious? On
1148    * surface it splits the message to parameters from
1149    * blank spaces. But, if paramcount has been reached,
1150    * the rest of the message goes into this last parameter
1151    * (about same effect as ":" has...) --msa
1152    */
1153
1154   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1155
1156   i = 0;
1157   if (s)
1158   {
1159     if (paramcount > MAXPARA)
1160       paramcount = MAXPARA;
1161     for (;;)
1162     {
1163       /*
1164        * Never "FRANCE " again!! ;-) Clean
1165        * out *all* blanks.. --msa
1166        */
1167       while (*s == ' ')
1168         *s++ = '\0';
1169
1170       if (*s == '\0')
1171         break;
1172       if (*s == ':')
1173       {
1174         /*
1175          * The rest is single parameter--can
1176          * include blanks also.
1177          */
1178         para[++i] = s + 1;
1179         break;
1180       }
1181       para[++i] = s;
1182       if (i >= paramcount)
1183         break;
1184       for (; *s != ' ' && *s; s++);
1185     }
1186   }
1187   para[++i] = NULL;
1188   if (numeric)
1189     return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1190   mptr->count++;
1191
1192   return (*mptr->handlers[cptr->handler]) (cptr, from, i, para);
1193 }