]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Tidyup
[user/henk/code/inspircd.git] / src / users.cpp
index f8b8efeb0589a00f2fe3aeb30dab38d5ce85a201..046d6dcef45691a9243c8332edaf6830bc344b24 100644 (file)
 #include "xline.h"
 #include "cull_list.h"
 
-irc::whowas::whowas_users whowas;
 static unsigned long already_sent[MAX_DESCRIPTORS] = {0};
 
-typedef std::map<irc::string,char*> opertype_t;
-typedef opertype_t operclass_t;
-
-opertype_t opertypes;
-operclass_t operclass;
-
 /* XXX: Used for speeding up WriteCommon operations */
 unsigned long uniq_id = 0;
 
 bool InitTypes(ServerConfig* conf, const char* tag)
 {
-       for (opertype_t::iterator n = opertypes.begin(); n != opertypes.end(); n++)
+       if (conf->opertypes.size())
        {
-               if (n->second)
-                       delete[] n->second;
+               conf->GetInstance()->Log(DEBUG,"Currently %d items to clear",conf->opertypes.size());
+               for (opertype_t::iterator n = conf->opertypes.begin(); n != conf->opertypes.end(); n++)
+               {
+                       conf->GetInstance()->Log(DEBUG,"Clear item");
+                       if (n->second)
+                               delete[] n->second;
+               }
        }
        
-       opertypes.clear();
+       conf->opertypes.clear();
        return true;
 }
 
 bool InitClasses(ServerConfig* conf, const char* tag)
 {
-       for (operclass_t::iterator n = operclass.begin(); n != operclass.end(); n++)
+       if (conf->operclass.size())
        {
-               if (n->second)
-                       delete[] n->second;
+               for (operclass_t::iterator n = conf->operclass.begin(); n != conf->operclass.end(); n++)
+               {
+                       if (n->second)
+                               delete[] n->second;
+               }
        }
        
-       operclass.clear();
+       conf->operclass.clear();
        return true;
 }
 
-bool DoType(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
+bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
 {
-       char* TypeName = (char*)values[0];
-       char* Classes = (char*)values[1];
+       const char* TypeName = values[0].GetString();
+       const char* Classes = values[1].GetString();
        
-       opertypes[TypeName] = strdup(Classes);
+       conf->opertypes[TypeName] = strdup(Classes);
        conf->GetInstance()->Log(DEBUG,"Read oper TYPE '%s' with classes '%s'",TypeName,Classes);
        return true;
 }
 
-bool DoClass(ServerConfig* conf, const char* tag, char** entries, void** values, int* types)
+bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types)
 {
-       char* ClassName = (char*)values[0];
-       char* CommandList = (char*)values[1];
+       const char* ClassName = values[0].GetString();
+       const char* CommandList = values[1].GetString();
        
-       operclass[ClassName] = strdup(CommandList);
+       conf->operclass[ClassName] = strdup(CommandList);
        conf->GetInstance()->Log(DEBUG,"Read oper CLASS '%s' with commands '%s'",ClassName,CommandList);
        return true;
 }
