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 }
543 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
546 * Message Tree stuff mostly written by orabidoo, with changes by Dianora.
547 * Adapted to Undernet, adding token support, etc by comstud 10/06/97
550 static struct MessageTree msg_tree_cmd;
551 static struct MessageTree msg_tree_tok;
554 * Guts of making the token tree...
556 static struct Message **do_msg_tree_tok(struct MessageTree *mtree, char *prefix,
557 struct Message **mptr)
559 char newprefix[64]; /* Must be longer than every command name */
561 struct MessageTree *mtree1;
564 if (!lp || !strncmp((*mptr)->tok, prefix, lp))
566 if (!mptr[1] || (lp && strncmp(mptr[1]->tok, prefix, lp)))
568 /* last command in the message struct or last command in this prefix */
569 mtree->final = (*mptr)->tok + lp;
571 for (c = 0; c < 26; ++c)
572 mtree->pointers[c] = NULL;
575 /* command in this prefix */
576 if (0 == ircd_strcmp((*mptr)->tok, prefix))
579 mtree->msg = *mptr++;
584 for (c = 'A'; c <= 'Z'; ++c)
586 if ((*mptr)->tok[lp] == c)
588 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
589 mtree1->final = NULL;
590 mtree->pointers[c - 'A'] = mtree1;
591 strcpy(newprefix, prefix);
593 newprefix[lp + 1] = '\0';
594 mptr = do_msg_tree_tok(mtree1, newprefix, mptr);
595 if (!*mptr || strncmp((*mptr)->tok, prefix, lp))
597 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
598 mtree->pointers[c2] = NULL;
603 mtree->pointers[c - 'A'] = NULL;
608 * XXX - should never be here, quick hack, this can be done better
615 * Guts of making the command tree...
617 static struct Message *do_msg_tree_cmd(struct MessageTree *mtree, char *prefix,
618 struct Message *mptr)
620 char newprefix[64]; /* Must be longer than every command name */
622 struct MessageTree *mtree1;
625 if (!lp || !strncmp(mptr->cmd, prefix, lp))
627 if (!mptr[1].cmd || (lp && strncmp(mptr[1].cmd, prefix, lp)))
629 /* last command in the message struct or last command in this prefix */
630 mtree->final = mptr->cmd + lp;
632 for (c = 0; c < 26; ++c)
633 mtree->pointers[c] = NULL;
636 /* command in this prefix */
637 if (0 == ircd_strcmp(mptr->cmd, prefix))
645 for (c = 'A'; c <= 'Z'; ++c)
647 if (mptr->cmd[lp] == c)
649 mtree1 = (struct MessageTree *)MyMalloc(sizeof(struct MessageTree));
650 mtree1->final = NULL;
651 mtree->pointers[c - 'A'] = mtree1;
652 strcpy(newprefix, prefix);
654 newprefix[lp + 1] = '\0';
655 mptr = do_msg_tree_cmd(mtree1, newprefix, mptr);
656 if (!mptr->cmd || strncmp(mptr->cmd, prefix, lp))
658 for (c2 = c + 1 - 'A'; c2 < 26; ++c2)
659 mtree->pointers[c2] = NULL;
664 mtree->pointers[c - 'A'] = NULL;
669 * This should never happen
675 static int mcmdcmp(const struct Message *m1, const struct Message *m2)
677 return strcmp(m1->cmd, m2->cmd);
680 static int mtokcmp(const struct Message **m1, const struct Message **m2)
682 return strcmp((*m1)->tok, (*m2)->tok);
686 * Sort the command names.
687 * Create table of pointers into msgtab for tokens.
688 * Create trees for ->cmd and ->tok and free the token pointers.
690 void initmsgtree(void)
693 struct Message *msg = msgtab;
695 struct Message **msgtab_tok;
696 struct Message **msgtok;
698 for (i = 0; msg->cmd; ++i, ++msg)
700 qsort(msgtab, i, sizeof(struct Message),
701 (int (*)(const void *, const void *))mcmdcmp);
702 msgtab_tok = (struct Message **)MyMalloc((i + 1) * sizeof(struct Message *));
703 for (ii = 0; ii < i; ++ii)
704 msgtab_tok[ii] = msgtab + ii;
705 msgtab_tok[i] = NULL; /* Needed by `do_msg_tree_tok' */
706 qsort(msgtab_tok, i, sizeof(struct Message *),
707 (int (*)(const void *, const void *))mtokcmp);
708 msg = do_msg_tree_cmd(&msg_tree_cmd, "", msgtab);
709 msgtok = do_msg_tree_tok(&msg_tree_tok, "", msgtab_tok);
714 * Generic tree parser which works for both commands and tokens.
717 static struct Message *msg_tree_parse(char *cmd, struct MessageTree *root)
719 struct MessageTree *mtree;
720 unsigned char r = (0xdf & (unsigned char)*cmd) - 'A';
721 if (r > 25 || !(mtree = root->pointers[r]))
725 r = 0xdf & (unsigned char)*++cmd;
726 if (mtree->final && *mtree->final == r)
728 if ((r -= 'A') > 25 || !(mtree = mtree->pointers[r]))
734 * This one is identical to the one above, but it is slower because it
735 * makes sure that `cmd' matches the _full_ command, exactly.
736 * This is to avoid confusion with commands like /quake on clients
737 * that send unknown commands directly to the server.
739 static struct Message *msg_tree_parse_client(char *cmd,
740 struct MessageTree *root)
742 struct MessageTree *mtree;
743 unsigned char q = (0xdf & (unsigned char)*cmd) - 'A';
744 if (q > 25 || !(mtree = root->pointers[q]))
748 q = 0xdf & (unsigned char)*++cmd;
749 if (mtree->final && 0 == ircd_strcmp(mtree->final, cmd))
751 if ((q -= 'A') > 25 || !(mtree = mtree->pointers[q]))
759 * NOTE: parse_*() should not be called recusively by any other fucntions!
761 int parse_client(struct Client *cptr, char *buffer, char *bufend)
763 struct Client* from = cptr;
769 struct Message* mptr;
770 MessageHandler handler = 0;
772 Debug((DEBUG_DEBUG, "Client Parsing: %s", buffer));
777 para[0] = from->name;
778 for (ch = buffer; *ch == ' '; ch++); /* Eat leading spaces */
779 if (*ch == ':') /* Is any client doing this ? */
781 for (++ch; *ch && *ch != ' '; ++ch)
782 ; /* Ignore sender prefix from client */
784 ch++; /* Advance to command */
790 ServerStats->is_empt++;
791 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
792 cptr->name, from->name));
796 if ((s = strchr(ch, ' ')))
800 * This is a client/unregistered entity.
801 * Check long command list only.
803 if (!(mptr = msg_tree_parse_client(ch, &msg_tree_cmd)))
806 * Note: Give error message *only* to recognized
807 * persons. It's a nightmare situation to have
808 * two programs sending "Unknown command"'s or
809 * equivalent to each other at full blast....
810 * If it has got to person state, it at least
811 * seems to be well behaving. Perhaps this message
812 * should never be generated, though... --msa
813 * Hm, when is the buffer empty -- if a command
814 * code has been found ?? -Armin
816 if (buffer[0] != '\0')
819 send_reply(from, ERR_UNKNOWNCOMMAND, ch);
820 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
821 ch, get_client_name(cptr, HIDE_IP)));
823 ServerStats->is_unco++;
827 paramcount = mptr->parameters;
828 i = bufend - ((s) ? s : ch);
830 if ((mptr->flags & MFLG_SLOW))
831 cptr->since += (2 + i / 120);
833 * Allow only 1 msg per 2 seconds
834 * (on average) to prevent dumping.
835 * to keep the response rate up,
836 * bursts of up to 5 msgs are allowed
841 * Must the following loop really be so devious? On
842 * surface it splits the message to parameters from
843 * blank spaces. But, if paramcount has been reached,
844 * the rest of the message goes into this last parameter
845 * (about same effect as ":" has...) --msa
848 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
853 if (paramcount > MAXPARA)
854 paramcount = MAXPARA;
858 * Never "FRANCE " again!! ;-) Clean
859 * out *all* blanks.. --msa
869 * The rest is single parameter--can
870 * include blanks also.
878 for (; *s != ' ' && *s; s++);
885 * The "unregistered command check" was ugly and mildly inefficient.
886 * I fixed it. :) --Shadow
888 if (!IsUser(cptr) && 0 == (mptr->flags & MFLG_UNREG)) {
889 if (0 == (mptr->flags & MFLG_IGNORE))
890 sendto_one(from, ":%s %d * %s :Register first.", /* XXX DEAD */
891 me.name, ERR_NOTREGISTERED, ch);
895 handler = mptr->handlers[cptr->handler];
896 assert(0 != handler);
898 #ifndef IDLE_FROM_MSG
899 if (IsUser(cptr) && handler != m_ping && handler != m_ignore)
900 from->user->last = CurrentTime;
903 return (*handler) (cptr, from, i, para);
906 int parse_server(struct Client *cptr, char *buffer, char *bufend)
908 struct Client* from = cptr;
915 struct Message* mptr;
917 Debug((DEBUG_DEBUG, "Server Parsing: %s", buffer));
920 len = strlen(buffer);
924 char c = buffer[200];
926 sendto_ops("RCV:%-8.8s(%.4d): \"%s...%s\"",
927 cptr->name, len, buffer, &buffer[len - 200]);
931 sendto_ops("RCV:%-8.8s(%.4d): \"%s\"", cptr->name, len, buffer);
938 para[0] = from->name;
941 * A server ALWAYS sends a prefix. When it starts with a ':' it's the
942 * protocol 9 prefix: a nick or a server name. Otherwise it's a numeric
947 /* Let para[0] point to the name of the sender */
949 if (!(ch = strchr(ch, ' ')))
953 /* And let `from' point to its client structure,
954 opps.. a server is _also_ a client --Nem */
955 from = FindClient(para[0]);
958 * If the client corresponding to the
959 * prefix is not found. We must ignore it,
960 * it is simply a lagged message travelling
961 * upstream a SQUIT that removed the client
966 Debug((DEBUG_NOTICE, "Unknown prefix (%s)(%s) from (%s)",
967 para[0], buffer, cptr->name));
968 ++ServerStats->is_unpf;
972 * However, the only thing that MUST be
973 * allowed to travel upstream against an
974 * squit, is an SQUIT itself (the timestamp
975 * protects us from being used wrong)
979 para[0] = cptr->name;
985 else if (from->from != cptr)
987 ++ServerStats->is_wrdi;
988 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
989 buffer, cptr->name));
993 else if (Protocol(cptr) > 9) /* Well, not ALWAYS, 2.9 can send no prefix */
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';
1004 * We got a numeric nick as prefix
1005 * 1 or 2 character prefixes are from servers
1006 * 3 or 5 chars are from clients
1008 if (' ' == ch[1] || ' ' == ch[2])
1009 from = FindNServer(numeric_prefix);
1011 from = findNUser(numeric_prefix);
1017 while (*ch != ' ' && *ch);
1020 * If the client corresponding to the
1021 * prefix is not found. We must ignore it,
1022 * it is simply a lagged message travelling
1023 * upstream a SQUIT that removed the client
1025 * There turned out to be other reasons that
1026 * a prefix is unknown, needing an upstream
1027 * KILL. Also, next to an SQUIT we better
1028 * allow a KILL to pass too.
1033 ServerStats->is_unpf++;
1036 if (*ch == 'N' && (ch[1] == ' ' || ch[1] == 'I'))
1037 /* Only sent a KILL for a nick change */
1039 struct Client *server;
1040 /* Kill the unknown numeric prefix upstream if
1041 * it's server still exists: */
1042 if ((server = FindNServer(numeric_prefix)) && server->from == cptr)
1043 sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (Unknown numeric nick)",
1044 numeric_prefix, me.name);
1047 * Things that must be allowed to travel
1048 * upstream against an squit:
1050 if (ch[1] == 'Q' || (*ch == 'D' && ch[1] == ' ') ||
1051 (*ch == 'K' && ch[2] == 'L'))
1057 /* Let para[0] point to the name of the sender */
1058 para[0] = from->name;
1060 if (from->from != cptr)
1062 ServerStats->is_wrdi++;
1063 Debug((DEBUG_NOTICE, "Fake direction: Message (%s) coming from (%s)",
1064 buffer, cptr->name));
1073 ServerStats->is_empt++;
1074 Debug((DEBUG_NOTICE, "Empty message from host %s:%s",
1075 cptr->name, from->name));
1080 * Extract the command code from the packet. Point s to the end
1081 * of the command code and calculate the length using pointer
1082 * arithmetic. Note: only need length for numerics and *all*
1083 * numerics must have parameters and thus a space after the command
1086 s = strchr(ch, ' '); /* s -> End of the command code */
1087 len = (s) ? (s - ch) : 0;
1088 if (len == 3 && IsDigit(*ch))
1090 numeric = (*ch - '0') * 100 + (*(ch + 1) - '0') * 10 + (*(ch + 2) - '0');
1091 paramcount = 2; /* destination, and the rest of it */
1092 ServerStats->is_num++;
1093 mptr = NULL; /* Init. to avoid stupid compiler warning :/ */
1100 /* Version Receive Send
1102 * 2.10.0 Tkn/Long Long
1103 * 2.10.10 Tkn/Long Tkn
1106 * Clients/unreg servers always receive/
1107 * send long commands -record
1111 * This is a server. Check the token command list.
1112 * -record!jegelhof@cloud9.net
1114 mptr = msg_tree_parse(ch, &msg_tree_tok);
1116 #if 1 /* for 2.10.0/2.10.10 */
1118 * This code supports 2.9 and 2.10.0 sending long commands.
1119 * It makes more calls to ircd_strcmp() than the above
1120 * so it will be somewhat slower.
1123 mptr = msg_tree_parse(ch, &msg_tree_cmd);
1129 * Note: Give error message *only* to recognized
1130 * persons. It's a nightmare situation to have
1131 * two programs sending "Unknown command"'s or
1132 * equivalent to each other at full blast....
1133 * If it has got to person state, it at least
1134 * seems to be well behaving. Perhaps this message
1135 * should never be generated, though... --msa
1136 * Hm, when is the buffer empty -- if a command
1137 * code has been found ?? -Armin
1140 if (buffer[0] != '\0')
1142 Debug((DEBUG_ERROR, "Unknown (%s) from %s",
1143 ch, get_client_name(cptr, HIDE_IP)));
1146 ServerStats->is_unco++;
1150 paramcount = mptr->parameters;
1151 i = bufend - ((s) ? s : ch);
1155 * Must the following loop really be so devious? On
1156 * surface it splits the message to parameters from
1157 * blank spaces. But, if paramcount has been reached,
1158 * the rest of the message goes into this last parameter
1159 * (about same effect as ":" has...) --msa
1162 /* Note initially true: s==NULL || *(s-1) == '\0' !! */
1167 if (paramcount > MAXPARA)
1168 paramcount = MAXPARA;
1172 * Never "FRANCE " again!! ;-) Clean
1173 * out *all* blanks.. --msa
1183 * The rest is single parameter--can
1184 * include blanks also.
1187 para[++i] = s; /* preserve the colon to make do_numeric happy */
1193 if (i >= paramcount)
1195 for (; *s != ' ' && *s; s++);
1200 return (do_numeric(numeric, (*buffer != ':'), cptr, from, i, para));
1203 return (*mptr->handlers[cptr->handler]) (cptr, from, i, para);