]> jfr.im git - irc/rqf/shadowircd.git/blobdiff - src/client.c
Send only one ERR_MLOCKRESTRICTED per MODE command.
[irc/rqf/shadowircd.git] / src / client.c
index da798f7352e357c95df4f14ef88a9c4856f016b5..9e8c737dd3bf74e8723e11556eb9367b4d77c180 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
  *  USA
  *
- *  $Id: client.c 3514 2007-06-06 16:25:21Z nenolod $
  */
 #include "stdinc.h"
 #include "config.h"
 
-#include "tools.h"
 #include "client.h"
 #include "class.h"
 #include "common.h"
-#include "event.h"
 #include "hash.h"
-#include "irc_string.h"
-#include "sprintf_irc.h"
+#include "match.h"
 #include "ircd.h"
-#include "s_gline.h"
 #include "numeric.h"
 #include "packet.h"
 #include "s_auth.h"
-#include "commio.h"
 #include "s_conf.h"
 #include "s_newconf.h"
-#include "s_log.h"
+#include "logger.h"
 #include "s_serv.h"
 #include "s_stats.h"
 #include "send.h"
 #include "whowas.h"
 #include "s_user.h"
-#include "linebuf.h"
 #include "hash.h"
-#include "memory.h"
 #include "hostmask.h"
-#include "balloc.h"
 #include "listener.h"
 #include "hook.h"
 #include "msg.h"
 #include "reject.h"
 #include "scache.h"
 #include "irc_dictionary.h"
+#include "sslproc.h"
 
 #define DEBUG_EXITED_CLIENTS
 
-static void check_pings_list(dlink_list * list);
-static void check_unknowns_list(dlink_list * list);
+static void check_pings_list(rb_dlink_list * list);
+static void check_unknowns_list(rb_dlink_list * list);
 static void free_exited_clients(void *unused);
 static void exit_aborted_clients(void *unused);
 
@@ -79,34 +71,34 @@ static int qs_server(struct Client *, struct Client *, struct Client *, const ch
 
 static EVH check_pings;
 
-extern BlockHeap *client_heap;
-extern BlockHeap *lclient_heap;
-extern BlockHeap *pclient_heap;
-
-extern char current_uid[IDLEN];
+static rb_bh *client_heap = NULL;
+static rb_bh *lclient_heap = NULL;
+static rb_bh *pclient_heap = NULL;
+static rb_bh *user_heap = NULL;
+static rb_bh *away_heap = NULL;
+static char current_uid[IDLEN];
 
 struct Dictionary *nd_dict = NULL;
 
 enum
 {
        D_LINED,
-       K_LINED,
-       G_LINED
+       K_LINED
 };
 
-dlink_list dead_list;
+rb_dlink_list dead_list;
 #ifdef DEBUG_EXITED_CLIENTS
-static dlink_list dead_remote_list;
+static rb_dlink_list dead_remote_list;
 #endif
 
 struct abort_client
 {
-       dlink_node node;
+       rb_dlink_node node;
        struct Client *client;
        char notice[REASONLEN];
 };
 
-static dlink_list abort_list;
+static rb_dlink_list abort_list;
 
 
 /*
@@ -123,12 +115,16 @@ init_client(void)
         * start off the check ping event ..  -- adrian
         * Every 30 seconds is plenty -- db
         */
-       client_heap = BlockHeapCreate(sizeof(struct Client), CLIENT_HEAP_SIZE);
-       lclient_heap = BlockHeapCreate(sizeof(struct LocalUser), LCLIENT_HEAP_SIZE);
-       pclient_heap = BlockHeapCreate(sizeof(struct PreClient), PCLIENT_HEAP_SIZE);
-       eventAddIsh("check_pings", check_pings, NULL, 30);
-       eventAddIsh("free_exited_clients", &free_exited_clients, NULL, 4);
-       eventAddIsh("exit_aborted_clients", exit_aborted_clients, NULL, 1);
+       client_heap = rb_bh_create(sizeof(struct Client), CLIENT_HEAP_SIZE, "client_heap");
+       lclient_heap = rb_bh_create(sizeof(struct LocalUser), LCLIENT_HEAP_SIZE, "lclient_heap");
+       pclient_heap = rb_bh_create(sizeof(struct PreClient), PCLIENT_HEAP_SIZE, "pclient_heap");
+       user_heap = rb_bh_create(sizeof(struct User), USER_HEAP_SIZE, "user_heap");
+       away_heap = rb_bh_create(AWAYLEN, AWAY_HEAP_SIZE, "away_heap");
+
+       rb_event_addish("check_pings", check_pings, NULL, 30);
+       rb_event_addish("free_exited_clients", &free_exited_clients, NULL, 4);
+       rb_event_addish("exit_aborted_clients", exit_aborted_clients, NULL, 1);
+       rb_event_add("flood_recalc", flood_recalc, NULL, 1);
 
        nd_dict = irc_dictionary_create(irccmp);
 }
@@ -150,25 +146,24 @@ make_client(struct Client *from)
        struct Client *client_p = NULL;
        struct LocalUser *localClient;
 
-       client_p = BlockHeapAlloc(client_heap);
+       client_p = rb_bh_alloc(client_heap);
 
        if(from == NULL)
        {
                client_p->from = client_p;      /* 'from' of local client is self! */
 
-               localClient = (struct LocalUser *) BlockHeapAlloc(lclient_heap);
+               localClient = rb_bh_alloc(lclient_heap);
                SetMyConnect(client_p);
                client_p->localClient = localClient;
 
-               client_p->localClient->lasttime = client_p->localClient->firsttime = CurrentTime;
+               client_p->localClient->lasttime = client_p->localClient->firsttime = rb_current_time();
 
                client_p->localClient->F = NULL;
-               client_p->localClient->ctrlfd = -1;
 
-               client_p->preClient = (struct PreClient *) BlockHeapAlloc(pclient_heap);
+               client_p->preClient = rb_bh_alloc(pclient_heap);;
 
                /* as good a place as any... */
-               dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list);
+               rb_dlinkAdd(client_p, &client_p->localClient->tnode, &unknown_list);
        }
        else
        {                       /* from is not NULL */
@@ -197,7 +192,7 @@ free_pre_client(struct Client *client_p)
        if (blptr != NULL)
                unref_blacklist(blptr);
        abort_blacklist_queries(client_p);
-       BlockHeapFree(pclient_heap, client_p->preClient);
+       rb_bh_free(pclient_heap, client_p->preClient);
        client_p->preClient = NULL;
 }
 
@@ -222,22 +217,40 @@ free_local_client(struct Client *client_p)
                client_p->localClient->listener = 0;
        }
 
