]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
*slaps self* for not test compiling
[user/henk/code/inspircd.git] / src / users.cpp
index 2f1916391da9396b99a421be71811057a2b48540..c01c4e4b617e0073ebc9610709a81163c47cb857 100644 (file)
@@ -2,14 +2,11 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                    E-mail:
- *             <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
- *         the file COPYING for details.
+ *            the file COPYING for details.
  *
  * ---------------------------------------------------
  */
@@ -156,7 +153,7 @@ void userrec::StartDNSLookup()
                res_reverse = new UserResolver(this->ServerInstance, this, this->GetIPString(), DNS_QUERY_REVERSE);
                this->ServerInstance->AddResolver(res_reverse);
        }
-       catch (ModuleException& e)
+       catch (CoreException& e)
        {
                ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
        }
@@ -189,7 +186,7 @@ void UserResolver::OnLookupComplete(const std::string &result)
                                this->ServerInstance->AddResolver(bound_user->res_forward);
                        }
                }
-               catch (ModuleException& e)
+               catch (CoreException& e)
                {
                        ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
                }
@@ -215,6 +212,8 @@ void UserResolver::OnLookupComplete(const std::string &result)
                                        this->bound_user->dns_done = true;
                                        strlcpy(this->bound_user->dhost, hostname.c_str(),64);
                                        strlcpy(this->bound_user->host, hostname.c_str(),64);
+                                       /* Invalidate cache */
+                                       this->bound_user->InvalidateCache();
                                }
                        }
                        else
@@ -234,7 +233,7 @@ void UserResolver::OnError(ResolverError e, const std::string &errormessage)
        if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
        {
                /* Error message here */
-               this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname, using your IP address (%s) instead.", this->bound_user->GetIPString());
+               this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), this->bound_user->GetIPString());
                this->bound_user->dns_done = true;
        }
 }
@@ -290,6 +289,19 @@ const char* userrec::FormatModes()
        return data;
 }
 
+void userrec::DecrementModes()
+{
+       for (int n = 0; n < 64; n++)
+       {
+               if (modes[n])
+               {
+                       ModeHandler* mh = ServerInstance->Modes->FindMode(n+65, MODETYPE_USER);
+                       if (mh)
+                               mh->ChangeCount(-1);
+               }
+       }
+}
+
 userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance)
 {
        ServerInstance->Log(DEBUG,"userrec::userrec(): Instance: %08x",ServerInstance);
@@ -311,10 +323,14 @@ userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance)
        invites.clear();
        memset(modes,0,sizeof(modes));
        memset(snomasks,0,sizeof(snomasks));
