added gnutls backend and moved backend code into new files
[ircu2.10.12-pk.git] / ircd / parse.c
index 936fe0050a4bbe3477b66fbaf7ec3acf38311935..b79ebb8ace48f6f52a9e50dce8812c4ddb2c82c2 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;
+static struct MessageTree tok_tree;
 
+/** Array of all supported commands. */
 struct Message msgtab[] = {
   {
     MSG_PRIVATE,
     TOK_PRIVATE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_privmsg, ms_privmsg, mo_privmsg, m_ignore }
   },
   {
     MSG_NICK,
     TOK_NICK,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_nick, m_nick, ms_nick, m_nick, m_ignore }
   },
   {
     MSG_NOTICE,
     TOK_NOTICE,
-    0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_IGNORE, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_notice, ms_notice, mo_notice, m_ignore }
   },
   {
     MSG_WALLCHOPS,
     TOK_WALLCHOPS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_wallchops, ms_wallchops, m_wallchops, m_ignore }
   },
   {
     MSG_WALLVOICES,
     TOK_WALLVOICES,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_wallvoices, ms_wallvoices, m_wallvoices, m_ignore }
   },
   {
     MSG_CPRIVMSG,
     TOK_CPRIVMSG,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_cprivmsg, m_ignore, m_cprivmsg, m_ignore }
   },
   {
     MSG_CNOTICE,
     TOK_CNOTICE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_cnotice, m_ignore, m_cnotice, m_ignore }
   },
   {
     MSG_JOIN,
     TOK_JOIN,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_join, ms_join, m_join, m_ignore }
   },
   {
     MSG_MODE,
     TOK_MODE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_mode, ms_mode, m_mode, m_ignore }
   },
   {
     MSG_BURST,
     TOK_BURST,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_ignore, ms_burst, m_ignore, m_ignore }
   },
   {
     MSG_CREATE,
     TOK_CREATE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_ignore, ms_create, m_ignore, m_ignore }
   },
   {
     MSG_DESTRUCT,
     TOK_DESTRUCT,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_ignore, ms_destruct, m_ignore, m_ignore }
   },
   {
     MSG_QUIT,
     TOK_QUIT,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_quit, m_quit, ms_quit, m_quit, m_ignore }
   },
   {
     MSG_PART,
     TOK_PART,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_part, ms_part, m_part, m_ignore }
   },
   {
     MSG_TOPIC,
     TOK_TOPIC,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_topic, ms_topic, m_topic, m_ignore }
   },
   {
     MSG_INVITE,
     TOK_INVITE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_invite, ms_invite, m_invite, m_ignore }
   },
   {
     MSG_KICK,
     TOK_KICK,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_kick, ms_kick, m_kick, m_ignore }
   },
   {
     MSG_WALLOPS,
     TOK_WALLOPS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_wallops, mo_wallops, m_ignore }
   },
   {
     MSG_WALLUSERS,
     TOK_WALLUSERS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_wallusers, mo_wallusers, m_ignore }
   },
   {
     MSG_DESYNCH,
     TOK_DESYNCH,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_ignore, ms_desynch, m_ignore, m_ignore }
   },
   {
     MSG_PING,
     TOK_PING,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_ping, ms_ping, mo_ping, m_ignore }
   },
   {
     MSG_PONG,
     TOK_PONG,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { mr_pong, m_pong, ms_pong, m_pong, m_ignore }
   },
   {
     MSG_ERROR,
     TOK_ERROR,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { mr_error, m_ignore, ms_error, m_ignore, m_ignore }
   },
   {
     MSG_KILL,
     TOK_KILL,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_kill, mo_kill, m_ignore }
   },
   {
     MSG_USER,
     TOK_USER,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_user, m_registered, m_ignore, m_registered, m_ignore }
   },
   {
     MSG_AWAY,
     TOK_AWAY,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_away, ms_away, m_away, m_ignore }
   },
   {
     MSG_ISON,
     TOK_ISON,
-    0, 1, MFLG_SLOW, 0,
+    0, 1, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_ison, m_ignore, m_ison, m_ignore }
   },
   {
     MSG_SERVER,
     TOK_SERVER,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { mr_server, m_registered, ms_server, m_registered, m_ignore }
   },
   {
     MSG_SQUIT,
     TOK_SQUIT,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_squit, mo_squit, m_ignore }
   },
   {
     MSG_WHOIS,
     TOK_WHOIS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_whois, ms_whois, m_whois, m_ignore }
   },
   {
     MSG_WHO,
     TOK_WHO,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_who, m_ignore, m_who, m_ignore }
   },
   {
     MSG_WHOWAS,
     TOK_WHOWAS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_whowas, m_whowas, m_whowas, m_ignore }
   },
   {
     MSG_LIST,
     TOK_LIST,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_list, m_ignore, m_list, m_ignore }
   },
   {
     MSG_NAMES,
     TOK_NAMES,
-    0, MAXPARA, MFLG_SLOW, 0,
+    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_USERHOST,
-    0, 1, MFLG_SLOW, 0,
+    0, 1, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_userhost, m_ignore, m_userhost, m_ignore }
   },
   {
     MSG_USERIP,
     TOK_USERIP,
-    0, 1, MFLG_SLOW, 0,
+    0, 1, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_userip, m_ignore, m_userip, m_ignore }
   },
   {
     MSG_TRACE,
     TOK_TRACE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_trace, ms_trace, mo_trace, m_ignore }
   },
   {
     MSG_PASS,
     TOK_PASS,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { mr_pass, m_registered, m_ignore, m_registered, m_ignore }
   },
   {
     MSG_LUSERS,
     TOK_LUSERS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_lusers, ms_lusers, m_lusers, m_ignore }
   },
   {
     MSG_TIME,
     TOK_TIME,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_time, m_time, m_time, m_ignore }
   },
   {
     MSG_SETTIME,
     TOK_SETTIME,
-    0, MAXPARA, MFLG_SLOW, 0,
+    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,
     TOK_RPING,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_rping, mo_rping, m_ignore }
   },
   {
     MSG_RPONG,
     TOK_RPONG,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_ignore, ms_rpong, m_ignore, m_ignore }
   },
   {
     MSG_OPER,
     TOK_OPER,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_oper, ms_oper, mo_oper, m_ignore }
   },
   {
     MSG_CONNECT,
     TOK_CONNECT,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_connect, mo_connect, m_ignore }
   },
   {
     MSG_MAP,
     TOK_MAP,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_map, m_ignore, m_map, m_ignore }
   },
   {
     MSG_VERSION,
     TOK_VERSION,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_version, m_version, ms_version, mo_version, m_ignore }
   },
   {
     MSG_STATS,
     TOK_STATS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_stats, m_stats, m_stats, m_ignore }
   },
   {
     MSG_LINKS,
     TOK_LINKS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_links, ms_links, m_links, m_ignore }
   },
   {
     MSG_ADMIN,
     TOK_ADMIN,
-    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,
+    0, MAXPARA, MFLG_SLOW | MFLG_UNREG, 0,  NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_admin, m_admin, ms_admin, mo_admin, m_ignore }
   },
   {
     MSG_HELP,
     TOK_HELP,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_help, m_ignore, m_help, m_ignore }
   },
   {
     MSG_INFO,
     TOK_INFO,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_info, ms_info, mo_info, m_ignore }
   },
   {
     MSG_MOTD,
     TOK_MOTD,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_motd, m_motd, m_motd, m_ignore }
   },
   {
     MSG_CLOSE,
     TOK_CLOSE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, m_ignore, mo_close, m_ignore }
   },
   {
     MSG_SILENCE,
     TOK_SILENCE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_silence, ms_silence, m_silence, m_ignore }
   },
   {
     MSG_GLINE,
     TOK_GLINE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA,         0, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_gline, ms_gline, mo_gline, m_ignore }
   },
   {
     MSG_JUPE,
     TOK_JUPE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    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,
     TOK_OPMODE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_opmode, mo_opmode, m_ignore }
   },
   {
     MSG_CLEARMODE,
     TOK_CLEARMODE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_clearmode, mo_clearmode, m_ignore }
   },
   {
     MSG_UPING,
     TOK_UPING,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, ms_uping, mo_uping, m_ignore }
   },
   {
     MSG_END_OF_BURST,
     TOK_END_OF_BURST,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_ignore, ms_end_of_burst, m_ignore, m_ignore }
   },
   {
     MSG_END_OF_BURST_ACK,
     TOK_END_OF_BURST_ACK,
-    0, MAXPARA, 1, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_ignore, ms_end_of_burst_ack, m_ignore, m_ignore }
   },
   {
     MSG_HASH,
     TOK_HASH,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_hash, m_hash, m_hash, m_ignore }
   },
