* 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"
#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"
#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>
* '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;
+static struct MessageTree tok_tree;
+/** Array of all supported commands. */
struct Message msgtab[] = {
{
MSG_PRIVATE,
TOK_NAMES,
0, MAXPARA, MFLG_SLOW, 0, NULL,
/* UNREG, CLIENT, SERVER, OPER, SERVICE */
- { m_unregistered, m_names, ms_names, m_names, m_ignore }
+ { m_unregistered, m_names, m_names, m_names, m_ignore }
},
{
MSG_USERHOST,
TOK_SETTIME,
0, MAXPARA, MFLG_SLOW, 0, NULL,
/* UNREG, CLIENT, SERVER, OPER, SERVICE */
- { m_unregistered, m_ignore, ms_settime, mo_settime, m_ignore }
+ { m_unregistered, m_not_oper, ms_settime, mo_settime, m_ignore }
},
{
MSG_RPING,
{
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 }
},
/* 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,
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,
/* UNREG, CLIENT, SERVER, OPER, SERVICE */
{ m_ignore, m_not_oper, ms_asll, mo_asll, m_ignore }
},
+ {
+ MSG_XQUERY,
+ TOK_XQUERY,
+ 0, MAXPARA, MFLG_SLOW, 0, NULL,
+ /* UNREG, CLIENT, SERVER, OPER, SERVICE */
+ { m_ignore, m_ignore, ms_xquery, mo_xquery, m_ignore }
+ },
+ {
+ MSG_XREPLY,
+ TOK_XREPLY,
+ 0, MAXPARA, MFLG_SLOW, 0, NULL,
+ /* UNREG, CLIENT, SERVER, OPER, SERVICE */
+ { m_ignore, m_ignore, ms_xreply, m_ignore, m_ignore }
+ },
+#if WE_HAVE_A_REAL_CAPABILITY_NOW
+ {
+ MSG_CAP,
+ TOK_CAP,
+ 0, MAXPARA, 0, 0, NULL,
+ /* UNREG, CLIENT, SERVER, OPER, SERVICE */
+ { m_cap, m_cap, m_ignore, m_cap, m_ignore }
+ },
+#endif
/* 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
{ 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)
}
}
-#if ircu_unused
-/* This is unused in ircu, trivial to do, but left here for later
- * use if desired.
- *
- * - Dianora
+/** 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.
*/
-/*
- * del_msg_element
- *
- * inputs -
- * -
- * -
- * output - NONE
- * side effects - recursively deletes a token from the Message Tree ;-)
- */
-void
+struct MessageTree *
del_msg_element(struct MessageTree *mtree_p, char *cmd)
{
- struct MessageTree *ntree_p;
+ int slot = *cmd & (MAXPTRLEN-1);
+ /* Either remove leaf message or from appropriate child. */
if (*cmd == '\0')
- return;
-
- if ((ntree_p = mtree_p->pointers[*cmd & (MAXPTRLEN-1)]) != NULL)
- {
- del_msg_element(ntree_p, cmd+1);
- MyFree(ntree_p);
- mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = NULL;
- }
+ mtree_p->msg = NULL;
+ else
+ mtree_p->pointers[slot] = del_msg_element(mtree_p->pointers[slot], cmd + 1);
+
+ /* If current message or any child still exists, keep this node. */
+ if (mtree_p->msg)
+ return mtree_p;
+ for (slot = 0; slot < MAXPTRLEN; ++slot)
+ if (mtree_p->pointers[slot])
+ return mtree_p;
+
+ /* Otherwise, if we're not a root node, free it and return null. */
+ if (mtree_p != &msg_tree && mtree_p != &tok_tree)
+ MyFree(mtree_p);
+ return 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)
{
int i;
memset(&msg_tree, 0, sizeof(msg_tree));
+ memset(&tok_tree, 0, sizeof(tok_tree));
for (i = 0; msgtab[i].cmd != NULL ; i++)
{
add_msg_element(&msg_tree, &msgtab[i], msgtab[i].cmd);
- add_msg_element(&msg_tree, &msgtab[i], msgtab[i].tok);
+ add_msg_element(&tok_tree, &msgtab[i], msgtab[i].tok);
}
}
-/*
- * 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;
+ for (mtree = root; mtree; mtree = mtree->pointers[(*cmd++) & (MAXPTRLEN-1)]) {
+ if (*cmd == '\0' && mtree->msg)
+ return mtree->msg;
+ else if (!IsAlpha(*cmd))
+ return NULL;
}
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;
- }
- 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;
msg->tok = map->command;
msg->count = 0;
msg->parameters = 2;
- msg->flags = MFLG_SLOW | MFLG_EXTRA;
+ msg->flags = MFLG_EXTRA;
+ if (!(map->flags & SMAP_FAST))
+ msg->flags |= MFLG_SLOW;
msg->bytes = 0;
msg->extra = map;
msg->handlers[UNREGISTERED_HANDLER] = m_ignore;
msg->handlers[CLIENT_HANDLER] = m_pseudo;
msg->handlers[SERVER_HANDLER] = m_ignore;
- msg->handlers[OPER_HANDLER] = m_pseudo;
+ msg->handlers[OPER_HANDLER] = m_pseudo;
msg->handlers[SERVICE_HANDLER] = m_ignore;
- /* 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))
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);
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)
char* s;
int i;
int paramcount;
- int noprefix = 0;
struct Message* mptr;
MessageHandler handler = 0;
while (*ch == ' ')
ch++; /* Advance to command */
}
- else
- noprefix = 1;
if (*ch == '\0')
{
ServerStats->is_empt++;
paramcount = mptr->parameters;
i = bufend - ((s) ? s : ch);
mptr->bytes += i;
- if ((mptr->flags & MFLG_SLOW))
+ if ((mptr->flags & MFLG_SLOW) || !IsAnOper(cptr))
cli_since(cptr) += (2 + i / 120);
/*
* Allow only 1 msg per 2 seconds
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;
/*
* 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
*/
/*
* 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
* And for the record, this trie parser really does not care. - Dianora
*/
- mptr = msg_tree_parse(ch, &msg_tree);
+ mptr = msg_tree_parse(ch, &tok_tree);
+
+ if (mptr == NULL)
+ {
+ mptr = msg_tree_parse(ch, &msg_tree);
+ }
if (mptr == NULL)
{