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