-       if(client_p->localClient->F)
-               rb_close(client_p->localClient->F->fd);
+       if(client_p->localClient->F != NULL)
+       {
+               del_from_cli_fd_hash(client_p);
+               rb_close(client_p->localClient->F);
+       }
 
        if(client_p->localClient->passwd)
        {
                memset(client_p->localClient->passwd, 0,
                        strlen(client_p->localClient->passwd));
-               MyFree(client_p->localClient->passwd);
+               rb_free(client_p->localClient->passwd);
+       }
+
+       rb_free(client_p->localClient->auth_user);
+
+       if(client_p->localClient->override_timeout_event)
+       {
+               rb_event_delete(client_p->localClient->override_timeout_event);
        }
 
-       MyFree(client_p->localClient->challenge);
-       MyFree(client_p->localClient->fullcaps);
-       MyFree(client_p->localClient->opername);
-       MyFree(client_p->localClient->mangledhost);
+       rb_free(client_p->localClient->challenge);
+       rb_free(client_p->localClient->fullcaps);
+       rb_free(client_p->localClient->opername);
+       rb_free(client_p->localClient->mangledhost);
+       if (client_p->localClient->privset)
+               privilegeset_unref(client_p->localClient->privset);
 
-       BlockHeapFree(lclient_heap, client_p->localClient);
+       if(IsSSL(client_p))
+           ssld_decrement_clicount(client_p->localClient->ssl_ctl);
+           
+       if(IsCapable(client_p, CAP_ZIP))
+           ssld_decrement_clicount(client_p->localClient->z_ctl);
+
+       rb_bh_free(lclient_heap, client_p->localClient);
        client_p->localClient = NULL;
 }
 
@@ -248,7 +261,8 @@ free_client(struct Client *client_p)
        s_assert(&me != client_p);
        free_local_client(client_p);
        free_pre_client(client_p);
-       BlockHeapFree(client_heap, client_p);
+       rb_free(client_p->certfp);
+       rb_bh_free(client_heap, client_p);
 }
 
 /*
@@ -293,14 +307,14 @@ check_pings(void *notused)
  * side effects        - 
  */
 static void
