Return accurate privilege information for remote opers.
[ircu2.10.12-pk.git] / ircd / parse.c
index 6e539cf57a0d5b4c01fef6f89e83c39ce70ae2bc..16a062f89b5e78cb3d6b9b71f12f4b0f36f7656f 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
+ */
+/** @file
+ * @brief Parse input from IRC clients and other servers.
+ * @version $Id$
  */
 #include "config.h"
 
@@ -30,6 +32,7 @@
 #include "ircd_alloc.h"
 #include "ircd_chattr.h"
 #include "ircd_features.h"
+#include "ircd_log.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
 #include "msg.h"
@@ -50,7 +53,7 @@
 #include "whocmds.h"
 #include "whowas.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <string.h>
 #include <stdlib.h>
 
@@ -74,6 +77,7 @@
  *                              'i' -> [MessageTree *] -> 'e' and matches
  */
 
+/** Number of children under a trie node. */
 #define MAXPTRLEN      32      /* Must be a power of 2, and
                                 * larger than 26 [a-z]|[A-Z]
                                 * its used to allocate the set
                                 * - Dianora
                                 */
 
+/** Node in the command lookup trie. */
 struct MessageTree {
-  struct Message *msg;
-  struct MessageTree *pointers[MAXPTRLEN];
+  struct Message *msg; /**< Message (if any) if the string ends now. */
+  struct MessageTree *pointers[MAXPTRLEN]; /**< Child nodes for each letter. */
 };
 
+/** Root of command lookup trie. */
 static struct MessageTree msg_tree;
 
+/** Array of all supported commands. */
 struct Message msgtab[] = {
   {
     MSG_PRIVATE,
@@ -486,7 +493,7 @@ struct Message msgtab[] = {
   {
     MSG_GLINE,
     TOK_GLINE,
-    0, MAXPARA, MFLG_SLOW, 0, NULL,
+    0, MAXPARA,         0, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
   },
@@ -495,7 +502,7 @@ struct Message msgtab[] = {
     TOK_JUPE,
     0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
-    { m_unregistered, m_jupe, ms_jupe, mo_jupe, m_ignore }
+    { m_unregistered, m_not_oper, ms_jupe, mo_jupe, m_ignore }
   },
   {
     MSG_OPMODE,
@@ -539,13 +546,6 @@ struct Message msgtab[] = {
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
   },
-  {
-    MSG_DNS,
-    TOK_DNS,
-    0, MAXPARA, MFLG_SLOW, 0, NULL,
-    /* UNREG, CLIENT, SERVER, OPER, SERVICE */
-    { m_unregistered, m_ignore, m_ignore, m_dns, m_ignore }
-  },
   {
     MSG_REHASH,
     TOK_REHASH,
@@ -600,7 +600,7 @@ struct Message msgtab[] = {
     TOK_PRIVS,
     0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
-    { m_unregistered, m_not_oper, m_ignore, mo_privs, m_ignore }
+    { m_unregistered, m_not_oper, ms_privs, mo_privs, m_ignore }
   },
   {
     MSG_ACCOUNT,
@@ -616,6 +616,13 @@ struct Message msgtab[] = {
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_not_oper, ms_asll, mo_asll, m_ignore }
    },
+  {
+    MSG_CAP,
+    TOK_CAP,
+    0, MAXPARA, 0, 0, NULL,
+    /* UNREG, CLIENT, SERVER, OPER, SERVICE */
+    { m_cap, m_cap, m_ignore, m_cap, m_ignore }
+  },
   /* This command is an alias for QUIT during the unregistered part of
    * of the server.  This is because someone jumping via a broken web
    * proxy will send a 'POST' as their first command - which we will
@@ -632,18 +639,14 @@ struct Message msgtab[] = {
   { 0 }
 };
 
-
+/** Array of command parameters. */
 static char *para[MAXPARA + 2]; /* leave room for prefix and null */
 
 
-/*
- * add_msg_element
- *
- * inputs      - Pointer to current piece of message tree
- *             - Pointer to struct Message to add at final token position
- *             - Pointer to current portion of cmd or token to add
- * output      - NONE
- * side effects        - recursively build the Message Tree ;-)
+/** Add a message to the lookup trie.
+ * @param[in,out] mtree_p Trie node to insert under.
+ * @param[in] msg_p Message to insert.
+ * @param[in] cmd Text of command to insert.
  */
 void
 add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, char *cmd)
@@ -668,20 +671,9 @@ add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, char *cmd)
   }
 }
 
-#if ircu_unused
-/* This is unused in ircu, trivial to do, but left here for later
- * use if desired.
- *
- * - Dianora
- */
-/*
- * del_msg_element
- *
- * inputs      - 
- *             -
- *             -
- * output      - NONE
- * side effects        - recursively deletes a token from the Message Tree ;-)
+/** Remove a message from the lookup trie.
+ * @param[in,out] mtree_p Trie node to remove command from.
+ * @param[in] cmd Text of command to remove.
  */
 void
 del_msg_element(struct MessageTree *mtree_p, char *cmd)
@@ -698,15 +690,8 @@ del_msg_element(struct MessageTree *mtree_p, char *cmd)
     mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = NULL;
   }
 }
-#endif
 
-/*
- * initmsgtree()
- *
- * inputs      - none
- * output      - none
- * side effect - zero the msg_tree, recursively populate it
- */
+/** Initialize the message lookup trie with all known commands. */
 void
 initmsgtree(void)
 {
@@ -721,82 +706,29 @@ initmsgtree(void)
   }
 }
 
