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