@@ -89,7 +90,7 @@ std::string userrec::ProcessNoticeMasks(const char *sm)
 {
        bool adding = true, oldadding = false;
        const char *c = sm;
-       std::string output = "";
+       std::string output;
 
        ServerInstance->Log(DEBUG,"Process notice masks");
 
@@ -105,6 +106,24 @@ std::string userrec::ProcessNoticeMasks(const char *sm)
                        case '-':
                                adding = false;
                        break;
+                       case '*':
+                               for (unsigned char d = 'A'; d <= 'z'; d++)
+                               {
+                                       if (ServerInstance->SNO->IsEnabled(d))
+                                       {
+                                               if ((!IsNoticeMaskSet(d) && adding) || (IsNoticeMaskSet(d) && !adding))
+                                               {
+                                                       if ((oldadding != adding) || (!output.length()))
+                                                               output += (adding ? '+' : '-');
+
+                                                       this->SetNoticeMask(d, adding);
+
+                                                       output += d;
+                                               }
+                                       }
+                                       oldadding = adding;
+                               }
+                       break;
                        default:
                                if ((*c >= 'A') && (*c <= 'z') && (ServerInstance->SNO->IsEnabled(*c)))
                                {
@@ -134,7 +153,7 @@ void userrec::StartDNSLookup()
        try
        {
                ServerInstance->Log(DEBUG,"Passing instance: %08x",this->ServerInstance);
-               res_reverse = new UserResolver(this->ServerInstance, this, this->GetIPString(), false);
+               res_reverse = new UserResolver(this->ServerInstance, this, this->GetIPString(), DNS_QUERY_REVERSE);
                this->ServerInstance->AddResolver(res_reverse);
        }
        catch (ModuleException& e)
@@ -143,10 +162,10 @@ void userrec::StartDNSLookup()
        }
 }
 
-UserResolver::UserResolver(InspIRCd* Instance, userrec* user, std::string to_resolve, bool forward) :
-       Resolver(Instance, to_resolve, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE), bound_user(user)
+UserResolver::UserResolver(InspIRCd* Instance, userrec* user, std::string to_resolve, QueryType qt) :
+       Resolver(Instance, to_resolve, qt), bound_user(user)
 {
-       this->fwd = forward;
+       this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
        this->bound_fd = user->GetFd();
 }
 
@@ -161,7 +180,12 @@ void UserResolver::OnLookupComplete(const std::string &result)
                        /* Check we didnt time out */
                        if (this->bound_user->registered != REG_ALL)
                        {
-                               bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, true);
+#ifdef IPV6
+                               const char *ip = this->bound_user->GetIPString();
+                               bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (strstr(ip,"0::ffff:") == ip ? DNS_QUERY_A : DNS_QUERY_AAAA));
+#else
+                               bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A);
+#endif
                                this->ServerInstance->AddResolver(bound_user->res_forward);
                        }
                }
@@ -173,7 +197,9 @@ void UserResolver::OnLookupComplete(const std::string &result)
        else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
        {
                /* Both lookups completed */
-               if (this->bound_user->GetIPString() == result)
+               std::string result2 = "0::ffff:";
+               result2.append(result);
+               if (this->bound_user->GetIPString() == result || this->bound_user->GetIPString() == result2)
                {
                        std::string hostname = this->bound_user->stored_host;
                        if (hostname.length() < 65)
@@ -271,6 +297,7 @@ userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance)
        *password = *nick = *ident = *host = *dhost = *fullname = *awaymsg = *oper = 0;
        server = (char*)Instance->FindServerNamePtr(Instance->Config->ServerName);
        reset_due = ServerInstance->Time();
+       age = ServerInstance->Time(true);
        lines_in = lastping = signon = idle_lastmsg = nping = registered = 0;
        ChannelCount = timeout = flood = bytes_in = bytes_out = cmds_in = cmds_out = 0;
        haspassed = dns_done = false;
@@ -288,10 +315,9 @@ userrec::userrec(InspIRCd* Instance) : ServerInstance(Instance)
        
        for (unsigned int n = 0; n < MAXCHANS; n++)
        {
-               ucrec* x = new ucrec();
-               chans[n] = x;
-               x->channel = NULL;
-               x->uc_modes = 0;
+               chans[n] = new ucrec();
+               chans[n]->channel = NULL;
+               chans[n]->uc_modes = 0;
        }
 }
 
@@ -318,9 +344,9 @@ userrec::~userrec()
        }
 }
 