-check_pings_list(dlink_list * list)
+check_pings_list(rb_dlink_list * list)
 {
        char scratch[32];       /* way too generous but... */
        struct Client *client_p;        /* current local client_p being examined */
        int ping = 0;           /* ping time value from client */
-       dlink_node *ptr, *next_ptr;
+       rb_dlink_node *ptr, *next_ptr;
 
-       DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
        {
                client_p = ptr->data;
 
@@ -309,27 +323,27 @@ check_pings_list(dlink_list * list)
 
                ping = get_client_ping(client_p);
 
-               if(ping < (CurrentTime - client_p->localClient->lasttime))
+               if(ping < (rb_current_time() - client_p->localClient->lasttime))
                {
                        /*
                         * If the client/server hasnt talked to us in 2*ping seconds
                         * and it has a ping time, then close its connection.
                         */
-                       if(((CurrentTime - client_p->localClient->lasttime) >= (2 * ping)
+                       if(((rb_current_time() - client_p->localClient->lasttime) >= (2 * ping)
                            && (client_p->flags & FLAGS_PINGSENT)))
                        {
                                if(IsServer(client_p))
                                {
                                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                                             "No response from %s, closing link",
-                                                            get_server_name(client_p, HIDE_IP));
+                                                            client_p->name);
                                        ilog(L_SERVER,
                                             "No response from %s, closing link",
                                             log_client_name(client_p, HIDE_IP));
                                }
                                (void) rb_snprintf(scratch, sizeof(scratch),
                                                  "Ping timeout: %d seconds",
-                                                 (int) (CurrentTime - client_p->localClient->lasttime));
+                                                 (int) (rb_current_time() - client_p->localClient->lasttime));
 
                                exit_client(client_p, client_p, &me, scratch);
                                continue;
@@ -343,7 +357,7 @@ check_pings_list(dlink_list * list)
                                 */
                                client_p->flags |= FLAGS_PINGSENT;
                                /* not nice but does the job */
-                               client_p->localClient->lasttime = CurrentTime - ping;
+                               client_p->localClient->lasttime = rb_current_time() - ping;
                                sendto_one(client_p, "PING :%s", me.name);
                        }
                }
@@ -360,32 +374,37 @@ check_pings_list(dlink_list * list)
  * side effects        - unknown clients get marked for termination after n seconds
  */
 static void
-check_unknowns_list(dlink_list * list)
+check_unknowns_list(rb_dlink_list * list)
 {
-       dlink_node *ptr, *next_ptr;
+       rb_dlink_node *ptr, *next_ptr;
        struct Client *client_p;
        int timeout;
 
-       DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
        {
                client_p = ptr->data;
 
                if(IsDead(client_p) || IsClosing(client_p))
                        continue;
 
+               /* still has DNSbls to validate against */
+               if(client_p->preClient != NULL &&
+                               rb_dlink_list_length(&client_p->preClient->dnsbl_queries) > 0)
+                       continue;
+
                /*
                 * Check UNKNOWN connections - if they have been in this state
                 * for > 30s, close them.
                 */
 
                timeout = IsAnyServer(client_p) ? ConfigFileEntry.connect_timeout : 30;
-               if((CurrentTime - client_p->localClient->firsttime) > timeout)
+               if((rb_current_time() - client_p->localClient->firsttime) > timeout)
                {
                        if(IsAnyServer(client_p))
                        {
                                sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL,
                                                     "No response from %s, closing link",
-                                                    get_server_name(client_p, HIDE_IP));
+                                                    client_p->name);
                                ilog(L_SERVER,
                                     "No response from %s, closing link",
                                     log_client_name(client_p, HIDE_IP));
@@ -401,29 +420,17 @@ notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban)
        static const char conn_closed[] = "Connection closed";
        static const char d_lined[] = "D-lined";
        static const char k_lined[] = "K-lined";
-       static const char g_lined[] = "G-lined";
        const char *reason = NULL;
        const char *exit_reason = conn_closed;
 
-       if(ConfigFileEntry.kline_with_reason && !EmptyString(aconf->passwd))
+       if(ConfigFileEntry.kline_with_reason)
        {
-               reason = aconf->passwd;
-               exit_reason = aconf->passwd;
+               reason = get_user_ban_reason(aconf);
+               exit_reason = reason;
        }
        else
        {
-               switch (aconf->status)
-               {
-               case D_LINED:
-                       reason = d_lined;
-                       break;
-               case G_LINED:
-                       reason = g_lined;
-                       break;
-               default:
-                       reason = k_lined;
-                       break;
-               }
+               reason = aconf->status == D_LINED ? d_lined : k_lined;
        }
 
        if(ban == D_LINED && !IsPerson(client_p))
@@ -441,117 +448,15 @@ notify_banned_client(struct Client *client_p, struct ConfItem *aconf, int ban)
  * check_banned_lines
  * inputs      - NONE
  * output      - NONE
- * side effects - Check all connections for a pending k/d/gline against the
+ * side effects - Check all connections for a pending k/dline against the
  *               client, exit the client if found.
  */
 void
 check_banned_lines(void)
 {
-       struct Client *client_p;        /* current local client_p being examined */
-       struct ConfItem *aconf = NULL;
-       dlink_node *ptr, *next_ptr;
-
-       DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
-       {
-               client_p = ptr->data;
-
-               if(IsMe(client_p))
-                       continue;
-
-               /* if there is a returned struct ConfItem then kill it */
-               if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip, client_p->localClient->ip.ss_family)))
-               {
-                       if(aconf->status & CONF_EXEMPTDLINE)
-                               continue;
-
-                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                            "DLINE active for %s",
-                                            get_client_name(client_p, HIDE_IP));
-
-                       notify_banned_client(client_p, aconf, D_LINED);
-                       continue;       /* and go examine next fd/client_p */
-               }
-
-               if(!IsPerson(client_p))
-                       continue;
-
-               if((aconf = find_kline(client_p)) != NULL)
-               {
-                       if(IsExemptKline(client_p))
-                       {
-                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                               "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
-                                               get_client_name(client_p, HIDE_IP),
-                                               aconf->user, aconf->host);
-                               continue;
-                       }
-
-                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                       "KLINE active for %s",
-                                       get_client_name(client_p, HIDE_IP));
-                       notify_banned_client(client_p, aconf, K_LINED);
-                       continue;
-               }
-               else if((aconf = find_gline(client_p)) != NULL)
-               {
-                       if(IsExemptKline(client_p))
-                       {
-                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                               "GLINE over-ruled for %s, client is kline_exempt [%s@%s]",
-                                               get_client_name(client_p, HIDE_IP),
-                                               aconf->user, aconf->host);
-                               continue;
-                       }
-
-                       if(IsExemptGline(client_p))
-                       {
-                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                               "GLINE over-ruled for %s, client is gline_exempt [%s@%s]",
-                                               get_client_name(client_p, HIDE_IP),
-                                               aconf->user, aconf->host);
-                               continue;
-                       }
-
-                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                       "GLINE active for %s",
-                                       get_client_name(client_p, HIDE_IP));
-
-                       notify_banned_client(client_p, aconf, G_LINED);
-                       continue;
-               }
-               else if((aconf = find_xline(client_p->info, 1)) != NULL)
-               {
-                       if(IsExemptKline(client_p))
-                       {
-                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                               "XLINE over-ruled for %s, client is kline_exempt [%s]",
-                                               get_client_name(client_p, HIDE_IP),
-                                               aconf->name);
-                               continue;
-                       }
-
-                       sendto_realops_snomask(SNO_GENERAL, L_ALL, "XLINE active for %s",
-                                       get_client_name(client_p, HIDE_IP));
-
-                       (void) exit_client(client_p, client_p, &me, "Bad user info");
-                       continue;
-               }
-       }
-
-       /* also check the unknowns list for new dlines */
-       DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
-       {
-               client_p = ptr->data;
-
-               if((aconf = find_dline((struct sockaddr *)&client_p->localClient->ip,client_p->localClient->ip.ss_family)))
-               {
-                       if(aconf->status & CONF_EXEMPTDLINE)
-                               continue;
-
-                       notify_banned_client(client_p, aconf, D_LINED);
-               }
-       }
-
+       check_dlines();
+       check_klines();
+       check_xlines();
 }
 
 /* check_klines_event()
@@ -578,10 +483,10 @@ check_klines(void)
 {
        struct Client *client_p;
        struct ConfItem *aconf;
-       dlink_node *ptr;
-       dlink_node *next_ptr;
+       rb_dlink_node *ptr;
+       rb_dlink_node *next_ptr;
 
-       DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
        {
                client_p = ptr->data;
 
@@ -593,8 +498,9 @@ check_klines(void)
                        if(IsExemptKline(client_p))
                        {
                                sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                                    "KLINE over-ruled for %s, client is kline_exempt",
-                                                    get_client_name(client_p, HIDE_IP));
+                                                    "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
+                                                    get_client_name(client_p, HIDE_IP),
+                                                    aconf->user, aconf->host);
                                continue;
                        }
 
@@ -608,55 +514,6 @@ check_klines(void)
        }
 }
 
-/* check_glines()
- *
- * inputs       -
- * outputs      -
- * side effects - all clients will be checked for glines
- */
-void
-check_glines(void)
-{
-       struct Client *client_p;
-       struct ConfItem *aconf;
-       dlink_node *ptr;
-       dlink_node *next_ptr;
-
-       DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
-       {
-               client_p = ptr->data;
-
-               if(IsMe(client_p) || !IsPerson(client_p))
-                       continue;
-
-               if((aconf = find_gline(client_p)) != NULL)
-               {
-                       if(IsExemptKline(client_p))
-                       {
-                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                                    "GLINE over-ruled for %s, client is kline_exempt",
-                                                    get_client_name(client_p, HIDE_IP));
-                               continue;
-                       }
-
-                       if(IsExemptGline(client_p))
-                       {
-                               sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                                    "GLINE over-ruled for %s, client is gline_exempt",
-                                                    get_client_name(client_p, HIDE_IP));
-                               continue;
-                       }
-
-                       sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                            "GLINE active for %s",
-                                            get_client_name(client_p, HIDE_IP));
-
-                       notify_banned_client(client_p, aconf, K_LINED);
-                       continue;
-               }
-       }
-}
-
 /* check_dlines()
  *
  * inputs       -
@@ -668,10 +525,10 @@ check_dlines(void)
 {
        struct Client *client_p;
        struct ConfItem *aconf;
-       dlink_node *ptr;
-       dlink_node *next_ptr;
+       rb_dlink_node *ptr;
+       rb_dlink_node *next_ptr;
 
-       DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
        {
                client_p = ptr->data;
 
@@ -693,7 +550,7 @@ check_dlines(void)
        }
 
        /* dlines need to be checked against unknowns too */