+       /* Invalidate cache */
+       cached_fullhost = cached_hostip = cached_makehost = cached_fullrealhost = NULL;
 }
 
 userrec::~userrec()
 {
+       this->InvalidateCache();
+       this->DecrementModes();
        if (ip)
        {
                clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString());
@@ -352,7 +368,10 @@ userrec::~userrec()
 
 char* userrec::MakeHost()
 {
-       static char nhost[MAXBUF];
+       if (this->cached_makehost)
+               return this->cached_makehost;
+
+       char nhost[MAXBUF];
        /* This is much faster than snprintf */
        char* t = nhost;
        for(char* n = ident; *n; n++)
@@ -361,12 +380,18 @@ char* userrec::MakeHost()
        for(char* n = host; *n; n++)
                *t++ = *n;
        *t = 0;
-       return nhost;
+
+       this->cached_makehost = strdup(nhost);
+
+       return this->cached_makehost;
 }
 
 char* userrec::MakeHostIP()
 {
-       static char ihost[MAXBUF];
+       if (this->cached_hostip)
+               return this->cached_hostip;
+
+       char ihost[MAXBUF];
        /* This is much faster than snprintf */
        char* t = ihost;
        for(char* n = ident; *n; n++)
@@ -375,7 +400,10 @@ char* userrec::MakeHostIP()
        for(const char* n = this->GetIPString(); *n; n++)
                *t++ = *n;
        *t = 0;
-       return ihost;
+
+       this->cached_hostip = strdup(ihost);
+
+       return this->cached_hostip;
 }
 
 void userrec::CloseSocket()
@@ -386,7 +414,10 @@ void userrec::CloseSocket()
  
 char* userrec::GetFullHost()
 {
-       static char result[MAXBUF];
+       if (this->cached_fullhost)
+               return this->cached_fullhost;
+
+       char result[MAXBUF];
        char* t = result;
        for(char* n = nick; *n; n++)
                *t++ = *n;
@@ -397,7 +428,10 @@ char* userrec::GetFullHost()
        for(char* n = dhost; *n; n++)
                *t++ = *n;
        *t = 0;
-       return result;
+
+       this->cached_fullhost = strdup(result);
+
+       return this->cached_fullhost;
 }
 
 char* userrec::MakeWildHost()
@@ -425,7 +459,10 @@ int userrec::ReadData(void* buffer, size_t size)
 
 char* userrec::GetFullRealHost()
 {
-       static char fresult[MAXBUF];
+       if (this->cached_fullrealhost)
+               return this->cached_fullrealhost;
+
+       char fresult[MAXBUF];
        char* t = fresult;
        for(char* n = nick; *n; n++)
                *t++ = *n;
@@ -436,16 +473,17 @@ char* userrec::GetFullRealHost()
        for(char* n = host; *n; n++)
                *t++ = *n;
        *t = 0;
-       return fresult;
+
+       this->cached_fullrealhost = strdup(fresult);
+
+       return this->cached_fullrealhost;
 }
 
-bool userrec::IsInvited(irc::string &channel)
+bool userrec::IsInvited(const irc::string &channel)
 {
        for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
        {
-               irc::string compare = i->channel;
-               
-               if (compare == channel)
+               if (channel == *i)
                {
                        return true;
                }
@@ -458,30 +496,22 @@ InvitedList* userrec::GetInviteList()
        return &invites;
 }
 
-void userrec::InviteTo(irc::string &channel)
+void userrec::InviteTo(const irc::string &channel)
 {
-       Invited i;
-       i.channel = channel;
-       invites.push_back(i);
+       invites.push_back(channel);
 }
 
-void userrec::RemoveInvite(irc::string &channel)
+void userrec::RemoveInvite(const irc::string &channel)
 {
        ServerInstance->Log(DEBUG,"Removing invites");
-       
-       if (invites.size())
+       for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
        {
-               for (InvitedList::iterator i = invites.begin(); i != invites.end(); i++)
+               if (channel == *i)
                {
-                       irc::string compare = i->channel;
-                       
-                       if (compare == channel)
-                       {
-                               invites.erase(i);
-                               return;
-                               }
-                       }
-               }
+                       invites.erase(i);
+                       return;
+               }
+       }
 }
 
 bool userrec::HasPermission(const std::string &command)
@@ -774,20 +804,28 @@ void userrec::UnOper()
 
 void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &quitreason)
 {
-       user_hash::iterator iter = Instance->clientlist.find(user->nick);
+       user_hash::iterator iter = Instance->clientlist->find(user->nick);
        std::string reason = quitreason;
 
        if (reason.length() > MAXQUIT - 1)
                reason.resize(MAXQUIT - 1);
-       
+
+       if (user->registered != REG_ALL)
+               if (Instance->unregistered_count)
+                       Instance->unregistered_count--;
+
        if (IS_LOCAL(user))
+       {
                user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason.c_str());
+               if ((!user->sendq.empty()) && (!(*user->GetWriteError())))
+                       user->FlushWriteBuf();
+       }
 
        if (user->registered == REG_ALL)
        {
                user->PurgeEmptyChannels();
-               FOREACH_MOD_I(Instance,I_OnUserQuit,OnUserQuit(user,reason));
                user->WriteCommonExcept("QUIT :%s",reason.c_str());
+               FOREACH_MOD_I(Instance,I_OnUserQuit,OnUserQuit(user,reason));
        }
 
        FOREACH_MOD_I(Instance,I_OnUserDisconnect,OnUserDisconnect(user));
@@ -800,9 +838,9 @@ void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &qui
                        {
                                Instance->Config->GetIOHook(user->GetPort())->OnRawSocketClose(user->fd);
                        }
