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_reply.h"
31 #include "ircd_string.h"
36 #include "querycmds.h"
42 #include "s_numeric.h"
59 struct MessageTree *pointers[26];
63 struct Message msgtab[] = {
67 0, MAXPARA, MFLG_SLOW, 0,
68 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
69 { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
74 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
75 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
76 { m_nick, m_nick, ms_nick, m_nick, m_ignore }
81 0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0,
82 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
83 { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
88 0, MAXPARA, MFLG_SLOW, 0,
89 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
90 { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
95 0, MAXPARA, MFLG_SLOW, 0,
96 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
97 { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
102 0, MAXPARA, MFLG_SLOW, 0,
103 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
104 { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
109 0, MAXPARA, MFLG_SLOW, 0,
110 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
111 { m_unregistered, m_join, ms_join, m_join, m_ignore }
116 0, MAXPARA, MFLG_SLOW, 0,
117 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
118 { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
123 0, MAXPARA, MFLG_SLOW, 0,
124 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
125 { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
130 0, MAXPARA, MFLG_SLOW, 0,
131 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
132 { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
137 0, MAXPARA, MFLG_SLOW, 0,
138 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
139 { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
144 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
145 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
146 { m_quit, m_quit, ms_quit, m_quit, m_ignore }
151 0, MAXPARA, MFLG_SLOW, 0,
152 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
153 { m_unregistered, m_part, ms_part, m_part, m_ignore }
158 0, MAXPARA, MFLG_SLOW, 0,
159 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
160 { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
165 0, MAXPARA, MFLG_SLOW, 0,
166 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
167 { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
172 0, MAXPARA, MFLG_SLOW, 0,
173 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
174 { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
179 0, MAXPARA, MFLG_SLOW, 0,
180 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
181 { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
186 0, MAXPARA, MFLG_SLOW, 0,
187 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
188 { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
193 0, MAXPARA, MFLG_SLOW, 0,
194 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
195 { m_unregistered, m_ping, ms_ping, m_ping, m_ignore }
200 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
201 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
202 { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
207 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
208 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
209 { m_ignore, m_ignore, ms_error, m_ignore, m_ignore }
214 0, MAXPARA, MFLG_SLOW, 0,
215 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
216 { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
221 0, MAXPARA, MFLG_SLOW, 0,
222 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
223 { m_user, m_registered, m_ignore, m_registered, m_ignore }
228 0, MAXPARA, MFLG_SLOW, 0,
229 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
230 { m_unregistered, m_away, ms_away, m_away, m_ignore }
236 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
237 { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
242 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
243 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
244 { mr_server, m_registered, ms_server, m_registered, m_ignore }
249 0, MAXPARA, MFLG_SLOW, 0,
250 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
251 { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
256 0, MAXPARA, MFLG_SLOW, 0,
257 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
258 { m_unregistered, m_whois, m_whois, m_whois, m_ignore }
263 0, MAXPARA, MFLG_SLOW, 0,
264 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
265 { m_unregistered, m_who, m_ignore, m_who, m_ignore }
270 0, MAXPARA, MFLG_SLOW, 0,
271 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
272 { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
277 0, MAXPARA, MFLG_SLOW, 0,
278 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
279 { m_unregistered, m_list, m_ignore, m_list, m_ignore }
284 0, MAXPARA, MFLG_SLOW, 0,
285 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
286 { m_unregistered, m_names, ms_names, m_names, m_ignore }
292 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
293 { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
299 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
300 { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
305 0, MAXPARA, MFLG_SLOW, 0,
306 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
307 { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
312 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
313 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
314 { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
319 0, MAXPARA, MFLG_SLOW, 0,
320 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
321 { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
326 0, MAXPARA, MFLG_SLOW, 0,
327 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
328 { m_unregistered, m_time, m_time, m_time, m_ignore }
333 0, MAXPARA, MFLG_SLOW, 0,
334 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
335 { m_unregistered, m_ignore, ms_settime, mo_settime, m_ignore }
340 0, MAXPARA, MFLG_SLOW, 0,
341 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
342 { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
347 0, MAXPARA, MFLG_SLOW, 0,
348 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
349 { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
354 0, MAXPARA, MFLG_SLOW, 0,
355 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
357 { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
359 { m_unregistered, m_oper, m_ignore, m_ignore, m_ignore }
365 0, MAXPARA, MFLG_SLOW, 0,
366 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
367 { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
372 0, MAXPARA, MFLG_SLOW, 0,
373 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
374 { m_unregistered, m_map, m_ignore, m_map, m_ignore }
379 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
380 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
381 { m_version, m_version, ms_version, m_version, m_ignore }
386 0, MAXPARA, MFLG_SLOW, 0,
387 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
388 { m_unregistered, m_stats, ms_stats, mo_stats, m_ignore }
393 0, MAXPARA, MFLG_SLOW, 0,
394 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
395 { m_unregistered, m_links, ms_links, m_links, m_ignore }
400 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
401 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
402 { m_admin, m_admin, ms_admin, m_admin, m_ignore }
407 0, MAXPARA, MFLG_SLOW, 0,
408 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
409 { m_unregistered, m_help, m_ignore, m_help, m_ignore }
414 0, MAXPARA, MFLG_SLOW, 0,
415 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
416 { m_unregistered, m_info, ms_info, mo_info, m_ignore }
421 0, MAXPARA, MFLG_SLOW, 0,
422 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
423 { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
428 0, MAXPARA, MFLG_SLOW, 0,
429 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
430 { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
435 0, MAXPARA, MFLG_SLOW, 0,
436 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
437 { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
442 0, MAXPARA, MFLG_SLOW, 0,
443 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
444 { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
449 0, MAXPARA, MFLG_SLOW, 0,
450 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
451 #ifdef CONFIG_OPERCMDS
452 { m_unregistered, m_jupe, ms_jupe, mo_jupe, m_ignore }
454 { m_unregistered, m_jupe, ms_jupe, m_jupe, m_ignore }
455 #endif /* CONFIG_OPERCMDS */
460 0, MAXPARA, MFLG_SLOW, 0,
461 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
462 { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
467 0, MAXPARA, MFLG_SLOW, 0,
468 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
469 { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
474 0, MAXPARA, MFLG_SLOW, 0,
475 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
476 { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
481 0, MAXPARA, MFLG_SLOW, 0,
482 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
483 { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
486 MSG_END_OF_BURST_ACK,
487 TOK_END_OF_BURST_ACK,
489 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
490 { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
495 0, MAXPARA, MFLG_SLOW, 0,
496 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
497 { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
502 0, MAXPARA, MFLG_SLOW, 0,
503 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
504 { m_unregistered, m_dns, m_dns, m_dns, m_ignore }
509 0, MAXPARA, MFLG_SLOW, 0,
510 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
511 { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
516 0, MAXPARA, MFLG_SLOW, 0,
517 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
518 { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
523 0, MAXPARA, MFLG_SLOW, 0,
524 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
525 { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
530 0, MAXPARA, MFLG_SLOW, 0,
531 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
532 { m_proto, m_proto, m_proto, m_proto, m_ignore }
534 /* This command is an alias for QUIT during the unregistered part of
535 * of the server. This is because someone jumping via a broken web
536 * proxy will send a 'POST' as their first command - which we will
537 * obviously disconnect them immediately for, stopping people abusing
543 0, MAXPARA, MFLG_SLOW, 0,
544 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
545 { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
555 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
558 * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
559 * Adapted to Undernet, adding token support, etc by comstud 10/06/97
562 static struct MessageTree msg_tree_cmd;
563 static struct MessageTree msg_tree_tok;
566 * Guts of making the token tree...
568 static struct Message **do_msg_tree_tok(struct MessageTree *mtree, char *prefix,
569 struct Message **mptr)
571 char newprefix[64]; /* Must be longer than every command name */
573 struct MessageTree *mtree1;
576 if (!lp || !strncmp((*mptr)->tok, prefix, lp))
578 if (!mptr[1] || (lp && strncmp(mptr[1]->tok, prefix, lp)))
580 /* last command in the message struct or last command in this prefix */
581 mtree->final = (*mptr)->tok + lp;
583 for (c = 0; c < 26; ++c)
584 mtree->pointers[c] = NULL;
587 /* command in this prefix */
588 if (0 == ircd_strcmp((*mptr)->tok, prefix))
591 mtree->msg = *mptr++;
596 for (c = 'A'; c <= 'Z'; ++c)
598 if ((*mptr)->tok[lp] == c)
600 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
601 mtree1->final = NULL;
602 mtree->pointers[c - 'A'] = mtree1;
603 strcpy(newprefix, prefix);
605 newprefix[lp + 1] = '\0';
606 mptr = do_msg_tree_tok(mtree1, newprefix, mptr);
607 if (!*mptr || strncmp((*mptr)->tok, prefix, lp))
609 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
610 mtree->pointers[c2] = NULL;
615 mtree->pointers[c - 'A'] = NULL;
620 * XXX - should never be here, quick hack, this can be done better
627 * Guts of making the command tree...
629 static struct Message *do_msg_tree_cmd(struct MessageTree *mtree, char *prefix,
630 struct Message *mptr)
632 char newprefix[64]; /* Must be longer than every command name */
634 struct MessageTree *mtree1;
637 if (!lp || !strncmp(mptr->cmd, prefix, lp))
639 if (!mptr[1].cmd || (lp && strncmp(mptr[1].cmd, prefix, lp)))
641 /* last command in the message struct or last command in this prefix */
642 mtree->final = mptr->cmd + lp;
644 for (c = 0; c < 26; ++c)
645 mtree->pointers[c] = NULL;
648 /* command in this prefix */
649 if (0 == ircd_strcmp(mptr->cmd, prefix))
657 for (c = 'A'; c <= 'Z'; ++c)
659 if (mptr->cmd[lp] == c)
661 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
662 mtree1->final = NULL;
663 mtree->pointers[c - 'A'] = mtree1;
664 strcpy(newprefix, prefix);
666 newprefix[lp + 1] = '\0';
667 mptr = do_msg_tree_cmd(mtree1, newprefix, mptr);
668 if (!mptr->cmd || strncmp(mptr->cmd, prefix, lp))
670 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
671 mtree->pointers[c2] = NULL;
676 mtree->pointers[c - 'A'] = NULL;
681 * This should never happen
687 static int mcmdcmp(const struct Message *m1, const struct Message *m2)
689 return strcmp(m1->cmd, m2->cmd);
692 static int mtokcmp(const struct Message **m1, const struct Message **m2)
694 return strcmp((*m1)->tok, (*m2)->tok);
698 * Sort the command names.
699 * Create table of pointers into msgtab for tokens.
700 * Create trees for ->cmd and ->tok and free the token pointers.
702 void initmsgtree(void)
705 struct Message *msg = msgtab;
707 struct Message **msgtab_tok;
708 struct Message **msgtok;
710 for (i = 0; msg->cmd; ++i, ++msg)
712 qsort(msgtab, i, sizeof(struct Message),
713 (int (*)(const void *, const void *))mcmdcmp);
714 msgtab_tok = (struct Message **)MyMalloc((i + 1) * sizeof(struct Message *));
715 for (ii = 0; ii < i; ++ii)
716 msgtab_tok[ii] = msgtab + ii;
717 msgtab_tok[i] = NULL; /* Needed by `do_msg_tree_tok' */
718 qsort(msgtab_tok, i, sizeof(struct Message *),
719 (int (*)(const void *, const void *))mtokcmp);
720 msg = do_msg_tree_cmd(&msg_tree_cmd, "", msgtab);
721 msgtok = do_msg_tree_tok(&msg_tree_tok, "", msgtab_tok);
726 * Generic tree parser which works for both commands and tokens.
729 static struct Message *msg_tree_parse(char *cmd, struct MessageTree *root)
731 struct MessageTree *mtree;
732 unsigned char r = (0xdf & (unsigned char)*cmd) - 'A';
733 if (r > 25 || !(mtree = root->pointers[r]))
737 r = 0xdf & (unsigned char)*++cmd;
738 if (mtree->final && *mtree->final == r)
740 if ((r -= 'A') > 25 || !(mtree = mtree->pointers[r]))
746 * This one is identical to the one above, but it is slower because it
747 * makes sure that `cmd' matches the _full_ command, exactly.
748 * This is to avoid confusion with commands like /quake on clients
749 * that send unknown commands directly to the server.
751 static struct Message *msg_tree_parse_client(char *cmd,
752 struct MessageTree *root)
754 struct MessageTree *mtree;
755 unsigned char q = (0xdf & (unsigned char)*cmd) - 'A';
756 if (q > 25 || !(mtree = root->pointers[q]))
760 q = 0xdf & (unsigned char)*++cmd;
761 if (mtree->final && 0 == ircd_strcmp(mtree->final, cmd))
763 if ((q -= 'A') > 25 || !(mtree = mtree->pointers[q]))
771 * NOTE: parse_*() should not be called recusively by any other fucntions!
773 int parse_client(struct Client *cptr, char *buffer, char *bufend)
775 struct Client* from = cptr;
781 struct Message* mptr;
782 MessageHandler handler = 0;
784 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
789 para[0] = from->name;
790 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
791 if (*ch == ':') /* Is any client doing this ? */
793 for (++ch; *ch && *ch != ' '; ++ch)
794 ; /* Ignore sender prefix from client */
796 ch++; /* Advance to command */
802 ServerStats->is_empt++;
803 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
804 cptr->name, from->name));
808 if ((s = strchr(ch, ' ')))
812 * This is a client/unregistered entity.
813 * Check long command list only.
815 if (!(mptr = msg_tree_parse_client(ch, &msg_tree_cmd)))
818 * Note: Give error message *only* to recognized
819 * persons. It's a nightmare situation to have
820 * two programs sending "Unknown command"'s or
821 * equivalent to each other at full blast....
822 * If it has got to person state, it at least
823 * seems to be well behaving. Perhaps this message
824 * should never be generated, though... --msa
825 * Hm, when is the buffer empty -- if a command
826 * code has been found ?? -Armin
828 if (buffer[0] != '\0')
831 send_reply(from, ERR_UNKNOWNCOMMAND, ch);
832 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
833 ch, get_client_name(cptr, HIDE_IP)));
835 ServerStats->is_unco++;
839 paramcount = mptr->parameters;
840 i = bufend - ((s) ? s : ch);
842 if ((mptr->flags & MFLG_SLOW))
843 cptr->since += (2 + i / 120);
845 * Allow only 1 msg per 2 seconds
846 * (on average) to prevent dumping.
847 * to keep the response rate up,
848 * bursts of up to 5 msgs are allowed
853 * Must the following loop really be so devious? On
854 * surface it splits the message to parameters from
855 * blank spaces. But, if paramcount has been reached,
856 * the rest of the message goes into this last parameter
857 * (about same effect as ":" has...) --msa
860 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
865 if (paramcount > MAXPARA)
866 paramcount = MAXPARA;
870 * Never "FRANCE " again!! ;-) Clean
871 * out *all* blanks.. --msa
881 * The rest is single parameter--can
882 * include blanks also.
890 for (; *s != ' ' && *s; s++);
897 * The "unregistered command check" was ugly and mildly inefficient.
898 * I fixed it. :) --Shadow
900 if (!IsUser(cptr) && 0 == (mptr->flags & MFLG_UNREG)) {
901 if (0 == (mptr->flags & MFLG_IGNORE))
902 sendto_one(from, ":%s %d * %s :Register first.", /* XXX DEAD */
903 me.name, ERR_NOTREGISTERED, ch);
907 handler = mptr->handlers[cptr->handler];
908 assert(0 != handler);
910 #ifndef IDLE_FROM_MSG
911 if (IsUser(cptr) && handler != m_ping && handler != m_ignore)
912 from->user->last = CurrentTime;
915 return (*handler) (cptr, from, i, para);
918 int parse_server(struct Client *cptr, char *buffer, char *bufend)
920 struct Client* from = cptr;
927 struct Message* mptr;
929 Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
932 len = strlen(buffer);
936 char c = buffer[200];
938 sendto_ops("RCV:%-8.8s(%.4d): \"%s...%s\"",
939 cptr->name, len, buffer, &buffer[len - 200]);
943 sendto_ops("RCV:%-8.8s(%.4d): \"%s\"", cptr->name, len, buffer);
950 para[0] = from->name;
953 * A server ALWAYS sends a prefix. When it starts with a ':' it's the
954 * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
959 /* Let para[0] point to the name of the sender */
961 if (!(ch = strchr(ch, ' ')))
965 /* And let `from' point to its client structure,
966 opps.. a server is _also_ a client --Nem */
967 from = FindClient(para[0]);
970 * If the client corresponding to the
971 * prefix is not found. We must ignore it,
972 * it is simply a lagged message travelling
973 * upstream a SQUIT that removed the client
978 Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
979 para[0], buffer, cptr->name));
980 ++ServerStats->is_unpf;
984 * However, the only thing that MUST be
985 * allowed to travel upstream against an
986 * squit, is an SQUIT itself (the timestamp
987 * protects us from being used wrong)
991 para[0] = cptr->name;
997 else if (from->from != cptr)
999 ++ServerStats->is_wrdi;
1000 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1001 buffer, cptr->name));
1005 else if (Protocol(cptr) > 9) /* Well, not ALWAYS, 2.9 can send no prefix */
1007 char numeric_prefix[6];
1009 for (i = 0; i < 5; ++i) {
1010 if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i])) {
1014 numeric_prefix[i] = '\0';
1016 * We got a numeric nick as prefix
1017 * 1 or 2 character prefixes are from servers
1018 * 3 or 5 chars are from clients
1020 if (' ' == ch[1] || ' ' == ch[2])
1021 from = FindNServer(numeric_prefix);
1023 from = findNUser(numeric_prefix);
1029 while (*ch != ' ' && *ch);
1032 * If the client corresponding to the
1033 * prefix is not found. We must ignore it,
1034 * it is simply a lagged message travelling
1035 * upstream a SQUIT that removed the client
1037 * There turned out to be other reasons that
1038 * a prefix is unknown, needing an upstream
1039 * KILL. Also, next to an SQUIT we better
1040 * allow a KILL to pass too.
1045 ServerStats->is_unpf++;
1048 if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1049 /* Only sent a KILL for a nick change */
1051 struct Client *server;
1052 /* Kill the unknown numeric prefix upstream if
1053 * it's server still exists: */
1054 if ((server = FindNServer(numeric_prefix)) && server->from == cptr)
1055 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1056 numeric_prefix, me.name);
1059 * Things that must be allowed to travel
1060 * upstream against an squit:
1062 if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1063 (*ch == 'K' && ch[2] == 'L'))
1069 /* Let para[0] point to the name of the sender */
1070 para[0] = from->name;
1072 if (from->from != cptr)
1074 ServerStats->is_wrdi++;
1075 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1076 buffer, cptr->name));
1085 ServerStats->is_empt++;
1086 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1087 cptr->name, from->name));
1092 * Extract the command code from the packet. Point s to the end
1093 * of the command code and calculate the length using pointer
1094 * arithmetic. Note: only need length for numerics and *all*
1095 * numerics must have parameters and thus a space after the command
1098 s = strchr(ch, ' '); /* s -> End of the command code */
1099 len = (s) ? (s - ch) : 0;
1100 if (len == 3 && IsDigit(*ch))
1102 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1103 paramcount = 2; /* destination, and the rest of it */
1104 ServerStats->is_num++;
1105 mptr = NULL; /* Init. to avoid stupid compiler warning :/ */
1112 /* Version Receive Send
1114 * 2.10.0 Tkn/Long Long
1115 * 2.10.10 Tkn/Long Tkn
1118 * Clients/unreg servers always receive/
1119 * send long commands -record
1123 * This is a server. Check the token command list.
1124 * -record!jegelhof@cloud9.net
1126 mptr = msg_tree_parse(ch, &msg_tree_tok);
1128 #if 1 /* for 2.10.0/2.10.10 */
1130 * This code supports 2.9 and 2.10.0 sending long commands.
1131 * It makes more calls to ircd_strcmp() than the above
1132 * so it will be somewhat slower.
1135 mptr = msg_tree_parse(ch, &msg_tree_cmd);
1141 * Note: Give error message *only* to recognized
1142 * persons. It's a nightmare situation to have
1143 * two programs sending "Unknown command"'s or
1144 * equivalent to each other at full blast....
1145 * If it has got to person state, it at least
1146 * seems to be well behaving. Perhaps this message
1147 * should never be generated, though... --msa
1148 * Hm, when is the buffer empty -- if a command
1149 * code has been found ?? -Armin
1152 if (buffer[0] != '\0')
1154 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1155 ch, get_client_name(cptr, HIDE_IP)));
1158 ServerStats->is_unco++;
1162 paramcount = mptr->parameters;
1163 i = bufend - ((s) ? s : ch);
1167 * Must the following loop really be so devious? On
1168 * surface it splits the message to parameters from
1169 * blank spaces. But, if paramcount has been reached,
1170 * the rest of the message goes into this last parameter
1171 * (about same effect as ":" has...) --msa
1174 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1179 if (paramcount > MAXPARA)
1180 paramcount = MAXPARA;
1184 * Never "FRANCE " again!! ;-) Clean
1185 * out *all* blanks.. --msa
1195 * The rest is single parameter--can
1196 * include blanks also.
1199 para[++i] = s; /* preserve the colon to make do_numeric happy */
1205 if (i >= paramcount)
1207 for (; *s != ' ' && *s; s++);
1212 return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1215 return (*mptr->handlers[cptr->handler]) (cptr, from, i, para);