-       DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, unknown_list.head)
        {
                client_p = ptr->data;
 
@@ -718,10 +575,10 @@ check_xlines(void)
 {
        struct Client *client_p;
        struct ConfItem *aconf;
-       dlink_node *ptr;
-       dlink_node *next_ptr;
+       rb_dlink_node *ptr;
+       rb_dlink_node *next_ptr;
 
-       DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, lclient_list.head)
        {
                client_p = ptr->data;
 
@@ -733,8 +590,9 @@ check_xlines(void)
                        if(IsExemptKline(client_p))
                        {
                                sendto_realops_snomask(SNO_GENERAL, L_ALL,
-                                                    "XLINE over-ruled for %s, client is kline_exempt",
-                                                    get_client_name(client_p, HIDE_IP));
+                                                    "XLINE over-ruled for %s, client is kline_exempt [%s]",
+                                                    get_client_name(client_p, HIDE_IP),
+                                                    aconf->host);
                                continue;
                        }
 
@@ -797,8 +655,8 @@ release_client_state(struct Client *client_p)
                if(client_p->serv->user != NULL)
                        free_user(client_p->serv->user, client_p);
                if(client_p->serv->fullcaps)
-                       MyFree(client_p->serv->fullcaps);
-               MyFree(client_p->serv);
+                       rb_free(client_p->serv->fullcaps);
+               rb_free(client_p->serv);
        }
 }
 
@@ -826,7 +684,7 @@ remove_client_from_list(struct Client *client_p)
        if(client_p->node.prev == NULL && client_p->node.next == NULL)
                return;
 
-       dlinkDelete(&client_p->node, &global_client_list);
+       rb_dlinkDelete(&client_p->node, &global_client_list);
 
        update_client_exit_stats(client_p);
 }
@@ -932,10 +790,8 @@ get_client_name(struct Client *client, int showip)
                if(ConfigFileEntry.hide_spoof_ips && 
                   showip == SHOW_IP && IsIPSpoof(client))
                        showip = MASK_IP;
-#ifdef HIDE_SERVERS_IPS
                if(IsAnyServer(client))
                        showip = MASK_IP;
-#endif
 
                /* And finally, let's get the host information, ip or name */
                switch (showip)
@@ -961,49 +817,6 @@ get_client_name(struct Client *client, int showip)
         */
        return client->name;
 }
-
-const char *
-get_server_name(struct Client *target_p, int showip)
-{
-       static char nbuf[HOSTLEN * 2 + USERLEN + 5];
-
-       if(target_p == NULL)
-               return NULL;
-
-       if(!MyConnect(target_p) || !irccmp(target_p->name, target_p->host))
-               return target_p->name;
-
-#ifdef HIDE_SERVERS_IPS
-       if(EmptyString(target_p->name))
-       {
-               rb_snprintf(nbuf, sizeof(nbuf), "[%s@255.255.255.255]",
-                               target_p->username);
-               return nbuf;
-       }
-       else
-               return target_p->name;
-#endif
-
-       switch (showip)
-       {
-               case SHOW_IP:
-                       rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
-                               target_p->name, target_p->username, 
-                               target_p->sockhost);
-                       break;
-
-               case MASK_IP:
-                       rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@255.255.255.255]",
-                               target_p->name, target_p->username);
-
-               default:
-                       rb_snprintf(nbuf, sizeof(nbuf), "%s[%s@%s]",
-                               target_p->name, target_p->username,
-                               target_p->host);
-       }
-
-       return nbuf;
-}
        
 /* log_client_name()
  *
@@ -1061,20 +874,20 @@ is_remote_connect(struct Client *client_p)
 static void
 free_exited_clients(void *unused)
 {
-       dlink_node *ptr, *next;
+       rb_dlink_node *ptr, *next;
        struct Client *target_p;
 
-       DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next, dead_list.head)
        {
                target_p = ptr->data;
 
 #ifdef DEBUG_EXITED_CLIENTS
                {
                        struct abort_client *abt;
-                       dlink_node *aptr;
+                       rb_dlink_node *aptr;
                        int found = 0;
 
-                       DLINK_FOREACH(aptr, abort_list.head)
+                       RB_DLINK_FOREACH(aptr, abort_list.head)
                        {
                                abt = aptr->data;
                                if(abt->client == target_p)
@@ -1092,7 +905,7 @@ free_exited_clients(void *unused)
 
                        if(found)
                        {
-                               dlinkDestroy(ptr, &dead_list);
+                               rb_dlinkDestroy(ptr, &dead_list);
                                continue;
                        }
                }
@@ -1102,16 +915,16 @@ free_exited_clients(void *unused)
                {
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                             "Warning: null client on dead_list!");
-                       dlinkDestroy(ptr, &dead_list);
+                       rb_dlinkDestroy(ptr, &dead_list);
                        continue;
                }
                release_client_state(target_p);
                free_client(target_p);
-               dlinkDestroy(ptr, &dead_list);
+               rb_dlinkDestroy(ptr, &dead_list);
        }
 
 #ifdef DEBUG_EXITED_CLIENTS
-       DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next, dead_remote_list.head)
        {
                target_p = ptr->data;
 
@@ -1119,12 +932,12 @@ free_exited_clients(void *unused)
                {
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                             "Warning: null client on dead_list!");
-                       dlinkDestroy(ptr, &dead_list);
+                       rb_dlinkDestroy(ptr, &dead_list);
                        continue;
                }
                release_client_state(target_p);
                free_client(target_p);
-               dlinkDestroy(ptr, &dead_remote_list);
+               rb_dlinkDestroy(ptr, &dead_remote_list);
        }
 #endif
        
@@ -1144,7 +957,7 @@ recurse_send_quits(struct Client *client_p, struct Client *source_p,
                   const char *comment)
 {
        struct Client *target_p;
-       dlink_node *ptr, *ptr_next;
+       rb_dlink_node *ptr, *ptr_next;
        /* If this server can handle quit storm (QS) removal
         * of dependents, just send the SQUIT
         */