-                       catch (ModuleException& modexcept)
+                       catch (CoreException& modexcept)
                        {
-                               Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+                               Instance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                        }
                }
                
@@ -823,15 +861,16 @@ void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &qui
                user->AddToWhoWas();
        }
 
-       if (iter != Instance->clientlist.end())
+       if (iter != Instance->clientlist->end())
        {
                Instance->Log(DEBUG,"deleting user hash value %lx",(unsigned long)user);
                if (IS_LOCAL(user))
                {
-                       if (find(Instance->local_users.begin(),Instance->local_users.end(),user) != Instance->local_users.end())
-                               Instance->local_users.erase(find(Instance->local_users.begin(),Instance->local_users.end(),user));
+                       std::vector<userrec*>::iterator x = find(Instance->local_users.begin(),Instance->local_users.end(),user);
+                       if (x != Instance->local_users.end())
+                               Instance->local_users.erase(x);
                }
-               Instance->clientlist.erase(iter);
+               Instance->clientlist->erase(iter);
                DELETE(user);
        }
 }
@@ -887,49 +926,61 @@ namespace irc
                        int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
                        int maxkeep =   ServerInstance->Config->WhoWasMaxKeep;
 
-                       int groupcount = ServerInstance->whowas.size();
-                       /* iterate whowas_fifo oldest first */
-                       whowas_users_fifo::iterator iter, safeiter;
-                       for (iter = ServerInstance->whowas_fifo.begin(); iter != ServerInstance->whowas_fifo.end(); iter++)
+                       /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
+                       whowas_users::iterator iter;
+                       int fifosize;
+                       while ((fifosize = (int)ServerInstance->whowas_fifo.size()) > 0)
                        {
-                               /** prune all groups that has expired due to new maxkeep time and
-                                *  also any group number higher than new maxgroups. The oldest are
-                                *  removed first due to iteration over whowas_fifo 
-                                */
-                               if (groupcount > maxgroups || iter->first < t - maxkeep)
+                               if (fifosize > maxgroups || ServerInstance->whowas_fifo[0].first < t - maxkeep)
                                {
-                                       whowas_set* n = (whowas_set*)ServerInstance->whowas.find(iter->second)->second;
+                                       iter = ServerInstance->whowas.find(ServerInstance->whowas_fifo[0].second);
+                                       /* hopefully redundant integrity check, but added while debugging r6216 */
+                                       if (iter == ServerInstance->whowas.end())
+                                       {
+                                               /* this should never happen, if it does maps are corrupt */
+                                               ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
+                                               return;
+                                       }
+                                       whowas_set* n = (whowas_set*)iter->second;
                                        if (n->size())
                                        {
                                                while (n->begin() != n->end())
                                                {
                                                        WhoWasGroup *a = *(n->begin());
                                                        DELETE(a);
-                                                       n->erase(n->begin());
+                                                       n->pop_front();
                                                }
                                        }
-                                       ServerInstance->whowas.erase(iter->second);
-                                       /* use a safe iter copy for erase and set the orig iter old valid ref by decrementing it */
-                                       safeiter = iter;
-                                       --iter;
-                                       ServerInstance->whowas_fifo.erase(safeiter);
+                                       ServerInstance->whowas.erase(iter);
+                                       ServerInstance->whowas_fifo.pop_front();
                                }
-                               else {
-                                       /* also trim individual groupsizes in case groupsize should have been lowered */
-                                       whowas_set* n = (whowas_set*)ServerInstance->whowas.find(iter->second)->second;
-                                       if (n->size())
+                               else
+                                       break;
+                       }
+
+                       /* Then cut the whowas sets to new size (groupsize) */
+                       for (int i = 0; i < fifosize; i++)
+                       {
+                               iter = ServerInstance->whowas.find(ServerInstance->whowas_fifo[0].second);
+                               /* hopefully redundant integrity check, but added while debugging r6216 */
+                               if (iter == ServerInstance->whowas.end())
+                               {
+                                       /* this should never happen, if it does maps are corrupt */
+                                       ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (2)");
+                                       return;
+                               }
+                               whowas_set* n = (whowas_set*)iter->second;
+                               if (n->size())
+                               {
+                                       int nickcount = n->size();
+                                       while (n->begin() != n->end() && nickcount > groupsize)
                                        {
-                                               int nickcount = n->size();
-                                               while (n->begin() != n->end() && nickcount > groupsize)
-                                               {
-                                                       WhoWasGroup *a = *(n->begin());
-                                                       DELETE(a);
-                                                       n->erase(n->begin());
-                                                       nickcount--;
-                                               }
+                                               WhoWasGroup *a = *(n->begin());
+                                               DELETE(a);
+                                               n->pop_front();
+                                               nickcount--;
                                        }
                                }
-                               groupcount--;
                        }
                }
        };
