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.
32 #include "ircd_alloc.h"
33 #include "ircd_chattr.h"
34 #include "ircd_features.h"
35 #include "ircd_reply.h"
36 #include "ircd_string.h"
41 #include "querycmds.h"
47 #include "s_numeric.h"
60 * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
61 * Adapted to Undernet, adding token support, etc by comstud 10/06/97
63 * completely rewritten June 2, 2003 - Dianora
65 * This has always just been a trie. Look at volume III of Knuth ACP
68 * ok, you start out with an array of pointers, each one corresponds
69 * to a letter at the current position in the command being examined.
71 * so roughly you have this for matching 'trie' or 'tie'
73 * 't' points -> [MessageTree *] 'r' -> [MessageTree *] -> 'i'
74 * -> [MessageTree *] -> [MessageTree *] -> 'e' and matches
76 * 'i' -> [MessageTree *] -> 'e' and matches
79 /** Number of children under a trie node. */
80 #define MAXPTRLEN 32 /* Must be a power of 2, and
81 * larger than 26 [a-z]|[A-Z]
82 * its used to allocate the set
83 * of pointers at each node of the tree
84 * There are MAXPTRLEN pointers at each node.
85 * Obviously, there have to be more pointers
86 * Than ASCII letters. 32 is a nice number
87 * since there is then no need to shift
88 * 'A'/'a' to base 0 index, at the expense
89 * of a few never used pointers. For a small
90 * parser like this, this is a good compromise
91 * and does make it somewhat faster.
96 /** Node in the command lookup trie. */
98 struct Message *msg; /**< Message (if any) if the string ends now. */
99 struct MessageTree *pointers[MAXPTRLEN]; /**< Child nodes for each letter. */
102 /** Root of command lookup trie. */
103 static struct MessageTree msg_tree;
105 /** Array of all supported commands. */
106 struct Message msgtab[] = {
110 0, MAXPARA, MFLG_SLOW, 0, NULL,
111 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
112 { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
117 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
118 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
119 { m_nick, m_nick, ms_nick, m_nick, m_ignore }
124 0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0, NULL,
125 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
126 { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
131 0, MAXPARA, MFLG_SLOW, 0, NULL,
132 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
133 { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
138 0, MAXPARA, MFLG_SLOW, 0, NULL,
139 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
140 { m_unregistered, m_wallvoices, ms_wallvoices, m_wallvoices, m_ignore }
145 0, MAXPARA, MFLG_SLOW, 0, NULL,
146 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
147 { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
152 0, MAXPARA, MFLG_SLOW, 0, NULL,
153 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
154 { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
159 0, MAXPARA, MFLG_SLOW, 0, NULL,
160 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
161 { m_unregistered, m_join, ms_join, m_join, m_ignore }
166 0, MAXPARA, MFLG_SLOW, 0, NULL,
167 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
168 { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
173 0, MAXPARA, MFLG_SLOW, 0, NULL,
174 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
175 { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
180 0, MAXPARA, MFLG_SLOW, 0, NULL,
181 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
182 { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
187 0, MAXPARA, MFLG_SLOW, 0, NULL,
188 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
189 { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
194 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
195 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
196 { m_quit, m_quit, ms_quit, m_quit, m_ignore }
201 0, MAXPARA, MFLG_SLOW, 0, NULL,
202 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
203 { m_unregistered, m_part, ms_part, m_part, m_ignore }
208 0, MAXPARA, MFLG_SLOW, 0, NULL,
209 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
210 { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
215 0, MAXPARA, MFLG_SLOW, 0, NULL,
216 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
217 { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
222 0, MAXPARA, MFLG_SLOW, 0, NULL,
223 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
224 { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
229 0, MAXPARA, MFLG_SLOW, 0, NULL,
230 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
231 { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
236 0, MAXPARA, MFLG_SLOW, 0, NULL,
237 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
238 { m_unregistered, m_not_oper, ms_wallusers, mo_wallusers, m_ignore }
243 0, MAXPARA, MFLG_SLOW, 0, NULL,
244 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
245 { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
250 0, MAXPARA, MFLG_SLOW, 0, NULL,
251 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
252 { m_unregistered, m_ping, ms_ping, mo_ping, m_ignore }
257 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
258 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
259 { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
264 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
265 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
266 { mr_error, m_ignore, ms_error, m_ignore, m_ignore }
271 0, MAXPARA, MFLG_SLOW, 0, NULL,
272 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
273 { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
278 0, MAXPARA, MFLG_SLOW, 0, NULL,
279 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
280 { m_user, m_registered, m_ignore, m_registered, m_ignore }
285 0, MAXPARA, MFLG_SLOW, 0, NULL,
286 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
287 { m_unregistered, m_away, ms_away, m_away, m_ignore }
292 0, 1, MFLG_SLOW, 0, NULL,
293 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
294 { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
299 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
300 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
301 { mr_server, m_registered, ms_server, m_registered, m_ignore }
306 0, MAXPARA, MFLG_SLOW, 0, NULL,
307 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
308 { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
313 0, MAXPARA, MFLG_SLOW, 0, NULL,
314 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
315 { m_unregistered, m_whois, ms_whois, m_whois, m_ignore }
320 0, MAXPARA, MFLG_SLOW, 0, NULL,
321 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
322 { m_unregistered, m_who, m_ignore, m_who, m_ignore }
327 0, MAXPARA, MFLG_SLOW, 0, NULL,
328 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
329 { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
334 0, MAXPARA, MFLG_SLOW, 0, NULL,
335 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
336 { m_unregistered, m_list, m_ignore, m_list, m_ignore }
341 0, MAXPARA, MFLG_SLOW, 0, NULL,
342 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
343 { m_unregistered, m_names, ms_names, m_names, m_ignore }
348 0, 1, MFLG_SLOW, 0, NULL,
349 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
350 { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
355 0, 1, MFLG_SLOW, 0, NULL,
356 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
357 { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
362 0, MAXPARA, MFLG_SLOW, 0, NULL,
363 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
364 { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
369 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
370 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
371 { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
376 0, MAXPARA, MFLG_SLOW, 0, NULL,
377 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
378 { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
383 0, MAXPARA, MFLG_SLOW, 0, NULL,
384 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
385 { m_unregistered, m_time, m_time, m_time, m_ignore }
390 0, MAXPARA, MFLG_SLOW, 0, NULL,
391 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
392 { m_unregistered, m_ignore, ms_settime, mo_settime, m_ignore }
397 0, MAXPARA, MFLG_SLOW, 0, NULL,
398 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
399 { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
404 0, MAXPARA, MFLG_SLOW, 0, NULL,
405 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
406 { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
411 0, MAXPARA, MFLG_SLOW, 0, NULL,
412 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
413 { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
418 0, MAXPARA, MFLG_SLOW, 0, NULL,
419 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
420 { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
425 0, MAXPARA, MFLG_SLOW, 0, NULL,
426 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
427 { m_unregistered, m_map, m_ignore, m_map, m_ignore }
432 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
433 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
434 { m_version, m_version, ms_version, mo_version, m_ignore }
439 0, MAXPARA, MFLG_SLOW, 0, NULL,
440 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
441 { m_unregistered, m_stats, m_stats, m_stats, m_ignore }
446 0, MAXPARA, MFLG_SLOW, 0, NULL,
447 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
448 { m_unregistered, m_links, ms_links, m_links, m_ignore }
453 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
454 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
455 { m_admin, m_admin, ms_admin, mo_admin, m_ignore }
460 0, MAXPARA, MFLG_SLOW, 0, NULL,
461 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
462 { m_unregistered, m_help, m_ignore, m_help, m_ignore }
467 0, MAXPARA, MFLG_SLOW, 0, NULL,
468 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
469 { m_unregistered, m_info, ms_info, mo_info, m_ignore }
474 0, MAXPARA, MFLG_SLOW, 0, NULL,
475 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
476 { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
481 0, MAXPARA, MFLG_SLOW, 0, NULL,
482 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
483 { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
488 0, MAXPARA, MFLG_SLOW, 0, NULL,
489 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
490 { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
495 0, MAXPARA, 0, 0, NULL,
496 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
497 { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
502 0, MAXPARA, MFLG_SLOW, 0, NULL,
503 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
504 { m_unregistered, m_not_oper, ms_jupe, mo_jupe, m_ignore }
509 0, MAXPARA, MFLG_SLOW, 0, NULL,
510 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
511 { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
516 0, MAXPARA, MFLG_SLOW, 0, NULL,
517 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
518 { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
523 0, MAXPARA, MFLG_SLOW, 0, NULL,
524 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
525 { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
530 0, MAXPARA, MFLG_SLOW, 0, NULL,
531 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
532 { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
535 MSG_END_OF_BURST_ACK,
536 TOK_END_OF_BURST_ACK,
537 0, MAXPARA, MFLG_SLOW, 0, NULL,
538 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
539 { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
544 0, MAXPARA, MFLG_SLOW, 0, NULL,
545 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
546 { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
551 0, MAXPARA, MFLG_SLOW, 0, NULL,
552 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
553 { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
558 0, MAXPARA, MFLG_SLOW, 0, NULL,
559 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
560 { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
565 0, MAXPARA, MFLG_SLOW, 0, NULL,
566 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
567 { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
572 0, MAXPARA, MFLG_SLOW, 0, NULL,
573 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
574 { m_proto, m_proto, m_proto, m_proto, m_ignore }
579 0, MAXPARA, MFLG_SLOW, 0, NULL,
580 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
581 { m_unregistered, m_not_oper, m_ignore, mo_set, m_ignore }
586 0, MAXPARA, MFLG_SLOW, 0, NULL,
587 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
588 { m_unregistered, m_not_oper, m_ignore, mo_reset, m_ignore }
593 0, MAXPARA, MFLG_SLOW, 0, NULL,
594 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
595 { m_unregistered, m_not_oper, m_ignore, mo_get, m_ignore }
600 0, MAXPARA, MFLG_SLOW, 0, NULL,
601 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
602 { m_unregistered, m_not_oper, m_ignore, mo_privs, m_ignore }
607 0, MAXPARA, MFLG_SLOW, 0, NULL,
608 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
609 { m_ignore, m_ignore, ms_account, m_ignore, m_ignore }
614 0, MAXPARA, MFLG_SLOW, 0, NULL,
615 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
616 { m_ignore, m_not_oper, ms_asll, mo_asll, m_ignore }
618 /* This command is an alias for QUIT during the unregistered part of
619 * of the server. This is because someone jumping via a broken web
620 * proxy will send a 'POST' as their first command - which we will
621 * obviously disconnect them immediately for, stopping people abusing
627 0, MAXPARA, MFLG_SLOW, 0, NULL,
628 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
629 { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
634 /** Array of command parameters. */
635 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
638 /** Add a message to the lookup trie.
639 * @param[in,out] mtree_p Trie node to insert under.
640 * @param[in] msg_p Message to insert.
641 * @param[in] cmd Text of command to insert.
644 add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, char *cmd)
646 struct MessageTree *ntree_p;
650 mtree_p->msg = msg_p;
654 if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL)
656 add_msg_element(ntree_p, msg_p, cmd+1);
660 ntree_p = (struct MessageTree *)MyCalloc(sizeof(struct MessageTree), 1);
661 mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = ntree_p;
662 add_msg_element(ntree_p, msg_p, cmd+1);
666 /** Remove a message from the lookup trie.
667 * @param[in,out] mtree_p Trie node to remove command from.
668 * @param[in] cmd Text of command to remove.
671 del_msg_element(struct MessageTree *mtree_p, char *cmd)
673 struct MessageTree *ntree_p;
678 if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL)
680 del_msg_element(ntree_p, cmd+1);
682 mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = NULL;
686 /** Initialize the message lookup trie with all known commands. */
692 memset(&msg_tree, 0, sizeof(msg_tree));
694 for (i = 0; msgtab[i].cmd != NULL ; i++)
696 add_msg_element(&msg_tree, &msgtab[i], msgtab[i].cmd);
697 add_msg_element(&msg_tree, &msgtab[i], msgtab[i].tok);
701 /** Look up a command in the message trie.
702 * @param cmd Text of command to look up.
703 * @param root Root of message trie.
704 * @return Pointer to matching message, or NULL if non exists.
706 static struct Message *
707 msg_tree_parse(char *cmd, struct MessageTree *root)
709 struct MessageTree *mtree;
711 for (mtree = root; mtree; mtree = mtree->pointers[(*cmd++) & (MAXPTRLEN-1)]) {
712 if (*cmd == '\0' && mtree->msg)
714 else if (!IsAlpha(*cmd))
720 /** Registers a service mapping to the pseudocommand handler.
721 * @param[in] map Service mapping to add.
722 * @return Non-zero on success; zero if a command already used the name.
724 int register_mapping(struct s_map *map)
728 if (msg_tree_parse(map->command, &msg_tree))
731 msg = (struct Message *)MyMalloc(sizeof(struct Message));
732 msg->cmd = map->command;
733 msg->tok = map->command;
736 msg->flags = MFLG_SLOW | MFLG_EXTRA;
740 msg->handlers[UNREGISTERED_HANDLER] = m_ignore;
741 msg->handlers[CLIENT_HANDLER] = m_pseudo;
742 msg->handlers[SERVER_HANDLER] = m_ignore;
743 msg->handlers[OPER_HANDLER] = m_pseudo;
744 msg->handlers[SERVICE_HANDLER] = m_ignore;
746 /* Service mappings are only applicable to clients; insert the
747 pseudocommand into the command tree only. */
748 add_msg_element(&msg_tree, msg, msg->cmd);
754 /** Removes a service mapping.
755 * @param[in] map Service mapping to remove.
756 * @return Non-zero on success; zero if no command used the name.
758 int unregister_mapping(struct s_map *map)
760 if (!msg_tree_parse(map->command, &msg_tree))
762 /* This simply should never happen. */
767 del_msg_element(&msg_tree, map->msg->cmd);
769 map->msg->extra = NULL;
776 /** Parse a line of data from a user.
777 * NOTE: parse_*() should not be called recusively by any other functions!
778 * @param[in] cptr Client that sent the data.
779 * @param[in] buffer Start of input line.
780 * @param[in] bufend End of input line.
781 * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
782 * handler returns it.
785 parse_client(struct Client *cptr, char *buffer, char *bufend)
787 struct Client* from = cptr;
793 struct Message* mptr;
794 MessageHandler handler = 0;
796 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
801 para[0] = cli_name(from);
802 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
803 if (*ch == ':') /* Is any client doing this ? */
805 for (++ch; *ch && *ch != ' '; ++ch)
806 ; /* Ignore sender prefix from client */
808 ch++; /* Advance to command */
814 ServerStats->is_empt++;
815 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
816 cli_name(cptr), cli_name(from)));
820 if ((s = strchr(ch, ' ')))
823 if ((mptr = msg_tree_parse(ch, &msg_tree)) == NULL)
826 * Note: Give error message *only* to recognized
827 * persons. It's a nightmare situation to have
828 * two programs sending "Unknown command"'s or
829 * equivalent to each other at full blast....
830 * If it has got to person state, it at least
831 * seems to be well behaving. Perhaps this message
832 * should never be generated, though... --msa
833 * Hm, when is the buffer empty -- if a command
834 * code has been found ?? -Armin
836 if (buffer[0] != '\0')
839 send_reply(from, ERR_UNKNOWNCOMMAND, ch);
840 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
841 ch, get_client_name(cptr, HIDE_IP)));
843 ServerStats->is_unco++;
847 paramcount = mptr->parameters;
848 i = bufend - ((s) ? s : ch);
850 if ((mptr->flags & MFLG_SLOW))
851 cli_since(cptr) += (2 + i / 120);
853 * Allow only 1 msg per 2 seconds
854 * (on average) to prevent dumping.
855 * to keep the response rate up,
856 * bursts of up to 5 msgs are allowed
861 * Must the following loop really be so devious? On
862 * surface it splits the message to parameters from
863 * blank spaces. But, if paramcount has been reached,
864 * the rest of the message goes into this last parameter
865 * (about same effect as ":" has...) --msa
868 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
870 if (mptr->flags & MFLG_EXTRA) {
871 /* This is a horrid kludge to avoid changing the command handler
873 para[1] = (char*)mptr->extra;
880 if (paramcount > MAXPARA)
881 paramcount = MAXPARA;
885 * Never "FRANCE " again!! ;-) Clean
886 * out *all* blanks.. --msa
896 * The rest is single parameter--can
897 * include blanks also.
905 for (; *s != ' ' && *s; s++);
911 handler = mptr->handlers[cli_handler(cptr)];
912 assert(0 != handler);
914 if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
915 handler != m_ping && handler != m_ignore)
916 cli_user(from)->last = CurrentTime;
918 return (*handler) (cptr, from, i, para);
921 /** Parse a line of data from a server.
922 * @param[in] cptr Client that sent the data.
923 * @param[in] buffer Start of input line.
924 * @param[in] bufend End of input line.
925 * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
926 * handler returns it.
928 int parse_server(struct Client *cptr, char *buffer, char *bufend)
930 struct Client* from = cptr;
937 struct Message* mptr;
939 Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
944 para[0] = cli_name(from);
947 * A server ALWAYS sends a prefix. When it starts with a ':' it's the
948 * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
953 /* Let para[0] point to the name of the sender */
955 if (!(ch = strchr(ch, ' ')))
959 /* And let `from' point to its client structure,
960 opps.. a server is _also_ a client --Nem */
961 from = FindClient(para[0]);
964 * If the client corresponding to the
965 * prefix is not found. We must ignore it,
966 * it is simply a lagged message travelling
967 * upstream a SQUIT that removed the client
972 Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
973 para[0], buffer, cli_name(cptr)));
974 ++ServerStats->is_unpf;
978 * However, the only thing that MUST be
979 * allowed to travel upstream against an
980 * squit, is an SQUIT itself (the timestamp
981 * protects us from being used wrong)
985 para[0] = cli_name(cptr);
991 else if (cli_from(from) != cptr)
993 ++ServerStats->is_wrdi;
994 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
995 buffer, cli_name(cptr)));
1001 char numeric_prefix[6];
1003 for (i = 0; i < 5; ++i)
1005 if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i]))
1010 numeric_prefix[i] = '\0';
1013 * We got a numeric nick as prefix
1014 * 1 or 2 character prefixes are from servers
1015 * 3 or 5 chars are from clients
1019 protocol_violation(cptr,"Missing Prefix");
1022 else if (' ' == ch[1] || ' ' == ch[2])
1023 from = FindNServer(numeric_prefix);
1025 from = findNUser(numeric_prefix);
1031 while (*ch != ' ' && *ch);
1034 * If the client corresponding to the
1035 * prefix is not found. We must ignore it,
1036 * it is simply a lagged message travelling
1037 * upstream a SQUIT that removed the client
1039 * There turned out to be other reasons that
1040 * a prefix is unknown, needing an upstream
1041 * KILL. Also, next to an SQUIT we better
1042 * allow a KILL to pass too.
1047 ServerStats->is_unpf++;
1050 if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1051 /* Only sent a KILL for a nick change */
1053 struct Client *server;
1054 /* Kill the unknown numeric prefix upstream if
1055 * it's server still exists: */
1056 if ((server = FindNServer(numeric_prefix)) && cli_from(server) == cptr)
1057 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1058 numeric_prefix, cli_name(&me));
1061 * Things that must be allowed to travel
1062 * upstream against an squit:
1064 if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1065 (*ch == 'K' && ch[2] == 'L'))
1071 /* Let para[0] point to the name of the sender */
1072 para[0] = cli_name(from);
1074 if (cli_from(from) != cptr)
1076 ServerStats->is_wrdi++;
1077 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1078 buffer, cli_name(cptr)));
1087 ServerStats->is_empt++;
1088 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1089 cli_name(cptr), cli_name(from)));
1094 * Extract the command code from the packet. Point s to the end
1095 * of the command code and calculate the length using pointer
1096 * arithmetic. Note: only need length for numerics and *all*
1097 * numerics must have parameters and thus a space after the command
1100 s = strchr(ch, ' '); /* s -> End of the command code */
1101 len = (s) ? (s - ch) : 0;
1102 if (len == 3 && IsDigit(*ch))
1104 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1105 paramcount = 2; /* destination, and the rest of it */
1106 ServerStats->is_num++;
1107 mptr = NULL; /* Init. to avoid stupid compiler warning :/ */
1114 /* Version Receive Send
1116 * 2.10.0 Tkn/Long Long
1117 * 2.10.10 Tkn/Long Tkn
1120 * Clients/unreg servers always receive/
1121 * send long commands -record
1123 * And for the record, this trie parser really does not care. - Dianora
1126 mptr = msg_tree_parse(ch, &msg_tree);
1131 * Note: Give error message *only* to recognized
1132 * persons. It's a nightmare situation to have
1133 * two programs sending "Unknown command"'s or
1134 * equivalent to each other at full blast....
1135 * If it has got to person state, it at least
1136 * seems to be well behaving. Perhaps this message
1137 * should never be generated, though... --msa
1138 * Hm, when is the buffer empty -- if a command
1139 * code has been found ?? -Armin
1142 if (buffer[0] != '\0')
1144 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1145 ch, get_client_name(cptr, HIDE_IP)));
1148 ServerStats->is_unco++;
1152 paramcount = mptr->parameters;
1153 i = bufend - ((s) ? s : ch);
1157 * Must the following loop really be so devious? On
1158 * surface it splits the message to parameters from
1159 * blank spaces. But, if paramcount has been reached,
1160 * the rest of the message goes into this last parameter
1161 * (about same effect as ":" has...) --msa
1164 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1169 if (paramcount > MAXPARA)
1170 paramcount = MAXPARA;
1174 * Never "FRANCE " again!! ;-) Clean
1175 * out *all* blanks.. --msa
1185 * The rest is single parameter--can
1186 * include blanks also.
1189 para[++i] = s; /* preserve the colon to make do_numeric happy */
1195 if (i >= paramcount)
1197 for (; *s != ' ' && *s; s++);
1202 return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1205 return (*mptr->handlers[cli_handler(cptr)]) (cptr, from, i, para);