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"
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 { mr_error, 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 { m_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 */
358 { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
360 { m_unregistered, m_oper, m_ignore, m_ignore, m_ignore }
366 0, MAXPARA, MFLG_SLOW, 0,
367 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
368 { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
373 0, MAXPARA, MFLG_SLOW, 0,
374 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
375 { m_unregistered, m_map, m_ignore, m_map, m_ignore }
380 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
381 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
382 { m_version, m_version, ms_version, m_version, m_ignore }
387 0, MAXPARA, MFLG_SLOW, 0,
388 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
389 { m_unregistered, m_stats, ms_stats, mo_stats, m_ignore }
394 0, MAXPARA, MFLG_SLOW, 0,
395 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
396 { m_unregistered, m_links, ms_links, m_links, m_ignore }
401 0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
402 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
403 { m_admin, m_admin, ms_admin, m_admin, m_ignore }
408 0, MAXPARA, MFLG_SLOW, 0,
409 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
410 { m_unregistered, m_help, m_ignore, m_help, m_ignore }
415 0, MAXPARA, MFLG_SLOW, 0,
416 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
417 { m_unregistered, m_info, ms_info, mo_info, m_ignore }
422 0, MAXPARA, MFLG_SLOW, 0,
423 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
424 { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
429 0, MAXPARA, MFLG_SLOW, 0,
430 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
431 { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
436 0, MAXPARA, MFLG_SLOW, 0,
437 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
438 { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
443 0, MAXPARA, MFLG_SLOW, 0,
444 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
445 { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
450 0, MAXPARA, MFLG_SLOW, 0,
451 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
452 #ifdef CONFIG_OPERCMDS
453 { m_unregistered, m_jupe, ms_jupe, mo_jupe, m_ignore }
455 { m_unregistered, m_jupe, ms_jupe, m_jupe, m_ignore }
456 #endif /* CONFIG_OPERCMDS */
461 0, MAXPARA, MFLG_SLOW, 0,
462 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
463 { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
468 0, MAXPARA, MFLG_SLOW, 0,
469 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
470 { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
475 0, MAXPARA, MFLG_SLOW, 0,
476 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
477 { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
482 0, MAXPARA, MFLG_SLOW, 0,
483 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
484 { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
487 MSG_END_OF_BURST_ACK,
488 TOK_END_OF_BURST_ACK,
490 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
491 { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
496 0, MAXPARA, MFLG_SLOW, 0,
497 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
498 { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
503 0, MAXPARA, MFLG_SLOW, 0,
504 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
505 { m_unregistered, m_dns, m_dns, m_dns, m_ignore }
510 0, MAXPARA, MFLG_SLOW, 0,
511 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
512 { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
517 0, MAXPARA, MFLG_SLOW, 0,
518 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
519 { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
524 0, MAXPARA, MFLG_SLOW, 0,
525 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
526 { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
531 0, MAXPARA, MFLG_SLOW, 0,
532 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
533 { m_proto, m_proto, m_proto, m_proto, m_ignore }
535 /* This command is an alias for QUIT during the unregistered part of
536 * of the server. This is because someone jumping via a broken web
537 * proxy will send a 'POST' as their first command - which we will
538 * obviously disconnect them immediately for, stopping people abusing
544 0, MAXPARA, MFLG_SLOW, 0,
545 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
546 { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
556 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
559 * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
560 * Adapted to Undernet, adding token support, etc by comstud 10/06/97
563 static struct MessageTree msg_tree_cmd;
564 static struct MessageTree msg_tree_tok;
567 * Guts of making the token tree...
569 static struct Message **do_msg_tree_tok(struct MessageTree *mtree, char *prefix,
570 struct Message **mptr)
572 char newprefix[64]; /* Must be longer than every command name */
574 struct MessageTree *mtree1;
577 if (!lp || !strncmp((*mptr)->tok, prefix, lp))
579 if (!mptr[1] || (lp && strncmp(mptr[1]->tok, prefix, lp)))
581 /* last command in the message struct or last command in this prefix */
582 mtree->final = (*mptr)->tok + lp;
584 for (c = 0; c < 26; ++c)
585 mtree->pointers[c] = NULL;
588 /* command in this prefix */
589 if (0 == ircd_strcmp((*mptr)->tok, prefix))
592 mtree->msg = *mptr++;
597 for (c = 'A'; c <= 'Z'; ++c)
599 if ((*mptr)->tok[lp] == c)
601 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
602 mtree1->final = NULL;
603 mtree->pointers[c - 'A'] = mtree1;
604 strcpy(newprefix, prefix);
606 newprefix[lp + 1] = '\0';
607 mptr = do_msg_tree_tok(mtree1, newprefix, mptr);
608 if (!*mptr || strncmp((*mptr)->tok, prefix, lp))
610 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
611 mtree->pointers[c2] = NULL;
616 mtree->pointers[c - 'A'] = NULL;
621 * XXX - should never be here, quick hack, this can be done better
628 * Guts of making the command tree...
630 static struct Message *do_msg_tree_cmd(struct MessageTree *mtree, char *prefix,
631 struct Message *mptr)
633 char newprefix[64]; /* Must be longer than every command name */
635 struct MessageTree *mtree1;
638 if (!lp || !strncmp(mptr->cmd, prefix, lp))
640 if (!mptr[1].cmd || (lp && strncmp(mptr[1].cmd, prefix, lp)))
642 /* last command in the message struct or last command in this prefix */
643 mtree->final = mptr->cmd + lp;
645 for (c = 0; c < 26; ++c)
646 mtree->pointers[c] = NULL;
649 /* command in this prefix */
650 if (0 == ircd_strcmp(mptr->cmd, prefix))
658 for (c = 'A'; c <= 'Z'; ++c)
660 if (mptr->cmd[lp] == c)
662 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
663 mtree1->final = NULL;
664 mtree->pointers[c - 'A'] = mtree1;
665 strcpy(newprefix, prefix);
667 newprefix[lp + 1] = '\0';
668 mptr = do_msg_tree_cmd(mtree1, newprefix, mptr);
669 if (!mptr->cmd || strncmp(mptr->cmd, prefix, lp))
671 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
672 mtree->pointers[c2] = NULL;
677 mtree->pointers[c - 'A'] = NULL;
682 * This should never happen
688 static int mcmdcmp(const struct Message *m1, const struct Message *m2)
690 return strcmp(m1->cmd, m2->cmd);
693 static int mtokcmp(const struct Message **m1, const struct Message **m2)
695 return strcmp((*m1)->tok, (*m2)->tok);
699 * Sort the command names.
700 * Create table of pointers into msgtab for tokens.
701 * Create trees for ->cmd and ->tok and free the token pointers.
703 void initmsgtree(void)
706 struct Message *msg = msgtab;
708 struct Message **msgtab_tok;
709 struct Message **msgtok;
711 for (i = 0; msg->cmd; ++i, ++msg)
713 qsort(msgtab, i, sizeof(struct Message),
714 (int (*)(const void *, const void *))mcmdcmp);
715 msgtab_tok = (struct Message **)MyMalloc((i + 1) * sizeof(struct Message *));
716 for (ii = 0; ii < i; ++ii)
717 msgtab_tok[ii] = msgtab + ii;
718 msgtab_tok[i] = NULL; /* Needed by `do_msg_tree_tok' */
719 qsort(msgtab_tok, i, sizeof(struct Message *),
720 (int (*)(const void *, const void *))mtokcmp);
721 msg = do_msg_tree_cmd(&msg_tree_cmd, "", msgtab);
722 msgtok = do_msg_tree_tok(&msg_tree_tok, "", msgtab_tok);
727 * Generic tree parser which works for both commands and tokens.
730 static struct Message *msg_tree_parse(char *cmd, struct MessageTree *root)
732 struct MessageTree *mtree;
733 unsigned char r = (0xdf & (unsigned char)*cmd) - 'A';
734 if (r > 25 || !(mtree = root->pointers[r]))
738 r = 0xdf & (unsigned char)*++cmd;
739 if (mtree->final && *mtree->final == r)
741 if ((r -= 'A') > 25 || !(mtree = mtree->pointers[r]))
747 * This one is identical to the one above, but it is slower because it
748 * makes sure that `cmd' matches the _full_ command, exactly.
749 * This is to avoid confusion with commands like /quake on clients
750 * that send unknown commands directly to the server.
752 static struct Message *msg_tree_parse_client(char *cmd,
753 struct MessageTree *root)
755 struct MessageTree *mtree;
756 unsigned char q = (0xdf & (unsigned char)*cmd) - 'A';
757 if (q > 25 || !(mtree = root->pointers[q]))
761 q = 0xdf & (unsigned char)*++cmd;
762 if (mtree->final && 0 == ircd_strcmp(mtree->final, cmd))
764 if ((q -= 'A') > 25 || !(mtree = mtree->pointers[q]))
772 * NOTE: parse_*() should not be called recusively by any other fucntions!
774 int parse_client(struct Client *cptr, char *buffer, char *bufend)
776 struct Client* from = cptr;
782 struct Message* mptr;
783 MessageHandler handler = 0;
785 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
790 para[0] = from->name;
791 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
792 if (*ch == ':') /* Is any client doing this ? */
794 for (++ch; *ch && *ch != ' '; ++ch)
795 ; /* Ignore sender prefix from client */
797 ch++; /* Advance to command */
803 ServerStats->is_empt++;
804 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
805 cptr->name, from->name));
809 if ((s = strchr(ch, ' ')))
813 * This is a client/unregistered entity.
814 * Check long command list only.
816 if (!(mptr = msg_tree_parse_client(ch, &msg_tree_cmd)))
819 * Note: Give error message *only* to recognized
820 * persons. It's a nightmare situation to have
821 * two programs sending "Unknown command"'s or
822 * equivalent to each other at full blast....
823 * If it has got to person state, it at least
824 * seems to be well behaving. Perhaps this message
825 * should never be generated, though... --msa
826 * Hm, when is the buffer empty -- if a command
827 * code has been found ?? -Armin
829 if (buffer[0] != '\0')
832 send_reply(from, ERR_UNKNOWNCOMMAND, ch);
833 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
834 ch, get_client_name(cptr, HIDE_IP)));
836 ServerStats->is_unco++;
840 paramcount = mptr->parameters;
841 i = bufend - ((s) ? s : ch);
843 if ((mptr->flags & MFLG_SLOW))
844 cptr->since += (2 + i / 120);
846 * Allow only 1 msg per 2 seconds
847 * (on average) to prevent dumping.
848 * to keep the response rate up,
849 * bursts of up to 5 msgs are allowed
854 * Must the following loop really be so devious? On
855 * surface it splits the message to parameters from
856 * blank spaces. But, if paramcount has been reached,
857 * the rest of the message goes into this last parameter
858 * (about same effect as ":" has...) --msa
861 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
866 if (paramcount > MAXPARA)
867 paramcount = MAXPARA;
871 * Never "FRANCE " again!! ;-) Clean
872 * out *all* blanks.. --msa
882 * The rest is single parameter--can
883 * include blanks also.
891 for (; *s != ' ' && *s; s++);
898 * The "unregistered command check" was ugly and mildly inefficient.
899 * I fixed it. :) --Shadow
901 if (!IsUser(cptr) && 0 == (mptr->flags & MFLG_UNREG)) {
902 if (0 == (mptr->flags & MFLG_IGNORE))
903 sendto_one(from, ":%s %d * %s :Register first.", /* XXX DEAD */
904 me.name, ERR_NOTREGISTERED, ch);
908 handler = mptr->handlers[cptr->handler];
909 assert(0 != handler);
911 #ifndef IDLE_FROM_MSG
912 if (IsUser(cptr) && handler != m_ping && handler != m_ignore)
913 from->user->last = CurrentTime;
916 return (*handler) (cptr, from, i, para);
919 int parse_server(struct Client *cptr, char *buffer, char *bufend)
921 struct Client* from = cptr;
928 struct Message* mptr;
930 Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
933 len = strlen(buffer);
937 char c = buffer[200];
939 sendto_ops("RCV:%-8.8s(%.4d): \"%s...%s\"",
940 cptr->name, len, buffer, &buffer[len - 200]);
944 sendto_ops("RCV:%-8.8s(%.4d): \"%s\"", cptr->name, len, buffer);
951 para[0] = from->name;
954 * A server ALWAYS sends a prefix. When it starts with a ':' it's the
955 * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
960 /* Let para[0] point to the name of the sender */
962 if (!(ch = strchr(ch, ' ')))
966 /* And let `from' point to its client structure,
967 opps.. a server is _also_ a client --Nem */
968 from = FindClient(para[0]);
971 * If the client corresponding to the
972 * prefix is not found. We must ignore it,
973 * it is simply a lagged message travelling
974 * upstream a SQUIT that removed the client
979 Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
980 para[0], buffer, cptr->name));
981 ++ServerStats->is_unpf;
985 * However, the only thing that MUST be
986 * allowed to travel upstream against an
987 * squit, is an SQUIT itself (the timestamp
988 * protects us from being used wrong)
992 para[0] = cptr->name;
998 else if (from->from != cptr)
1000 ++ServerStats->is_wrdi;
1001 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1002 buffer, cptr->name));
1006 else if (Protocol(cptr) > 9) /* Well, not ALWAYS, 2.9 can send no prefix */
1008 char numeric_prefix[6];
1010 for (i = 0; i < 5; ++i) {
1011 if ('\0' == ch[i] || ' ' == (numeric_prefix[i] = ch[i])) {
1015 numeric_prefix[i] = '\0';
1017 * We got a numeric nick as prefix
1018 * 1 or 2 character prefixes are from servers
1019 * 3 or 5 chars are from clients
1021 if (' ' == ch[1] || ' ' == ch[2])
1022 from = FindNServer(numeric_prefix);
1024 from = findNUser(numeric_prefix);
1030 while (*ch != ' ' && *ch);
1033 * If the client corresponding to the
1034 * prefix is not found. We must ignore it,
1035 * it is simply a lagged message travelling
1036 * upstream a SQUIT that removed the client
1038 * There turned out to be other reasons that
1039 * a prefix is unknown, needing an upstream
1040 * KILL. Also, next to an SQUIT we better
1041 * allow a KILL to pass too.
1046 ServerStats->is_unpf++;
1049 if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1050 /* Only sent a KILL for a nick change */
1052 struct Client *server;
1053 /* Kill the unknown numeric prefix upstream if
1054 * it's server still exists: */
1055 if ((server = FindNServer(numeric_prefix)) && server->from == cptr)
1056 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1057 numeric_prefix, me.name);
1060 * Things that must be allowed to travel
1061 * upstream against an squit:
1063 if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1064 (*ch == 'K' && ch[2] == 'L'))
1070 /* Let para[0] point to the name of the sender */
1071 para[0] = from->name;
1073 if (from->from != cptr)
1075 ServerStats->is_wrdi++;
1076 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1077 buffer, cptr->name));
1086 ServerStats->is_empt++;
1087 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1088 cptr->name, from->name));
1093 * Extract the command code from the packet. Point s to the end
1094 * of the command code and calculate the length using pointer
1095 * arithmetic. Note: only need length for numerics and *all*
1096 * numerics must have parameters and thus a space after the command
1099 s = strchr(ch, ' '); /* s -> End of the command code */
1100 len = (s) ? (s - ch) : 0;
1101 if (len == 3 && IsDigit(*ch))
1103 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1104 paramcount = 2; /* destination, and the rest of it */
1105 ServerStats->is_num++;
1106 mptr = NULL; /* Init. to avoid stupid compiler warning :/ */
1113 /* Version Receive Send
1115 * 2.10.0 Tkn/Long Long
1116 * 2.10.10 Tkn/Long Tkn
1119 * Clients/unreg servers always receive/
1120 * send long commands -record
1124 * This is a server. Check the token command list.
1125 * -record!jegelhof@cloud9.net
1127 mptr = msg_tree_parse(ch, &msg_tree_tok);
1129 #if 1 /* for 2.10.0/2.10.10 */
1131 * This code supports 2.9 and 2.10.0 sending long commands.
1132 * It makes more calls to ircd_strcmp() than the above
1133 * so it will be somewhat slower.
1136 mptr = msg_tree_parse(ch, &msg_tree_cmd);
1142 * Note: Give error message *only* to recognized
1143 * persons. It's a nightmare situation to have
1144 * two programs sending "Unknown command"'s or
1145 * equivalent to each other at full blast....
1146 * If it has got to person state, it at least
1147 * seems to be well behaving. Perhaps this message
1148 * should never be generated, though... --msa
1149 * Hm, when is the buffer empty -- if a command
1150 * code has been found ?? -Armin
1153 if (buffer[0] != '\0')
1155 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1156 ch, get_client_name(cptr, HIDE_IP)));
1159 ServerStats->is_unco++;
1163 paramcount = mptr->parameters;
1164 i = bufend - ((s) ? s : ch);
1168 * Must the following loop really be so devious? On
1169 * surface it splits the message to parameters from
1170 * blank spaces. But, if paramcount has been reached,
1171 * the rest of the message goes into this last parameter
1172 * (about same effect as ":" has...) --msa
1175 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1180 if (paramcount > MAXPARA)
1181 paramcount = MAXPARA;
1185 * Never "FRANCE " again!! ;-) Clean
1186 * out *all* blanks.. --msa
1196 * The rest is single parameter--can
1197 * include blanks also.
1200 para[++i] = s; /* preserve the colon to make do_numeric happy */
1206 if (i >= paramcount)
1208 for (; *s != ' ' && *s; s++);
1213 return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1216 return (*mptr->handlers[cptr->handler]) (cptr, from, i, para);