-/* XXX - minor point, other *Host functions return a char *, this one creates it. Might be nice to be consistant? */
-void userrec::MakeHost(char* nhost)
+char* userrec::MakeHost()
 {
+       static char nhost[MAXBUF];
        /* This is much faster than snprintf */
        char* t = nhost;
        for(char* n = ident; *n; n++)
@@ -329,6 +355,21 @@ void userrec::MakeHost(char* nhost)
        for(char* n = host; *n; n++)
                *t++ = *n;
        *t = 0;
+       return nhost;
+}
+
+char* userrec::MakeHostIP()
+{
+       static char ihost[MAXBUF];
+       /* This is much faster than snprintf */
+       char* t = ihost;
+       for(char* n = ident; *n; n++)
+               *t++ = *n;
+       *t++ = '@';
+       for(const char* n = this->GetIPString(); *n; n++)
+               *t++ = *n;
+       *t = 0;
+       return ihost;
 }
 
 void userrec::CloseSocket()
@@ -367,7 +408,7 @@ char* userrec::MakeWildHost()
 
 int userrec::ReadData(void* buffer, size_t size)
 {
-       if (this->fd > -1)
+       if (IS_LOCAL(this))
        {
                return read(this->fd, buffer, size);
        }
@@ -456,15 +497,15 @@ bool userrec::HasPermission(const std::string &command)
        // are they even an oper at all?
        if (*this->oper)
        {
-               opertype_t::iterator iter_opertype = opertypes.find(this->oper);
-               if (iter_opertype != opertypes.end())
+               opertype_t::iterator iter_opertype = ServerInstance->Config->opertypes.find(this->oper);
+               if (iter_opertype != ServerInstance->Config->opertypes.end())
                {
                        char* Classes = strdup(iter_opertype->second);
                        char* myclass = strtok_r(Classes," ",&savept);
                        while (myclass)
                        {
-                               operclass_t::iterator iter_operclass = operclass.find(myclass);
-                               if (iter_operclass != operclass.end())
+                               operclass_t::iterator iter_operclass = ServerInstance->Config->operclass.find(myclass);
+                               if (iter_operclass != ServerInstance->Config->operclass.end())
                                {
                                        char* CommandList = strdup(iter_operclass->second);
                                        mycmd = strtok_r(CommandList," ",&savept2);
@@ -615,27 +656,39 @@ void userrec::FlushWriteBuf()
 {
        try
        {
-               if (this->fd == FD_MAGIC_NUMBER)
+               if ((this->fd == FD_MAGIC_NUMBER) || (*this->GetWriteError()))
                {
                        sendq = "";
                }
                if ((sendq.length()) && (this->fd != FD_MAGIC_NUMBER))
                {
+                       int old_sendq_length = sendq.length();
                        const char* tb = this->sendq.c_str();
-                               int n_sent = write(this->fd,tb,this->sendq.length());
+                       int n_sent = write(this->fd,tb,this->sendq.length());
                        if (n_sent == -1)
                        {
-                               if (errno != EAGAIN)
+                               if (errno == EAGAIN)
+                               {
+                                       ServerInstance->Log(DEBUG,"EAGAIN, want write");
+                                       this->ServerInstance->SE->WantWrite(this);
+                               }
+                               else
                                        this->SetWriteError(strerror(errno));
                        }
                        else
                        {
+                               /*ServerInstance->Log(DEBUG,"Wrote: %d of %d: %s", n_sent, old_sendq_length, sendq.substr(0, n_sent).c_str());*/
                                // advance the queue
                                tb += n_sent;
                                this->sendq = tb;
                                // update the user's stats counters
                                this->bytes_out += n_sent;
                                this->cmds_out++;
+                               if (n_sent != old_sendq_length)
+                               {
+                                       ServerInstance->Log(DEBUG,"Not all written, want write");
+                                       this->ServerInstance->SE->WantWrite(this);
+                               }
                        }
                }
        }
@@ -696,7 +749,7 @@ void userrec::UnOper()
                if (*this->oper)
                {
                        *this->oper = 0;
-                               this->modes[UM_OPERATOR] = 0;
+                       this->modes[UM_OPERATOR] = 0;
                        for (std::vector<userrec*>::iterator a = ServerInstance->all_opers.begin(); a < ServerInstance->all_opers.end(); a++)
                        {
                                if (*a == this)
@@ -715,20 +768,9 @@ void userrec::UnOper()
        }
 }
 
-void userrec::QuitUser(InspIRCd* Instance, userrec *user,const std::string &quitreason)
+void userrec::QuitUser(InspIRCd* Instance, userrec *user, const std::string &quitreason)
 {
        user_hash::iterator iter = Instance->clientlist.find(user->nick);
-
-/*
- * I'm pretty sure returning here is causing a desync when part of the net thinks a user is gone,
- * and another part doesn't. We want to broadcast the quit/kill before bailing so the net stays in sync.
- *
- * I can't imagine this blowing up, so I'm commenting it out. We still check
- * before playing with a bad iterator below in our if(). DISCUSS THIS BEFORE YOU DO ANYTHING. --w00t
- *
- *     if (iter == clientlist.end())
- *             return;
- */
        std::string reason = quitreason;
 
        if (reason.length() > MAXQUIT - 1)
@@ -819,15 +861,15 @@ namespace irc
                                free(gecos);
                }
 
-               /* every hour, run this function which removes all entries over 3 days */
-               void MaintainWhoWas(time_t t)
+               /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
+               void MaintainWhoWas(InspIRCd* ServerInstance, time_t t)
                {
-                       for (whowas_users::iterator iter = ::whowas.begin(); iter != ::whowas.end(); iter++)
+                       for (whowas_users::iterator iter = ServerInstance->whowas.begin(); iter != ServerInstance->whowas.end(); iter++)
                        {
                                whowas_set* n = (whowas_set*)iter->second;
                                if (n->size())
                                {
-                                       while ((n->begin() != n->end()) && ((*n->begin())->signon < t - 259200)) // 3 days
+                                       while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
                                        {
                                                WhoWasGroup *a = *(n->begin());
                                                DELETE(a);
@@ -836,27 +878,99 @@ namespace irc
                                }
                        }
                }
+               /* on rehash, refactor maps according to new conf values */
+               void PruneWhoWas(InspIRCd* ServerInstance, time_t t)
+               {
+                       /* config values */
+                       int groupsize = ServerInstance->Config->WhoWasGroupSize;
+                       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++)
+                       {
+                               /** 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)
+                               {
+                                       whowas_set* n = (whowas_set*)ServerInstance->whowas.find(iter->second)->second;
+                                       if (n->size())
+                                       {
+                                               while (n->begin() != n->end())
+                                               {
+                                                       WhoWasGroup *a = *(n->begin());
+                                                       DELETE(a);
+                                                       n->erase(n->begin());
+                                               }
+                                       }
+                                       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);
+                               }
+                               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())
+                                       {
+                                               int nickcount = n->size();
+                                               while (n->begin() != n->end() && nickcount > groupsize)
+                                               {
+                                                       WhoWasGroup *a = *(n->begin());
+                                                       DELETE(a);
+                                                       n->erase(n->begin());
+                                                       nickcount--;
+                                               }
+                                       }
+                               }
+                               groupcount--;
+                       }
+               }
        };
 };
 
 /* adds or updates an entry in the whowas list */
 void userrec::AddToWhoWas()
 {
-       irc::whowas::whowas_users::iterator iter = whowas.find(this->nick);
+       /* if whowas disabled */
+       if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
+       {
+               return;
+       }
 
-       if (iter == whowas.end())
+       irc::whowas::whowas_users::iterator iter = ServerInstance->whowas.find(this->nick);
+
+       ServerInstance->Log(DEBUG,"Add to whowas lists");
+
+       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);
-               whowas[this->nick] = n;
+               ServerInstance->whowas[this->nick] = n;
+               ServerInstance->whowas_fifo[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());
+               }
        }
        else
        {
                irc::whowas::whowas_set* group = (irc::whowas::whowas_set*)iter->second;
 
-               if (group->size() > 10)
+               ServerInstance->Log(DEBUG,"Using existing whowas group for %s",this->nick);
+
+               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();
@@ -873,7 +987,7 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
        std::string tempnick = ConvToStr(socket) + "-unknown";
        user_hash::iterator iter = Instance->clientlist.find(tempnick);
        const char *ipaddr = insp_ntoa(ip);
-       userrec* _new;
+       userrec* New;
        int j = 0;
 
        /*
@@ -894,28 +1008,30 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
 
        Instance->Log(DEBUG,"AddClient: %d %d %s",socket,port,ipaddr);
        
-       _new = new userrec(Instance);
-       Instance->clientlist[tempnick] = _new;
-       _new->fd = socket;
-       strlcpy(_new->nick,tempnick.c_str(),NICKMAX-1);
+       New = new userrec(Instance);
+       Instance->clientlist[tempnick] = New;
+       New->fd = socket;
+       strlcpy(New->nick,tempnick.c_str(),NICKMAX-1);
 
-       _new->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
+       New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
        /* We don't need range checking here, we KNOW 'unknown\0' will fit into the ident field. */
-       strcpy(_new->ident, "unknown");
+       strcpy(New->ident, "unknown");
 
-       _new->registered = REG_NONE;
-       _new->signon = Instance->Time() + Instance->Config->dns_timeout;
-       _new->lastping = 1;
+       New->registered = REG_NONE;
+       New->signon = Instance->Time() + Instance->Config->dns_timeout;
+       New->lastping = 1;
 
        Instance->Log(DEBUG,"Setting socket addresses");
-       _new->SetSockAddr(AF_FAMILY, ipaddr, port);
+       New->SetSockAddr(AF_FAMILY, ipaddr, port);
        Instance->Log(DEBUG,"Socket addresses set.");
 
        /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
-       for (const char* temp = _new->GetIPString(); *temp && j < 64; temp++, j++)
-               _new->dhost[j] = _new->host[j] = *temp;
-       _new->dhost[j] = _new->host[j] = 0;
-                       
+       for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
+               New->dhost[j] = New->host[j] = *temp;
+       New->dhost[j] = New->host[j] = 0;
+       
+       Instance->Log(DEBUG,"Hosts set.");
+
        // set the registration timeout for this user
        unsigned long class_regtimeout = 90;
        int class_flood = 0;
@@ -923,13 +1039,15 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
        long class_sqmax = 262144;      // 256kb
        long class_rqmax = 4096;        // 4k
 
+       Instance->Log(DEBUG,"Class stuff set.");
+
        for (ClassVector::iterator i = Instance->Config->Classes.begin(); i != Instance->Config->Classes.end(); i++)
        {
                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;
+                       New->pingmax = i->pingtime;
                        class_threshold = i->threshold;
                        class_sqmax = i->sendqmax;
                        class_rqmax = i->recvqmax;
@@ -937,27 +1055,29 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
                }
        }
 
-       _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;
+       Instance->Log(DEBUG,"nping etc set.");
 
-       Instance->local_users.push_back(_new);
+       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;
 
-       if (Instance->local_users.size() > Instance->Config->SoftLimit)
-       {
-               userrec::QuitUser(Instance, _new,"No more connections allowed");
-               return;
-       }
+       Instance->Log(DEBUG,"Push back to local users.");
+       Instance->local_users.push_back(New);
 
-       if (Instance->local_users.size() >= MAXCLIENTS)
+       Instance->Log(DEBUG,"Check softlimit: %d %d %d",Instance->local_users.size(), Instance->Config->SoftLimit, MAXCLIENTS);
+       if ((Instance->local_users.size() > Instance->Config->SoftLimit) || (Instance->local_users.size() >= MAXCLIENTS))
        {
-               userrec::QuitUser(Instance, _new,"No more connections allowed");
+               Instance->Log(DEBUG,"Check softlimit failed");
+               Instance->WriteOpers("*** Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
+               userrec::QuitUser(Instance, New,"No more connections allowed");
                return;
        }
 
+       Instance->Log(DEBUG,"Softlimit passed.");
+
        /*
         * XXX -
         * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
@@ -968,49 +1088,61 @@ void userrec::AddClient(InspIRCd* Instance, int socket, int port, bool iscached,
         * which for the time being is a physical impossibility (even the largest networks dont have more
         * than about 10,000 users on ONE server!)
         */
-       if ((unsigned)socket >= MAX_DESCRIPTORS)
+       if ((unsigned int)socket >= MAX_DESCRIPTORS)
        {
-               userrec::QuitUser(Instance, _new,"Server is full");
+               userrec::QuitUser(Instance, New, "Server is full");
                return;
        }
-       char* e = Instance->XLines->matches_exception(ipaddr);
+
+       Instance->Log(DEBUG,"socket < MAX_DESCRIPTORS passed.");
+
+       ELine* e = Instance->XLines->matches_exception(New);
        if (!e)
        {
-               char* r = Instance->XLines->matches_zline(ipaddr);
+               Instance->Log(DEBUG,"Doesnt match eline.");
+               ZLine* r = Instance->XLines->matches_zline(ipaddr);
                if (r)
                {
+                       Instance->Log(DEBUG,"Matches zline.");
                        char reason[MAXBUF];
-                       snprintf(reason,MAXBUF,"Z-Lined: %s",r);
-                       userrec::QuitUser(Instance, _new,reason);
+                       snprintf(reason,MAXBUF,"Z-Lined: %s",r->reason);
+                       userrec::QuitUser(Instance, New, reason);
                        return;
                }
+               Instance->Log(DEBUG,"Doesnt match zline.");
        }
 
+       Instance->Log(DEBUG,"Check before AddFd.");
+
        if (socket > -1)
        {
-               if (!Instance->SE->AddFd(_new))
+               Instance->Log(DEBUG,"Adding fd.");
+               if (!Instance->SE->AddFd(New))
                {
-                       userrec::QuitUser(Instance, _new, "Internal error handling connection");
+                       Instance->Log(DEBUG,"Oops, fd already exists");
+                       userrec::QuitUser(Instance, New, "Internal error handling connection");
                        return;
                }
        }
 
-       _new->WriteServ("NOTICE Auth :*** Looking up your hostname...");
+       New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
 }
 
 long userrec::GlobalCloneCount()
 {
-       char u1[1024];
-       char u2[1024];
+       char u2[128];
        long x = 0;
+       strlcpy(u2, this->GetIPString(), 64);
+
        for (user_hash::const_iterator a = ServerInstance->clientlist.begin(); a != ServerInstance->clientlist.end(); a++)
        {
                /* We have to match ip's as strings - we don't know what protocol
                 * a remote user may be using
                 */
-               if (!strcasecmp(a->second->GetIPString(u1), this->GetIPString(u2)))
-                               x++;
+               if (strcasecmp(a->second->GetIPString(), u2) == 0)
+                       x++;
        }
+
        return x;
 }
 
@@ -1068,28 +1200,26 @@ void userrec::FullConnect(CullList* Goners)
                return;
        }
 
-       char match_against[MAXBUF];
-       snprintf(match_against,MAXBUF,"%s@%s", this->ident, this->host);
-       char* e = ServerInstance->XLines->matches_exception(match_against);
+       ELine* e = ServerInstance->XLines->matches_exception(this);
 
        if (!e)
        {
-               char* r = ServerInstance->XLines->matches_gline(match_against);
+               GLine* r = ServerInstance->XLines->matches_gline(this);
                
                if (r)
                {
                        char reason[MAXBUF];
-                       snprintf(reason,MAXBUF,"G-Lined: %s",r);
+                       snprintf(reason,MAXBUF,"G-Lined: %s",r->reason);
                        Goners->AddItem(this, reason);
                        return;
                }
                
-               r = ServerInstance->XLines->matches_kline(match_against);
+               KLine* n = ServerInstance->XLines->matches_kline(this);
                
-               if (r)
+               if (n)
                {
                        char reason[MAXBUF];
-                       snprintf(reason,MAXBUF,"K-Lined: %s",r);
+                       snprintf(reason,MAXBUF,"K-Lined: %s",n->reason);
                        Goners->AddItem(this, reason);
                        return;
                }
@@ -1130,8 +1260,11 @@ void userrec::FullConnect(CullList* Goners)
         * changes dont go out onto the network and produce 'fake direction'.
         */
        FOREACH_MOD(I_OnUserConnect,OnUserConnect(this));
-       FOREACH_MOD(I_OnPostConnect,OnPostConnect(this));
+
        this->registered = REG_ALL;
+
+       FOREACH_MOD(I_OnPostConnect,OnPostConnect(this));
+
        ServerInstance->SNO->WriteToSnoMask('c',"Client connecting on port %d: %s!%s@%s [%s]", this->GetPort(), this->nick, this->ident, this->host, this->GetIPString());
 }
 
@@ -1290,7 +1423,7 @@ const char* userrec::GetIPString()
                        /* IP addresses starting with a : on irc are a Bad Thing (tm) */
                        if (*buf == ':')
                        {
-                               strlcpy(&temp[1], buf, sizeof(temp));
+                               strlcpy(&temp[1], buf, sizeof(temp) - 1);
                                *temp = '0';
                                return temp;
                        }
@@ -1332,7 +1465,7 @@ const char* userrec::GetIPString(char* buf)
                        /* IP addresses starting with a : on irc are a Bad Thing (tm) */
                        if (*buf == ':')
                        {
-                               strlcpy(&temp[1], buf, sizeof(temp));
+                               strlcpy(&temp[1], buf, sizeof(temp) - 1);
                                *temp = '0';
                                strlcpy(buf, temp, sizeof(temp));
                        }
@@ -1392,6 +1525,7 @@ void userrec::Write(std::string text)
                this->AddWriteBuf(text);
        }
        ServerInstance->stats->statsSent += text.length();
+       this->ServerInstance->SE->WantWrite(this);
 }
 
 /** Write()
@@ -1717,6 +1851,9 @@ void userrec::ModChannelCount(int n)
 
 bool userrec::ChangeName(const char* gecos)
 {
+       if (!strcmp(gecos, this->fullname))
+               return true;
+
        if (IS_LOCAL(this))
        {
                int MOD_RESULT = 0;
@@ -1731,6 +1868,9 @@ bool userrec::ChangeName(const char* gecos)
 
 bool userrec::ChangeDisplayedHost(const char* host)
 {
+       if (!strcmp(host, this->dhost))
+               return true;
+
        if (IS_LOCAL(this))
        {
                int MOD_RESULT = 0;
@@ -1742,7 +1882,8 @@ bool userrec::ChangeDisplayedHost(const char* host)
        if (this->ServerInstance->Config->CycleHosts)
                this->WriteCommonExcept("%s","QUIT :Changing hosts");
 
-       strlcpy(this->dhost,host,63);
+       /* Fix by Om: userrec::dhost is 65 long, this was truncating some long hosts */
+       strlcpy(this->dhost,host,64);
 
        if (this->ServerInstance->Config->CycleHosts)
        {
@@ -1750,10 +1891,10 @@ bool userrec::ChangeDisplayedHost(const char* host)
                {
                        if ((*i)->channel)
                        {
-                               (*i)->channel->WriteAllExceptSender(this, 0, "JOIN %s", (*i)->channel->name);
+                               (*i)->channel->WriteAllExceptSender(this, false, 0, "JOIN %s", (*i)->channel->name);
                                std::string n = this->ServerInstance->Modes->ModeString(this, (*i)->channel);
-                               if (n.length())
-                                       (*i)->channel->WriteChannelWithServ(this->ServerInstance->Config->ServerName, "MODE %s +%s", (*i)->channel->name, n.c_str());
+                               if (n.length() > 0)
+                                       (*i)->channel->WriteAllExceptSender(this, true, 0, "MODE %s +%s", (*i)->channel->name, n.c_str());
                        }
                }
        }
@@ -1766,6 +1907,9 @@ bool userrec::ChangeDisplayedHost(const char* host)
 
 bool userrec::ChangeIdent(const char* newident)
 {
+       if (!strcmp(newident, this->ident))
+               return true;
+
        if (this->ServerInstance->Config->CycleHosts)
                this->WriteCommonExcept("%s","QUIT :Changing ident");
 
@@ -1777,10 +1921,10 @@ bool userrec::ChangeIdent(const char* newident)
                {
                        if ((*i)->channel)
                        {
-                               (*i)->channel->WriteAllExceptSender(this, 0, "JOIN %s", (*i)->channel->name);
+                               (*i)->channel->WriteAllExceptSender(this, false, 0, "JOIN %s", (*i)->channel->name);
                                std::string n = this->ServerInstance->Modes->ModeString(this, (*i)->channel);
-                               if (n.length())
-                                       (*i)->channel->WriteChannelWithServ(this->ServerInstance->Config->ServerName, "MODE %s +%s", (*i)->channel->name, n.c_str());
+                               if (n.length() > 0)
+                                       (*i)->channel->WriteAllExceptSender(this, true, 0, "MODE %s +%s", (*i)->channel->name, n.c_str());
                        }
                }
        }
@@ -1846,16 +1990,17 @@ void userrec::SplitChanList(userrec* dest, const std::string &cl)
 
        try
        {
-               prefix << ":" << ServerInstance->Config->ServerName << " 319 " << this->nick << " " << dest->nick << " :";
+               prefix << this->nick << " " << dest->nick << " :";
                line = prefix.str();
+               int namelen = strlen(ServerInstance->Config->ServerName) + 6;
        
                for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
                {
                        length = (pos == std::string::npos) ? cl.length() : pos;
        
-                       if (line.length() + length - start > 510)
+                       if (line.length() + namelen + length - start > 510)
                        {
-                               this->Write(line);
+                               ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str());
                                line = prefix.str();
                        }
        
@@ -1872,7 +2017,7 @@ void userrec::SplitChanList(userrec* dest, const std::string &cl)
        
                if (line.length())
                {
-                       this->Write(line);
+                       ServerInstance->SendWhoisLine(this, dest, 319, "%s", line.c_str());
                }
        }
 
@@ -1971,16 +2116,26 @@ void userrec::ShowRULES()
        this->WriteServ("NOTICE %s :End of %s rules.",this->nick,ServerInstance->Config->ServerName);
 }
 
-void userrec::HandleEvent(EventType et)
+void userrec::HandleEvent(EventType et, int errornum)
 {
        /* WARNING: May delete this user! */
        try
        {
-               ServerInstance->ProcessUser(this);
+               switch (et)
+               {
+                       case EVENT_READ:
+                               ServerInstance->ProcessUser(this);
+                       break;
+                       case EVENT_WRITE:
+                               this->FlushWriteBuf();
+                       break;
+                       case EVENT_ERROR:
+                               this->SetWriteError(errornum ? strerror(errornum) : "EOF from client");
+                       break;
+               }
        }
        catch (...)
        {
                ServerInstance->Log(DEBUG,"Exception in userrec::HandleEvent intercepted");
        }
 }
-