]> jfr.im git - solanum.git/blobdiff - ircd/parse.c
Change struct Dictionary(*) to rb_dictionary(_\1).
[solanum.git] / ircd / parse.c
index b9c50d3c079bac94a574327d8743c3019de466e7..76a3585de14b5d10019063cbe7e9901d9f4d69d0 100644 (file)
@@ -5,7 +5,7 @@
  *  Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
  *  Copyright (C) 1996-2002 Hybrid Development Team
  *  Copyright (C) 2002-2005 ircd-ratbox development team
- *  Copyright (C) 2007 William Pitcock
+ *  Copyright (C) 2007-2016 William Pitcock
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -21,8 +21,6 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
- *
- *  $Id: parse.c 3177 2007-02-01 00:19:14Z jilles $
  */
 
 #include "stdinc.h"
 #include "s_stats.h"
 #include "send.h"
 #include "msg.h"
+#include "msgbuf.h"
 #include "s_conf.h"
 #include "s_serv.h"
 #include "packet.h"
 #include "s_assert.h"
 
-static struct Dictionary *cmd_dict = NULL;
-struct Dictionary *alias_dict = NULL;
-
-/* parv[0] is not used, and parv[LAST] == NULL */
-static char *para[MAXPARA + 2];
+rb_dictionary *cmd_dict = NULL;
+rb_dictionary *alias_dict = NULL;
 
 static void cancel_clients(struct Client *, struct Client *);
-static void remove_unknown(struct Client *, char *, char *);
+static void remove_unknown(struct Client *, const char *, char *);
 
-static void do_numeric(char[], struct Client *, struct Client *, int, char **);
+static void do_numeric(int, struct Client *, struct Client *, int, const char **);
 static void do_alias(struct alias_entry *, struct Client *, char *);
 
-static int handle_command(struct Message *, struct Client *, struct Client *, int, const char**);
+static int handle_command(struct Message *, struct MsgBuf *, struct Client *, struct Client *);
 
 static char buffer[1024];
 
@@ -74,55 +70,6 @@ char *reconstruct_parv(int parc, const char *parv[])
        return tmpbuf;
 }
 
-static inline int
-string_to_array(char *string, char **parv)
-{
-       char *p, *buf = string;
-       int x = 1;
-
-       parv[x] = NULL;
-       while (*buf == ' ')     /* skip leading spaces */
-               buf++;
-       if(*buf == '\0')        /* ignore all-space args */
-               return x;
-
-       do
-       {
-               if(*buf == ':') /* Last parameter */
-               {
-                       buf++;
-                       parv[x++] = buf;
-                       parv[x] = NULL;
-                       return x;
-               }
-               else
-               {
-                       parv[x++] = buf;
-                       parv[x] = NULL;
-                       if((p = strchr(buf, ' ')) != NULL)
-                       {
-                               *p++ = '\0';
-                               buf = p;
-                       }
-                       else
-                               return x;
-               }
-               while (*buf == ' ')
-                       buf++;
-               if(*buf == '\0')
-                       return x;
-       }
-       /* we can go upto parv[MAXPARA], as parv[0] is skipped */
-       while (x < MAXPARA);
-
-       if(*p == ':')
-               p++;
-
-       parv[x++] = p;
-       parv[x] = NULL;
-       return x;
-}
-
 /* parse()
  *
  * given a raw buffer, parses it and generates parv and parc
@@ -131,157 +78,103 @@ void
 parse(struct Client *client_p, char *pbuffer, char *bufend)
 {
        struct Client *from = client_p;
-       char *sender;
-       char *ch;
-       char *s;
        char *end;
-       int i = 1;
-       char *numeric = 0;
+       int res;
+       int numeric = 0;
        struct Message *mptr;
+       struct MsgBuf msgbuf;
 
        s_assert(MyConnect(client_p));
        s_assert(client_p->localClient->F != NULL);
        if(IsAnyDead(client_p))
                return;
 
-       for (ch = pbuffer; *ch == ' '; ch++)    /* skip spaces */
-               /* null statement */ ;
+       end = bufend - 1;
 