-  {
-    MSG_DNS,
-    TOK_DNS,
-    0, MAXPARA, MFLG_SLOW, 0,
-    /* UNREG, CLIENT, SERVER, OPER, SERVICE */
-    { m_unregistered, m_dns, m_dns, m_dns, m_ignore }
-  },
   {
     MSG_REHASH,
     TOK_REHASH,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, m_ignore, mo_rehash, m_ignore }
   },
   {
     MSG_RESTART,
     TOK_RESTART,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, m_ignore, mo_restart, m_ignore }
   },
   {
     MSG_DIE,
     TOK_DIE,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, m_ignore, mo_die, m_ignore }
   },
   {
     MSG_PROTO,
     TOK_PROTO,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_proto, m_proto, m_proto, m_proto, m_ignore }
   },
   {
     MSG_SET,
     TOK_SET,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, m_ignore, mo_set, m_ignore }
   },
   {
     MSG_RESET,
     TOK_RESET,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, m_ignore, mo_reset, m_ignore }
   },
   {
     MSG_GET,
     TOK_GET,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_unregistered, m_not_oper, m_ignore, mo_get, m_ignore }
   },
   {
     MSG_PRIVS,
     TOK_PRIVS,
-    0, MAXPARA, MFLG_SLOW, 0,
+    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,
     TOK_ACCOUNT,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_ignore, m_ignore, ms_account, m_ignore, m_ignore }
   },
   {
     MSG_ASLL,
     TOK_ASLL,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* 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
@@ -625,25 +649,21 @@ struct Message msgtab[] = {
   {
     MSG_POST,
     TOK_POST,
-    0, MAXPARA, MFLG_SLOW, 0,
+    0, MAXPARA, MFLG_SLOW, 0, NULL,
     /* UNREG, CLIENT, SERVER, OPER, SERVICE */
     { m_quit, m_ignore, m_ignore, m_ignore, m_ignore }
   },
   { 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)
@@ -662,96 +682,139 @@ add_msg_element(struct MessageTree *mtree_p, struct Message *msg_p, char *cmd)
   }
   else
   {
-    ntree_p = (struct MessageTree *)MyCalloc(sizeof(struct MessageTree));
+    ntree_p = (struct MessageTree *)MyCalloc(sizeof(struct MessageTree), 1);
     mtree_p->pointers[*cmd & (MAXPTRLEN-1)] = ntree_p;
     add_msg_element(ntree_p, msg_p, cmd+1);
   }
 }
 