@@ -951,16 +1002,33 @@ void userrec::AddToWhoWas()
        if (iter == ServerInstance->whowas.end())
        {
                ServerInstance->Log(DEBUG,"Adding new whowas set for %s",this->nick);
+
                irc::whowas::whowas_set* n = new irc::whowas::whowas_set;
                irc::whowas::WhoWasGroup *a = new irc::whowas::WhoWasGroup(this);
                n->push_back(a);
                ServerInstance->whowas[this->nick] = n;
-               ServerInstance->whowas_fifo[ServerInstance->Time()] = this->nick;
+               ServerInstance->whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),this->nick));
+
                if ((int)(ServerInstance->whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
                {
-                       ServerInstance->Log(DEBUG,"Maxgroups of %d reached deleting oldest group '%s'",ServerInstance->Config->WhoWasMaxGroups, ServerInstance->whowas_fifo.begin()->second.c_str());
-                       ServerInstance->whowas.erase(ServerInstance->whowas_fifo.begin()->second);
-                       ServerInstance->whowas_fifo.erase(ServerInstance->whowas_fifo.begin());
+                       ServerInstance->Log(DEBUG,"Maxgroups of %d reached deleting oldest group '%s'",ServerInstance->Config->WhoWasMaxGroups, ServerInstance->whowas_fifo[0].second.c_str());
+
+                       irc::whowas::whowas_users::iterator iter = ServerInstance->whowas.find(ServerInstance->whowas_fifo[0].second);
+                       if (iter != ServerInstance->whowas.end())
+                       {
+                               irc::whowas::whowas_set* n = (irc::whowas::whowas_set*)iter->second;
+                               if (n->size())
+                               {
+                                       while (n->begin() != n->end())
+                                       {
+                                               irc::whowas::WhoWasGroup *a = *(n->begin());
+                                               DELETE(a);
+                                               n->pop_front();
+                                       }
+                               }
+                               ServerInstance->whowas.erase(iter);
+                       }
+                       ServerInstance->whowas_fifo.pop_front();
                }
        }
        else
@@ -969,16 +1037,16 @@ void userrec::AddToWhoWas()
 
                ServerInstance->Log(DEBUG,"Using existing whowas group for %s",this->nick);
 
-               if ((int)(group->size()) >= ServerInstance->Config->WhoWasGroupSize)
+               irc::whowas::WhoWasGroup *a = new irc::whowas::WhoWasGroup(this);
+               group->push_back(a);
+
+               if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
                {
                        ServerInstance->Log(DEBUG,"Trimming existing group '%s' to %d entries",this->nick, ServerInstance->Config->WhoWasGroupSize);
                        irc::whowas::WhoWasGroup *a = (irc::whowas::WhoWasGroup*)*(group->begin());
                        DELETE(a);
                        group->pop_front();
                }
-
-               irc::whowas::WhoWasGroup *a = new irc::whowas::WhoWasGroup(this);
-               group->push_back(a);
        }
 }
 
