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