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