@@ -986,11 +1054,13 @@ void userrec::AddToWhoWas()
 void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached, insp_inaddr ip)
 {
        std::string tempnick = ConvToStr(socket) + "-unknown";
-       user_hash::iterator iter = Instance->clientlist.find(tempnick);
+       user_hash::iterator iter = Instance->clientlist->find(tempnick);
        const char *ipaddr = insp_ntoa(ip);
        userrec* New;
        int j = 0;
 
+       Instance->unregistered_count++;
+
        /*
         * fix by brain.
         * as these nicknames are 'RFC impossible', we can be sure nobody is going to be
@@ -1000,17 +1070,17 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
         * this was probably the cause of 'server ignores me when i hammer it with reconnects'
         * issue in earlier alphas/betas
         */
-       if (iter != Instance->clientlist.end())
+       if (iter != Instance->clientlist->end())
        {
                userrec* goner = iter->second;
                DELETE(goner);
-               Instance->clientlist.erase(iter);
+               Instance->clientlist->erase(iter);
        }
 
        Instance->Log(DEBUG,"AddClient: %d %d %s",socket,port,ipaddr);
 
        New = new userrec(Instance);
-       Instance->clientlist[tempnick] = New;
+       (*(Instance->clientlist))[tempnick] = New;
        New->fd = socket;
        strlcpy(New->nick,tempnick.c_str(),NICKMAX-1);
 
@@ -1032,33 +1102,21 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
        Instance->AddLocalClone(New);
        Instance->AddGlobalClone(New);
 
-       // set the registration timeout for this user
-       unsigned long class_regtimeout = 90;
-       int class_flood = 0;
-       long class_threshold = 5;
-       long class_sqmax = 262144;      // 256kb
-       long class_rqmax = 4096;        // 4k
+       ConnectClass* i = New->GetClass();
 
-       for (ClassVector::iterator i = Instance->Config->Classes.begin(); i != Instance->Config->Classes.end(); i++)
+       if ((!i) || (i->GetType() == CC_DENY))
        {
-               if ((i->type == CC_ALLOW) && (match(ipaddr,i->host.c_str(),true)))
-               {
-                       class_regtimeout = (unsigned long)i->registration_timeout;
-                       class_flood = i->flood;
-                       New->pingmax = i->pingtime;
-                       class_threshold = i->threshold;
-                       class_sqmax = i->sendqmax;
-                       class_rqmax = i->recvqmax;
-                       break;
-               }
+               userrec::QuitUser(Instance, New,"Unauthorised connection");
+               return;
        }
 
-       New->nping = Instance->Time() + New->pingmax + Instance->Config->dns_timeout;
-       New->timeout = Instance->Time() + class_regtimeout;
-       New->flood = class_flood;
-       New->threshold = class_threshold;
-       New->sendqmax = class_sqmax;
-       New->recvqmax = class_rqmax;
+       New->pingmax = i->GetPingTime();
+       New->nping = Instance->Time() + i->GetPingTime() + Instance->Config->dns_timeout;
+       New->timeout = Instance->Time() + i->GetRegTimeout();
+       New->flood = i->GetFlood();
+       New->threshold = i->GetThreshold();
+       New->sendqmax = i->GetSendqMax();
+       New->recvqmax = i->GetRecvqMax();
 
        Instance->local_users.push_back(New);
 
@@ -1113,7 +1171,7 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
        New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
 }
 
-long userrec::GlobalCloneCount()
+unsigned long userrec::GlobalCloneCount()
 {
        clonemap::iterator x = ServerInstance->global_clones.find(this->GetIPString());
        if (x != ServerInstance->global_clones.end())
@@ -1122,7 +1180,7 @@ long userrec::GlobalCloneCount()
                return 0;
 }
 
