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_features.h"
31 #include "ircd_reply.h"
32 #include "ircd_string.h"
33 #include "msg.h"
34 #include "numeric.h"
35 #include "numnicks.h"
36 #include "opercmds.h"
37 #include "querycmds.h"
38 #include "res.h"
39 #include "s_bsd.h"
40 #include "s_conf.h"
41 #include "s_debug.h"
42 #include "s_misc.h"
43 #include "s_numeric.h"
44 #include "s_user.h"
45 #include "send.h"
46 #include "struct.h"
47 #include "sys.h"
48 #include "whocmds.h"
49 #include "whowas.h"
50
51 #include <assert.h>
52 #include <string.h>
53 #include <stdlib.h>
54
55
56
57 struct MessageTree {
58   char *final;
59   struct Message *msg;
60   struct MessageTree *pointers[26];
61 };
62
63
64 struct Message msgtab[] = {
65   {
66     MSG_PRIVATE,
67     TOK_PRIVATE,
68     0, MAXPARA, MFLG_SLOW, 0,
69     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
70     { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
71   },
72   {
73     MSG_NICK,
74     TOK_NICK,
75     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
76     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
77     { m_nick, m_nick, ms_nick, m_nick, m_ignore }
78   },
79   {
80     MSG_NOTICE,
81     TOK_NOTICE,
82     0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0,
83     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
84     { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
85   },
86   {
87     MSG_WALLCHOPS,
88     TOK_WALLCHOPS,
89     0, MAXPARA, MFLG_SLOW, 0,
90     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
91     { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
92   },
93   {
94     MSG_CPRIVMSG,
95     TOK_CPRIVMSG,
96     0, MAXPARA, MFLG_SLOW, 0,
97     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
98     { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
99   },
100   {
101     MSG_CNOTICE,
102     TOK_CNOTICE,
103     0, MAXPARA, MFLG_SLOW, 0,
104     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
105     { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
106   },
107   {
108     MSG_JOIN,
109     TOK_JOIN,
110     0, MAXPARA, MFLG_SLOW, 0,
111     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
112     { m_unregistered, m_join, ms_join, m_join, m_ignore }
113   },
114   {
115     MSG_MODE,
116     TOK_MODE,
117     0, MAXPARA, MFLG_SLOW, 0,
118     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
119     { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
120   },
121   {
122     MSG_BURST,
123     TOK_BURST,
124     0, MAXPARA, MFLG_SLOW, 0,
125     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
126     { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
127   },
128   {
129     MSG_CREATE,
130     TOK_CREATE,
131     0, MAXPARA, MFLG_SLOW, 0,
132     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
133     { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
134   },
135   {
136     MSG_DESTRUCT,
137     TOK_DESTRUCT,
138     0, MAXPARA, MFLG_SLOW, 0,
139     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
140     { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
141   },
142   {
143     MSG_QUIT,
144     TOK_QUIT,
145     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
146     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
147     { m_quit, m_quit, ms_quit, m_quit, m_ignore }
148   },
149   {
150     MSG_PART,
151     TOK_PART,
152     0, MAXPARA, MFLG_SLOW, 0,
153     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
154     { m_unregistered, m_part, ms_part, m_part, m_ignore }
155   },
156   {
157     MSG_TOPIC,
158     TOK_TOPIC,
159     0, MAXPARA, MFLG_SLOW, 0,
160     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
161     { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
162   },
163   {
164     MSG_INVITE,
165     TOK_INVITE,
166     0, MAXPARA, MFLG_SLOW, 0,
167     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
168     { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
169   },
170   {
171     MSG_KICK,
172     TOK_KICK,
173     0, MAXPARA, MFLG_SLOW, 0,
174     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
175     { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
176   },
177   {
178     MSG_WALLOPS,
179     TOK_WALLOPS,
180     0, MAXPARA, MFLG_SLOW, 0,
181     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
182     { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
183   },
184   {
185     MSG_WALLUSERS,
186     TOK_WALLUSERS,
187     0, MAXPARA, MFLG_SLOW, 0,
188     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
189     { m_unregistered, m_not_oper, ms_wallusers, mo_wallusers, m_ignore }
190   },
191   {
192     MSG_DESYNCH,
193     TOK_DESYNCH,
194     0, MAXPARA, MFLG_SLOW, 0,
195     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
196     { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
197   },
198   {
199     MSG_PING,
200     TOK_PING,
201     0, MAXPARA, MFLG_SLOW, 0,
202     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
203     { m_unregistered, m_ping, ms_ping, m_ping, m_ignore }
204   },
205   {
206     MSG_PONG,
207     TOK_PONG,
208     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
209     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
210     { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
211   },
212   {
213     MSG_ERROR,
214     TOK_ERROR,
215     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
216     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
217     { m_ignore, m_ignore, ms_error, m_ignore, m_ignore }
218   },
219   {
220     MSG_KILL,
221     TOK_KILL,
222     0, MAXPARA, MFLG_SLOW, 0,
223     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
224     { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
225   },
226   {
227     MSG_USER,
228     TOK_USER,
229     0, MAXPARA, MFLG_SLOW, 0,
230     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
231     { m_user, m_registered, m_ignore, m_registered, m_ignore }
232   },
233   {
234     MSG_AWAY,
235     TOK_AWAY,
236     0, MAXPARA, MFLG_SLOW, 0,
237     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
238     { m_unregistered, m_away, ms_away, m_away, m_ignore }
239   },
240   {
241     MSG_ISON,
242     TOK_ISON,
243     0, 1, MFLG_SLOW, 0,
244     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
245     { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
246   },
247   {
248     MSG_SERVER,
249     TOK_SERVER,
250     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
251     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
252     { mr_server, m_registered, ms_server, m_registered, m_ignore }
253   },
254   {
255     MSG_SQUIT,
256     TOK_SQUIT,
257     0, MAXPARA, MFLG_SLOW, 0,
258     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
259     { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
260   },
261   {
262     MSG_WHOIS,
263     TOK_WHOIS,
264     0, MAXPARA, MFLG_SLOW, 0,
265     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
266     { m_unregistered, m_whois, m_whois, m_whois, m_ignore }
267   },
268   {
269     MSG_WHO,
270     TOK_WHO,
271     0, MAXPARA, MFLG_SLOW, 0,
272     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
273     { m_unregistered, m_who, m_ignore, m_who, m_ignore }
274   },
275   {
276     MSG_WHOWAS,
277     TOK_WHOWAS,
278     0, MAXPARA, MFLG_SLOW, 0,
279     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
280     { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
281   },
282   {
283     MSG_LIST,
284     TOK_LIST,
285     0, MAXPARA, MFLG_SLOW, 0,
286     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
287     { m_unregistered, m_list, m_ignore, m_list, m_ignore }
288   },
289   {
290     MSG_NAMES,
291     TOK_NAMES,
292     0, MAXPARA, MFLG_SLOW, 0,
293     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
294     { m_unregistered, m_names, ms_names, m_names, m_ignore }
295   },
296   {
297     MSG_USERHOST,
298     TOK_USERHOST,
299     0, 1, MFLG_SLOW, 0,
300     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
301     { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
302   },
303   {
304     MSG_USERIP,
305     TOK_USERIP,
306     0, 1, MFLG_SLOW, 0,
307     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
308     { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
309   },
310   {
311     MSG_TRACE,
312     TOK_TRACE,
313     0, MAXPARA, MFLG_SLOW, 0,
314     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
315     { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
316   },
317   {
318     MSG_PASS,
319     TOK_PASS,
320     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
321     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
322     { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
323   },
324   {
325     MSG_LUSERS,
326     TOK_LUSERS,
327     0, MAXPARA, MFLG_SLOW, 0,
328     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
329     { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
330   },
331   {
332     MSG_TIME,
333     TOK_TIME,
334     0, MAXPARA, MFLG_SLOW, 0,
335     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
336     { m_unregistered, m_time, m_time, m_time, m_ignore }
337   },
338   {
339     MSG_SETTIME,
340     TOK_SETTIME,
341     0, MAXPARA, MFLG_SLOW, 0,
342     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
343     { m_unregistered, m_ignore, ms_settime, mo_settime, m_ignore }
344   },
345   {
346     MSG_RPING,
347     TOK_RPING,
348     0, MAXPARA, MFLG_SLOW, 0,
349     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
350     { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
351   },
352   {
353     MSG_RPONG,
354     TOK_RPONG,
355     0, MAXPARA, MFLG_SLOW, 0,
356     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
357     { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
358   },
359   {
360     MSG_OPER,
361     TOK_OPER,
362     0, MAXPARA, MFLG_SLOW, 0,
363     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
364     { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
365   },
366   {
367     MSG_CONNECT,
368     TOK_CONNECT,
369     0, MAXPARA, MFLG_SLOW, 0,
370     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
371     { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
372   },
373   {
374     MSG_MAP,
375     TOK_MAP,
376     0, MAXPARA, MFLG_SLOW, 0,
377     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
378     { m_unregistered, m_map, m_ignore, m_map, m_ignore }
379   },
380   {
381     MSG_VERSION,
382     TOK_VERSION,
383     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
384     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
385     { m_version, m_version, ms_version, m_version, m_ignore }
386   },
387   {
388     MSG_STATS,
389     TOK_STATS,
390     0, MAXPARA, MFLG_SLOW, 0,
391     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
392     { m_unregistered, m_stats, ms_stats, mo_stats, m_ignore }
393   },
394   {
395     MSG_LINKS,
396     TOK_LINKS,
397     0, MAXPARA, MFLG_SLOW, 0,
398     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
399     { m_unregistered, m_links, ms_links, m_links, m_ignore }
400   },
401   {
402     MSG_ADMIN,
403     TOK_ADMIN,
404     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
405     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
406     { m_admin, m_admin, ms_admin, m_admin, m_ignore }
407   },
408   {
409     MSG_HELP,
410     TOK_HELP,
411     0, MAXPARA, MFLG_SLOW, 0,
412     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
413     { m_unregistered, m_help, m_ignore, m_help, m_ignore }
414   },
415   {
416     MSG_INFO,
417     TOK_INFO,
418     0, MAXPARA, MFLG_SLOW, 0,
419     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
420     { m_unregistered, m_info, ms_info, mo_info, m_ignore }
421   },
422   {
423     MSG_MOTD,
424     TOK_MOTD,
425     0, MAXPARA, MFLG_SLOW, 0,
426     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
427     { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
428   },
429   {
430     MSG_CLOSE,
431     TOK_CLOSE,
432     0, MAXPARA, MFLG_SLOW, 0,
433     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
434     { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
435   },
436   {
437     MSG_SILENCE,
438     TOK_SILENCE,
439     0, MAXPARA, MFLG_SLOW, 0,
440     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
441     { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
442   },
443   {
444     MSG_GLINE,
445     TOK_GLINE,
446     0, MAXPARA, MFLG_SLOW, 0,
447     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
448     { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
449   },
450   {
451     MSG_JUPE,
452     TOK_JUPE,
453     0, MAXPARA, MFLG_SLOW, 0,
454     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
455     { m_unregistered, m_jupe, ms_jupe, mo_jupe, m_ignore }
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   {
556     MSG_PRIVS,
557     TOK_PRIVS,
558     0, MAXPARA, MFLG_SLOW, 0,
559     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
560     { m_unregistered, m_not_oper, m_ignore, mo_privs, m_ignore }
561   },
562   /* This command is an alias for QUIT during the unregistered part of
563    * of the server.  This is because someone jumping via a broken web
564    * proxy will send a 'POST' as their first command - which we will
565    * obviously disconnect them immediately for, stopping people abusing
566    * open gateways
567    */
568   {
569     MSG_POST,
570     TOK_POST,
571     0, MAXPARA, MFLG_SLOW, 0,
572     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
573     { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
574   },  
575   { 0 }
576 }; 
577
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   if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
924       handler != m_ping && handler != m_ignore)
925     cli_user(from)->last = CurrentTime;
926
927   return (*handler) (cptr, from, i, para);
928 }
929
930 int parse_server(struct Client *cptr, char *buffer, char *bufend)
931 {
932   struct Client*  from = cptr;
933   char*           ch = buffer;
934   char*           s;
935   int             len;
936   int             i;
937   int             numeric = 0;
938   int             paramcount;
939   struct Message* mptr;
940
941   Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
942
943   if (IsDead(cptr))
944     return 0;
945
946   para[0] = cli_name(from);
947
948   /*
949    * A server ALWAYS sends a prefix. When it starts with a ':' it's the
950    * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
951    * nick or server
952    */
953   if (*ch == ':')
954   {
955     /* Let para[0] point to the name of the sender */
956     para[0] = ch + 1;
957     if (!(ch = strchr(ch, ' ')))
958       return -1;
959     *ch++ = '\0';
960
961     /* And let `from' point to its client structure,
962        opps.. a server is _also_ a client --Nem */
963     from = FindClient(para[0]);
964
965     /*
966      * If the client corresponding to the
967      * prefix is not found. We must ignore it,
968      * it is simply a lagged message travelling
969      * upstream a SQUIT that removed the client
970      * --Run
971      */
972     if (!from)
973     {
974       Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
975           para[0], buffer, cli_name(cptr)));
976       ++ServerStats->is_unpf;
977       while (*ch == ' ')
978         ch++;
979       /*
980        * However, the only thing that MUST be
981        * allowed to travel upstream against an
982        * squit, is an SQUIT itself (the timestamp
983        * protects us from being used wrong)
984        */
985       if (ch[1] == 'Q')
986       {
987         para[0] = cli_name(cptr);
988         from = cptr;
989       }
990       else
991         return 0;
992     }
993     else if (cli_from(from) != cptr)
994     {
995       ++ServerStats->is_wrdi;
996       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
997           buffer, cli_name(cptr)));
998       return 0;
999     }
1000   }
1001   else {
1002     char numeric_prefix[6];
1003     int  i;
1004     for (i = 0; i < 5; ++i) {
1005       if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i])) {
1006         break;
1007       }
1008     }
1009     numeric_prefix[i] = '\0';
1010
1011     /*
1012      * We got a numeric nick as prefix
1013      * 1 or 2 character prefixes are from servers
1014      * 3 or 5 chars are from clients
1015      */
1016     if (0 == i) {
1017       protocol_violation(cptr,"Missing Prefix");
1018       from = cptr;
1019     }
1020     else if (' ' == ch[1] || ' ' == ch[2])
1021       from = FindNServer(numeric_prefix);
1022     else 
1023       from = findNUser(numeric_prefix);
1024
1025     do
1026     {
1027       ++ch;
1028     }
1029     while (*ch != ' ' && *ch);
1030
1031     /*
1032      * If the client corresponding to the
1033      * prefix is not found. We must ignore it,
1034      * it is simply a lagged message travelling
1035      * upstream a SQUIT that removed the client
1036      * --Run
1037      * There turned out to be other reasons that
1038      * a prefix is unknown, needing an upstream
1039      * KILL.  Also, next to an SQUIT we better
1040      * allow a KILL to pass too.
1041      * --Run
1042      */
1043     if (!from)
1044     {
1045       ServerStats->is_unpf++;
1046       while (*ch == ' ')
1047         ch++;
1048       if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1049         /* Only sent a KILL for a nick change */
1050       {
1051         struct Client *server;
1052         /* Kill the unknown numeric prefix upstream if
1053          * it's server still exists: */
1054         if ((server = FindNServer(numeric_prefix)) && cli_from(server) == cptr)
1055           sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1056                         numeric_prefix, cli_name(&me));
1057       }
1058       /*
1059        * Things that must be allowed to travel
1060        * upstream against an squit:
1061        */
1062       if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1063           (*ch == 'K' && ch[2] == 'L'))
1064         from = cptr;
1065       else
1066         return 0;
1067     }
1068
1069     /* Let para[0] point to the name of the sender */
1070     para[0] = cli_name(from);
1071
1072     if (cli_from(from) != cptr)
1073     {
1074       ServerStats->is_wrdi++;
1075       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1076           buffer, cli_name(cptr)));
1077       return 0;
1078     }
1079   }
1080
1081   while (*ch == ' ')
1082     ch++;
1083   if (*ch == '\0')
1084   {
1085     ServerStats->is_empt++;
1086     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1087         cli_name(cptr), cli_name(from)));
1088     return (-1);
1089   }
1090
1091   /*
1092    * Extract the command code from the packet.   Point s to the end
1093    * of the command code and calculate the length using pointer
1094    * arithmetic.  Note: only need length for numerics and *all*
1095    * numerics must have parameters and thus a space after the command
1096    * code. -avalon
1097    */
1098   s = strchr(ch, ' ');          /* s -> End of the command code */
1099   len = (s) ? (s - ch) : 0;
1100   if (len == 3 && IsDigit(*ch))
1101   {
1102     numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1103     paramcount = 2; /* destination, and the rest of it */
1104     ServerStats->is_num++;
1105     mptr = NULL;                /* Init. to avoid stupid compiler warning :/ */
1106   }
1107   else
1108   {
1109     if (s)
1110       *s++ = '\0';
1111
1112     /* Version      Receive         Send
1113      * 2.9          Long            Long
1114      * 2.10.0       Tkn/Long        Long
1115      * 2.10.10      Tkn/Long        Tkn
1116      * 2.10.20      Tkn             Tkn
1117      *
1118      * Clients/unreg servers always receive/
1119      * send long commands   -record
1120      */
1121
1122     /*
1123      * This is a server. Check the token command list.
1124      * -record!jegelhof@cloud9.net
1125      */
1126     mptr = msg_tree_parse(ch, &msg_tree_tok);
1127
1128 #if 1                           /* for 2.10.0/2.10.10 */
1129     /*
1130      * This code supports 2.9 and 2.10.0 sending long commands.
1131      * It makes more calls to ircd_strcmp() than the above
1132      * so it will be somewhat slower.
1133      */
1134     if (!mptr)
1135       mptr = msg_tree_parse(ch, &msg_tree_cmd);
1136 #endif /* 1 */
1137
1138     if (!mptr)
1139     {
1140       /*
1141        * Note: Give error message *only* to recognized
1142        * persons. It's a nightmare situation to have
1143        * two programs sending "Unknown command"'s or
1144        * equivalent to each other at full blast....
1145        * If it has got to person state, it at least
1146        * seems to be well behaving. Perhaps this message
1147        * should never be generated, though...   --msa
1148        * Hm, when is the buffer empty -- if a command
1149        * code has been found ?? -Armin
1150        */
1151 #ifdef DEBUGMODE
1152       if (buffer[0] != '\0')
1153       {
1154         Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1155               ch, get_client_name(cptr, HIDE_IP)));
1156       }
1157 #endif
1158       ServerStats->is_unco++;
1159       return (-1);
1160     }
1161
1162     paramcount = mptr->parameters;
1163     i = bufend - ((s) ? s : ch);
1164     mptr->bytes += i;
1165   }
1166   /*
1167    * Must the following loop really be so devious? On
1168    * surface it splits the message to parameters from
1169    * blank spaces. But, if paramcount has been reached,
1170    * the rest of the message goes into this last parameter
1171    * (about same effect as ":" has...) --msa
1172    */
1173
1174   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1175
1176   i = 0;
1177   if (s)
1178   {
1179     if (paramcount > MAXPARA)
1180       paramcount = MAXPARA;
1181     for (;;)
1182     {
1183       /*
1184        * Never "FRANCE " again!! ;-) Clean
1185        * out *all* blanks.. --msa
1186        */
1187       while (*s == ' ')
1188         *s++ = '\0';
1189
1190       if (*s == '\0')
1191         break;
1192       if (*s == ':')
1193       {
1194         /*
1195          * The rest is single parameter--can
1196          * include blanks also.
1197          */
1198         if (numeric)
1199           para[++i] = s; /* preserve the colon to make do_numeric happy */
1200         else
1201           para[++i] = s + 1;
1202         break;
1203       }
1204       para[++i] = s;
1205       if (i >= paramcount)
1206         break;
1207       for (; *s != ' ' && *s; s++);
1208     }
1209   }
1210   para[++i] = NULL;
1211   if (numeric)
1212     return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1213   mptr->count++;
1214
1215   return (*mptr->handlers[cli_handler(cptr)]) (cptr, from, i, para);
1216 }