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.
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"
39 #include "querycmds.h"
45 #include "s_numeric.h"
58 * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
59 * Adapted to Undernet, adding token support, etc by comstud 10/06/97
61 * completely rewritten June 2, 2003 - Dianora
63 * This has always just been a trie. Look at volume III of Knuth ACP
66 * ok, you start out with an array of pointers, each one corresponds
67 * to a letter at the current position in the command being examined.
69 * so roughly you have this for matching 'trie' or 'tie'
71 * 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i'
72 * -> [MessageTree *] -> [MessageTree *] -> 'e' and matches
74 * 'i' -> [MessageTree *] -> 'e' and matches
77 #define MAXPTRLEN 32 /* Must be a power of 2, and
78 * larger than 26 [a-z]|[A-Z]
79 * its used to allocate the set
80 * of pointers at each node of the tree
81 * There are MAXPTRLEN pointers at each node.
82 * Obviously, there have to be more pointers
83 * Than ASCII letters. 32 is a nice number
84 * since there is then no need to shift
85 * 'A'/'a' to base 0 index, at the expense
86 * of a few never used pointers. For a small
87 * parser like this, this is a good compromise
88 * and does make it somewhat faster.
95 struct MessageTree *pointers[MAXPTRLEN];
98 static struct MessageTree msg_tree;
100 struct Message msgtab[] = {
104 0, MAXPARA, MFLG_SLOW, 0, NULL,
105 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
106 { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
111 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
112 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
113 { m_nick, m_nick, ms_nick, m_nick, m_ignore }
118 0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0, NULL,
119 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
120 { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
125 0, MAXPARA, MFLG_SLOW, 0, NULL,
126 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
127 { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
132 0, MAXPARA, MFLG_SLOW, 0, NULL,
133 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
134 { m_unregistered, m_wallvoices, ms_wallvoices, m_wallvoices, m_ignore }
139 0, MAXPARA, MFLG_SLOW, 0, NULL,
140 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
141 { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
146 0, MAXPARA, MFLG_SLOW, 0, NULL,
147 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
148 { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
153 0, MAXPARA, MFLG_SLOW, 0, NULL,
154 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
155 { m_unregistered, m_join, ms_join, m_join, m_ignore }
160 0, MAXPARA, MFLG_SLOW, 0, NULL,
161 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
162 { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
167 0, MAXPARA, MFLG_SLOW, 0, NULL,
168 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
169 { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
174 0, MAXPARA, MFLG_SLOW, 0, NULL,
175 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
176 { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
181 0, MAXPARA, MFLG_SLOW, 0, NULL,
182 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
183 { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
188 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
189 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
190 { m_quit, m_quit, ms_quit, m_quit, m_ignore }
195 0, MAXPARA, MFLG_SLOW, 0, NULL,
196 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
197 { m_unregistered, m_part, ms_part, m_part, m_ignore }
202 0, MAXPARA, MFLG_SLOW, 0, NULL,
203 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
204 { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
209 0, MAXPARA, MFLG_SLOW, 0, NULL,
210 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
211 { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
216 0, MAXPARA, MFLG_SLOW, 0, NULL,
217 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
218 { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
223 0, MAXPARA, MFLG_SLOW, 0, NULL,
224 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
225 { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
230 0, MAXPARA, MFLG_SLOW, 0, NULL,
231 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
232 { m_unregistered, m_not_oper, ms_wallusers, mo_wallusers, m_ignore }
237 0, MAXPARA, MFLG_SLOW, 0, NULL,
238 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
239 { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
244 0, MAXPARA, MFLG_SLOW, 0, NULL,
245 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
246 { m_unregistered, m_ping, ms_ping, mo_ping, m_ignore }
251 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
252 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
253 { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
258 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
259 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
260 { mr_error, m_ignore, ms_error, m_ignore, m_ignore }
265 0, MAXPARA, MFLG_SLOW, 0, NULL,
266 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
267 { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
272 0, MAXPARA, MFLG_SLOW, 0, NULL,
273 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
274 { m_user, m_registered, m_ignore, m_registered, m_ignore }
279 0, MAXPARA, MFLG_SLOW, 0, NULL,
280 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
281 { m_unregistered, m_away, ms_away, m_away, m_ignore }
286 0, 1, MFLG_SLOW, 0, NULL,
287 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
288 { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
293 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
294 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
295 { mr_server, m_registered, ms_server, m_registered, m_ignore }
300 0, MAXPARA, MFLG_SLOW, 0, NULL,
301 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
302 { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
307 0, MAXPARA, MFLG_SLOW, 0, NULL,
308 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
309 { m_unregistered, m_whois, ms_whois, m_whois, m_ignore }
314 0, MAXPARA, MFLG_SLOW, 0, NULL,
315 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
316 { m_unregistered, m_who, m_ignore, m_who, m_ignore }
321 0, MAXPARA, MFLG_SLOW, 0, NULL,
322 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
323 { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
328 0, MAXPARA, MFLG_SLOW, 0, NULL,
329 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
330 { m_unregistered, m_list, m_ignore, m_list, m_ignore }
335 0, MAXPARA, MFLG_SLOW, 0, NULL,
336 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
337 { m_unregistered, m_names, ms_names, m_names, m_ignore }
342 0, 1, MFLG_SLOW, 0, NULL,
343 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
344 { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
349 0, 1, MFLG_SLOW, 0, NULL,
350 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
351 { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
356 0, MAXPARA, MFLG_SLOW, 0, NULL,
357 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
358 { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
363 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
364 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
365 { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
370 0, MAXPARA, MFLG_SLOW, 0, NULL,
371 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
372 { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
377 0, MAXPARA, MFLG_SLOW, 0, NULL,
378 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
379 { m_unregistered, m_time, m_time, m_time, m_ignore }
384 0, MAXPARA, MFLG_SLOW, 0, NULL,
385 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
386 { m_unregistered, m_ignore, ms_settime, mo_settime, m_ignore }
391 0, MAXPARA, MFLG_SLOW, 0, NULL,
392 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
393 { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
398 0, MAXPARA, MFLG_SLOW, 0, NULL,
399 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
400 { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
405 0, MAXPARA, MFLG_SLOW, 0, NULL,
406 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
407 { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
412 0, MAXPARA, MFLG_SLOW, 0, NULL,
413 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
414 { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
419 0, MAXPARA, MFLG_SLOW, 0, NULL,
420 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
421 { m_unregistered, m_map, m_ignore, m_map, m_ignore }
426 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
427 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
428 { m_version, m_version, ms_version, mo_version, m_ignore }
433 0, MAXPARA, MFLG_SLOW, 0, NULL,
434 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
435 { m_unregistered, m_stats, m_stats, m_stats, m_ignore }
440 0, MAXPARA, MFLG_SLOW, 0, NULL,
441 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
442 { m_unregistered, m_links, ms_links, m_links, m_ignore }
447 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
448 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
449 { m_admin, m_admin, ms_admin, mo_admin, m_ignore }
454 0, MAXPARA, MFLG_SLOW, 0, NULL,
455 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
456 { m_unregistered, m_help, m_ignore, m_help, m_ignore }
461 0, MAXPARA, MFLG_SLOW, 0, NULL,
462 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
463 { m_unregistered, m_info, ms_info, mo_info, m_ignore }
468 0, MAXPARA, MFLG_SLOW, 0, NULL,
469 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
470 { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
475 0, MAXPARA, MFLG_SLOW, 0, NULL,
476 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
477 { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
482 0, MAXPARA, MFLG_SLOW, 0, NULL,
483 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
484 { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
489 0, MAXPARA, MFLG_SLOW, 0, NULL,
490 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
491 { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
496 0, MAXPARA, MFLG_SLOW, 0, NULL,
497 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
498 { m_unregistered, m_not_oper, ms_jupe, mo_jupe, m_ignore }
503 0, MAXPARA, MFLG_SLOW, 0, NULL,
504 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
505 { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
510 0, MAXPARA, MFLG_SLOW, 0, NULL,
511 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
512 { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
517 0, MAXPARA, MFLG_SLOW, 0, NULL,
518 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
519 { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
524 0, MAXPARA, MFLG_SLOW, 0, NULL,
525 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
526 { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
529 MSG_END_OF_BURST_ACK,
530 TOK_END_OF_BURST_ACK,
531 0, MAXPARA, MFLG_SLOW, 0, NULL,
532 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
533 { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
538 0, MAXPARA, MFLG_SLOW, 0, NULL,
539 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
540 { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
545 0, MAXPARA, MFLG_SLOW, 0, NULL,
546 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
547 { m_unregistered, m_ignore, m_ignore, m_dns, m_ignore }
552 0, MAXPARA, MFLG_SLOW, 0, NULL,
553 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
554 { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
559 0, MAXPARA, MFLG_SLOW, 0, NULL,
560 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
561 { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
566 0, MAXPARA, MFLG_SLOW, 0, NULL,
567 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
568 { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
573 0, MAXPARA, MFLG_SLOW, 0, NULL,
574 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
575 { m_proto, m_proto, m_proto, m_proto, m_ignore }
580 0, MAXPARA, MFLG_SLOW, 0, NULL,
581 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
582 { m_unregistered, m_not_oper, m_ignore, mo_set, m_ignore }
587 0, MAXPARA, MFLG_SLOW, 0, NULL,
588 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
589 { m_unregistered, m_not_oper, m_ignore, mo_reset, m_ignore }
594 0, MAXPARA, MFLG_SLOW, 0, NULL,
595 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
596 { m_unregistered, m_not_oper, m_ignore, mo_get, m_ignore }
601 0, MAXPARA, MFLG_SLOW, 0, NULL,
602 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
603 { m_unregistered, m_not_oper, m_ignore, mo_privs, m_ignore }
608 0, MAXPARA, MFLG_SLOW, 0, NULL,
609 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
610 { m_ignore, m_ignore, ms_account, m_ignore, m_ignore }
615 0, MAXPARA, MFLG_SLOW, 0, NULL,
616 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
617 { m_ignore, m_not_oper, ms_asll, mo_asll, m_ignore }
619 /* This command is an alias for QUIT during the unregistered part of
620 * of the server. This is because someone jumping via a broken web
621 * proxy will send a 'POST' as their first command - which we will
622 * obviously disconnect them immediately for, stopping people abusing
628 0, MAXPARA, MFLG_SLOW, 0, NULL,
629 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
630 { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
636 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
642 * inputs - Pointer to current piece of message tree
643 * - Pointer to struct Message to add at final token position
644 * - Pointer to current portion of cmd or token to add
646 * side effects - recursively build the Message Tree ;-)
649 add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, char *cmd)
651 struct MessageTree *ntree_p;
655 mtree_p->msg = msg_p;
659 if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL)
661 add_msg_element(ntree_p, msg_p, cmd+1);
665 ntree_p = (struct MessageTree *)MyCalloc(sizeof(struct MessageTree), 1);
666 mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = ntree_p;
667 add_msg_element(ntree_p, msg_p, cmd+1);
672 /* This is unused in ircu, trivial to do, but left here for later
684 * side effects - recursively deletes a token from the Message Tree ;-)
687 del_msg_element(struct MessageTree *mtree_p, char *cmd)
689 struct MessageTree *ntree_p;
694 if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL)
696 del_msg_element(ntree_p, cmd+1);
698 mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = NULL;
708 * side effect - zero the msg_tree, recursively populate it
715 memset(&msg_tree, 0, sizeof(msg_tree));
717 for (i = 0; msgtab[i].cmd != NULL ; i++)
719 add_msg_element(&msg_tree, &msgtab[i], msgtab[i].cmd);
720 add_msg_element(&msg_tree, &msgtab[i], msgtab[i].tok);
727 * inputs - pointer to command/token
728 * - pointer to MessageTree root
729 * output - found Message * for this token/command or NULL if not found
731 * Generic tree parser which works for both commands and tokens.
733 * Re-written by Dianora (db) (tail recursive)
736 static struct Message *
737 msg_tree_parse(char *cmd, struct MessageTree *root)
739 struct MessageTree *mtree;
741 for (mtree = root->pointers[(*cmd++) & (MAXPTRLEN-1)];
743 mtree = mtree->pointers[(*cmd++) & (MAXPTRLEN-1)])
745 if ((mtree->msg != NULL) && (*cmd == '\0'))
751 /* Inserts a single entry into a message tree; must use this function
752 when inserting messages at runtime. */
753 static void msg_tree_insert(struct MessageTree *mtree, int pfxlen,
754 char *key, struct Message *mptr)
756 struct MessageTree *child;
765 child = mtree->pointers[c & (MAXPTRLEN-1)];
768 child = (struct MessageTree *)MyCalloc(1, sizeof(struct MessageTree));
769 mtree->pointers[c & (MAXPTRLEN-1)] = child;
771 msg_tree_insert(child, pfxlen+1, key, mptr);
774 /* Removes an entry from the message tree; suitable for use at runtime. */
775 static struct MessageTree *msg_tree_remove(struct MessageTree *root, char *key)
781 struct MessageTree *child = root->pointers[*key & (MAXPTRLEN-1)];
782 if (msg_tree_remove(child, key + 1))
784 root->pointers[*key & (MAXPTRLEN-1)] = NULL;
790 for (c = 0; c < MAXPTRLEN; ++c)
792 if (root->pointers[c])
799 /* Registers a service mapping to the pseudocommand handler. */
800 int register_mapping(struct s_map *map)
804 if (msg_tree_parse(map->command, &msg_tree))
807 msg = (struct Message *)MyMalloc(sizeof(struct Message));
808 msg->cmd = map->command;
809 msg->tok = map->command;
812 msg->flags = MFLG_SLOW | MFLG_EXTRA;
816 msg->handlers[UNREGISTERED_HANDLER] = m_ignore;
817 msg->handlers[CLIENT_HANDLER] = m_pseudo;
818 msg->handlers[SERVER_HANDLER] = m_ignore;
819 msg->handlers[OPER_HANDLER] = m_pseudo;
820 msg->handlers[SERVICE_HANDLER] = m_ignore;
822 /* Service mappings are only applicable to clients; insert the
823 pseudocommand into the command tree only. */
824 msg_tree_insert(&msg_tree, 0, msg->cmd, msg);
830 /* Removes a service mapping. */
831 int unregister_mapping(struct s_map *map)
833 if (!msg_tree_parse(map->command, &msg_tree))
835 /* This simply should never happen. */
840 msg_tree_remove(&msg_tree, map->msg->cmd);
842 map->msg->extra = NULL;
852 * NOTE: parse_*() should not be called recusively by any other functions!
855 parse_client(struct Client *cptr, char *buffer, char *bufend)
857 struct Client* from = cptr;
863 struct Message* mptr;
864 MessageHandler handler = 0;
866 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
871 para[0] = cli_name(from);
872 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
873 if (*ch == ':') /* Is any client doing this ? */
875 for (++ch; *ch && *ch != ' '; ++ch)
876 ; /* Ignore sender prefix from client */
878 ch++; /* Advance to command */
884 ServerStats->is_empt++;
885 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
886 cli_name(cptr), cli_name(from)));
890 if ((s = strchr(ch, ' ')))
893 if ((mptr = msg_tree_parse(ch, &msg_tree)) == NULL)
896 * Note: Give error message *only* to recognized
897 * persons. It's a nightmare situation to have
898 * two programs sending "Unknown command"'s or
899 * equivalent to each other at full blast....
900 * If it has got to person state, it at least
901 * seems to be well behaving. Perhaps this message
902 * should never be generated, though... --msa
903 * Hm, when is the buffer empty -- if a command
904 * code has been found ?? -Armin
906 if (buffer[0] != '\0')
909 send_reply(from, ERR_UNKNOWNCOMMAND, ch);
910 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
911 ch, get_client_name(cptr, HIDE_IP)));
913 ServerStats->is_unco++;
917 paramcount = mptr->parameters;
918 i = bufend - ((s) ? s : ch);
920 if ((mptr->flags & MFLG_SLOW))
921 cli_since(cptr) += (2 + i / 120);
923 * Allow only 1 msg per 2 seconds
924 * (on average) to prevent dumping.
925 * to keep the response rate up,
926 * bursts of up to 5 msgs are allowed
931 * Must the following loop really be so devious? On
932 * surface it splits the message to parameters from
933 * blank spaces. But, if paramcount has been reached,
934 * the rest of the message goes into this last parameter
935 * (about same effect as ":" has...) --msa
938 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
940 if (mptr->flags & MFLG_EXTRA) {
941 /* This is a horrid kludge to avoid changing the command handler
943 para[1] = (char*)mptr->extra;
950 if (paramcount > MAXPARA)
951 paramcount = MAXPARA;
955 * Never "FRANCE " again!! ;-) Clean
956 * out *all* blanks.. --msa
966 * The rest is single parameter--can
967 * include blanks also.
975 for (; *s != ' ' && *s; s++);
981 handler = mptr->handlers[cli_handler(cptr)];
982 assert(0 != handler);
984 if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
985 handler != m_ping && handler != m_ignore)
986 cli_user(from)->last = CurrentTime;
988 return (*handler) (cptr, from, i, para);
991 int parse_server(struct Client *cptr, char *buffer, char *bufend)
993 struct Client* from = cptr;
1000 struct Message* mptr;
1002 Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
1007 para[0] = cli_name(from);
1010 * A server ALWAYS sends a prefix. When it starts with a ':' it's the
1011 * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
1016 /* Let para[0] point to the name of the sender */
1018 if (!(ch = strchr(ch, ' ')))
1022 /* And let `from' point to its client structure,
1023 opps.. a server is _also_ a client --Nem */
1024 from = FindClient(para[0]);
1027 * If the client corresponding to the
1028 * prefix is not found. We must ignore it,
1029 * it is simply a lagged message travelling
1030 * upstream a SQUIT that removed the client
1035 Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
1036 para[0], buffer, cli_name(cptr)));
1037 ++ServerStats->is_unpf;
1041 * However, the only thing that MUST be
1042 * allowed to travel upstream against an
1043 * squit, is an SQUIT itself (the timestamp
1044 * protects us from being used wrong)
1048 para[0] = cli_name(cptr);
1054 else if (cli_from(from) != cptr)
1056 ++ServerStats->is_wrdi;
1057 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1058 buffer, cli_name(cptr)));
1064 char numeric_prefix[6];
1066 for (i = 0; i < 5; ++i)
1068 if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i]))
1073 numeric_prefix[i] = '\0';
1076 * We got a numeric nick as prefix
1077 * 1 or 2 character prefixes are from servers
1078 * 3 or 5 chars are from clients
1082 protocol_violation(cptr,"Missing Prefix");
1085 else if (' ' == ch[1] || ' ' == ch[2])
1086 from = FindNServer(numeric_prefix);
1088 from = findNUser(numeric_prefix);
1094 while (*ch != ' ' && *ch);
1097 * If the client corresponding to the
1098 * prefix is not found. We must ignore it,
1099 * it is simply a lagged message travelling
1100 * upstream a SQUIT that removed the client
1102 * There turned out to be other reasons that
1103 * a prefix is unknown, needing an upstream
1104 * KILL. Also, next to an SQUIT we better
1105 * allow a KILL to pass too.
1110 ServerStats->is_unpf++;
1113 if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1114 /* Only sent a KILL for a nick change */
1116 struct Client *server;
1117 /* Kill the unknown numeric prefix upstream if
1118 * it's server still exists: */
1119 if ((server = FindNServer(numeric_prefix)) && cli_from(server) == cptr)
1120 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1121 numeric_prefix, cli_name(&me));
1124 * Things that must be allowed to travel
1125 * upstream against an squit:
1127 if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1128 (*ch == 'K' && ch[2] == 'L'))
1134 /* Let para[0] point to the name of the sender */
1135 para[0] = cli_name(from);
1137 if (cli_from(from) != cptr)
1139 ServerStats->is_wrdi++;
1140 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1141 buffer, cli_name(cptr)));
1150 ServerStats->is_empt++;
1151 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1152 cli_name(cptr), cli_name(from)));
1157 * Extract the command code from the packet. Point s to the end
1158 * of the command code and calculate the length using pointer
1159 * arithmetic. Note: only need length for numerics and *all*
1160 * numerics must have parameters and thus a space after the command
1163 s = strchr(ch, ' '); /* s -> End of the command code */
1164 len = (s) ? (s - ch) : 0;
1165 if (len == 3 && IsDigit(*ch))
1167 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1168 paramcount = 2; /* destination, and the rest of it */
1169 ServerStats->is_num++;
1170 mptr = NULL; /* Init. to avoid stupid compiler warning :/ */
1177 /* Version Receive Send
1179 * 2.10.0 Tkn/Long Long
1180 * 2.10.10 Tkn/Long Tkn
1183 * Clients/unreg servers always receive/
1184 * send long commands -record
1186 * And for the record, this trie parser really does not care. - Dianora
1189 mptr = msg_tree_parse(ch, &msg_tree);
1194 * Note: Give error message *only* to recognized
1195 * persons. It's a nightmare situation to have
1196 * two programs sending "Unknown command"'s or
1197 * equivalent to each other at full blast....
1198 * If it has got to person state, it at least
1199 * seems to be well behaving. Perhaps this message
1200 * should never be generated, though... --msa
1201 * Hm, when is the buffer empty -- if a command
1202 * code has been found ?? -Armin
1205 if (buffer[0] != '\0')
1207 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1208 ch, get_client_name(cptr, HIDE_IP)));
1211 ServerStats->is_unco++;
1215 paramcount = mptr->parameters;
1216 i = bufend - ((s) ? s : ch);
1220 * Must the following loop really be so devious? On
1221 * surface it splits the message to parameters from
1222 * blank spaces. But, if paramcount has been reached,
1223 * the rest of the message goes into this last parameter
1224 * (about same effect as ":" has...) --msa
1227 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1232 if (paramcount > MAXPARA)
1233 paramcount = MAXPARA;
1237 * Never "FRANCE " again!! ;-) Clean
1238 * out *all* blanks.. --msa
1248 * The rest is single parameter--can
1249 * include blanks also.
1252 para[++i] = s; /* preserve the colon to make do_numeric happy */
1258 if (i >= paramcount)
1260 for (; *s != ' ' && *s; s++);
1265 return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1268 return (*mptr->handlers[cli_handler(cptr)]) (cptr, from, i, para);