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