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