-long userrec::LocalCloneCount()
+unsigned long userrec::LocalCloneCount()
 {
        clonemap::iterator x = ServerInstance->local_clones.find(this->GetIPString());
        if (x != ServerInstance->local_clones.end())
@@ -1136,30 +1194,30 @@ void userrec::FullConnect(CullList* Goners)
        ServerInstance->stats->statsConnects++;
        this->idle_lastmsg = ServerInstance->Time();
 
-       ConnectClass a = this->GetClass();
+       ConnectClass* a = this->GetClass();
 
-       if (a.type == CC_DENY)
+       if ((!a) || (a->GetType() == CC_DENY))
        {
                Goners->AddItem(this,"Unauthorised connection");
                return;
        }
-       
-       if ((*(a.pass.c_str())) && (!this->haspassed))
+
+       if ((!a->GetPass().empty()) && (!this->haspassed))
        {
                Goners->AddItem(this,"Invalid password");
                return;
        }
        
-       if (this->LocalCloneCount() > a.maxlocal)
+       if (this->LocalCloneCount() > a->GetMaxLocal())
        {
                Goners->AddItem(this, "No more connections allowed from your host via this connect class (local)");
-               ServerInstance->WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s", a.maxlocal, this->GetIPString());
+               ServerInstance->WriteOpers("*** WARNING: maximum LOCAL connections (%ld) exceeded for IP %s", a->GetMaxLocal(), this->GetIPString());
                return;
        }
-       else if (this->GlobalCloneCount() > a.maxglobal)
+       else if (this->GlobalCloneCount() > a->GetMaxGlobal())
        {
                Goners->AddItem(this, "No more connections allowed from your host via this connect class (global)");
-               ServerInstance->WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a.maxglobal, this->GetIPString());
+               ServerInstance->WriteOpers("*** WARNING: maximum GLOBAL connections (%ld) exceeded for IP %s",a->GetMaxGlobal(), this->GetIPString());
                return;
        }
 
@@ -1184,8 +1242,8 @@ void userrec::FullConnect(CullList* Goners)
                        Goners->AddItem(this, reason);
                        return;
                }
-       }
 
+       }
 
        this->WriteServ("NOTICE Auth :Welcome to \002%s\002!",ServerInstance->Config->Network);
        this->WriteServ("001 %s :Welcome to the %s IRC Network %s!%s@%s",this->nick, ServerInstance->Config->Network, this->nick, this->ident, this->host);
@@ -1197,6 +1255,10 @@ void userrec::FullConnect(CullList* Goners)
 
        this->ShowMOTD();
 
+       /* Now registered */
+       if (ServerInstance->unregistered_count)
+               ServerInstance->unregistered_count--;
+
        /*
         * fix 3 by brain, move registered = 7 below these so that spurious modes and host
         * changes dont go out onto the network and produce 'fake direction'.
@@ -1219,18 +1281,18 @@ userrec* userrec::UpdateNickHash(const char* New)
        try
        {
                //user_hash::iterator newnick;
-               user_hash::iterator oldnick = ServerInstance->clientlist.find(this->nick);
+               user_hash::iterator oldnick = ServerInstance->clientlist->find(this->nick);
 
                if (!strcasecmp(this->nick,New))
                        return oldnick->second;
 
-               if (oldnick == ServerInstance->clientlist.end())
+               if (oldnick == ServerInstance->clientlist->end())
                        return NULL; /* doesnt exist */
 
                userrec* olduser = oldnick->second;
-               ServerInstance->clientlist[New] = olduser;
-               ServerInstance->clientlist.erase(oldnick);
-               return ServerInstance->clientlist[New];
+               (*(ServerInstance->clientlist))[New] = olduser;
+               ServerInstance->clientlist->erase(oldnick);
+               return olduser;
        }
 
        catch (...)
@@ -1240,11 +1302,27 @@ userrec* userrec::UpdateNickHash(const char* New)
        }
 }
 
+void userrec::InvalidateCache()
+{
+       /* Invalidate cache */
+       if (cached_fullhost)
+               free(cached_fullhost);
+       if (cached_hostip)
+               free(cached_hostip);
+       if (cached_makehost)
+               free(cached_makehost);
+       if (cached_fullrealhost)
+               free(cached_fullrealhost);
+       cached_fullhost = cached_hostip = cached_makehost = cached_fullrealhost = NULL;
+}
+
 bool userrec::ForceNickChange(const char* newnick)
 {
        try
        {
                int MOD_RESULT = 0;
+
+               this->InvalidateCache();
        
                FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(this, newnick));
 
@@ -1331,7 +1409,6 @@ int userrec::GetPort()
                }
                break;
                default:
-                       ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
                break;
        }
        return 0;
@@ -1381,7 +1458,6 @@ const char* userrec::GetIPString()
                }
                break;
                default:
-                       ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
                break;
        }
        return "";
@@ -1424,7 +1500,6 @@ const char* userrec::GetIPString(char* buf)
                break;
 
                default:
-                       ServerInstance->Log(DEBUG,"Ut oh, '%s' has an unknown protocol family!",this->nick);
                break;
        }
        return "";