-/*
- * msg_tree_parse
- *
- * inputs      - pointer to command/token 
- *             - pointer to MessageTree root
- * output      - found Message * for this token/command or NULL if not found
- * side effects        -
- * Generic tree parser which works for both commands and tokens.
- * Optimized by Run.
- * Re-written by Dianora (db) (tail recursive)
- *
+/** Look up a command in the message trie.
+ * @param cmd Text of command to look up.
+ * @param root Root of message trie.
+ * @return Pointer to matching message, or NULL if non exists.
  */
 static struct Message *
 msg_tree_parse(char *cmd, struct MessageTree *root)
 {
   struct MessageTree *mtree;
 
-  for (mtree = root->pointers[(*cmd++) & (MAXPTRLEN-1)];
-           mtree != NULL;
-               mtree = mtree->pointers[(*cmd++) & (MAXPTRLEN-1)])
-  {
-    if ((mtree->msg != NULL) && (*cmd == '\0'))
-      return mtree->msg;
-  }
-  return NULL;
-}
-
-/* Inserts a single entry into a message tree; must use this function
-   when inserting messages at runtime. */
-static void msg_tree_insert(struct MessageTree *mtree, int pfxlen,
-    char *key, struct Message *mptr)
-{
-  struct MessageTree *child;
-  int c;
-
-  if (!key[pfxlen])
-  {
-    mtree->msg = mptr;
-    return;
-  }
-  c = key[pfxlen];
-  child = mtree->pointers[c & (MAXPTRLEN-1)];
-  if(!child)
-  {
-    child = (struct MessageTree *)MyCalloc(1, sizeof(struct MessageTree));
-    mtree->pointers[c & (MAXPTRLEN-1)] = child;
-  }
-  msg_tree_insert(child, pfxlen+1, key, mptr);
-}
-
-/* Removes an entry from the message tree; suitable for use at runtime. */
-static struct MessageTree *msg_tree_remove(struct MessageTree *root, char *key)
-{
-  int c;
-
-  if (*key)
-  {
-    struct MessageTree *child = root->pointers[*key & (MAXPTRLEN-1)];
-    if (msg_tree_remove(child, key + 1))
-      return root;
-    root->pointers[*key & (MAXPTRLEN-1)] = NULL;
+  for (mtree = root; mtree; mtree = mtree->pointers[(*cmd++) & (MAXPTRLEN-1)]) {
+      if (*cmd == '\0' && mtree->msg)
+          return mtree->msg;
+      else if (!IsAlpha(*cmd))
+          return NULL;
   }
-  else
-  {
-    root->msg = NULL;
-  }
-  for (c = 0; c < MAXPTRLEN; ++c)
-  {
-    if (root->pointers[c])
-      return root;
-  }
-  MyFree(root);
   return NULL;
 }
 
-/* Registers a service mapping to the pseudocommand handler. */
+/** Registers a service mapping to the pseudocommand handler.
+ * @param[in] map Service mapping to add.
+ * @return Non-zero on success; zero if a command already used the name.
+ */
 int register_mapping(struct s_map *map)
 {
   struct Message *msg;
@@ -821,13 +753,16 @@ int register_mapping(struct s_map *map)
 
   /* Service mappings are only applicable to clients; insert the
      pseudocommand into the command tree only. */
-  msg_tree_insert(&msg_tree, 0, msg->cmd, msg);
+  add_msg_element(&msg_tree, msg, msg->cmd);
   map->msg = msg;
 
   return 1;
 }
 
-/* Removes a service mapping. */
+/** Removes a service mapping.
+ * @param[in] map Service mapping to remove.
+ * @return Non-zero on success; zero if no command used the name.
+ */
 int unregister_mapping(struct s_map *map)
 {
   if (!msg_tree_parse(map->command, &msg_tree))
@@ -837,7 +772,7 @@ int unregister_mapping(struct s_map *map)
     return 0;
   }
 
-  msg_tree_remove(&msg_tree, map->msg->cmd);
+  del_msg_element(&msg_tree, map->msg->cmd);
 
   map->msg->extra = NULL;
   MyFree(map->msg);
@@ -846,10 +781,14 @@ int unregister_mapping(struct s_map *map)
   return 1;
 }
 
-/*
- * parse a buffer.
- *
- * NOTE: parse_*() should not be called recusively by any other functions!
+/** Parse a line of data from a user.
+ * NOTE: parse_*() should not be called recursively by any other
+ * functions!
+ * @param[in] cptr Client that sent the data.
+ * @param[in] buffer Start of input line.
+ * @param[in] bufend End of input line.
+ * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
+ * handler returns it.
  */
 int
 parse_client(struct Client *cptr, char *buffer, char *bufend)
@@ -988,6 +927,13 @@ parse_client(struct Client *cptr, char *buffer, char *bufend)
   return (*handler) (cptr, from, i, para);
 }
 
+/** Parse a line of data from a server.
+ * @param[in] cptr Client that sent the data.
+ * @param[in] buffer Start of input line.
+ * @param[in] bufend End of input line.
+ * @return 0 on success, -1 on parse error, or CPTR_KILLED if message
+ * handler returns it.
+ */
 int parse_server(struct Client *cptr, char *buffer, char *bufend)
 {
   struct Client*  from = cptr;
@@ -1026,7 +972,7 @@ int parse_server(struct Client *cptr, char *buffer, char *bufend)
     /*
      * If the client corresponding to the
      * prefix is not found. We must ignore it,
-     * it is simply a lagged message travelling
+     * it is simply a lagged message traveling
      * upstream a SQUIT that removed the client
      * --Run
      */
@@ -1096,7 +1042,7 @@ int parse_server(struct Client *cptr, char *buffer, char *bufend)
     /*
      * If the client corresponding to the
      * prefix is not found. We must ignore it,
-     * it is simply a lagged message travelling
+     * it is simply a lagged message traveling
      * upstream a SQUIT that removed the client
      * --Run
      * There turned out to be other reasons that