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