@@ -1457,9 +1532,9 @@ void userrec::Write(std::string text)
                {
                        ServerInstance->Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd, text.data(), text.length());
                }
-               catch (ModuleException& modexcept)
+               catch (CoreException& modexcept)
                {
-                       ServerInstance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
+                       ServerInstance->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                }
        }
        else
@@ -1573,11 +1648,16 @@ void userrec::WriteCommon(const std::string &text)
        try
        {
                bool sent_to_at_least_one = false;
+               char tb[MAXBUF];
        
                if (this->registered != REG_ALL)
                        return;
        
                uniq_id++;
+
+               /* We dont want to be doing this n times, just once */
+               snprintf(tb,MAXBUF,":%s %s",this->GetFullHost(),text.c_str());
+               std::string out = tb;
        
                for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
                {
@@ -1587,7 +1667,7 @@ void userrec::WriteCommon(const std::string &text)
                                if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
                                {
                                        already_sent[i->second->fd] = uniq_id;
-                                       i->second->WriteFrom(this, std::string(text));
+                                       i->second->Write(out);
                                        sent_to_at_least_one = true;
                                }
                        }
@@ -1599,7 +1679,7 @@ void userrec::WriteCommon(const std::string &text)
                 */
                if (!sent_to_at_least_one)
                {
-                       this->WriteFrom(this,std::string(text));
+                       this->Write(std::string(tb));
                }
        }
 
@@ -1631,6 +1711,10 @@ void userrec::WriteCommonExcept(const std::string &text)
        bool quit_munge = false;
        char oper_quit[MAXBUF];
        char textbuffer[MAXBUF];
+       char tb1[MAXBUF];
+       char tb2[MAXBUF];
+       std::string out1;
+       std::string out2;
 
        strlcpy(textbuffer, text.c_str(), MAXBUF);
 
@@ -1639,6 +1723,8 @@ void userrec::WriteCommonExcept(const std::string &text)
 
        uniq_id++;
 
+       snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost(),textbuffer);
+
        /* TODO: We need some form of WriteCommonExcept that will send two lines, one line to
         * opers and the other line to non-opers, then all this hidebans and hidesplits gunk
         * can go byebye.
@@ -1661,6 +1747,8 @@ void userrec::WriteCommonExcept(const std::string &text)
                                strlcpy(oper_quit,textbuffer,MAXQUIT);
                                strlcpy(check,"*.net *.split",MAXQUIT);
                                quit_munge = true;
+                               snprintf(tb2,MAXBUF,":%s %s",this->GetFullHost(),oper_quit);
+                               out2 = tb2;
                        }
                }
        }
@@ -1674,9 +1762,13 @@ void userrec::WriteCommonExcept(const std::string &text)
                        strlcpy(oper_quit,textbuffer,MAXQUIT);
                        *check = 0;  // We don't need to strlcpy, we just chop it from the :
                        quit_munge = true;
+                       snprintf(tb2,MAXBUF,":%s %s",this->GetFullHost(),oper_quit);
+                       out2 = tb2;
                }
        }
 
+       out1 = tb1;
+
        for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
        {
                CUList *ulist = v->first->GetUsers();
@@ -1688,9 +1780,9 @@ void userrec::WriteCommonExcept(const std::string &text)
                                {
                                        already_sent[i->second->fd] = uniq_id;
                                        if (quit_munge)
-                                               i->second->WriteFrom(this, *i->second->oper ? std::string(oper_quit) : std::string(textbuffer));
+                                               i->second->Write(*i->second->oper ? out2 : out1);
                                        else
-                                               i->second->WriteFrom(this, std::string(textbuffer));
+                                               i->second->Write(out1);
                                }
                        }
                }
@@ -1765,16 +1857,6 @@ bool userrec::SharesChannelWith(userrec *other)
        return false;
 }
 
-int userrec::CountChannels()
-{
-       return ChannelCount;
-}
-
-void userrec::ModChannelCount(int n)
-{
-       ChannelCount += n;
-}
-
 bool userrec::ChangeName(const char* gecos)
 {
        if (!strcmp(gecos, this->fullname))
@@ -1789,6 +1871,7 @@ bool userrec::ChangeName(const char* gecos)
                FOREACH_MOD(I_OnChangeName,OnChangeName(this,gecos));
        }
        strlcpy(this->fullname,gecos,MAXGECOS+1);
+
        return true;
 }
 
@@ -1811,6 +1894,8 @@ bool userrec::ChangeDisplayedHost(const char* host)
        /* Fix by Om: userrec::dhost is 65 long, this was truncating some long hosts */
        strlcpy(this->dhost,host,64);
 
