Merge branch 'master' into KeepConn
[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 /** @file
21  * @brief Parse input from IRC clients and other servers.
22  * @version $Id: parse.c 1827 2007-08-14 03:02:24Z entrope $
23  */
24 #include "config.h"
25
26 #include "parse.h"
27 #include "client.h"
28 #include "channel.h"
29 #include "handlers.h"
30 #include "hash.h"
31 #include "ircd.h"
32 #include "ircd_alloc.h"
33 #include "ircd_chattr.h"
34 #include "ircd_features.h"
35 #include "ircd_log.h"
36 #include "ircd_reply.h"
37 #include "ircd_string.h"
38 #include "msg.h"
39 #include "numeric.h"
40 #include "numnicks.h"
41 #include "opercmds.h"
42 #include "querycmds.h"
43 #include "res.h"
44 #include "s_bsd.h"
45 #include "s_conf.h"
46 #include "s_debug.h"
47 #include "s_misc.h"
48 #include "s_numeric.h"
49 #include "s_user.h"
50 #include "send.h"
51 #include "struct.h"
52 #include "sys.h"
53 #include "whocmds.h"
54 #include "whowas.h"
55
56 /* #include <assert.h> -- Now using assert in ircd_log.h */
57 #include <string.h>
58 #include <stdlib.h>
59
60 /*
61  * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
62  * Adapted to Undernet, adding token support, etc by comstud 10/06/97
63  *
64  * completely rewritten June 2, 2003 - Dianora
65  *
66  * This has always just been a trie. Look at volume III of Knuth ACP
67  *
68  *
69  * ok, you start out with an array of pointers, each one corresponds
70  * to a letter at the current position in the command being examined.
71  *
72  * so roughly you have this for matching 'trie' or 'tie'
73  *
74  * 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i'
75  *   -> [MessageTree *] -> [MessageTree *] -> 'e' and matches
76  *
77  *                               'i' -> [MessageTree *] -> 'e' and matches
78  */
79
80 /** Number of children under a trie node. */
81 #define MAXPTRLEN       32      /* Must be a power of 2, and
82                                  * larger than 26 [a-z]|[A-Z]
83                                  * its used to allocate the set
84                                  * of pointers at each node of the tree
85                                  * There are MAXPTRLEN pointers at each node.
86                                  * Obviously, there have to be more pointers
87                                  * Than ASCII letters. 32 is a nice number
88                                  * since there is then no need to shift
89                                  * 'A'/'a' to base 0 index, at the expense
90                                  * of a few never used pointers. For a small
91                                  * parser like this, this is a good compromise
92                                  * and does make it somewhat faster.
93                                  *
94                                  * - Dianora
95                                  */
96
97 /** Node in the command lookup trie. */
98 struct MessageTree {
99   struct Message *msg; /**< Message (if any) if the string ends now. */
100   struct MessageTree *pointers[MAXPTRLEN]; /**< Child nodes for each letter. */
101 };
102
103 /** Root of command lookup trie. */
104 static struct MessageTree msg_tree;
105 static struct MessageTree tok_tree;
106
107 /** Array of all supported commands. */
108 struct Message msgtab[] = {
109   {
110     MSG_PRIVATE,
111     TOK_PRIVATE,
112     0, MAXPARA, MFLG_SLOW, 0, NULL,
113     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
114     { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
115   },
116   {
117     MSG_NICK,
118     TOK_NICK,
119     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
120     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
121     { m_nick, m_nick, ms_nick, m_nick, m_ignore }
122   },
123   {
124     MSG_NOTICE,
125     TOK_NOTICE,
126     0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0, NULL,
127     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
128     { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
129   },
130   {
131     MSG_WALLCHOPS,
132     TOK_WALLCHOPS,
133     0, MAXPARA, MFLG_SLOW, 0, NULL,
134     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
135     { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
136   },
137   {
138     MSG_WALLVOICES,
139     TOK_WALLVOICES,
140     0, MAXPARA, MFLG_SLOW, 0, NULL,
141     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
142     { m_unregistered, m_wallvoices, ms_wallvoices, m_wallvoices, m_ignore }
143   },
144   {
145     MSG_CPRIVMSG,
146     TOK_CPRIVMSG,
147     0, MAXPARA, MFLG_SLOW, 0, NULL,
148     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
149     { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
150   },
151   {
152     MSG_CNOTICE,
153     TOK_CNOTICE,
154     0, MAXPARA, MFLG_SLOW, 0, NULL,
155     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
156     { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
157   },
158   {
159     MSG_JOIN,
160     TOK_JOIN,
161     0, MAXPARA, MFLG_SLOW, 0, NULL,
162     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
163     { m_unregistered, m_join, ms_join, m_join, m_ignore }
164   },
165   {
166     MSG_MODE,
167     TOK_MODE,
168     0, MAXPARA, MFLG_SLOW, 0, NULL,
169     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
170     { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
171   },
172   {
173     MSG_BURST,
174     TOK_BURST,
175     0, MAXPARA, MFLG_SLOW, 0, NULL,
176     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
177     { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
178   },
179   {
180     MSG_CREATE,
181     TOK_CREATE,
182     0, MAXPARA, MFLG_SLOW, 0, NULL,
183     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
184     { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
185   },
186   {
187     MSG_DESTRUCT,
188     TOK_DESTRUCT,
189     0, MAXPARA, MFLG_SLOW, 0, NULL,
190     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
191     { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
192   },
193   {
194     MSG_QUIT,
195     TOK_QUIT,
196     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
197     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
198     { m_quit, m_quit, ms_quit, m_quit, m_ignore }
199   },
200   {
201     MSG_PART,
202     TOK_PART,
203     0, MAXPARA, MFLG_SLOW, 0, NULL,
204     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
205     { m_unregistered, m_part, ms_part, m_part, m_ignore }
206   },
207   {
208     MSG_TOPIC,
209     TOK_TOPIC,
210     0, MAXPARA, MFLG_SLOW, 0, NULL,
211     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
212     { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
213   },
214   {
215     MSG_INVITE,
216     TOK_INVITE,
217     0, MAXPARA, MFLG_SLOW, 0, NULL,
218     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
219     { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
220   },
221   {
222     MSG_UNINVITE,
223     TOK_UNINVITE,
224     0, MAXPARA, MFLG_SLOW, 0, NULL,
225     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
226     { m_unregistered, m_uninvite, ms_uninvite, m_uninvite, m_ignore }
227   },
228   {
229     MSG_KICK,
230     TOK_KICK,
231     0, MAXPARA, MFLG_SLOW, 0, NULL,
232     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
233     { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
234   },
235   {
236     MSG_WALLOPS,
237     TOK_WALLOPS,
238     0, MAXPARA, MFLG_SLOW, 0, NULL,
239     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
240     { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
241   },
242   {
243     MSG_WALLUSERS,
244     TOK_WALLUSERS,
245     0, MAXPARA, MFLG_SLOW, 0, NULL,
246     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
247     { m_unregistered, m_not_oper, ms_wallusers, mo_wallusers, m_ignore }
248   },
249   {
250     MSG_DESYNCH,
251     TOK_DESYNCH,
252     0, MAXPARA, MFLG_SLOW, 0, NULL,
253     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
254     { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
255   },
256   {
257     MSG_PING,
258     TOK_PING,
259     0, MAXPARA, MFLG_SLOW, 0, NULL,
260     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
261     { m_unregistered, m_ping, ms_ping, mo_ping, m_ignore }
262   },
263   {
264     MSG_PONG,
265     TOK_PONG,
266     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
267     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
268     { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
269   },
270   {
271     MSG_ERROR,
272     TOK_ERROR,
273     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
274     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
275     { mr_error, m_ignore, ms_error, m_ignore, m_ignore }
276   },
277   {
278     MSG_KILL,
279     TOK_KILL,
280     0, MAXPARA, MFLG_SLOW, 0, NULL,
281     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
282     { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
283   },
284   {
285     MSG_USER,
286     TOK_USER,
287     0, MAXPARA, MFLG_SLOW, 0, NULL,
288     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
289     { m_user, m_registered, m_ignore, m_registered, m_ignore }
290   },
291   {
292     MSG_AWAY,
293     TOK_AWAY,
294     0, MAXPARA, MFLG_SLOW, 0, NULL,
295     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
296     { m_unregistered, m_away, ms_away, m_away, m_ignore }
297   },
298   {
299     MSG_ISON,
300     TOK_ISON,
301     0, 1, MFLG_SLOW, 0, NULL,
302     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
303     { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
304   },
305   {
306     MSG_SERVER,
307     TOK_SERVER,
308     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
309     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
310     { mr_server, m_registered, ms_server, m_registered, m_ignore }
311   },
312   {
313     MSG_SQUIT,
314     TOK_SQUIT,
315     0, MAXPARA, MFLG_SLOW, 0, NULL,
316     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
317     { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
318   },
319   {
320     MSG_WHOIS,
321     TOK_WHOIS,
322     0, MAXPARA, MFLG_SLOW, 0, NULL,
323     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
324     { m_unregistered, m_whois, ms_whois, m_whois, m_ignore }
325   },
326   {
327     MSG_WHO,
328     TOK_WHO,
329     0, MAXPARA, MFLG_SLOW, 0, NULL,
330     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
331     { m_unregistered, m_who, m_ignore, m_who, m_ignore }
332   },
333   {
334     MSG_WHOWAS,
335     TOK_WHOWAS,
336     0, MAXPARA, MFLG_SLOW, 0, NULL,
337     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
338     { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
339   },
340   {
341     MSG_LIST,
342     TOK_LIST,
343     0, MAXPARA, MFLG_SLOW, 0, NULL,
344     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
345     { m_unregistered, m_list, m_ignore, m_list, m_ignore }
346   },
347   {
348     MSG_NAMES,
349     TOK_NAMES,
350     0, MAXPARA, MFLG_SLOW, 0, NULL,
351     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
352     { m_unregistered, m_names, m_names, m_names, m_ignore }
353   },
354   {
355     MSG_USERHOST,
356     TOK_USERHOST,
357     0, 1, MFLG_SLOW, 0, NULL,
358     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
359     { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
360   },
361   {
362     MSG_USERIP,
363     TOK_USERIP,
364     0, 1, MFLG_SLOW, 0, NULL,
365     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
366     { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
367   },
368   {
369     MSG_TRACE,
370     TOK_TRACE,
371     0, MAXPARA, MFLG_SLOW, 0, NULL,
372     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
373     { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
374   },
375   {
376     MSG_PASS,
377     TOK_PASS,
378     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
379     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
380     { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
381   },
382   {
383     MSG_LUSERS,
384     TOK_LUSERS,
385     0, MAXPARA, MFLG_SLOW, 0, NULL,
386     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
387     { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
388   },
389   {
390     MSG_TIME,
391     TOK_TIME,
392     0, MAXPARA, MFLG_SLOW, 0, NULL,
393     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
394     { m_unregistered, m_time, m_time, m_time, m_ignore }
395   },
396   {
397     MSG_SETTIME,
398     TOK_SETTIME,
399     0, MAXPARA, MFLG_SLOW, 0, NULL,
400     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
401     { m_unregistered, m_not_oper, ms_settime, mo_settime, m_ignore }
402   },
403   {
404     MSG_RPING,
405     TOK_RPING,
406     0, MAXPARA, MFLG_SLOW, 0, NULL,
407     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
408     { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
409   },
410   {
411     MSG_RPONG,
412     TOK_RPONG,
413     0, MAXPARA, MFLG_SLOW, 0, NULL,
414     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
415     { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
416   },
417   {
418     MSG_OPER,
419     TOK_OPER,
420     0, MAXPARA, MFLG_SLOW, 0, NULL,
421     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
422     { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
423   },
424   {
425     MSG_CONNECT,
426     TOK_CONNECT,
427     0, MAXPARA, MFLG_SLOW, 0, NULL,
428     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
429     { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
430   },
431   {
432     MSG_MAP,
433     TOK_MAP,
434     0, MAXPARA, MFLG_SLOW, 0, NULL,
435     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
436     { m_unregistered, m_map, m_ignore, m_map, m_ignore }
437   },
438   {
439     MSG_VERSION,
440     TOK_VERSION,
441     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
442     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
443     { m_version, m_version, ms_version, mo_version, m_ignore }
444   },
445   {
446     MSG_STATS,
447     TOK_STATS,
448     0, MAXPARA, MFLG_SLOW, 0, NULL,
449     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
450     { m_unregistered, m_stats, m_stats, m_stats, m_ignore }
451   },
452   {
453     MSG_LINKS,
454     TOK_LINKS,
455     0, MAXPARA, MFLG_SLOW, 0, NULL,
456     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
457     { m_unregistered, m_links, ms_links, m_links, m_ignore }
458   },
459   {
460     MSG_ADMIN,
461     TOK_ADMIN,
462     0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,  NULL,
463     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
464     { m_admin, m_admin, ms_admin, mo_admin, m_ignore }
465   },
466   {
467     MSG_HELP,
468     TOK_HELP,
469     0, MAXPARA, MFLG_SLOW, 0, NULL,
470     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
471     { m_unregistered, m_help, m_ignore, m_help, m_ignore }
472   },
473   {
474     MSG_INFO,
475     TOK_INFO,
476     0, MAXPARA, MFLG_SLOW, 0, NULL,
477     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
478     { m_unregistered, m_info, ms_info, mo_info, m_ignore }
479   },
480   {
481     MSG_MOTD,
482     TOK_MOTD,
483     0, MAXPARA, MFLG_SLOW, 0, NULL,
484     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
485     { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
486   },
487   {
488     MSG_CLOSE,
489     TOK_CLOSE,
490     0, MAXPARA, MFLG_SLOW, 0, NULL,
491     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
492     { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
493   },
494   {
495     MSG_SILENCE,
496     TOK_SILENCE,
497     0, MAXPARA, MFLG_SLOW, 0, NULL,
498     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
499     { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
500   },
501   {
502     MSG_GLINE,
503     TOK_GLINE,
504     0, MAXPARA,         0, 0, NULL,
505     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
506     { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
507   },
508   {
509     MSG_JUPE,
510     TOK_JUPE,
511     0, MAXPARA, MFLG_SLOW, 0, NULL,
512     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
513     { m_unregistered, m_not_oper, ms_jupe, mo_jupe, m_ignore }
514   },
515   {
516     MSG_OPMODE,
517     TOK_OPMODE,
518     0, MAXPARA, MFLG_SLOW, 0, NULL,
519     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
520     { m_unregistered, mo_opmode, ms_opmode, mo_opmode, m_ignore }
521   },
522   {
523     MSG_CLEARMODE,
524     TOK_CLEARMODE,
525     0, MAXPARA, MFLG_SLOW, 0, NULL,
526     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
527     { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
528   },
529   {
530     MSG_UPING,
531     TOK_UPING,
532     0, MAXPARA, MFLG_SLOW, 0, NULL,
533     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
534     { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
535   },
536   {
537     MSG_END_OF_BURST,
538     TOK_END_OF_BURST,
539     0, MAXPARA, MFLG_SLOW, 0, NULL,
540     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
541     { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
542   },
543   {
544     MSG_END_OF_BURST_ACK,
545     TOK_END_OF_BURST_ACK,
546     0, MAXPARA, MFLG_SLOW, 0, NULL,
547     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
548     { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
549   },
550   {
551     MSG_HASH,
552     TOK_HASH,
553     0, MAXPARA, MFLG_SLOW, 0, NULL,
554     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
555     { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
556   },
557   {
558     MSG_REHASH,
559     TOK_REHASH,
560     0, MAXPARA, MFLG_SLOW, 0, NULL,
561     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
562     { m_unregistered, m_not_oper, ms_rehash, mo_rehash, m_ignore }
563   },
564   {
565     MSG_RESTART,
566     TOK_RESTART,
567     0, MAXPARA, MFLG_SLOW, 0, NULL,
568     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
569     { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
570   },
571   {
572     MSG_DIE,
573     TOK_DIE,
574     0, MAXPARA, MFLG_SLOW, 0, NULL,
575     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
576     { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
577   },
578   {
579     MSG_PROTO,
580     TOK_PROTO,
581     0, MAXPARA, MFLG_SLOW, 0, NULL,
582     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
583     { m_proto, m_proto, m_proto, m_proto, m_ignore }
584   },
585   {
586     MSG_SET,
587     TOK_SET,
588     0, MAXPARA, MFLG_SLOW, 0, NULL,
589     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
590     { m_unregistered, m_not_oper, m_ignore, mo_set, m_ignore }
591   },
592   {
593     MSG_RESET,
594     TOK_RESET,
595     0, MAXPARA, MFLG_SLOW, 0, NULL,
596     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
597     { m_unregistered, m_not_oper, m_ignore, mo_reset, m_ignore }
598   },
599   {
600     MSG_GET,
601     TOK_GET,
602     0, MAXPARA, MFLG_SLOW, 0, NULL,
603     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
604     { m_unregistered, m_not_oper, m_ignore, mo_get, m_ignore }
605   },
606   {
607     MSG_PRIVS,
608     TOK_PRIVS,
609     0, MAXPARA, MFLG_SLOW, 0, NULL,
610     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
611     { m_unregistered, m_privs, ms_privs, m_privs, m_ignore }
612   },
613   {
614     MSG_ACCOUNT,
615     TOK_ACCOUNT,
616     0, MAXPARA, MFLG_SLOW, 0, NULL,
617     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
618     { m_ignore, m_ignore, ms_account, m_ignore, m_ignore }
619   },
620   {
621     MSG_RECOVER,
622     TOK_RECOVER,
623     0, MAXPARA, MFLG_SLOW, 0, NULL,
624     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
625     { m_unregistered, m_recover, m_ignore, m_recover, m_ignore }
626   },
627   {
628     MSG_ZOMBIE,
629     TOK_ZOMBIE,
630     0, MAXPARA, MFLG_SLOW, 0, NULL,
631     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
632     { m_ignore, m_ignore, ms_zombie, m_ignore, m_ignore }
633   },
634   {
635     MSG_UNZOMBIE,
636     TOK_UNZOMBIE,
637     0, MAXPARA, MFLG_SLOW, 0, NULL,
638     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
639     { m_ignore, m_ignore, ms_unzombie, m_ignore, m_ignore }
640   },
641   {
642     MSG_ASLL,
643     TOK_ASLL,
644     0, MAXPARA, MFLG_SLOW, 0, NULL,
645     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
646     { m_ignore, m_not_oper, ms_asll, mo_asll, m_ignore }
647    },
648 #if WE_HAVE_A_REAL_CAPABILITY_NOW
649   {
650     MSG_CAP,
651     TOK_CAP,
652     0, MAXPARA, 0, 0, NULL,
653     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
654     { m_cap, m_cap, m_ignore, m_cap, m_ignore }
655   },
656 #endif
657   {
658     MSG_FAKEHOST,
659     TOK_FAKEHOST,
660     0, MAXPARA, MFLG_SLOW, 0, NULL,
661     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
662     { m_ignore, m_not_oper, ms_fakehost, m_fakehost, m_ignore }
663   },
664   {
665     MSG_FAKEHOST2,
666     TOK_FAKEHOST2,
667     0, MAXPARA, MFLG_SLOW, 0, NULL,
668     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
669     { m_ignore, m_ignore, ms_fakehost2, m_ignore, m_ignore }
670   },
671   {
672     MSG_FAKEHOST_OLD,
673     TOK_FAKEHOST_OLD,
674     0, MAXPARA, MFLG_SLOW, 0, NULL,
675     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
676     { m_ignore, m_not_oper, ms_fakehost_old, m_fakehost, m_ignore }
677   },
678   {
679     MSG_HIDEHOST,
680     TOK_HIDEHOST,
681     0, MAXPARA, MFLG_SLOW, 0, NULL,
682     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
683     { m_ignore, m_ignore, ms_hidehost, m_ignore, m_ignore }
684    },
685   {
686     MSG_SVSMODE,
687     TOK_SVSMODE,
688     0, MAXPARA, MFLG_SLOW, 0, NULL,
689     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
690     { m_ignore, m_ignore, ms_svsmode, m_svsmode, m_ignore }
691   },
692   {
693     MSG_SVSNICK,
694     TOK_SVSNICK,
695     0, MAXPARA, MFLG_SLOW, 0, NULL,
696     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
697     { m_ignore, m_ignore, ms_svsnick, m_svsnick, m_ignore }
698   },
699   {
700     MSG_SVSNICK_OLD,
701     TOK_SVSNICK_OLD,
702     0, MAXPARA, MFLG_SLOW, 0, NULL,
703     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
704     { m_ignore, m_ignore, ms_svsnick_old, m_svsnick, m_ignore }
705   },
706   {
707     MSG_SVSJOIN,
708     TOK_SVSJOIN,
709     0, MAXPARA, MFLG_SLOW, 0, NULL,
710     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
711     { m_ignore, m_ignore, ms_svsjoin, m_svsjoin, m_ignore }
712   },
713   {
714     MSG_SVSPART,
715     TOK_SVSPART,
716     0, MAXPARA, MFLG_SLOW, 0, NULL,
717     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
718     { m_ignore, m_ignore, ms_svspart, m_ignore, m_ignore }
719   },
720   {
721     MSG_WEBIRC,
722     TOK_WEBIRC,
723     0, MAXPARA, MFLG_SLOW, 0, NULL,
724     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
725     { m_webirc, m_registered, m_ignore, m_registered, m_ignore }
726   },
727   {
728     MSG_RELAY,
729     TOK_RELAY,
730     0, MAXPARA, MFLG_SLOW, 0, NULL,
731     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
732     { m_ignore, m_ignore, ms_relay, m_ignore, m_ignore }
733   },
734   {
735     MSG_CHECK,
736     TOK_CHECK,
737     0, MAXPARA, MFLG_SLOW, 0, NULL,
738     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
739     { m_ignore, m_not_oper, m_ignore, mo_check, m_ignore }
740   },
741   /* This command is an alias for QUIT during the unregistered part of
742    * of the server.  This is because someone jumping via a broken web
743    * proxy will send a 'POST' as their first command - which we will
744    * obviously disconnect them immediately for, stopping people abusing
745    * open gateways
746    */
747   {
748     MSG_POST,
749     TOK_POST,
750     0, MAXPARA, MFLG_SLOW, 0, NULL,
751     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
752     { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
753   },
754   { 0 }
755 };
756
757 /** Array of command parameters. */
758 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
759
760
761 /** Add a message to the lookup trie.
762  * @param[in,out] mtree_p Trie node to insert under.
763  * @param[in] msg_p Message to insert.
764  * @param[in] cmd Text of command to insert.
765  */
766 void
767 add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, char *cmd)
768 {
769   struct MessageTree *ntree_p;
770
771   if (*cmd == '\0')
772   {
773     mtree_p->msg = msg_p;
774     return;
775   }
776
777   if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL)
778   {
779     add_msg_element(ntree_p, msg_p, cmd+1);
780   }
781   else
782   {
783     ntree_p = (struct MessageTree *)MyCalloc(sizeof(struct MessageTree), 1);
784     mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = ntree_p;
785     add_msg_element(ntree_p, msg_p, cmd+1);
786   }
787 }
788
789 /** Remove a message from the lookup trie.
790  * @param[in,out] mtree_p Trie node to remove command from.
791  * @param[in] cmd Text of command to remove.
792  */
793 struct MessageTree *
794 del_msg_element(struct MessageTree *mtree_p, char *cmd)
795 {
796   int slot = *cmd & (MAXPTRLEN-1);
797
798   /* Either remove leaf message or from appropriate child. */
799   if (*cmd == '\0')
800     mtree_p->msg = NULL;
801   else
802     mtree_p->pointers[slot] = del_msg_element(mtree_p->pointers[slot], cmd + 1);
803
804   /* If current message or any child still exists, keep this node. */
805   if (mtree_p->msg)
806     return mtree_p;
807   for (slot = 0; slot < MAXPTRLEN; ++slot)
808     if (mtree_p->pointers[slot])
809       return mtree_p;
810
811   /* Otherwise, if we're not a root node, free it and return null. */
812   if (mtree_p != &msg_tree && mtree_p != &tok_tree)
813     MyFree(mtree_p);
814   return NULL;
815 }
816
817 /** Initialize the message lookup trie with all known commands. */
818 void
819 initmsgtree(void)
820 {
821   int i;
822
823   memset(&msg_tree, 0, sizeof(msg_tree));
824   memset(&tok_tree, 0, sizeof(tok_tree));
825
826   for (i = 0; msgtab[i].cmd != NULL ; i++)
827   {
828     add_msg_element(&msg_tree, &msgtab[i], msgtab[i].cmd);
829     add_msg_element(&tok_tree, &msgtab[i], msgtab[i].tok);
830   }
831 }
832
833 /** Look up a command in the message trie.
834  * @param cmd Text of command to look up.
835  * @param root Root of message trie.
836  * @return Pointer to matching message, or NULL if non exists.
837  */
838 static struct Message *
839 msg_tree_parse(char *cmd, struct MessageTree *root)
840 {
841   struct MessageTree *mtree;
842
843   for (mtree = root; mtree; mtree = mtree->pointers[(*cmd++) & (MAXPTRLEN-1)]) {
844       if (*cmd == '\0' && mtree->msg)
845           return mtree->msg;
846       else if (!IsAlpha(*cmd))
847           return NULL;
848   }
849   return NULL;
850 }
851
852 /** Registers a service mapping to the pseudocommand handler.
853  * @param[in] map Service mapping to add.
854  * @return Non-zero on success; zero if a command already used the name.
855  */
856 int register_mapping(struct s_map *map)
857 {
858   struct Message *msg;
859
860   if (msg_tree_parse(map->command, &msg_tree))
861     return 0;
862
863   msg = (struct Message *)MyMalloc(sizeof(struct Message));
864   msg->cmd = map->command;
865   msg->tok = map->command;
866   msg->count = 0;
867   msg->parameters = 2;
868   msg->flags = MFLG_EXTRA;
869   if (!(map->flags & SMAP_FAST))
870     msg->flags |= MFLG_SLOW;
871   msg->bytes = 0;
872   msg->extra = map;
873
874   msg->handlers[UNREGISTERED_HANDLER] = m_ignore;
875   msg->handlers[CLIENT_HANDLER] = m_pseudo;
876   msg->handlers[SERVER_HANDLER] = m_ignore;
877   msg->handlers[OPER_HANDLER]   = m_pseudo;
878   msg->handlers[SERVICE_HANDLER] = m_ignore;
879
880   add_msg_element(&msg_tree, msg, msg->cmd);
881   map->msg = msg;
882
883   return 1;
884 }
885
886 /** Removes a service mapping.
887  * @param[in] map Service mapping to remove.
888  * @return Non-zero on success; zero if no command used the name.
889  */
890 int unregister_mapping(struct s_map *map)
891 {
892   if (!msg_tree_parse(map->command, &msg_tree))
893   {
894     /* This simply should never happen. */
895     assert(0);
896     return 0;
897   }
898
899   del_msg_element(&msg_tree, map->msg->cmd);
900
901   map->msg->extra = NULL;
902   MyFree(map->msg);
903   map->msg = NULL;
904
905   return 1;
906 }
907
908 /** Parse a line of data from a user.
909  * NOTE: parse_*() should not be called recursively by any other
910  * functions!
911  * @param[in] cptr Client that sent the data.
912  * @param[in] buffer Start of input line.
913  * @param[in] bufend End of input line.
914  * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
915  * handler returns it.
916  */
917 int
918 parse_client(struct Client *cptr, char *buffer, char *bufend)
919 {
920   struct Client*  from = cptr;
921   char*           ch;
922   char*           s;
923   int             i;
924   int             paramcount;
925   int             noprefix = 0;
926   struct Message* mptr;
927   MessageHandler  handler = 0;
928
929   Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
930
931   if (IsDead(cptr))
932     return 0;
933
934   para[0] = cli_name(from);
935   for (ch = buffer; *ch == ' '; ch++);  /* Eat leading spaces */
936   if (*ch == ':')               /* Is any client doing this ? */
937   {
938     for (++ch; *ch && *ch != ' '; ++ch)
939       ; /* Ignore sender prefix from client */
940     while (*ch == ' ')
941       ch++;                     /* Advance to command */
942   }
943   else
944     noprefix = 1;
945   if (*ch == '\0')
946   {
947     ServerStats->is_empt++;
948     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
949         cli_name(cptr), cli_name(from)));
950     return (-1);
951   }
952
953   if ((s = strchr(ch, ' ')))
954     *s++ = '\0';
955
956   if ((mptr = msg_tree_parse(ch, &msg_tree)) == NULL)
957   {
958     /*
959      * Note: Give error message *only* to recognized
960      * persons. It's a nightmare situation to have
961      * two programs sending "Unknown command"'s or
962      * equivalent to each other at full blast....
963      * If it has got to person state, it at least
964      * seems to be well behaving. Perhaps this message
965      * should never be generated, though...  --msa
966      * Hm, when is the buffer empty -- if a command
967      * code has been found ?? -Armin
968      */
969     if (buffer[0] != '\0')
970     {
971       if (IsUser(from)) {
972            struct Client *acptr;
973            if(feature_bool(FEAT_UNKNOWN_CMD_ENABLE) && feature_str(FEAT_UNKNOWN_CMD_TARGET) && (acptr = FindUser(feature_str(FEAT_UNKNOWN_CMD_TARGET))) && IsNetServ(acptr) && IsService(cli_user(acptr)->server)) {
974             sendcmdto_one(&me, CMD_RELAY, acptr, "%C UC %C %s :%s", acptr, from, ch, s);
975            } else {
976             send_reply(from, ERR_UNKNOWNCOMMAND, ch);
977            }
978           }
979       Debug((DEBUG_ERROR, "Unknown (%s) from %s",
980             ch, get_client_name(cptr, HIDE_IP)));
981     }
982     ServerStats->is_unco++;
983     return (-1);
984   }
985
986   paramcount = mptr->parameters;
987   i = bufend - ((s) ? s : ch);
988   mptr->bytes += i;
989   if ((mptr->flags & MFLG_SLOW) || (!IsAnOper(cptr) && !HasPriv(cptr, PRIV_FLOOD))) {
990     if(HasPriv(cptr, PRIV_HALFFLOOD))
991       cli_since(cptr) += 1;
992     else
993       cli_since(cptr) += (2 + i / 120);
994   }
995
996   /*
997    * Allow only 1 msg per 2 seconds
998    * (on average) to prevent dumping.
999    * to keep the response rate up,
1000    * bursts of up to 5 msgs are allowed
1001    * -SRB
1002    */
1003
1004   /*
1005    * Must the following loop really be so devious? On
1006    * surface it splits the message to parameters from
1007    * blank spaces. But, if paramcount has been reached,
1008    * the rest of the message goes into this last parameter
1009    * (about same effect as ":" has...) --msa
1010    */
1011
1012   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1013
1014   if (mptr->flags & MFLG_EXTRA) {
1015     /* This is a horrid kludge to avoid changing the command handler
1016      * argument list. */
1017     para[1] = (char*)mptr->extra;
1018     i = 1;
1019   } else {
1020     i = 0;
1021   }
1022   if (s)
1023   {
1024     if (paramcount > MAXPARA)
1025       paramcount = MAXPARA;
1026     for (;;)
1027     {
1028       /*
1029        * Never "FRANCE " again!! ;-) Clean
1030        * out *all* blanks.. --msa
1031        */
1032       while (*s == ' ')
1033         *s++ = '\0';
1034
1035       if (*s == '\0')
1036         break;
1037       if (*s == ':')
1038       {
1039         /*
1040          * The rest is single parameter--can
1041          * include blanks also.
1042          */
1043         para[++i] = s + 1;
1044         break;
1045       }
1046       para[++i] = s;
1047       if (i >= paramcount)
1048         break;
1049       for (; *s != ' ' && *s; s++);
1050     }
1051   }
1052   para[++i] = NULL;
1053   ++mptr->count;
1054
1055   handler = mptr->handlers[cli_handler(cptr)];
1056   assert(0 != handler);
1057
1058   if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
1059       handler != m_ping && handler != m_ignore)
1060     cli_user(from)->last = CurrentTime;
1061
1062   return (*handler) (cptr, from, i, para);
1063 }
1064
1065 int
1066 parse_simul_client(struct Client *cptr, char *buffer)
1067 {
1068   struct Client*  from = cptr;
1069   char*           ch;
1070   char*           s;
1071   int             i;
1072   int             paramcount;
1073   int             noprefix = 0;
1074   struct Message* mptr;
1075   MessageHandler  handler = 0;
1076
1077   Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
1078
1079   if (IsDead(cptr))
1080     return 0;
1081
1082   para[0] = cli_name(from);
1083   for (ch = buffer; *ch == ' '; ch++);  /* Eat leading spaces */
1084   if (*ch == ':')               /* Is any client doing this ? */
1085   {
1086     for (++ch; *ch && *ch != ' '; ++ch)
1087       ; /* Ignore sender prefix from client */
1088     while (*ch == ' ')
1089       ch++;                     /* Advance to command */
1090   }
1091   else
1092     noprefix = 1;
1093   if (*ch == '\0')
1094   {
1095     ServerStats->is_empt++;
1096     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1097         cli_name(cptr), cli_name(from)));
1098     return (-1);
1099   }
1100
1101   if ((s = strchr(ch, ' ')))
1102     *s++ = '\0';
1103
1104   if ((mptr = msg_tree_parse(ch, &msg_tree)) == NULL)
1105   {
1106     /*
1107      * Note: Give error message *only* to recognized
1108      * persons. It's a nightmare situation to have
1109      * two programs sending "Unknown command"'s or
1110      * equivalent to each other at full blast....
1111      * If it has got to person state, it at least
1112      * seems to be well behaving. Perhaps this message
1113      * should never be generated, though...  --msa
1114      * Hm, when is the buffer empty -- if a command
1115      * code has been found ?? -Armin
1116      */
1117     if (buffer[0] != '\0')
1118     {
1119       Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1120             ch, get_client_name(cptr, HIDE_IP)));
1121     }
1122     ServerStats->is_unco++;
1123     return (-1);
1124   }
1125
1126   paramcount = mptr->parameters;
1127
1128   /*
1129    * Allow only 1 msg per 2 seconds
1130    * (on average) to prevent dumping.
1131    * to keep the response rate up,
1132    * bursts of up to 5 msgs are allowed
1133    * -SRB
1134    */
1135
1136   /*
1137    * Must the following loop really be so devious? On
1138    * surface it splits the message to parameters from
1139    * blank spaces. But, if paramcount has been reached,
1140    * the rest of the message goes into this last parameter
1141    * (about same effect as ":" has...) --msa
1142    */
1143
1144   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1145
1146   if (mptr->flags & MFLG_EXTRA) {
1147     /* This is a horrid kludge to avoid changing the command handler
1148      * argument list. */
1149     para[1] = (char*)mptr->extra;
1150     i = 1;
1151   } else {
1152     i = 0;
1153   }
1154   if (s)
1155   {
1156     if (paramcount > MAXPARA)
1157       paramcount = MAXPARA;
1158     for (;;)
1159     {
1160       /*
1161        * Never "FRANCE " again!! ;-) Clean
1162        * out *all* blanks.. --msa
1163        */
1164       while (*s == ' ')
1165         *s++ = '\0';
1166
1167       if (*s == '\0')
1168         break;
1169       if (*s == ':')
1170       {
1171         /*
1172          * The rest is single parameter--can
1173          * include blanks also.
1174          */
1175         para[++i] = s + 1;
1176         break;
1177       }
1178       para[++i] = s;
1179       if (i >= paramcount)
1180         break;
1181       for (; *s != ' ' && *s; s++);
1182     }
1183   }
1184   para[++i] = NULL;
1185   ++mptr->count;
1186
1187   handler = mptr->handlers[cli_handler(cptr)];
1188   assert(0 != handler);
1189
1190   if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
1191       handler != m_ping && handler != m_ignore)
1192     cli_user(from)->last = CurrentTime;
1193
1194   return (*handler) (cptr, from, i, para);
1195 }
1196
1197
1198
1199 /** Parse a line of data from a server.
1200  * @param[in] cptr Client that sent the data.
1201  * @param[in] buffer Start of input line.
1202  * @param[in] bufend End of input line.
1203  * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
1204  * handler returns it.
1205  */
1206 int parse_server(struct Client *cptr, char *buffer, char *bufend)
1207 {
1208   struct Client*  from = cptr;
1209   char*           ch = buffer;
1210   char*           s;
1211   int             len;
1212   int             i;
1213   int             numeric = 0;
1214   int             paramcount;
1215   struct Message* mptr;
1216
1217   Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
1218
1219   if (IsDead(cptr))
1220     return 0;
1221
1222   para[0] = cli_name(from);
1223
1224   /*
1225    * A server ALWAYS sends a prefix. When it starts with a ':' it's the
1226    * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
1227    * nick or server
1228    */
1229   if (*ch == ':')
1230   {
1231     /* Let para[0] point to the name of the sender */
1232     para[0] = ch + 1;
1233     if (!(ch = strchr(ch, ' ')))
1234       return -1;
1235     *ch++ = '\0';
1236
1237     /* And let `from' point to its client structure,
1238        opps.. a server is _also_ a client --Nem */
1239     from = FindClient(para[0]);
1240
1241     /*
1242      * If the client corresponding to the
1243      * prefix is not found. We must ignore it,
1244      * it is simply a lagged message traveling
1245      * upstream a SQUIT that removed the client
1246      * --Run
1247      */
1248     if (from == NULL)
1249     {
1250       Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
1251           para[0], buffer, cli_name(cptr)));
1252       ++ServerStats->is_unpf;
1253       while (*ch == ' ')
1254         ch++;
1255       /*
1256        * However, the only thing that MUST be
1257        * allowed to travel upstream against an
1258        * squit, is an SQUIT itself (the timestamp
1259        * protects us from being used wrong)
1260        */
1261       if (ch[1] == 'Q')
1262       {
1263         para[0] = cli_name(cptr);
1264         from = cptr;
1265       }
1266       else
1267         return 0;
1268     }
1269     else if (cli_from(from) != cptr)
1270     {
1271       ++ServerStats->is_wrdi;
1272       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1273           buffer, cli_name(cptr)));
1274       return 0;
1275     }
1276   }
1277   else
1278   {
1279     char numeric_prefix[6];
1280     int  i;
1281     for (i = 0; i < 5; ++i)
1282     {
1283       if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i]))
1284       {
1285         break;
1286       }
1287     }
1288     numeric_prefix[i] = '\0';
1289
1290     /*
1291      * We got a numeric nick as prefix
1292      * 1 or 2 character prefixes are from servers
1293      * 3 or 5 chars are from clients
1294      */
1295     if (0 == i)
1296     {
1297       protocol_violation(cptr,"Missing Prefix");
1298       from = cptr;
1299     }
1300     else if (' ' == ch[1] || ' ' == ch[2])
1301       from = FindNServer(numeric_prefix);
1302     else
1303       from = findNUser(numeric_prefix);
1304
1305     do
1306     {
1307       ++ch;
1308     }
1309     while (*ch != ' ' && *ch);
1310
1311     /*
1312      * If the client corresponding to the
1313      * prefix is not found. We must ignore it,
1314      * it is simply a lagged message traveling
1315      * upstream a SQUIT that removed the client
1316      * --Run
1317      * There turned out to be other reasons that
1318      * a prefix is unknown, needing an upstream
1319      * KILL.  Also, next to an SQUIT we better
1320      * allow a KILL to pass too.
1321      * --Run
1322      */
1323     if (from == NULL)
1324     {
1325       ServerStats->is_unpf++;
1326       while (*ch == ' ')
1327         ch++;
1328       if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1329         /* Only sent a KILL for a nick change */
1330       {
1331         struct Client *server;
1332         /* Kill the unknown numeric prefix upstream if
1333          * it's server still exists: */
1334         if ((server = FindNServer(numeric_prefix)) && cli_from(server) == cptr)
1335           sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1336                         numeric_prefix, cli_name(&me));
1337       }
1338       /*
1339        * Things that must be allowed to travel
1340        * upstream against an squit:
1341        */
1342       if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1343           (*ch == 'K' && ch[2] == 'L'))
1344         from = cptr;
1345       else
1346         return 0;
1347     }
1348
1349     /* Let para[0] point to the name of the sender */
1350     para[0] = cli_name(from);
1351
1352     if (cli_from(from) != cptr)
1353     {
1354       ServerStats->is_wrdi++;
1355       Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1356           buffer, cli_name(cptr)));
1357       return 0;
1358     }
1359   }
1360
1361   while (*ch == ' ')
1362     ch++;
1363   if (*ch == '\0')
1364   {
1365     ServerStats->is_empt++;
1366     Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1367         cli_name(cptr), cli_name(from)));
1368     return (-1);
1369   }
1370
1371   /*
1372    * Extract the command code from the packet.   Point s to the end
1373    * of the command code and calculate the length using pointer
1374    * arithmetic.  Note: only need length for numerics and *all*
1375    * numerics must have parameters and thus a space after the command
1376    * code. -avalon
1377    */
1378   s = strchr(ch, ' ');          /* s -> End of the command code */
1379   len = (s) ? (s - ch) : 0;
1380   if (len == 3 && IsDigit(*ch))
1381   {
1382     numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1383     paramcount = 2; /* destination, and the rest of it */
1384     ServerStats->is_num++;
1385     mptr = NULL;                /* Init. to avoid stupid compiler warning :/ */
1386   }
1387   else
1388   {
1389     if (s)
1390       *s++ = '\0';
1391
1392     /* Version      Receive         Send
1393      * 2.9          Long            Long
1394      * 2.10.0       Tkn/Long        Long
1395      * 2.10.10      Tkn/Long        Tkn
1396      * 2.10.20      Tkn             Tkn
1397      *
1398      * Clients/unreg servers always receive/
1399      * send long commands   -record
1400      *
1401      * And for the record, this trie parser really does not care. - Dianora
1402      */
1403
1404     mptr = msg_tree_parse(ch, &tok_tree);
1405
1406     if (mptr == NULL)
1407     {
1408       mptr = msg_tree_parse(ch, &msg_tree);
1409     }
1410
1411     if (mptr == NULL)
1412     {
1413       /*
1414        * Note: Give error message *only* to recognized
1415        * persons. It's a nightmare situation to have
1416        * two programs sending "Unknown command"'s or
1417        * equivalent to each other at full blast....
1418        * If it has got to person state, it at least
1419        * seems to be well behaving. Perhaps this message
1420        * should never be generated, though...   --msa
1421        * Hm, when is the buffer empty -- if a command
1422        * code has been found ?? -Armin
1423        */
1424 #ifdef DEBUGMODE
1425       if (buffer[0] != '\0')
1426       {
1427         Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1428               ch, get_client_name(cptr, HIDE_IP)));
1429       }
1430 #endif
1431       ServerStats->is_unco++;
1432       return (-1);
1433     }
1434
1435     paramcount = mptr->parameters;
1436     i = bufend - ((s) ? s : ch);
1437     mptr->bytes += i;
1438   }
1439   /*
1440    * Must the following loop really be so devious? On
1441    * surface it splits the message to parameters from
1442    * blank spaces. But, if paramcount has been reached,
1443    * the rest of the message goes into this last parameter
1444    * (about same effect as ":" has...) --msa
1445    */
1446
1447   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1448
1449   i = 0;
1450   if (s)
1451   {
1452     if (paramcount > MAXPARA)
1453       paramcount = MAXPARA;
1454     for (;;)
1455     {
1456       /*
1457        * Never "FRANCE " again!! ;-) Clean
1458        * out *all* blanks.. --msa
1459        */
1460       while (*s == ' ')
1461         *s++ = '\0';
1462
1463       if (*s == '\0')
1464         break;
1465       if (*s == ':')
1466       {
1467         /*
1468          * The rest is single parameter--can
1469          * include blanks also.
1470          */
1471         if (numeric)
1472           para[++i] = s; /* preserve the colon to make do_numeric happy */
1473         else
1474           para[++i] = s + 1;
1475         break;
1476       }
1477       para[++i] = s;
1478       if (i >= paramcount)
1479         break;
1480       for (; *s != ' ' && *s; s++);
1481     }
1482   }
1483   para[++i] = NULL;
1484   if (numeric)
1485     return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1486   mptr->count++;
1487
1488   return (*mptr->handlers[cli_handler(cptr)]) (cptr, from, i, para);
1489 }