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