-       para[0] = from->name;
+       /* XXX this should be done before parse() is called */
+       if(*end == '\n')
+               *end-- = '\0';
+       if(*end == '\r')
+               *end = '\0';
 
-       if(*ch == ':')
+       res = msgbuf_parse(&msgbuf, pbuffer);
+       if (res)
        {
-               ch++;
+               ServerStats.is_empt++;
+               return;
+       }
 
-               /* point sender to the sender param */
-               sender = ch;
+       if (msgbuf.origin != NULL && IsServer(client_p))
+       {
+               from = find_client(msgbuf.origin);
 
-               if((s = strchr(ch, ' ')))
+               /* didnt find any matching client, issue a kill */
+               if(from == NULL)
                {
-                       *s = '\0';
-                       s++;
-                       ch = s;
+                       ServerStats.is_unpf++;
+                       remove_unknown(client_p, msgbuf.origin, pbuffer);
+                       return;
                }
 
-               if(*sender && IsServer(client_p))
+               /* fake direction, hmm. */
+               if(from->from != client_p)
                {
-                       from = find_client(sender);
-
-                       /* didnt find any matching client, issue a kill */
-                       if(from == NULL)
-                       {
-                               ServerStats.is_unpf++;
-                               remove_unknown(client_p, sender, pbuffer);
-                               return;
-                       }
-
-                       para[0] = from->name;
-
-                       /* fake direction, hmm. */
-                       if(from->from != client_p)
-                       {
-                               ServerStats.is_wrdi++;
-                               cancel_clients(client_p, from);
-                               return;
-                       }
+                       ServerStats.is_wrdi++;
+                       cancel_clients(client_p, from);
+                       return;
                }
-               while (*ch == ' ')
-                       ch++;
        }
 
-       if(*ch == '\0')
-       {
-               ServerStats.is_empt++;
-               return;
-       }
-
-       /* at this point there must be some sort of command parameter */
-
-       /*
-        * Extract the command code from the packet.  Point s to the end
-        * of the command code and calculate the length using pointer
-        * arithmetic.  Note: only need length for numerics and *all*
-        * numerics must have parameters and thus a space after the command
-        * code. -avalon
-        */
-
-       /* EOB is 3 chars long but is not a numeric */
-
-       if(*(ch + 3) == ' ' &&  /* ok, lets see if its a possible numeric.. */
-          IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
+       if(IsDigit(*msgbuf.cmd) && IsDigit(*(msgbuf.cmd + 1)) && IsDigit(*(msgbuf.cmd + 2)))
        {
                mptr = NULL;
-               numeric = ch;
+               numeric = atoi(msgbuf.cmd);
                ServerStats.is_num++;
-               s = ch + 3;     /* I know this is ' ' from above if */
-               *s++ = '\0';    /* blow away the ' ', and point s to next part */
        }
        else
        {
-               int ii = 0;
-
-               if((s = strchr(ch, ' ')))
-                       *s++ = '\0';
-
-               mptr = irc_dictionary_retrieve(cmd_dict, ch);
+               mptr = rb_dictionary_retrieve(cmd_dict, msgbuf.cmd);
 
                /* no command or its encap only, error */
                if(!mptr || !mptr->cmd)
                {
-                       /*
-                        * Note: Give error message *only* to recognized
-                        * persons. It's a nightmare situation to have
-                        * two programs sending "Unknown command"'s or
-                        * equivalent to each other at full blast....
-                        * If it has got to person state, it at least
-                        * seems to be well behaving. Perhaps this message
-                        * should never be generated, though...  --msa
-                        * Hm, when is the buffer empty -- if a command
-                        * code has been found ?? -Armin
-                        */
-                       if(pbuffer[0] != '\0')
+                       if (IsPerson(client_p))
                        {
-                               if (IsPerson(client_p))
+                               struct alias_entry *aptr = rb_dictionary_retrieve(alias_dict, msgbuf.cmd);
+                               if (aptr != NULL)
                                {
-                                       struct alias_entry *aptr = irc_dictionary_retrieve(alias_dict, ch);
-                                       if (aptr != NULL)
+                                       if (msgbuf.n_para < 2)
                                        {
-                                               do_alias(aptr, client_p, s);
+                                               sendto_one(client_p, form_str(ERR_NEEDMOREPARAMS),
+                                                          me.name,
+                                                          EmptyString(client_p->name) ? "*" : client_p->name,
+                                                          msgbuf.cmd);
                                                return;
                                        }
+
+                                       do_alias(aptr, client_p, reconstruct_parv(msgbuf.n_para - 1, msgbuf.para + 1));
+                                       return;
                                }
-                               if(IsPerson(from))
-                               {
-                                       sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
-                                                  me.name, from->name, ch);
-                               }
+                       }
+                       if(IsPerson(from))
+                       {
+                               sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
+                                          me.name, from->name, msgbuf.cmd);
                        }
                        ServerStats.is_unco++;
                        return;
                }
 