-#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
+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;
+    mtree_p->msg = NULL;
+  else
+    mtree_p->pointers[slot] = del_msg_element(mtree_p->pointers[slot], cmd + 1);
 
-  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;
-  }
+  /* 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;
 }
 
-/*
- * parse a buffer.
- *
- * NOTE: parse_*() should not be called recusively by any other functions!
+/** 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;
+
+  if (msg_tree_parse(map->command, &msg_tree))
+    return 0;
+
+  msg = (struct Message *)MyMalloc(sizeof(struct Message));
+  msg->cmd = map->command;
+  msg->tok = map->command;
+  msg->count = 0;
+  msg->parameters = 2;
+  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[SERVICE_HANDLER] = m_ignore;
+
+  add_msg_element(&msg_tree, msg, msg->cmd);
+  map->msg = msg;
+
+  return 1;
+}
+
+/** 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))
+  {
+    /* This simply should never happen. */
+    assert(0);
+    return 0;
+  }
+
+  del_msg_element(&msg_tree, map->msg->cmd);
+
+  map->msg->extra = NULL;
+  MyFree(map->msg);
+  map->msg = NULL;
+
+  return 1;
+}
+
+/** 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)
@@ -761,7 +824,6 @@ parse_client(struct Client *cptr, char *buffer, char *bufend)
   char*           s;
   int             i;
   int             paramcount;
-  int             noprefix = 0;
   struct Message* mptr;
   MessageHandler  handler = 0;
 
@@ -779,8 +841,6 @@ parse_client(struct Client *cptr, char *buffer, char *bufend)
     while (*ch == ' ')
       ch++;                     /* Advance to command */
   }
-  else
-    noprefix = 1;
   if (*ch == '\0')
   {
     ServerStats->is_empt++;
@@ -819,7 +879,7 @@ parse_client(struct Client *cptr, char *buffer, char *bufend)
   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
@@ -839,7 +899,14 @@ parse_client(struct Client *cptr, char *buffer, char *bufend)
 
   /* Note initially true: s==NULL || *(s-1) == '\0' !! */
 
-  i = 0;
+  if (mptr->flags & MFLG_EXTRA) {
+    /* This is a horrid kludge to avoid changing the command handler
+     * argument list. */
+    para[1] = (char*)mptr->extra;
+    i = 1;
+  } else {
+    i = 0;
+  }
   if (s)
   {
     if (paramcount > MAXPARA)
@@ -883,6 +950,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;
@@ -921,7 +995,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
      */
@@ -991,7 +1065,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
@@ -1081,7 +1155,12 @@ int parse_server(struct Client *cptr, char *buffer, char *bufend)
      * 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)
     {