+       this->InvalidateCache();
+
        if (this->ServerInstance->Config->CycleHosts)
        {
                for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
@@ -1838,6 +1923,8 @@ bool userrec::ChangeIdent(const char* newident)
 
        strlcpy(this->ident, newident, IDENTMAX+2);
 
+       this->InvalidateCache();
+
        if (this->ServerInstance->Config->CycleHosts)
        {
                for (UCListIter i = this->chans.begin(); i != this->chans.end(); i++)
@@ -1862,12 +1949,12 @@ void userrec::NoticeAll(char* text, ...)
        vsnprintf(textbuffer, MAXBUF, text, argsPtr);
        va_end(argsPtr);
 
-       snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
+       snprintf(formatbuffer,MAXBUF,":%s NOTICE $* :%s", this->GetFullHost(), textbuffer);
+       std::string fmt = formatbuffer;
 
        for (std::vector<userrec*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
        {
-               userrec* t = *i;
-               t->WriteFrom(this, std::string(formatbuffer));
+               (*i)->Write(fmt);
        }
 }
 
@@ -1948,15 +2035,14 @@ void userrec::SplitChanList(userrec* dest, const std::string &cl)
  * then their ip will be taken as 'priority' anyway, so for example,
  * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
  */
-ConnectClass& userrec::GetClass()
+ConnectClass* userrec::GetClass()
 {
        for (ClassVector::iterator i = ServerInstance->Config->Classes.begin(); i != ServerInstance->Config->Classes.end(); i++)
        {
-               if ((match(this->GetIPString(),i->host.c_str(),true)) || (match(this->host,i->host.c_str())))
-                       return *i;
+               if ((match(this->GetIPString(),i->GetHost().c_str(),true)) || (match(this->host,i->GetHost().c_str())))
+                       return &(*i);
        }
-
-       return *(ServerInstance->Config->Classes.begin());
+       return NULL;
 }
 
 void userrec::PurgeEmptyChannels()
@@ -1984,12 +2070,13 @@ void userrec::PurgeEmptyChannels()
        for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
        {
                chanrec* thischan = *n;
-               chan_hash::iterator i2 = ServerInstance->chanlist.find(thischan->name);
-               if (i2 != ServerInstance->chanlist.end())
+               chan_hash::iterator i2 = ServerInstance->chanlist->find(thischan->name);
+               if (i2 != ServerInstance->chanlist->end())
                {
                        FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
                        DELETE(i2->second);
-                       ServerInstance->chanlist.erase(i2);
+                       ServerInstance->chanlist->erase(i2);
+                       this->chans.erase(*n);
                }
        }
 
@@ -2029,13 +2116,14 @@ void userrec::ShowRULES()
 void userrec::HandleEvent(EventType et, int errornum)
 {
        /* WARNING: May delete this user! */
+       int thisfd = this->GetFd();
+
        try
        {
                switch (et)
                {
                        case EVENT_READ:
                                ServerInstance->ProcessUser(this);
-
                        break;
                        case EVENT_WRITE:
                                this->FlushWriteBuf();
@@ -2052,7 +2140,12 @@ void userrec::HandleEvent(EventType et, int errornum)
        }
 
        /* If the user has raised an error whilst being processed, quit them now we're safe to */
-       if (!WriteError.empty())
-               userrec::QuitUser(ServerInstance, this, GetWriteError());
+       if ((ServerInstance->SE->GetRef(thisfd) == this))
+       {
+               if (!WriteError.empty())
+               {
+                       userrec::QuitUser(ServerInstance, this, GetWriteError());
+               }
+       }
 }