-               ii = bufend - ((s) ? s : ch);
-               mptr->bytes += ii;
+               mptr->bytes += msgbuf.parselen;
        }
 
-       end = bufend - 1;
-
-       /* XXX this should be done before parse() is called */
-       if(*end == '\n')
-               *end-- = '\0';
-       if(*end == '\r')
-               *end = '\0';
-
-       if(s != NULL)
-               i = string_to_array(s, para);
-
        if(mptr == NULL)
        {
-               do_numeric(numeric, client_p, from, i, para);
+               do_numeric(numeric, client_p, from, msgbuf.n_para, msgbuf.para);
                return;
        }
 
-       if(handle_command(mptr, client_p, from, i, /* XXX discards const!!! */ (const char **)(void *)para) < -1)
+       if(handle_command(mptr, &msgbuf, client_p, from) < -1)
        {
                char *p;
                for (p = pbuffer; p <= end; p += 8)
@@ -290,7 +183,7 @@ parse(struct Client *client_p, char *pbuffer, char *bufend)
                        /* Its expected this nasty code can be removed
                         * or rewritten later if still needed.
                         */
-                       if((unsigned long) (p + 8) > (unsigned long) end)
+                       if((p + 8) > end)
                        {
                                for (; p <= end; p++)
                                {
@@ -311,16 +204,14 @@ parse(struct Client *client_p, char *pbuffer, char *bufend)
  * handle_command
  *
  * inputs      - pointer to message block
+ *             - pointer to message buffer
  *             - pointer to client
  *             - pointer to client message is from
- *             - count of number of args
- *             - pointer to argv[] array
  * output      - -1 if error from server
  * side effects        -
  */
 static int
-handle_command(struct Message *mptr, struct Client *client_p,
-              struct Client *from, int i, const char** hpara)
+handle_command(struct Message *mptr, struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *from)
 {
        struct MessageEntry ehandler;
        MessageHandler handler = 0;
@@ -334,25 +225,12 @@ handle_command(struct Message *mptr, struct Client *client_p,
 
        mptr->count++;
 
-       /* New patch to avoid server flooding from unregistered connects
-          - Pie-Man 07/27/2000 */
-
-       if(!IsRegistered(client_p))
-       {
-               /* if its from a possible server connection
-                * ignore it.. more than likely its a header thats sneaked through
-                */
-
-               if(IsAnyServer(client_p) && !(mptr->flags & MFLG_UNREG))
-                       return (1);
-       }
-
        ehandler = mptr->handlers[from->handler];
        handler = ehandler.handler;
 
        /* check right amount of params is passed... --is */
-       if(i < ehandler.min_para ||
-          (ehandler.min_para && EmptyString(hpara[ehandler.min_para - 1])))
+       if(msgbuf_p->n_para < ehandler.min_para ||
+          (ehandler.min_para && EmptyString(msgbuf_p->para[ehandler.min_para - 1])))
        {
                if(!IsServer(client_p))
                {
@@ -368,31 +246,31 @@ handle_command(struct Message *mptr, struct Client *client_p,
 
                sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                     "Dropping server %s due to (invalid) command '%s'"
-                                    " with only %d arguments (expecting %d).",
-                                    client_p->name, mptr->cmd, i, ehandler.min_para);
+                                    " with only %zu arguments (expecting %zu).",
+                                    client_p->name, mptr->cmd, msgbuf_p->n_para, ehandler.min_para);
                ilog(L_SERVER,
-                    "Insufficient parameters (%d < %d) for command '%s' from %s.",
-                    i, ehandler.min_para, mptr->cmd, client_p->name);
+                    "Insufficient parameters (%zu < %zu) for command '%s' from %s.",
+                    msgbuf_p->n_para, ehandler.min_para, mptr->cmd, client_p->name);
                snprintf(squitreason, sizeof squitreason,
-                               "Insufficient parameters (%d < %d) for command '%s'",
-                               i, ehandler.min_para, mptr->cmd);
+                               "Insufficient parameters (%zu < %zu) for command '%s'",
+                               msgbuf_p->n_para, ehandler.min_para, mptr->cmd);
                exit_client(client_p, client_p, client_p, squitreason);
                return (-1);
        }
 
-       (*handler) (client_p, from, i, hpara);
+       (*handler) (msgbuf_p, client_p, from, msgbuf_p->n_para, msgbuf_p->para);
        return (1);
 }
 
 void
-handle_encap(struct Client *client_p, struct Client *source_p,
+handle_encap(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
             const char *command, int parc, const char *parv[])
 {
        struct Message *mptr;
        struct MessageEntry ehandler;
        MessageHandler handler = 0;
 
-       mptr = irc_dictionary_retrieve(cmd_dict, command);
+       mptr = rb_dictionary_retrieve(cmd_dict, command);
 
        if(mptr == NULL || mptr->cmd == NULL)
                return;
@@ -400,11 +278,11 @@ handle_encap(struct Client *client_p, struct Client *source_p,
        ehandler = mptr->handlers[ENCAP_HANDLER];
        handler = ehandler.handler;
 
-       if(parc < ehandler.min_para ||
+       if((size_t)parc < ehandler.min_para ||
           (ehandler.min_para && EmptyString(parv[ehandler.min_para - 1])))
                return;
 
-       (*handler) (client_p, source_p, parc, parv);
+       (*handler) (msgbuf_p, client_p, source_p, parc, parv);
 }
 
 /*
@@ -414,12 +292,11 @@ handle_encap(struct Client *client_p, struct Client *source_p,
  * output       - NONE
  * side effects - MUST MUST be called at startup ONCE before
  *                any other keyword hash routine is used.
- *
  */
 void
 clear_hash_parse()
 {
-       cmd_dict = irc_dictionary_create("command", strcasecmp);
+       cmd_dict = rb_dictionary_create("command", strcasecmp);
 }
 
 /* mod_add_cmd
@@ -438,14 +315,14 @@ mod_add_cmd(struct Message *msg)
        if(msg == NULL)
                return;
 
-       if (irc_dictionary_find(cmd_dict, msg->cmd) != NULL)
+       if (rb_dictionary_find(cmd_dict, msg->cmd) != NULL)
                return;
 
        msg->count = 0;
        msg->rcount = 0;
        msg->bytes = 0;
 
-       irc_dictionary_add(cmd_dict, msg->cmd, msg);
+       rb_dictionary_add(cmd_dict, msg->cmd, msg);
 }
 
 /* mod_del_cmd
@@ -461,39 +338,7 @@ mod_del_cmd(struct Message *msg)
        if(msg == NULL)
                return;
 
-       irc_dictionary_delete(cmd_dict, msg->cmd);
-}
-
-/*
- * report_messages
- *
- * inputs      - pointer to client to report to
- * output      - NONE
- * side effects        - NONE
- */
-void
-report_messages(struct Client *source_p)
-{
-       struct DictionaryIter iter;
-       struct Message *msg;
-       struct alias_entry *amsg;
-
-       DICTIONARY_FOREACH(msg, &iter, cmd_dict)
-       {
-               s_assert(msg->cmd != NULL);
-               sendto_one_numeric(source_p, RPL_STATSCOMMANDS,
-                                  form_str(RPL_STATSCOMMANDS),
-                                  msg->cmd, msg->count,
-                                  msg->bytes, msg->rcount);
-       }
-
-       DICTIONARY_FOREACH(amsg, &iter, alias_dict)
-       {
-               s_assert(amsg->name != NULL);
-               sendto_one_numeric(source_p, RPL_STATSCOMMANDS,
-                                  form_str(RPL_STATSCOMMANDS),
-                                  amsg->name, amsg->hits, 0L, 0);
-       }
+       rb_dictionary_delete(cmd_dict, msg->cmd);
 }
 
 /* cancel_clients()
@@ -536,7 +381,7 @@ cancel_clients(struct Client *client_p, struct Client *source_p)
  * side effects        - kills issued for clients, squits for servers
  */
 static void
-remove_unknown(struct Client *client_p, char *lsender, char *lbuffer)
+remove_unknown(struct Client *client_p, const char *lsender, char *lbuffer)
 {
        int slen = strlen(lsender);
        char sid[4];
@@ -592,7 +437,7 @@ remove_unknown(struct Client *client_p, char *lsender, char *lbuffer)
  *      a ping pong error message...
  */
 static void
-do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int parc, char *parv[])
+do_numeric(int numeric, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        struct Client *target_p;
        struct Channel *chptr;
@@ -601,8 +446,8 @@ do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int
                return;
 
        /* Remap low number numerics. */
-       if(numeric[0] == '0')
-               numeric[0] = '1';
+       if(numeric < 100)
+               numeric += 100;
 
        /*
         * Prepare the parameter portion of the message into 'buffer'.
@@ -649,10 +494,10 @@ do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int
                        /* note, now we send PING on server connect, we can
                         * also get ERR_NOSUCHSERVER..
                         */
-                       if(atoi(numeric) != ERR_NOSUCHNICK &&
-                          atoi(numeric) != ERR_NOSUCHSERVER)
+                       if(numeric != ERR_NOSUCHNICK &&
+                          numeric != ERR_NOSUCHSERVER)
                                sendto_realops_snomask(SNO_GENERAL, L_ADMIN,
-                                                    "*** %s(via %s) sent a %s numeric to me: %s",
+                                                    "*** %s(via %s) sent a %03d numeric to me: %s",
                                                     source_p->name,
                                                     client_p->name, numeric, buffer);
                        return;
@@ -666,18 +511,18 @@ do_numeric(char numeric[], struct Client *client_p, struct Client *source_p, int
                }
 
                /* csircd will send out unknown umode flag for +a (admin), drop it here. */
-               if((atoi(numeric) == ERR_UMODEUNKNOWNFLAG) && MyClient(target_p))
+               if(numeric == ERR_UMODEUNKNOWNFLAG && MyClient(target_p))
                        return;
 
                /* Fake it for server hiding, if its our client */
-               sendto_one(target_p, ":%s %s %s%s",
+               sendto_one(target_p, ":%s %03d %s%s",
                           get_id(source_p, target_p), numeric,
                           get_id(target_p, target_p), buffer);
                return;
        }
        else if((chptr = find_channel(parv[1])) != NULL)
                sendto_channel_flags(client_p, ALL_MEMBERS, source_p, chptr,
-                                    "%s %s%s",
+                                    "%03d %s%s",
                                     numeric, chptr->chname, buffer);
 }
 
@@ -728,15 +573,14 @@ static void do_alias(struct alias_entry *aptr, struct Client *source_p, char *te
                        text);
 }
 
-int
-m_not_oper(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+void
+m_not_oper(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        sendto_one_numeric(source_p, ERR_NOPRIVILEGES, form_str(ERR_NOPRIVILEGES));
-       return 0;
 }
 
-int
-m_unregistered(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+void
+m_unregistered(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        /* bit of a hack.
         * I don't =really= want to waste a bit in a flag
@@ -748,19 +592,16 @@ m_unregistered(struct Client *client_p, struct Client *source_p, int parc, const
                sendto_one(client_p, form_str(ERR_NOTREGISTERED), me.name);
                client_p->localClient->number_of_nick_changes++;
        }
-
-       return 0;
 }
 
-int
-m_registered(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+void
+m_registered(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
        sendto_one(client_p, form_str(ERR_ALREADYREGISTRED), me.name, source_p->name);
-       return 0;
 }
 
-int
-m_ignore(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
+void
+m_ignore(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
 {
-       return 0;
+       /* Does nothing */
 }