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.
28 #include "ircd_alloc.h"
29 #include "ircd_chattr.h"
30 #include "ircd_features.h"
31 #include "ircd_reply.h"
32 #include "ircd_string.h"
37 #include "querycmds.h"
43 #include "s_numeric.h"
60 struct MessageTree *pointers[26];
64 struct Message msgtab[] = {
68 0, MAXPARA, MFLG_SLOW, 0,
69 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
70 { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
75 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
76 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
77 { m_nick, m_nick, ms_nick, m_nick, m_ignore }
82 0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0,
83 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
84 { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
89 0, MAXPARA, MFLG_SLOW, 0,
90 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
91 { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
96 0, MAXPARA, MFLG_SLOW, 0,
97 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
98 { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
103 0, MAXPARA, MFLG_SLOW, 0,
104 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
105 { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
110 0, MAXPARA, MFLG_SLOW, 0,
111 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
112 { m_unregistered, m_join, ms_join, m_join, m_ignore }
117 0, MAXPARA, MFLG_SLOW, 0,
118 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
119 { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
124 0, MAXPARA, MFLG_SLOW, 0,
125 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
126 { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
131 0, MAXPARA, MFLG_SLOW, 0,
132 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
133 { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
138 0, MAXPARA, MFLG_SLOW, 0,
139 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
140 { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
145 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
146 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
147 { m_quit, m_quit, ms_quit, m_quit, m_ignore }
152 0, MAXPARA, MFLG_SLOW, 0,
153 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
154 { m_unregistered, m_part, ms_part, m_part, m_ignore }
159 0, MAXPARA, MFLG_SLOW, 0,
160 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
161 { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
166 0, MAXPARA, MFLG_SLOW, 0,
167 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
168 { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
173 0, MAXPARA, MFLG_SLOW, 0,
174 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
175 { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
180 0, MAXPARA, MFLG_SLOW, 0,
181 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
182 { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
187 0, MAXPARA, MFLG_SLOW, 0,
188 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
189 { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
194 0, MAXPARA, MFLG_SLOW, 0,
195 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
196 { m_unregistered, m_ping, ms_ping, m_ping, m_ignore }
201 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
202 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
203 { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
208 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
209 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
210 { m_ignore, m_ignore, ms_error, m_ignore, m_ignore }
215 0, MAXPARA, MFLG_SLOW, 0,
216 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
217 { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
222 0, MAXPARA, MFLG_SLOW, 0,
223 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
224 { m_user, m_registered, m_ignore, m_registered, m_ignore }
229 0, MAXPARA, MFLG_SLOW, 0,
230 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
231 { m_unregistered, m_away, ms_away, m_away, m_ignore }
237 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
238 { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
243 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
244 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
245 { mr_server, m_registered, ms_server, m_registered, m_ignore }
250 0, MAXPARA, MFLG_SLOW, 0,
251 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
252 { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
257 0, MAXPARA, MFLG_SLOW, 0,
258 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
259 { m_unregistered, m_whois, m_whois, m_whois, m_ignore }
264 0, MAXPARA, MFLG_SLOW, 0,
265 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
266 { m_unregistered, m_who, m_ignore, m_who, m_ignore }
271 0, MAXPARA, MFLG_SLOW, 0,
272 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
273 { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
278 0, MAXPARA, MFLG_SLOW, 0,
279 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
280 { m_unregistered, m_list, m_ignore, m_list, m_ignore }
285 0, MAXPARA, MFLG_SLOW, 0,
286 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
287 { m_unregistered, m_names, ms_names, m_names, m_ignore }
293 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
294 { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
300 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
301 { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
306 0, MAXPARA, MFLG_SLOW, 0,
307 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
308 { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
313 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
314 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
315 { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
320 0, MAXPARA, MFLG_SLOW, 0,
321 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
322 { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
327 0, MAXPARA, MFLG_SLOW, 0,
328 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
329 { m_unregistered, m_time, m_time, m_time, m_ignore }
334 0, MAXPARA, MFLG_SLOW, 0,
335 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
336 { m_unregistered, m_ignore, ms_settime, mo_settime, m_ignore }
341 0, MAXPARA, MFLG_SLOW, 0,
342 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
343 { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
348 0, MAXPARA, MFLG_SLOW, 0,
349 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
350 { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
355 0, MAXPARA, MFLG_SLOW, 0,
356 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
357 { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
362 0, MAXPARA, MFLG_SLOW, 0,
363 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
364 { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
369 0, MAXPARA, MFLG_SLOW, 0,
370 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
371 { m_unregistered, m_map, m_ignore, m_map, m_ignore }
376 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
377 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
378 { m_version, m_version, ms_version, m_version, m_ignore }
383 0, MAXPARA, MFLG_SLOW, 0,
384 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
385 { m_unregistered, m_stats, ms_stats, mo_stats, m_ignore }
390 0, MAXPARA, MFLG_SLOW, 0,
391 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
392 { m_unregistered, m_links, ms_links, m_links, m_ignore }
397 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
398 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
399 { m_admin, m_admin, ms_admin, m_admin, m_ignore }
404 0, MAXPARA, MFLG_SLOW, 0,
405 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
406 { m_unregistered, m_help, m_ignore, m_help, m_ignore }
411 0, MAXPARA, MFLG_SLOW, 0,
412 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
413 { m_unregistered, m_info, ms_info, mo_info, m_ignore }
418 0, MAXPARA, MFLG_SLOW, 0,
419 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
420 { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
425 0, MAXPARA, MFLG_SLOW, 0,
426 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
427 { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
432 0, MAXPARA, MFLG_SLOW, 0,
433 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
434 { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
439 0, MAXPARA, MFLG_SLOW, 0,
440 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
441 { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
446 0, MAXPARA, MFLG_SLOW, 0,
447 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
448 { m_unregistered, m_jupe, ms_jupe, mo_jupe, m_ignore }
453 0, MAXPARA, MFLG_SLOW, 0,
454 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
455 { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
460 0, MAXPARA, MFLG_SLOW, 0,
461 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
462 { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
467 0, MAXPARA, MFLG_SLOW, 0,
468 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
469 { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
474 0, MAXPARA, MFLG_SLOW, 0,
475 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
476 { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
479 MSG_END_OF_BURST_ACK,
480 TOK_END_OF_BURST_ACK,
482 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
483 { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
488 0, MAXPARA, MFLG_SLOW, 0,
489 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
490 { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
495 0, MAXPARA, MFLG_SLOW, 0,
496 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
497 { m_unregistered, m_dns, m_dns, m_dns, m_ignore }
502 0, MAXPARA, MFLG_SLOW, 0,
503 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
504 { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
509 0, MAXPARA, MFLG_SLOW, 0,
510 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
511 { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
516 0, MAXPARA, MFLG_SLOW, 0,
517 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
518 { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
523 0, MAXPARA, MFLG_SLOW, 0,
524 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
525 { m_proto, m_proto, m_proto, m_proto, m_ignore }
530 0, MAXPARA, MFLG_SLOW, 0,
531 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
532 { m_unregistered, m_not_oper, m_ignore, mo_set, m_ignore }
537 0, MAXPARA, MFLG_SLOW, 0,
538 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
539 { m_unregistered, m_not_oper, m_ignore, mo_reset, m_ignore }
544 0, MAXPARA, MFLG_SLOW, 0,
545 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
546 { m_unregistered, m_not_oper, m_ignore, mo_get, m_ignore }
551 0, MAXPARA, MFLG_SLOW, 0,
552 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
553 { m_unregistered, m_not_oper, m_ignore, mo_privs, m_ignore }
555 /* This command is an alias for QUIT during the unregistered part of
556 * of the server. This is because someone jumping via a broken web
557 * proxy will send a 'POST' as their first command - which we will
558 * obviously disconnect them immediately for, stopping people abusing
564 0, MAXPARA, MFLG_SLOW, 0,
565 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
566 { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
572 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
575 * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
576 * Adapted to Undernet, adding token support, etc by comstud 10/06/97
579 static struct MessageTree msg_tree_cmd;
580 static struct MessageTree msg_tree_tok;
583 * Guts of making the token tree...
585 static struct Message **do_msg_tree_tok(struct MessageTree *mtree, char *prefix,
586 struct Message **mptr)
588 char newprefix[64]; /* Must be longer than every command name */
590 struct MessageTree *mtree1;
593 if (!lp || !strncmp((*mptr)->tok, prefix, lp))
595 if (!mptr[1] || (lp && strncmp(mptr[1]->tok, prefix, lp)))
597 /* last command in the message struct or last command in this prefix */
598 mtree->final = (*mptr)->tok + lp;
600 for (c = 0; c < 26; ++c)
601 mtree->pointers[c] = NULL;
604 /* command in this prefix */
605 if (0 == ircd_strcmp((*mptr)->tok, prefix))
608 mtree->msg = *mptr++;
613 for (c = 'A'; c <= 'Z'; ++c)
615 if ((*mptr)->tok[lp] == c)
617 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
618 mtree1->final = NULL;
619 mtree->pointers[c - 'A'] = mtree1;
620 strcpy(newprefix, prefix);
622 newprefix[lp + 1] = '\0';
623 mptr = do_msg_tree_tok(mtree1, newprefix, mptr);
624 if (!*mptr || strncmp((*mptr)->tok, prefix, lp))
626 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
627 mtree->pointers[c2] = NULL;
632 mtree->pointers[c - 'A'] = NULL;
637 * XXX - should never be here, quick hack, this can be done better
644 * Guts of making the command tree...
646 static struct Message *do_msg_tree_cmd(struct MessageTree *mtree, char *prefix,
647 struct Message *mptr)
649 char newprefix[64]; /* Must be longer than every command name */
651 struct MessageTree *mtree1;
654 if (!lp || !strncmp(mptr->cmd, prefix, lp))
656 if (!mptr[1].cmd || (lp && strncmp(mptr[1].cmd, prefix, lp)))
658 /* last command in the message struct or last command in this prefix */
659 mtree->final = mptr->cmd + lp;
661 for (c = 0; c < 26; ++c)
662 mtree->pointers[c] = NULL;
665 /* command in this prefix */
666 if (0 == ircd_strcmp(mptr->cmd, prefix))
674 for (c = 'A'; c <= 'Z'; ++c)
676 if (mptr->cmd[lp] == c)
678 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
679 mtree1->final = NULL;
680 mtree->pointers[c - 'A'] = mtree1;
681 strcpy(newprefix, prefix);
683 newprefix[lp + 1] = '\0';
684 mptr = do_msg_tree_cmd(mtree1, newprefix, mptr);
685 if (!mptr->cmd || strncmp(mptr->cmd, prefix, lp))
687 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
688 mtree->pointers[c2] = NULL;
693 mtree->pointers[c - 'A'] = NULL;
698 * This should never happen
704 static int mcmdcmp(const struct Message *m1, const struct Message *m2)
706 return strcmp(m1->cmd, m2->cmd);
709 static int mtokcmp(const struct Message **m1, const struct Message **m2)
711 return strcmp((*m1)->tok, (*m2)->tok);
715 * Sort the command names.
716 * Create table of pointers into msgtab for tokens.
717 * Create trees for ->cmd and ->tok and free the token pointers.
719 void initmsgtree(void)
722 struct Message *msg = msgtab;
724 struct Message **msgtab_tok;
725 struct Message **msgtok;
727 for (i = 0; msg->cmd; ++i, ++msg)
729 qsort(msgtab, i, sizeof(struct Message),
730 (int (*)(const void *, const void *))mcmdcmp);
731 msgtab_tok = (struct Message **)MyMalloc((i + 1) * sizeof(struct Message *));
732 for (ii = 0; ii < i; ++ii)
733 msgtab_tok[ii] = msgtab + ii;
734 msgtab_tok[i] = NULL; /* Needed by `do_msg_tree_tok' */
735 qsort(msgtab_tok, i, sizeof(struct Message *),
736 (int (*)(const void *, const void *))mtokcmp);
737 msg = do_msg_tree_cmd(&msg_tree_cmd, "", msgtab);
738 msgtok = do_msg_tree_tok(&msg_tree_tok, "", msgtab_tok);
743 * Generic tree parser which works for both commands and tokens.
746 static struct Message *msg_tree_parse(char *cmd, struct MessageTree *root)
748 struct MessageTree *mtree;
749 unsigned char r = (0xdf & (unsigned char)*cmd) - 'A';
750 if (r > 25 || !(mtree = root->pointers[r]))
754 r = 0xdf & (unsigned char)*++cmd;
755 if (mtree->final && *mtree->final == r)
757 if ((r -= 'A') > 25 || !(mtree = mtree->pointers[r]))
763 * This one is identical to the one above, but it is slower because it
764 * makes sure that `cmd' matches the _full_ command, exactly.
765 * This is to avoid confusion with commands like /quake on clients
766 * that send unknown commands directly to the server.
768 static struct Message *msg_tree_parse_client(char *cmd,
769 struct MessageTree *root)
771 struct MessageTree *mtree;
772 unsigned char q = (0xdf & (unsigned char)*cmd) - 'A';
773 if (q > 25 || !(mtree = root->pointers[q]))
777 q = 0xdf & (unsigned char)*++cmd;
778 if (mtree->final && 0 == ircd_strcmp(mtree->final, cmd))
780 if ((q -= 'A') > 25 || !(mtree = mtree->pointers[q]))
788 * NOTE: parse_*() should not be called recusively by any other fucntions!
790 int parse_client(struct Client *cptr, char *buffer, char *bufend)
792 struct Client* from = cptr;
798 struct Message* mptr;
799 MessageHandler handler = 0;
801 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
806 para[0] = cli_name(from);
807 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
808 if (*ch == ':') /* Is any client doing this ? */
810 for (++ch; *ch && *ch != ' '; ++ch)
811 ; /* Ignore sender prefix from client */
813 ch++; /* Advance to command */
819 ServerStats->is_empt++;
820 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
821 cli_name(cptr), cli_name(from)));
825 if ((s = strchr(ch, ' ')))
829 * This is a client/unregistered entity.
830 * Check long command list only.
832 if (!(mptr = msg_tree_parse_client(ch, &msg_tree_cmd)))
835 * Note: Give error message *only* to recognized
836 * persons. It's a nightmare situation to have
837 * two programs sending "Unknown command"'s or
838 * equivalent to each other at full blast....
839 * If it has got to person state, it at least
840 * seems to be well behaving. Perhaps this message
841 * should never be generated, though... --msa
842 * Hm, when is the buffer empty -- if a command
843 * code has been found ?? -Armin
845 if (buffer[0] != '\0')
848 send_reply(from, ERR_UNKNOWNCOMMAND, ch);
849 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
850 ch, get_client_name(cptr, HIDE_IP)));
852 ServerStats->is_unco++;
856 paramcount = mptr->parameters;
857 i = bufend - ((s) ? s : ch);
859 if ((mptr->flags & MFLG_SLOW))
860 cli_since(cptr) += (2 + i / 120);
862 * Allow only 1 msg per 2 seconds
863 * (on average) to prevent dumping.
864 * to keep the response rate up,
865 * bursts of up to 5 msgs are allowed
870 * Must the following loop really be so devious? On
871 * surface it splits the message to parameters from
872 * blank spaces. But, if paramcount has been reached,
873 * the rest of the message goes into this last parameter
874 * (about same effect as ":" has...) --msa
877 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
882 if (paramcount > MAXPARA)
883 paramcount = MAXPARA;
887 * Never "FRANCE " again!! ;-) Clean
888 * out *all* blanks.. --msa
898 * The rest is single parameter--can
899 * include blanks also.
907 for (; *s != ' ' && *s; s++);
913 handler = mptr->handlers[cli_handler(cptr)];
914 assert(0 != handler);
916 if (!feature_bool(FEAT_IDLE_FROM_MSG) && IsUser(cptr) &&
917 handler != m_ping && handler != m_ignore)
918 cli_user(from)->last = CurrentTime;
920 return (*handler) (cptr, from, i, para);
923 int parse_server(struct Client *cptr, char *buffer, char *bufend)
925 struct Client* from = cptr;
932 struct Message* mptr;
934 Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
939 para[0] = cli_name(from);
942 * A server ALWAYS sends a prefix. When it starts with a ':' it's the
943 * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
948 /* Let para[0] point to the name of the sender */
950 if (!(ch = strchr(ch, ' ')))
954 /* And let `from' point to its client structure,
955 opps.. a server is _also_ a client --Nem */
956 from = FindClient(para[0]);
959 * If the client corresponding to the
960 * prefix is not found. We must ignore it,
961 * it is simply a lagged message travelling
962 * upstream a SQUIT that removed the client
967 Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
968 para[0], buffer, cli_name(cptr)));
969 ++ServerStats->is_unpf;
973 * However, the only thing that MUST be
974 * allowed to travel upstream against an
975 * squit, is an SQUIT itself (the timestamp
976 * protects us from being used wrong)
980 para[0] = cli_name(cptr);
986 else if (cli_from(from) != cptr)
988 ++ServerStats->is_wrdi;
989 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
990 buffer, cli_name(cptr)));
995 char numeric_prefix[6];
997 for (i = 0; i < 5; ++i) {
998 if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i])) {
1002 numeric_prefix[i] = '\0';
1005 * We got a numeric nick as prefix
1006 * 1 or 2 character prefixes are from servers
1007 * 3 or 5 chars are from clients
1010 protocol_violation(cptr,"Missing Prefix");
1013 else if (' ' == ch[1] || ' ' == ch[2])
1014 from = FindNServer(numeric_prefix);
1016 from = findNUser(numeric_prefix);
1022 while (*ch != ' ' && *ch);
1025 * If the client corresponding to the
1026 * prefix is not found. We must ignore it,
1027 * it is simply a lagged message travelling
1028 * upstream a SQUIT that removed the client
1030 * There turned out to be other reasons that
1031 * a prefix is unknown, needing an upstream
1032 * KILL. Also, next to an SQUIT we better
1033 * allow a KILL to pass too.
1038 ServerStats->is_unpf++;
1041 if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1042 /* Only sent a KILL for a nick change */
1044 struct Client *server;
1045 /* Kill the unknown numeric prefix upstream if
1046 * it's server still exists: */
1047 if ((server = FindNServer(numeric_prefix)) && cli_from(server) == cptr)
1048 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1049 numeric_prefix, cli_name(&me));
1052 * Things that must be allowed to travel
1053 * upstream against an squit:
1055 if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1056 (*ch == 'K' && ch[2] == 'L'))
1062 /* Let para[0] point to the name of the sender */
1063 para[0] = cli_name(from);
1065 if (cli_from(from) != cptr)
1067 ServerStats->is_wrdi++;
1068 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1069 buffer, cli_name(cptr)));
1078 ServerStats->is_empt++;
1079 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1080 cli_name(cptr), cli_name(from)));
1085 * Extract the command code from the packet. Point s to the end
1086 * of the command code and calculate the length using pointer
1087 * arithmetic. Note: only need length for numerics and *all*
1088 * numerics must have parameters and thus a space after the command
1091 s = strchr(ch, ' '); /* s -> End of the command code */
1092 len = (s) ? (s - ch) : 0;
1093 if (len == 3 && IsDigit(*ch))
1095 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1096 paramcount = 2; /* destination, and the rest of it */
1097 ServerStats->is_num++;
1098 mptr = NULL; /* Init. to avoid stupid compiler warning :/ */
1105 /* Version Receive Send
1107 * 2.10.0 Tkn/Long Long
1108 * 2.10.10 Tkn/Long Tkn
1111 * Clients/unreg servers always receive/
1112 * send long commands -record
1116 * This is a server. Check the token command list.
1117 * -record!jegelhof@cloud9.net
1119 mptr = msg_tree_parse(ch, &msg_tree_tok);
1121 #if 1 /* for 2.10.0/2.10.10 */
1123 * This code supports 2.9 and 2.10.0 sending long commands.
1124 * It makes more calls to ircd_strcmp() than the above
1125 * so it will be somewhat slower.
1128 mptr = msg_tree_parse(ch, &msg_tree_cmd);
1134 * Note: Give error message *only* to recognized
1135 * persons. It's a nightmare situation to have
1136 * two programs sending "Unknown command"'s or
1137 * equivalent to each other at full blast....
1138 * If it has got to person state, it at least
1139 * seems to be well behaving. Perhaps this message
1140 * should never be generated, though... --msa
1141 * Hm, when is the buffer empty -- if a command
1142 * code has been found ?? -Armin
1145 if (buffer[0] != '\0')
1147 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1148 ch, get_client_name(cptr, HIDE_IP)));
1151 ServerStats->is_unco++;
1155 paramcount = mptr->parameters;
1156 i = bufend - ((s) ? s : ch);
1160 * Must the following loop really be so devious? On
1161 * surface it splits the message to parameters from
1162 * blank spaces. But, if paramcount has been reached,
1163 * the rest of the message goes into this last parameter
1164 * (about same effect as ":" has...) --msa
1167 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1172 if (paramcount > MAXPARA)
1173 paramcount = MAXPARA;
1177 * Never "FRANCE " again!! ;-) Clean
1178 * out *all* blanks.. --msa
1188 * The rest is single parameter--can
1189 * include blanks also.
1192 para[++i] = s; /* preserve the colon to make do_numeric happy */
1198 if (i >= paramcount)
1200 for (; *s != ' ' && *s; s++);
1205 return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1208 return (*mptr->handlers[cli_handler(cptr)]) (cptr, from, i, para);