@@ -1156,12 +969,12 @@ recurse_send_quits(struct Client *client_p, struct Client *source_p,
        }
        else
        {
-               DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
+               RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
                {
                        target_p = ptr->data;
                        sendto_one(to, ":%s QUIT :%s", target_p->name, comment1);
                }
-               DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
+               RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
                {
                        target_p = ptr->data;
                        recurse_send_quits(client_p, target_p, to, comment1, comment);
@@ -1183,7 +996,7 @@ static void
 recurse_remove_clients(struct Client *source_p, const char *comment)
 {
        struct Client *target_p;
-       dlink_node *ptr, *ptr_next;
+       rb_dlink_node *ptr, *ptr_next;
 
        if(IsMe(source_p))
                return;
@@ -1194,7 +1007,7 @@ recurse_remove_clients(struct Client *source_p, const char *comment)
        /* this is very ugly, but it saves cpu :P */
        if(ConfigFileEntry.nick_delay > 0)
        {
-               DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
+               RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
                {
                        target_p = ptr->data;
                        target_p->flags |= FLAGS_KILLED;
@@ -1206,7 +1019,7 @@ recurse_remove_clients(struct Client *source_p, const char *comment)
        }
        else
        {
-               DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
+               RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->users.head)
                {
                        target_p = ptr->data;
                        target_p->flags |= FLAGS_KILLED;
@@ -1216,7 +1029,7 @@ recurse_remove_clients(struct Client *source_p, const char *comment)
                }
        }       
 
-       DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
+       RB_DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->serv->servers.head)
        {
                target_p = ptr->data;
                recurse_remove_clients(target_p, comment);
@@ -1235,9 +1048,9 @@ remove_dependents(struct Client *client_p,
                  struct Client *from, const char *comment, const char *comment1)
 {
        struct Client *to;
-       dlink_node *ptr, *next;
+       rb_dlink_node *ptr, *next;
 
-       DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next, serv_list.head)
        {
                to = ptr->data;
 
@@ -1255,14 +1068,14 @@ void
 exit_aborted_clients(void *unused)
 {
        struct abort_client *abt;
-       dlink_node *ptr, *next;
-       DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
+       rb_dlink_node *ptr, *next;
+       RB_DLINK_FOREACH_SAFE(ptr, next, abort_list.head)
        {
                abt = ptr->data;
 
 #ifdef DEBUG_EXITED_CLIENTS
                {
-                       if(dlinkFind(abt->client, &dead_list))
+                       if(rb_dlinkFind(abt->client, &dead_list))
                        {
                                s_assert(0);
                                sendto_realops_snomask(SNO_GENERAL, L_ALL, 
@@ -1277,19 +1090,19 @@ exit_aborted_clients(void *unused)
 #endif
 
                s_assert(*((unsigned long*)abt->client) != 0xdeadbeef); /* This is lame but its a debug thing */
-               dlinkDelete(ptr, &abort_list);
+               rb_dlinkDelete(ptr, &abort_list);
 
                if(IsAnyServer(abt->client))
                        sendto_realops_snomask(SNO_GENERAL, L_ALL,
                                             "Closing link to %s: %s",
-                                            get_server_name(abt->client, HIDE_IP), abt->notice);
+                                             abt->client->name, abt->notice);
 
                /* its no longer on abort list - we *must* remove
                 * FLAGS_CLOSING otherwise exit_client() will not run --fl
                 */
                abt->client->flags &= ~FLAGS_CLOSING;
                exit_client(abt->client, abt->client, &me, abt->notice);
-               MyFree(abt);
+               rb_free(abt);
        }
 }
 
@@ -1299,7 +1112,7 @@ exit_aborted_clients(void *unused)
  *
  */
 void
-dead_link(struct Client *client_p)
+dead_link(struct Client *client_p, int sendqex)
 {
        struct abort_client *abt;
 
@@ -1307,10 +1120,10 @@ dead_link(struct Client *client_p)
        if(IsDead(client_p) || IsClosing(client_p) || IsMe(client_p))
                return;
 
-       abt = (struct abort_client *) MyMalloc(sizeof(struct abort_client));
+       abt = (struct abort_client *) rb_malloc(sizeof(struct abort_client));
 
-       if(client_p->flags & FLAGS_SENDQEX)
-               strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
+       if(sendqex)
+               rb_strlcpy(abt->notice, "Max SendQ exceeded", sizeof(abt->notice));
        else
                rb_snprintf(abt->notice, sizeof(abt->notice), "Write error: %s", strerror(errno));
 
@@ -1318,7 +1131,7 @@ dead_link(struct Client *client_p)
        SetIOError(client_p);
        SetDead(client_p);
        SetClosing(client_p);
-       dlinkAdd(abt, &abt->node, &abort_list);
+       rb_dlinkAdd(abt, &abt->node, &abort_list);
 }
 
 
@@ -1327,10 +1140,13 @@ static inline void
 exit_generic_client(struct Client *client_p, struct Client *source_p, struct Client *from,
                   const char *comment)
 {
-       dlink_node *ptr, *next_ptr;
+       rb_dlink_node *ptr, *next_ptr;
 
        if(IsOper(source_p))
-               dlinkFindDestroy(source_p, &oper_list);
+               rb_dlinkFindDestroy(source_p, &oper_list);
+
+       /* get rid of any metadata the user may have */
+       user_metadata_clear(source_p);
 
        sendto_common_channels_local(source_p, ":%s!%s@%s QUIT :%s",
                                     source_p->name,
@@ -1342,7 +1158,7 @@ exit_generic_client(struct Client *client_p, struct Client *source_p, struct Cli
        s_assert(source_p->user->channel.head == NULL);
 
        /* Clean up invitefield */
-       DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, source_p->user->invited.head)
        {
                del_invite(ptr->data, source_p);
        }
@@ -1375,22 +1191,20 @@ exit_remote_client(struct Client *client_p, struct Client *source_p, struct Clie
        
        if(source_p->servptr && source_p->servptr->serv)
        {
-               dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
+               rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->users);
        }
 
        if((source_p->flags & FLAGS_KILLED) == 0)
        {
                sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
                              ":%s QUIT :%s", use_id(source_p), comment);
-               sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
-                             ":%s QUIT :%s", source_p->name, comment);
        }
 
        SetDead(source_p);
 #ifdef DEBUG_EXITED_CLIENTS
-       dlinkAddAlloc(source_p, &dead_remote_list);
+       rb_dlinkAddAlloc(source_p, &dead_remote_list);
 #else
-       dlinkAddAlloc(source_p, &dead_list);
+       rb_dlinkAddAlloc(source_p, &dead_list);
 #endif
        return(CLIENT_EXITED);
 }
@@ -1404,9 +1218,12 @@ exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Cli
                  const char *comment)
 {
        delete_auth_queries(source_p);
-       client_flush_input(source_p);
-       del_unknown_ip(source_p);
-       dlinkDelete(&source_p->localClient->tnode, &unknown_list);
+       if (source_p->localClient->dnsquery)
+       {
+               delete_resolver_queries(source_p->localClient->dnsquery);
+               rb_free(source_p->localClient->dnsquery);
+       }
+       rb_dlinkDelete(&source_p->localClient->tnode, &unknown_list);
 
        if(!IsIOError(source_p))
                sendto_one(source_p, "ERROR :Closing Link: %s (%s)",
@@ -1422,7 +1239,7 @@ exit_unknown_client(struct Client *client_p, struct Client *source_p, struct Cli
        del_from_client_hash(source_p->name, source_p);
        remove_client_from_list(source_p);
        SetDead(source_p);
-       dlinkAddAlloc(source_p, &dead_list);
+       rb_dlinkAddAlloc(source_p, &dead_list);
 
        /* Note that we don't need to add unknowns to the dead_list */
        return(CLIENT_EXITED);
@@ -1452,11 +1269,11 @@ exit_remote_server(struct Client *client_p, struct Client *source_p, struct Clie
                remove_dependents(client_p, source_p, from, IsPerson(from) ? newcomment : comment, comment1);
 
        if(source_p->servptr && source_p->servptr->serv)
-               dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
+               rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
        else
                s_assert(0);
 
-       dlinkFindDestroy(source_p, &global_serv_list);
+       rb_dlinkFindDestroy(source_p, &global_serv_list);
        target_p = source_p->from;
        
        if(target_p != NULL && IsServer(target_p) && target_p != client_p &&
@@ -1476,9 +1293,9 @@ exit_remote_server(struct Client *client_p, struct Client *source_p, struct Clie
        
        SetDead(source_p);
 #ifdef DEBUG_EXITED_CLIENTS
-       dlinkAddAlloc(source_p, &dead_remote_list);
+       rb_dlinkAddAlloc(source_p, &dead_remote_list);
 #else
-       dlinkAddAlloc(source_p, &dead_list);
+       rb_dlinkAddAlloc(source_p, &dead_list);
 #endif
        return 0;
 }
@@ -1487,15 +1304,12 @@ static int
 qs_server(struct Client *client_p, struct Client *source_p, struct Client *from, 
                  const char *comment)
 {
-       struct Client *target_p;
-
        if(source_p->servptr && source_p->servptr->serv)
-               dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
+               rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
        else
                s_assert(0);
 
-       dlinkFindDestroy(source_p, &global_serv_list);
-       target_p = source_p->from;
+       rb_dlinkFindDestroy(source_p, &global_serv_list);
        
        if(has_id(source_p))
                del_from_id_hash(source_p->id, source_p);
@@ -1505,7 +1319,7 @@ qs_server(struct Client *client_p, struct Client *source_p, struct Client *from,
        scache_split(source_p->serv->nameinfo);
        
        SetDead(source_p);
-       dlinkAddAlloc(source_p, &dead_list);    
+       rb_dlinkAddAlloc(source_p, &dead_list); 
        return 0;
 }
 
@@ -1517,8 +1331,8 @@ exit_local_server(struct Client *client_p, struct Client *source_p, struct Clien
        static char newcomment[BUFSIZE];
        unsigned int sendk, recvk;
        
-       dlinkDelete(&source_p->localClient->tnode, &serv_list);
-       dlinkFindDestroy(source_p, &global_serv_list);
+       rb_dlinkDelete(&source_p->localClient->tnode, &serv_list);
+       rb_dlinkFindDestroy(source_p, &global_serv_list);
        
        unset_chcap_usage_counts(source_p);
        sendk = source_p->localClient->sendK;
@@ -1539,7 +1353,7 @@ exit_local_server(struct Client *client_p, struct Client *source_p, struct Clien
        }
        
        if(source_p->servptr && source_p->servptr->serv)
-               dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
+               rb_dlinkDelete(&source_p->lnode, &source_p->servptr->serv->servers);
        else
                s_assert(0);
 
@@ -1560,10 +1374,10 @@ exit_local_server(struct Client *client_p, struct Client *source_p, struct Clien
 
        sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s was connected"
                             " for %ld seconds.  %d/%d sendK/recvK.",
-                            source_p->name, CurrentTime - source_p->localClient->firsttime, sendk, recvk);
+                            source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
 
        ilog(L_SERVER, "%s was connected for %ld seconds.  %d/%d sendK/recvK.",
-            source_p->name, CurrentTime - source_p->localClient->firsttime, sendk, recvk);
+            source_p->name, (long) rb_current_time() - source_p->localClient->firsttime, sendk, recvk);
         
        if(has_id(source_p))
                del_from_id_hash(source_p->id, source_p);
@@ -1573,7 +1387,7 @@ exit_local_server(struct Client *client_p, struct Client *source_p, struct Clien
        scache_split(source_p->serv->nameinfo);
        
        SetDead(source_p);
-       dlinkAddAlloc(source_p, &dead_list);
+       rb_dlinkAddAlloc(source_p, &dead_list);
        return 0;
 }
 
@@ -1587,17 +1401,17 @@ exit_local_client(struct Client *client_p, struct Client *source_p, struct Clien
                  const char *comment)
 {
        unsigned long on_for;
+       char tbuf[26];
 
        exit_generic_client(client_p, source_p, from, comment);
        clear_monitor(source_p);
 
        s_assert(IsPerson(source_p));
-       client_flush_input(source_p);
-       dlinkDelete(&source_p->localClient->tnode, &lclient_list);
-       dlinkDelete(&source_p->lnode, &me.serv->users);
+       rb_dlinkDelete(&source_p->localClient->tnode, &lclient_list);
+       rb_dlinkDelete(&source_p->lnode, &me.serv->users);
 
        if(IsOper(source_p))
-               dlinkFindDestroy(source_p, &local_oper_list);
+               rb_dlinkFindDestroy(source_p, &local_oper_list);
 
        sendto_realops_snomask(SNO_CCONN, L_ALL,
                             "Client exiting: %s (%s@%s) [%s] [%s]",
@@ -1611,12 +1425,13 @@ exit_local_client(struct Client *client_p, struct Client *source_p, struct Clien
                         show_ip(NULL, source_p) ? source_p->sockhost : "255.255.255.255",
                        comment);
 
-       on_for = CurrentTime - source_p->localClient->firsttime;
+       on_for = rb_current_time() - source_p->localClient->firsttime;
 
-       ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
-               myctime(CurrentTime), on_for / 3600,
+       ilog(L_USER, "%s (%3lu:%02lu:%02lu): %s!%s@%s %s %d/%d",
+               rb_ctime(rb_current_time(), tbuf, sizeof(tbuf)), on_for / 3600,
                (on_for % 3600) / 60, on_for % 60,
                source_p->name, source_p->username, source_p->host,
+               source_p->sockhost,
                source_p->localClient->sendK, source_p->localClient->receiveK);
 
        sendto_one(source_p, "ERROR :Closing Link: %s (%s)", source_p->host, comment);
@@ -1626,12 +1441,10 @@ exit_local_client(struct Client *client_p, struct Client *source_p, struct Clien
        {
                sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
                              ":%s QUIT :%s", use_id(source_p), comment);
-               sendto_server(client_p, NULL, NOCAPS, CAP_TS6,
-                             ":%s QUIT :%s", source_p->name, comment);
        }
 
        SetDead(source_p);
-       dlinkAddAlloc(source_p, &dead_list);
+       rb_dlinkAddAlloc(source_p, &dead_list);
        return(CLIENT_EXITED);
 }
 
@@ -1717,8 +1530,8 @@ void
 count_local_client_memory(size_t * count, size_t * local_client_memory_used)
 {
        size_t lusage;
-       BlockHeapUsage(lclient_heap, count, NULL, &lusage);
-       *local_client_memory_used = lusage + (*count * (sizeof(MemBlock) + sizeof(struct Client)));
+       rb_bh_usage(lclient_heap, count, NULL, &lusage, NULL);
+       *local_client_memory_used = lusage + (*count * (sizeof(void *) + sizeof(struct Client)));
 }
 
 /*
@@ -1728,10 +1541,10 @@ void
 count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
 {
        size_t lcount, rcount;
-       BlockHeapUsage(lclient_heap, &lcount, NULL, NULL);
-       BlockHeapUsage(client_heap, &rcount, NULL, NULL);
+       rb_bh_usage(lclient_heap, &lcount, NULL, NULL, NULL);
+       rb_bh_usage(client_heap, &rcount, NULL, NULL, NULL);
        *count = rcount - lcount;
-       *remote_client_memory_used = *count * (sizeof(MemBlock) + sizeof(struct Client));
+       *remote_client_memory_used = *count * (sizeof(void *) + sizeof(struct Client));
 }
 
 
@@ -1763,8 +1576,8 @@ count_remote_client_memory(size_t * count, size_t * remote_client_memory_used)
 void
 del_all_accepts(struct Client *client_p)
 {
-       dlink_node *ptr;
-       dlink_node *next_ptr;
+       rb_dlink_node *ptr;
+       rb_dlink_node *next_ptr;
        struct Client *target_p;
 
        if(MyClient(client_p) && client_p->localClient->allow_list.head)
@@ -1772,20 +1585,20 @@ del_all_accepts(struct Client *client_p)
                /* clear this clients accept list, and remove them from
                 * everyones on_accept_list
                 */
-               DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
+               RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->allow_list.head)
                {
                        target_p = ptr->data;
-                       dlinkFindDestroy(client_p, &target_p->on_allow_list);
-                       dlinkDestroy(ptr, &client_p->localClient->allow_list);
+                       rb_dlinkFindDestroy(client_p, &target_p->on_allow_list);
+                       rb_dlinkDestroy(ptr, &client_p->localClient->allow_list);
                }
        }
 
        /* remove this client from everyones accept list */
-       DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
+       RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->on_allow_list.head)
        {
                target_p = ptr->data;
-               dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
-               dlinkDestroy(ptr, &client_p->on_allow_list);
+               rb_dlinkFindDestroy(client_p, &target_p->localClient->allow_list);
+               rb_dlinkDestroy(ptr, &client_p->on_allow_list);
        }
 }
 
@@ -1804,10 +1617,6 @@ show_ip(struct Client *source_p, struct Client *target_p)
 {
        if(IsAnyServer(target_p))
        {
-#ifndef HIDE_SERVERS_IPS
-               if(source_p == NULL || IsOper(source_p))
-                       return 1;
-#endif
                return 0;
        }
        else if(IsIPSpoof(target_p))
@@ -1840,24 +1649,6 @@ show_ip_conf(struct ConfItem *aconf, struct Client *source_p)
                return 1;
 }
 
-/*
- * initUser
- *
- * inputs      - none
- * outputs     - none
- *
- * side effects - Creates a block heap for struct Users
- *
- */
-static BlockHeap *user_heap;
-void
-initUser(void)
-{
-       user_heap = BlockHeapCreate(sizeof(struct User), USER_HEAP_SIZE);
-       if(!user_heap)
-               outofmemory();
-}
-
 /*
  * make_user
  *
@@ -1870,13 +1661,17 @@ struct User *
 make_user(struct Client *client_p)
 {
        struct User *user;
+       struct Dictionary *metadata;
 
        user = client_p->user;
        if(!user)
        {
-               user = (struct User *) BlockHeapAlloc(user_heap);
+               user = (struct User *) rb_bh_alloc(user_heap);
                user->refcnt = 1;
                client_p->user = user;
+
+               metadata = irc_dictionary_create(irccmp);
+               client_p->user->metadata = metadata;
        }
        return user;
 }
@@ -1889,14 +1684,14 @@ make_user(struct Client *client_p)
  * side effects - add's an Server information block to a client
  *                if it was not previously allocated.
  */
-server_t *
+struct Server *
 make_server(struct Client *client_p)
 {
-       server_t *serv = client_p->serv;
+       struct Server *serv = client_p->serv;
 
        if(!serv)
        {
-               serv = (server_t *) MyMalloc(sizeof(server_t));
+               serv = (struct Server *) rb_malloc(sizeof(struct Server));
                client_p->serv = serv;
        }
        return client_p->serv;
@@ -1914,10 +1709,13 @@ make_server(struct Client *client_p)
 void
 free_user(struct User *user, struct Client *client_p)
 {
+       free_away(client_p);
+
        if(--user->refcnt <= 0)
        {
                if(user->away)
-                       MyFree((char *) user->away);
+                       rb_free((char *) user->away);
+               
                /*
                 * sanity check
                 */
@@ -1933,14 +1731,31 @@ free_user(struct User *user, struct Client *client_p)
                                             (unsigned long) user,
                                             (unsigned long) user->invited.head,
                                             (unsigned long) user->channel.head, 
-                                            dlink_list_length(&user->channel),
+                                            rb_dlink_list_length(&user->channel),
                                             user->refcnt);
                        s_assert(!user->refcnt);
                        s_assert(!user->invited.head);
                        s_assert(!user->channel.head);
                }
 
-               BlockHeapFree(user_heap, user);
+               rb_bh_free(user_heap, user);
+       }
+}
+
+void
+allocate_away(struct Client *client_p)
+{
+       if(client_p->user->away == NULL)
+               client_p->user->away = rb_bh_alloc(away_heap);  
+}
+
+
+void
+free_away(struct Client *client_p)
+{
+       if(client_p->user != NULL && client_p->user->away != NULL) {
+               rb_bh_free(away_heap, client_p->user->away);
+               client_p->user->away = NULL;
        }
 }
 
@@ -2012,22 +1827,10 @@ close_connection(struct Client *client_p)
        {
                struct server_conf *server_p;
 
-               ServerStats->is_sv++;
-               ServerStats->is_sbs += client_p->localClient->sendB;
-               ServerStats->is_sbr += client_p->localClient->receiveB;
-               ServerStats->is_sks += client_p->localClient->sendK;
-               ServerStats->is_skr += client_p->localClient->receiveK;
-               ServerStats->is_sti += CurrentTime - client_p->localClient->firsttime;
-               if(ServerStats->is_sbs > 2047)
-               {
-                       ServerStats->is_sks += (ServerStats->is_sbs >> 10);
-                       ServerStats->is_sbs &= 0x3ff;
-               }
-               if(ServerStats->is_sbr > 2047)
-               {
-                       ServerStats->is_skr += (ServerStats->is_sbr >> 10);
-                       ServerStats->is_sbr &= 0x3ff;
-               }
+               ServerStats.is_sv++;
+               ServerStats.is_sbs += client_p->localClient->sendB;
+               ServerStats.is_sbr += client_p->localClient->receiveB;
+               ServerStats.is_sti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime);
 
                /*
                 * If the connection has been up for a long amount of time, schedule
@@ -2050,44 +1853,27 @@ close_connection(struct Client *client_p)
        }
        else if(IsClient(client_p))
        {
-               ServerStats->is_cl++;
-               ServerStats->is_cbs += client_p->localClient->sendB;
-               ServerStats->is_cbr += client_p->localClient->receiveB;
-               ServerStats->is_cks += client_p->localClient->sendK;
-               ServerStats->is_ckr += client_p->localClient->receiveK;
-               ServerStats->is_cti += CurrentTime - client_p->localClient->firsttime;
-               if(ServerStats->is_cbs > 2047)
-               {
-                       ServerStats->is_cks += (ServerStats->is_cbs >> 10);
-                       ServerStats->is_cbs &= 0x3ff;
-               }
-               if(ServerStats->is_cbr > 2047)
-               {
-                       ServerStats->is_ckr += (ServerStats->is_cbr >> 10);
-                       ServerStats->is_cbr &= 0x3ff;
-               }
+               ServerStats.is_cl++;
+               ServerStats.is_cbs += client_p->localClient->sendB;
+               ServerStats.is_cbr += client_p->localClient->receiveB;
+               ServerStats.is_cti += (unsigned long long)(rb_current_time() - client_p->localClient->firsttime);
        }
        else
-               ServerStats->is_ni++;
+               ServerStats.is_ni++;
 
-       if(client_p->localClient->F)
+       if(client_p->localClient->F != NULL)
        {
                /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
                if(!IsIOError(client_p))
-                       send_queued_write(client_p->localClient->F->fd, client_p);
+                       send_queued(client_p);
 
-               rb_close(client_p->localClient->F->fd);
+               del_from_cli_fd_hash(client_p);
+               rb_close(client_p->localClient->F);
                client_p->localClient->F = NULL;
        }
 
-       if(-1 < client_p->localClient->ctrlfd)
-       {
-               rb_close(client_p->localClient->ctrlfd);
-               client_p->localClient->ctrlfd = -1;
-       }
-
-       linebuf_donebuf(&client_p->localClient->buf_sendq);
-       linebuf_donebuf(&client_p->localClient->buf_recvq);
+       rb_linebuf_donebuf(&client_p->localClient->buf_sendq);
+       rb_linebuf_donebuf(&client_p->localClient->buf_recvq);
        detach_conf(client_p);
 
        /* XXX shouldnt really be done here. */
@@ -2113,7 +1899,7 @@ error_exit_client(struct Client *client_p, int error)
         * for reading even though it ends up being an EOF. -avalon
         */
        char errmsg[255];
-       int current_error = rb_get_sockerr(client_p->localClient->F->fd);
+       int current_error = rb_get_sockerr(client_p->localClient->F);
 
        SetIOError(client_p);
 
@@ -2123,7 +1909,7 @@ error_exit_client(struct Client *client_p, int error)
                {
                        sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) && !IsServer(client_p) ? L_NETWIDE : L_ALL,
                                             "Server %s closed the connection",
-                                            get_server_name(client_p, SHOW_IP));
+                                            client_p->name);
 
                        ilog(L_SERVER, "Server %s closed the connection",
                             log_client_name(client_p, SHOW_IP));
@@ -2139,9 +1925,102 @@ error_exit_client(struct Client *client_p, int error)
        }
 
        if(error == 0)
-               strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
+               rb_strlcpy(errmsg, "Remote host closed the connection", sizeof(errmsg));
        else
                rb_snprintf(errmsg, sizeof(errmsg), "Read error: %s", strerror(current_error));
 
        exit_client(client_p, client_p, &me, errmsg);
 }
+
+/*
+ * user_metadata_add
+ * 
+ * inputs      - pointer to client struct
+ *             - name of metadata item you wish to add
+ *             - value of metadata item
+ *             - 1 if metadata should be propegated, 0 if not
+ * output      - none
+ * side effects - metadata is added to the user in question
+ *             - metadata is propegated if propegate is set.
+ */
+struct Metadata *
+user_metadata_add(struct Client *target, const char *name, const char *value, int propegate)
+{
+       struct Metadata *md;
+
+       md = rb_malloc(sizeof(struct Metadata));
+       md->name = rb_strdup(name);
+       md->value = rb_strdup(value);
+
+       irc_dictionary_add(target->user->metadata, md->name, md);
+       
+       if(propegate)
+               sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA ADD %s %s :%s",
+                               target->id, name, value);
+
+       return md;
+}
+
+/*
+ * user_metadata_delete
+ * 
+ * inputs      - pointer to client struct
+ *             - name of metadata item you wish to delete
+ * output      - none
+ * side effects - metadata is deleted from the user in question
+ *             - deletion is propegated if propegate is set
+ */
+void
+user_metadata_delete(struct Client *target, const char *name, int propegate)
+{
+       struct Metadata *md = user_metadata_find(target, name);
+
+       if(!md)
+               return;
+
+       irc_dictionary_delete(target->user->metadata, md->name);
+
+       rb_free(md);
+
+       if(propegate)
+               sendto_match_servs(&me, "*", CAP_ENCAP, NOCAPS, "ENCAP * METADATA DELETE %s %s",
+                               target->id, name);
+}
+
+/*
+ * user_metadata_find
+ * 
+ * inputs      - pointer to client struct
+ *             - name of metadata item you wish to read
+ * output      - the requested metadata, if it exists, elsewise null.
+ * side effects - 
+ */
+struct Metadata *
+user_metadata_find(struct Client *target, const char *name)
+{
+       if(!target->user)
+               return NULL;
+
+       if(!target->user->metadata)
+               return NULL;
+
+       return irc_dictionary_retrieve(target->user->metadata, name);
+}
+/*
+ * user_metadata_clear
+ * 
+ * inputs      - pointer to user struct
+ * output      - none
+ * side effects - metadata is cleared from the user in question
+ */
+void
+user_metadata_clear(struct Client *target)
+{
+       struct Metadata *md;
+       struct DictionaryIter iter;
+       
+       DICTIONARY_FOREACH(md, &iter, target->user->metadata)
+       {
+               user_metadata_delete(target, md->name, 0);
+       }
+}