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