2 * IRC - Internet Relay Chat, common/parse.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
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)
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.
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.
21 * @brief Parse input from IRC clients and other servers.
22 * @version $Id: parse.c 1827 2007-08-14 03:02:24Z entrope $
32 #include "ircd_alloc.h"
33 #include "ircd_chattr.h"
34 #include "ircd_features.h"
36 #include "ircd_reply.h"
37 #include "ircd_string.h"
42 #include "querycmds.h"
48 #include "s_numeric.h"
56 /* #include <assert.h> -- Now using assert in ircd_log.h */
61 * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
62 * Adapted to Undernet, adding token support, etc by comstud 10/06/97
64 * completely rewritten June 2, 2003 - Dianora
66 * This has always just been a trie. Look at volume III of Knuth ACP
69 * ok, you start out with an array of pointers, each one corresponds
70 * to a letter at the current position in the command being examined.
72 * so roughly you have this for matching 'trie' or 'tie'
74 * 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i'
75 * -> [MessageTree *] -> [MessageTree *] -> 'e' and matches
77 * 'i' -> [MessageTree *] -> 'e' and matches
80 /** Number of children under a trie node. */
81 #define MAXPTRLEN 32 /* Must be a power of 2, and
82 * larger than 26 [a-z]|[A-Z]
83 * its used to allocate the set
84 * of pointers at each node of the tree
85 * There are MAXPTRLEN pointers at each node.
86 * Obviously, there have to be more pointers
87 * Than ASCII letters. 32 is a nice number
88 * since there is then no need to shift
89 * 'A'/'a' to base 0 index, at the expense
90 * of a few never used pointers. For a small
91 * parser like this, this is a good compromise
92 * and does make it somewhat faster.
97 /** Node in the command lookup trie. */
99 struct Message *msg; /**< Message (if any) if the string ends now. */
100 struct MessageTree *pointers[MAXPTRLEN]; /**< Child nodes for each letter. */
103 /** Root of command lookup trie. */
104 static struct MessageTree msg_tree;
105 static struct MessageTree tok_tree;
107 /** Array of all supported commands. */
108 struct Message msgtab[] = {
112 0, MAXPARA, MFLG_SLOW, 0, NULL,
113 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
114 { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
119 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
120 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
121 { m_nick, m_nick, ms_nick, m_nick, m_ignore }
126 0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0, NULL,
127 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
128 { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
133 0, MAXPARA, MFLG_SLOW, 0, NULL,
134 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
135 { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
140 0, MAXPARA, MFLG_SLOW, 0, NULL,
141 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
142 { m_unregistered, m_wallvoices, ms_wallvoices, m_wallvoices, m_ignore }
147 0, MAXPARA, MFLG_SLOW, 0, NULL,
148 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
149 { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
154 0, MAXPARA, MFLG_SLOW, 0, NULL,
155 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
156 { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
161 0, MAXPARA, MFLG_SLOW, 0, NULL,
162 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
163 { m_unregistered, m_join, ms_join, m_join, m_ignore }
168 0, MAXPARA, MFLG_SLOW, 0, NULL,
169 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
170 { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
175 0, MAXPARA, MFLG_SLOW, 0, NULL,
176 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
177 { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
182 0, MAXPARA, MFLG_SLOW, 0, NULL,
183 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
184 { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
189 0, MAXPARA, MFLG_SLOW, 0, NULL,
190 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
191 { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
196 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
197 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
198 { m_quit, m_quit, ms_quit, m_quit, m_ignore }
203 0, MAXPARA, MFLG_SLOW, 0, NULL,
204 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
205 { m_unregistered, m_part, ms_part, m_part, m_ignore }
210 0, MAXPARA, MFLG_SLOW, 0, NULL,
211 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
212 { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
217 0, MAXPARA, MFLG_SLOW, 0, NULL,
218 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
219 { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
224 0, MAXPARA, MFLG_SLOW, 0, NULL,
225 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
226 { m_unregistered, m_uninvite, ms_uninvite, m_uninvite, m_ignore }
231 0, MAXPARA, MFLG_SLOW, 0, NULL,
232 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
233 { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
238 0, MAXPARA, MFLG_SLOW, 0, NULL,
239 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
240 { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
245 0, MAXPARA, MFLG_SLOW, 0, NULL,
246 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
247 { m_unregistered, m_not_oper, ms_wallusers, mo_wallusers, m_ignore }
252 0, MAXPARA, MFLG_SLOW, 0, NULL,
253 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
254 { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
259 0, MAXPARA, MFLG_SLOW, 0, NULL,
260 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
261 { m_unregistered, m_ping, ms_ping, mo_ping, m_ignore }
266 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
267 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
268 { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
273 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
274 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
275 { mr_error, m_ignore, ms_error, m_ignore, m_ignore }
280 0, MAXPARA, MFLG_SLOW, 0, NULL,
281 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
282 { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
287 0, MAXPARA, MFLG_SLOW, 0, NULL,
288 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
289 { m_user, m_registered, m_ignore, m_registered, m_ignore }
294 0, MAXPARA, MFLG_SLOW, 0, NULL,
295 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
296 { m_unregistered, m_away, ms_away, m_away, m_ignore }
301 0, 1, MFLG_SLOW, 0, NULL,
302 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
303 { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
308 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
309 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
310 { mr_server, m_registered, ms_server, m_registered, m_ignore }
315 0, MAXPARA, MFLG_SLOW, 0, NULL,
316 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
317 { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
322 0, MAXPARA, MFLG_SLOW, 0, NULL,
323 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
324 { m_unregistered, m_whois, ms_whois, m_whois, m_ignore }
329 0, MAXPARA, MFLG_SLOW, 0, NULL,
330 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
331 { m_unregistered, m_who, m_ignore, m_who, m_ignore }
336 0, MAXPARA, MFLG_SLOW, 0, NULL,
337 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
338 { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
343 0, MAXPARA, MFLG_SLOW, 0, NULL,
344 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
345 { m_unregistered, m_list, m_ignore, m_list, m_ignore }
350 0, MAXPARA, MFLG_SLOW, 0, NULL,
351 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
352 { m_unregistered, m_names, m_names, m_names, m_ignore }
357 0, 1, MFLG_SLOW, 0, NULL,
358 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
359 { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
364 0, 1, MFLG_SLOW, 0, NULL,
365 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
366 { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
371 0, MAXPARA, MFLG_SLOW, 0, NULL,
372 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
373 { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
378 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
379 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
380 { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
385 0, MAXPARA, MFLG_SLOW, 0, NULL,
386 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
387 { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
392 0, MAXPARA, MFLG_SLOW, 0, NULL,
393 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
394 { m_unregistered, m_time, m_time, m_time, m_ignore }
399 0, MAXPARA, MFLG_SLOW, 0, NULL,
400 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
401 { m_unregistered, m_not_oper, ms_settime, mo_settime, m_ignore }
406 0, MAXPARA, MFLG_SLOW, 0, NULL,
407 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
408 { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
413 0, MAXPARA, MFLG_SLOW, 0, NULL,
414 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
415 { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
420 0, MAXPARA, MFLG_SLOW, 0, NULL,
421 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
422 { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
427 0, MAXPARA, MFLG_SLOW, 0, NULL,
428 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
429 { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
434 0, MAXPARA, MFLG_SLOW, 0, NULL,
435 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
436 { m_unregistered, m_map, m_ignore, m_map, m_ignore }
441 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
442 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
443 { m_version, m_version, ms_version, mo_version, m_ignore }
448 0, MAXPARA, MFLG_SLOW, 0, NULL,
449 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
450 { m_unregistered, m_stats, m_stats, m_stats, m_ignore }
455 0, MAXPARA, MFLG_SLOW, 0, NULL,
456 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
457 { m_unregistered, m_links, ms_links, m_links, m_ignore }
462 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
463 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
464 { m_admin, m_admin, ms_admin, mo_admin, m_ignore }
469 0, MAXPARA, MFLG_SLOW, 0, NULL,
470 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
471 { m_unregistered, m_help, m_ignore, m_help, m_ignore }
476 0, MAXPARA, MFLG_SLOW, 0, NULL,
477 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
478 { m_unregistered, m_info, ms_info, mo_info, m_ignore }
483 0, MAXPARA, MFLG_SLOW, 0, NULL,
484 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
485 { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
490 0, MAXPARA, MFLG_SLOW, 0, NULL,
491 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
492 { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
497 0, MAXPARA, MFLG_SLOW, 0, NULL,
498 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
499 { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
504 0, MAXPARA, 0, 0, NULL,
505 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
506 { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
511 0, MAXPARA, MFLG_SLOW, 0, NULL,
512 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
513 { m_unregistered, m_not_oper, ms_jupe, mo_jupe, m_ignore }
518 0, MAXPARA, MFLG_SLOW, 0, NULL,
519 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
520 { m_unregistered, mo_opmode, ms_opmode, mo_opmode, m_ignore }
525 0, MAXPARA, MFLG_SLOW, 0, NULL,
526 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
527 { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
532 0, MAXPARA, MFLG_SLOW, 0, NULL,
533 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
534 { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
539 0, MAXPARA, MFLG_SLOW, 0, NULL,
540 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
541 { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
544 MSG_END_OF_BURST_ACK,
545 TOK_END_OF_BURST_ACK,
546 0, MAXPARA, MFLG_SLOW, 0, NULL,
547 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
548 { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
553 0, MAXPARA, MFLG_SLOW, 0, NULL,
554 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
555 { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
560 0, MAXPARA, MFLG_SLOW, 0, NULL,
561 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
562 { m_unregistered, m_not_oper, ms_rehash, mo_rehash, m_ignore }
567 0, MAXPARA, MFLG_SLOW, 0, NULL,
568 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
569 { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
574 0, MAXPARA, MFLG_SLOW, 0, NULL,
575 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
576 { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
581 0, MAXPARA, MFLG_SLOW, 0, NULL,
582 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
583 { m_proto, m_proto, m_proto, m_proto, m_ignore }
588 0, MAXPARA, MFLG_SLOW, 0, NULL,
589 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
590 { m_unregistered, m_not_oper, m_ignore, mo_set, m_ignore }
595 0, MAXPARA, MFLG_SLOW, 0, NULL,
596 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
597 { m_unregistered, m_not_oper, m_ignore, mo_reset, m_ignore }
602 0, MAXPARA, MFLG_SLOW, 0, NULL,
603 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
604 { m_unregistered, m_not_oper, m_ignore, mo_get, m_ignore }
609 0, MAXPARA, MFLG_SLOW, 0, NULL,
610 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
611 { m_unregistered, m_privs, ms_privs, m_privs, m_ignore }
616 0, MAXPARA, MFLG_SLOW, 0, NULL,
617 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
618 { m_ignore, m_ignore, ms_account, m_ignore, m_ignore }
623 0, MAXPARA, MFLG_SLOW, 0, NULL,
624 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
625 { m_ignore, m_not_oper, ms_asll, mo_asll, m_ignore }
627 #if WE_HAVE_A_REAL_CAPABILITY_NOW
631 0, MAXPARA, 0, 0, NULL,
632 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
633 { m_cap, m_cap, m_ignore, m_cap, m_ignore }
639 0, MAXPARA, MFLG_SLOW, 0, NULL,
640 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
641 { m_ignore, m_not_oper, ms_fakehost, m_fakehost, m_ignore }
646 0, MAXPARA, MFLG_SLOW, 0, NULL,
647 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
648 { m_ignore, m_ignore, ms_fakehost2, m_ignore, m_ignore }
653 0, MAXPARA, MFLG_SLOW, 0, NULL,
654 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
655 { m_ignore, m_not_oper, ms_fakehost_old, m_fakehost, m_ignore }
660 0, MAXPARA, MFLG_SLOW, 0, NULL,
661 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
662 { m_ignore, m_ignore, ms_hidehost, m_ignore, m_ignore }
667 0, MAXPARA, MFLG_SLOW, 0, NULL,
668 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
669 { m_ignore, m_ignore, ms_svsmode, m_svsmode, m_ignore }
674 0, MAXPARA, MFLG_SLOW, 0, NULL,
675 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
676 { m_ignore, m_ignore, ms_svsnick, m_svsnick, m_ignore }
681 0, MAXPARA, MFLG_SLOW, 0, NULL,
682 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
683 { m_ignore, m_ignore, ms_svsnick_old, m_svsnick, m_ignore }
688 0, MAXPARA, MFLG_SLOW, 0, NULL,
689 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
690 { m_ignore, m_ignore, ms_svsjoin, m_svsjoin, m_ignore }
695 0, MAXPARA, MFLG_SLOW, 0, NULL,
696 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
697 { m_ignore, m_ignore, ms_svspart, m_ignore, m_ignore }
702 0, MAXPARA, MFLG_SLOW, 0, NULL,
703 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
704 { m_webirc, m_registered, m_ignore, m_registered, m_ignore }
709 0, MAXPARA, MFLG_SLOW, 0, NULL,
710 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
711 { m_ignore, m_ignore, ms_relay, m_ignore, m_ignore }
716 0, MAXPARA, MFLG_SLOW, 0, NULL,
717 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
718 { m_ignore, m_not_oper, m_ignore, mo_check, m_ignore }
720 /* This command is an alias for QUIT during the unregistered part of
721 * of the server. This is because someone jumping via a broken web
722 * proxy will send a 'POST' as their first command - which we will
723 * obviously disconnect them immediately for, stopping people abusing
729 0, MAXPARA, MFLG_SLOW, 0, NULL,
730 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
731 { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
736 /** Array of command parameters. */
737 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
740 /** Add a message to the lookup trie.
741 * @param[in,out] mtree_p Trie node to insert under.
742 * @param[in] msg_p Message to insert.
743 * @param[in] cmd Text of command to insert.
746 add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, char *cmd)
748 struct MessageTree *ntree_p;
752 mtree_p->msg = msg_p;
756 if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL)
758 add_msg_element(ntree_p, msg_p, cmd+1);
762 ntree_p = (struct MessageTree *)MyCalloc(sizeof(struct MessageTree), 1);
763 mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = ntree_p;
764 add_msg_element(ntree_p, msg_p, cmd+1);
768 /** Remove a message from the lookup trie.
769 * @param[in,out] mtree_p Trie node to remove command from.
770 * @param[in] cmd Text of command to remove.
773 del_msg_element(struct MessageTree *mtree_p, char *cmd)
775 int slot = *cmd & (MAXPTRLEN-1);
777 /* Either remove leaf message or from appropriate child. */
781 mtree_p->pointers[slot] = del_msg_element(mtree_p->pointers[slot], cmd + 1);
783 /* If current message or any child still exists, keep this node. */
786 for (slot = 0; slot < MAXPTRLEN; ++slot)
787 if (mtree_p->pointers[slot])
790 /* Otherwise, if we're not a root node, free it and return null. */
791 if (mtree_p != &msg_tree && mtree_p != &tok_tree)
796 /** Initialize the message lookup trie with all known commands. */
802 memset(&msg_tree, 0, sizeof(msg_tree));
803 memset(&tok_tree, 0, sizeof(tok_tree));
805 for (i = 0; msgtab[i].cmd != NULL ; i++)
807 add_msg_element(&msg_tree, &msgtab[i], msgtab[i].cmd);
808 add_msg_element(&tok_tree, &msgtab[i], msgtab[i].tok);
812 /** Look up a command in the message trie.
813 * @param cmd Text of command to look up.
814 * @param root Root of message trie.
815 * @return Pointer to matching message, or NULL if non exists.
817 static struct Message *
818 msg_tree_parse(char *cmd, struct MessageTree *root)
820 struct MessageTree *mtree;
822 for (mtree = root; mtree; mtree = mtree->pointers[(*cmd++) & (MAXPTRLEN-1)]) {
823 if (*cmd == '\0' && mtree->msg)
825 else if (!IsAlpha(*cmd))
831 /** Registers a service mapping to the pseudocommand handler.
832 * @param[in] map Service mapping to add.
833 * @return Non-zero on success; zero if a command already used the name.
835 int register_mapping(struct s_map *map)
839 if (msg_tree_parse(map->command, &msg_tree))
842 msg = (struct Message *)MyMalloc(sizeof(struct Message));
843 msg->cmd = map->command;
844 msg->tok = map->command;
847 msg->flags = MFLG_EXTRA;
848 if (!(map->flags & SMAP_FAST))
849 msg->flags |= MFLG_SLOW;
853 msg->handlers[UNREGISTERED_HANDLER] = m_ignore;
854 msg->handlers[CLIENT_HANDLER] = m_pseudo;
855 msg->handlers[SERVER_HANDLER] = m_ignore;
856 msg->handlers[OPER_HANDLER] = m_pseudo;
857 msg->handlers[SERVICE_HANDLER] = m_ignore;
859 add_msg_element(&msg_tree, msg, msg->cmd);
865 /** Removes a service mapping.
866 * @param[in] map Service mapping to remove.
867 * @return Non-zero on success; zero if no command used the name.
869 int unregister_mapping(struct s_map *map)
871 if (!msg_tree_parse(map->command, &msg_tree))
873 /* This simply should never happen. */
878 del_msg_element(&msg_tree, map->msg->cmd);
880 map->msg->extra = NULL;
887 /** Parse a line of data from a user.
888 * NOTE: parse_*() should not be called recursively by any other
890 * @param[in] cptr Client that sent the data.
891 * @param[in] buffer Start of input line.
892 * @param[in] bufend End of input line.
893 * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
894 * handler returns it.
897 parse_client(struct Client *cptr, char *buffer, char *bufend)
899 struct Client* from = cptr;
905 struct Message* mptr;
906 MessageHandler handler = 0;
908 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
913 para[0] = cli_name(from);
914 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
915 if (*ch == ':') /* Is any client doing this ? */
917 for (++ch; *ch && *ch != ' '; ++ch)
918 ; /* Ignore sender prefix from client */
920 ch++; /* Advance to command */
926 ServerStats->is_empt++;
927 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
928 cli_name(cptr), cli_name(from)));
932 if ((s = strchr(ch, ' ')))
935 if ((mptr = msg_tree_parse(ch, &msg_tree)) == NULL)
938 * Note: Give error message *only* to recognized
939 * persons. It's a nightmare situation to have
940 * two programs sending "Unknown command"'s or
941 * equivalent to each other at full blast....
942 * If it has got to person state, it at least
943 * seems to be well behaving. Perhaps this message
944 * should never be generated, though... --msa
945 * Hm, when is the buffer empty -- if a command
946 * code has been found ?? -Armin
948 if (buffer[0] != '\0')
951 struct Client *acptr;
952 if(feature_bool(FEAT_UNKNOWN_CMD_ENABLE) && feature_str(FEAT_UNKNOWN_CMD_TARGET) && (acptr = FindUser(feature_str(FEAT_UNKNOWN_CMD_TARGET))) && IsNetServ(acptr) && IsService(cli_user(acptr)->server)) {
953 sendcmdto_one(&me, CMD_RELAY, acptr, "%C UC %C %s :%s", acptr, from, ch, s);
955 send_reply(from, ERR_UNKNOWNCOMMAND, ch);
958 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
959 ch, get_client_name(cptr, HIDE_IP)));
961 ServerStats->is_unco++;
965 paramcount = mptr->parameters;
966 i = bufend - ((s) ? s : ch);
968 if ((mptr->flags & MFLG_SLOW) || (!IsAnOper(cptr) && !HasPriv(cptr, PRIV_FLOOD))) {
969 if(HasPriv(cptr, PRIV_HALFFLOOD))
970 cli_since(cptr) += 1;
972 cli_since(cptr) += (2 + i / 120);
976 * Allow only 1 msg per 2 seconds
977 * (on average) to prevent dumping.
978 * to keep the response rate up,
979 * bursts of up to 5 msgs are allowed
984 * Must the following loop really be so devious? On
985 * surface it splits the message to parameters from
986 * blank spaces. But, if paramcount has been reached,
987 * the rest of the message goes into this last parameter
988 * (about same effect as ":" has...) --msa
991 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
993 if (mptr->flags & MFLG_EXTRA) {
994 /* This is a horrid kludge to avoid changing the command handler
996 para[1] = (char*)mptr->extra;
1003 if (paramcount > MAXPARA)
1004 paramcount = MAXPARA;
1008 * Never "FRANCE " again!! ;-) Clean
1009 * out *all* blanks.. --msa
1019 * The rest is single parameter--can
1020 * include blanks also.
1026 if (i >= paramcount)
1028 for (; *s != ' ' && *s; s++);
1034 handler = mptr->handlers[cli_handler(cptr)];
1035 assert(0 != handler);
1037 if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
1038 handler != m_ping && handler != m_ignore)
1039 cli_user(from)->last = CurrentTime;
1041 return (*handler) (cptr, from, i, para);
1045 parse_simul_client(struct Client *cptr, char *buffer)
1047 struct Client* from = cptr;
1053 struct Message* mptr;
1054 MessageHandler handler = 0;
1056 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
1061 para[0] = cli_name(from);
1062 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
1063 if (*ch == ':') /* Is any client doing this ? */
1065 for (++ch; *ch && *ch != ' '; ++ch)
1066 ; /* Ignore sender prefix from client */
1068 ch++; /* Advance to command */
1074 ServerStats->is_empt++;
1075 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1076 cli_name(cptr), cli_name(from)));
1080 if ((s = strchr(ch, ' ')))
1083 if ((mptr = msg_tree_parse(ch, &msg_tree)) == NULL)
1086 * Note: Give error message *only* to recognized
1087 * persons. It's a nightmare situation to have
1088 * two programs sending "Unknown command"'s or
1089 * equivalent to each other at full blast....
1090 * If it has got to person state, it at least
1091 * seems to be well behaving. Perhaps this message
1092 * should never be generated, though... --msa
1093 * Hm, when is the buffer empty -- if a command
1094 * code has been found ?? -Armin
1096 if (buffer[0] != '\0')
1098 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1099 ch, get_client_name(cptr, HIDE_IP)));
1101 ServerStats->is_unco++;
1105 paramcount = mptr->parameters;
1108 * Allow only 1 msg per 2 seconds
1109 * (on average) to prevent dumping.
1110 * to keep the response rate up,
1111 * bursts of up to 5 msgs are allowed
1116 * Must the following loop really be so devious? On
1117 * surface it splits the message to parameters from
1118 * blank spaces. But, if paramcount has been reached,
1119 * the rest of the message goes into this last parameter
1120 * (about same effect as ":" has...) --msa
1123 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1125 if (mptr->flags & MFLG_EXTRA) {
1126 /* This is a horrid kludge to avoid changing the command handler
1128 para[1] = (char*)mptr->extra;
1135 if (paramcount > MAXPARA)
1136 paramcount = MAXPARA;
1140 * Never "FRANCE " again!! ;-) Clean
1141 * out *all* blanks.. --msa
1151 * The rest is single parameter--can
1152 * include blanks also.
1158 if (i >= paramcount)
1160 for (; *s != ' ' && *s; s++);
1166 handler = mptr->handlers[cli_handler(cptr)];
1167 assert(0 != handler);
1169 if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
1170 handler != m_ping && handler != m_ignore)
1171 cli_user(from)->last = CurrentTime;
1173 return (*handler) (cptr, from, i, para);
1178 /** Parse a line of data from a server.
1179 * @param[in] cptr Client that sent the data.
1180 * @param[in] buffer Start of input line.
1181 * @param[in] bufend End of input line.
1182 * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
1183 * handler returns it.
1185 int parse_server(struct Client *cptr, char *buffer, char *bufend)
1187 struct Client* from = cptr;
1194 struct Message* mptr;
1196 Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
1201 para[0] = cli_name(from);
1204 * A server ALWAYS sends a prefix. When it starts with a ':' it's the
1205 * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
1210 /* Let para[0] point to the name of the sender */
1212 if (!(ch = strchr(ch, ' ')))
1216 /* And let `from' point to its client structure,
1217 opps.. a server is _also_ a client --Nem */
1218 from = FindClient(para[0]);
1221 * If the client corresponding to the
1222 * prefix is not found. We must ignore it,
1223 * it is simply a lagged message traveling
1224 * upstream a SQUIT that removed the client
1229 Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
1230 para[0], buffer, cli_name(cptr)));
1231 ++ServerStats->is_unpf;
1235 * However, the only thing that MUST be
1236 * allowed to travel upstream against an
1237 * squit, is an SQUIT itself (the timestamp
1238 * protects us from being used wrong)
1242 para[0] = cli_name(cptr);
1248 else if (cli_from(from) != cptr)
1250 ++ServerStats->is_wrdi;
1251 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1252 buffer, cli_name(cptr)));
1258 char numeric_prefix[6];
1260 for (i = 0; i < 5; ++i)
1262 if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i]))
1267 numeric_prefix[i] = '\0';
1270 * We got a numeric nick as prefix
1271 * 1 or 2 character prefixes are from servers
1272 * 3 or 5 chars are from clients
1276 protocol_violation(cptr,"Missing Prefix");
1279 else if (' ' == ch[1] || ' ' == ch[2])
1280 from = FindNServer(numeric_prefix);
1282 from = findNUser(numeric_prefix);
1288 while (*ch != ' ' && *ch);
1291 * If the client corresponding to the
1292 * prefix is not found. We must ignore it,
1293 * it is simply a lagged message traveling
1294 * upstream a SQUIT that removed the client
1296 * There turned out to be other reasons that
1297 * a prefix is unknown, needing an upstream
1298 * KILL. Also, next to an SQUIT we better
1299 * allow a KILL to pass too.
1304 ServerStats->is_unpf++;
1307 if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1308 /* Only sent a KILL for a nick change */
1310 struct Client *server;
1311 /* Kill the unknown numeric prefix upstream if
1312 * it's server still exists: */
1313 if ((server = FindNServer(numeric_prefix)) && cli_from(server) == cptr)
1314 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1315 numeric_prefix, cli_name(&me));
1318 * Things that must be allowed to travel
1319 * upstream against an squit:
1321 if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1322 (*ch == 'K' && ch[2] == 'L'))
1328 /* Let para[0] point to the name of the sender */
1329 para[0] = cli_name(from);
1331 if (cli_from(from) != cptr)
1333 ServerStats->is_wrdi++;
1334 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1335 buffer, cli_name(cptr)));
1344 ServerStats->is_empt++;
1345 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1346 cli_name(cptr), cli_name(from)));
1351 * Extract the command code from the packet. Point s to the end
1352 * of the command code and calculate the length using pointer
1353 * arithmetic. Note: only need length for numerics and *all*
1354 * numerics must have parameters and thus a space after the command
1357 s = strchr(ch, ' '); /* s -> End of the command code */
1358 len = (s) ? (s - ch) : 0;
1359 if (len == 3 && IsDigit(*ch))
1361 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1362 paramcount = 2; /* destination, and the rest of it */
1363 ServerStats->is_num++;
1364 mptr = NULL; /* Init. to avoid stupid compiler warning :/ */
1371 /* Version Receive Send
1373 * 2.10.0 Tkn/Long Long
1374 * 2.10.10 Tkn/Long Tkn
1377 * Clients/unreg servers always receive/
1378 * send long commands -record
1380 * And for the record, this trie parser really does not care. - Dianora
1383 mptr = msg_tree_parse(ch, &tok_tree);
1387 mptr = msg_tree_parse(ch, &msg_tree);
1393 * Note: Give error message *only* to recognized
1394 * persons. It's a nightmare situation to have
1395 * two programs sending "Unknown command"'s or
1396 * equivalent to each other at full blast....
1397 * If it has got to person state, it at least
1398 * seems to be well behaving. Perhaps this message
1399 * should never be generated, though... --msa
1400 * Hm, when is the buffer empty -- if a command
1401 * code has been found ?? -Armin
1404 if (buffer[0] != '\0')
1406 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1407 ch, get_client_name(cptr, HIDE_IP)));
1410 ServerStats->is_unco++;
1414 paramcount = mptr->parameters;
1415 i = bufend - ((s) ? s : ch);
1419 * Must the following loop really be so devious? On
1420 * surface it splits the message to parameters from
1421 * blank spaces. But, if paramcount has been reached,
1422 * the rest of the message goes into this last parameter
1423 * (about same effect as ":" has...) --msa
1426 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1431 if (paramcount > MAXPARA)
1432 paramcount = MAXPARA;
1436 * Never "FRANCE " again!! ;-) Clean
1437 * out *all* blanks.. --msa
1447 * The rest is single parameter--can
1448 * include blanks also.
1451 para[++i] = s; /* preserve the colon to make do_numeric happy */
1457 if (i >= paramcount)
1459 for (; *s != ' ' && *s; s++);
1464 return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1467 return (*mptr->handlers[cli_handler(cptr)]) (cptr, from, i, para);