]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Fixed quit-bouncing issue when a user is g-lined
[user/henk/code/inspircd.git] / src / inspircd.cpp
index fe0b65e591d94bb37ee93a616ae2e32bdecde6ad..8f095889090c5a7e28d7c669319eca7092b32338 100644 (file)
@@ -54,6 +54,7 @@ using namespace std;
 #include "message.h"
 #include "mode.h"
 #include "commands.h"
+#include "xline.h"
 
 #ifdef GCC3
 #define nspace __gnu_cxx
@@ -266,184 +267,189 @@ void log(int level,char *text, ...)
 
 void readfile(file_cache &F, const char* fname)
 {
-  FILE* file;
-  char linebuf[MAXBUF];
-
-  log(DEBUG,"readfile: loading %s",fname);
-  F.clear();
-  file =  fopen(fname,"r");
-  if (file)
-  {
-       while (!feof(file))
-       {
-               fgets(linebuf,sizeof(linebuf),file);
-               linebuf[strlen(linebuf)-1]='\0';
-               if (!strcmp(linebuf,""))
-               {
-                       strcpy(linebuf,"  ");
-               }
-               if (!feof(file))
-               {
-                       F.push_back(linebuf);
-               }
-       }
-       fclose(file);
-  }
-  else
-  {
-         log(DEBUG,"readfile: failed to load file: %s",fname);
-  }
-  log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
+       FILE* file;
+       char linebuf[MAXBUF];
+       
+       log(DEBUG,"readfile: loading %s",fname);
+       F.clear();
+       file =  fopen(fname,"r");
+       if (file)
+       {
+               while (!feof(file))
+               {
+                       fgets(linebuf,sizeof(linebuf),file);
+                       linebuf[strlen(linebuf)-1]='\0';
+                       if (!strcmp(linebuf,""))
+                       {
+                               strcpy(linebuf,"  ");
+                       }
+                       if (!feof(file))
+                       {
+                               F.push_back(linebuf);
+                       }
+               }
+               fclose(file);
+       }
+       else
+       {
+               log(DEBUG,"readfile: failed to load file: %s",fname);
+       }
+       log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
 }
 
 void ReadConfig(void)
 {
-  char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF];
-  ConnectClass c;
-
-  LoadConf(CONFIG_FILE,&config_f);
-  
-  ConfValue("server","name",0,ServerName,&config_f);
-  ConfValue("server","description",0,ServerDesc,&config_f);
-  ConfValue("server","network",0,Network,&config_f);
-  ConfValue("admin","name",0,AdminName,&config_f);
-  ConfValue("admin","email",0,AdminEmail,&config_f);
-  ConfValue("admin","nick",0,AdminNick,&config_f);
-  ConfValue("files","motd",0,motd,&config_f);
-  ConfValue("files","rules",0,rules,&config_f);
-  ConfValue("power","diepass",0,diepass,&config_f);
-  ConfValue("power","pause",0,pauseval,&config_f);
-  ConfValue("power","restartpass",0,restartpass,&config_f);
-  ConfValue("options","prefixquit",0,PrefixQuit,&config_f);
-  ConfValue("die","value",0,DieValue,&config_f);
-  ConfValue("options","loglevel",0,dbg,&config_f);
-  ConfValue("options","netbuffersize",0,NB,&config_f);
-  NetBufferSize = atoi(NB);
-  if ((!NetBufferSize) || (NetBufferSize > 65535) || (NetBufferSize < 1024))
-  {
-       log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
-       NetBufferSize = 10240;
-  }
-  if (!strcmp(dbg,"debug"))
-       LogLevel = DEBUG;
-  if (!strcmp(dbg,"verbose"))
-       LogLevel = VERBOSE;
-  if (!strcmp(dbg,"default"))
-       LogLevel = DEFAULT;
-  if (!strcmp(dbg,"sparse"))
-       LogLevel = SPARSE;
-  if (!strcmp(dbg,"none"))
-       LogLevel = NONE;
-  readfile(MOTD,motd);
-  log(DEBUG,"Reading message of the day");
-  readfile(RULES,rules);
-  log(DEBUG,"Reading connect classes");
-  Classes.clear();
-  for (int i = 0; i < ConfValueEnum("connect",&config_f); i++)
-  {
-       strcpy(Value,"");
-       ConfValue("connect","allow",i,Value,&config_f);
-       ConfValue("connect","timeout",i,timeout,&config_f);
-       ConfValue("connect","flood",i,flood,&config_f);
-       if (strcmp(Value,""))
-       {
-               strcpy(c.host,Value);
-               c.type = CC_ALLOW;
+       char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF];
+       ConnectClass c;
+       
+       LoadConf(CONFIG_FILE,&config_f);
+         
+       ConfValue("server","name",0,ServerName,&config_f);
+       ConfValue("server","description",0,ServerDesc,&config_f);
+       ConfValue("server","network",0,Network,&config_f);
+       ConfValue("admin","name",0,AdminName,&config_f);
+       ConfValue("admin","email",0,AdminEmail,&config_f);
+       ConfValue("admin","nick",0,AdminNick,&config_f);
+       ConfValue("files","motd",0,motd,&config_f);
+       ConfValue("files","rules",0,rules,&config_f);
+       ConfValue("power","diepass",0,diepass,&config_f);
+       ConfValue("power","pause",0,pauseval,&config_f);
+       ConfValue("power","restartpass",0,restartpass,&config_f);
+       ConfValue("options","prefixquit",0,PrefixQuit,&config_f);
+       ConfValue("die","value",0,DieValue,&config_f);
+       ConfValue("options","loglevel",0,dbg,&config_f);
+       ConfValue("options","netbuffersize",0,NB,&config_f);
+       NetBufferSize = atoi(NB);
+       if ((!NetBufferSize) || (NetBufferSize > 65535) || (NetBufferSize < 1024))
+       {
+               log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
+               NetBufferSize = 10240;
+       }
+       if (!strcmp(dbg,"debug"))
+               LogLevel = DEBUG;
+       if (!strcmp(dbg,"verbose"))
+               LogLevel = VERBOSE;
+       if (!strcmp(dbg,"default"))
+               LogLevel = DEFAULT;
+       if (!strcmp(dbg,"sparse"))
+               LogLevel = SPARSE;
+       if (!strcmp(dbg,"none"))
+               LogLevel = NONE;
+       readfile(MOTD,motd);
+       log(DEFAULT,"Reading message of the day...");
+       readfile(RULES,rules);
+       log(DEFAULT,"Reading connect classes...");
+       Classes.clear();
+       for (int i = 0; i < ConfValueEnum("connect",&config_f); i++)
+       {
                strcpy(Value,"");
-               ConfValue("connect","password",i,Value,&config_f);
-               strcpy(c.pass,Value);
-               c.registration_timeout = 90; // default is 2 minutes
-               c.flood = atoi(flood);
-               if (atoi(timeout)>0)
+               ConfValue("connect","allow",i,Value,&config_f);
+               ConfValue("connect","timeout",i,timeout,&config_f);
+               ConfValue("connect","flood",i,flood,&config_f);
+               if (strcmp(Value,""))
                {
-                       c.registration_timeout = atoi(timeout);
+                       strcpy(c.host,Value);
+                       c.type = CC_ALLOW;
+                       strcpy(Value,"");
+                       ConfValue("connect","password",i,Value,&config_f);
+                       strcpy(c.pass,Value);
+                       c.registration_timeout = 90; // default is 2 minutes
+                       c.flood = atoi(flood);
+                       if (atoi(timeout)>0)
+                       {
+                               c.registration_timeout = atoi(timeout);
+                       }
+                       Classes.push_back(c);
+                       log(DEBUG,"Read connect class type ALLOW, host=%s password=%s timeout=%d flood=%d",c.host,c.pass,c.registration_timeout,c.flood);
+               }
+               else
+               {
+                       ConfValue("connect","deny",i,Value,&config_f);
+                       strcpy(c.host,Value);
+                       c.type = CC_DENY;
+                       Classes.push_back(c);
+                       log(DEBUG,"Read connect class type DENY, host=%s",c.host);
                }
-               Classes.push_back(c);
-               log(DEBUG,"Read connect class type ALLOW, host=%s password=%s timeout=%d flood=%d",c.host,c.pass,c.registration_timeout,c.flood);
-       }
-       else
-       {
-               ConfValue("connect","deny",i,Value,&config_f);
-                strcpy(c.host,Value);
-                c.type = CC_DENY;
-               Classes.push_back(c);
-               log(DEBUG,"Read connect class type DENY, host=%s",c.host);
-       }
        
-  }
+       }
+       log(DEFAULT,"Reading K lines,Q lines and Z lines from config...");
+       read_xline_defaults();
+       log(DEFAULT,"Applying K lines, Q lines and Z lines...");
+       apply_lines();
+       log(DEFAULT,"Done reading configuration file, InspIRCd is now running.");
 }
 
 /* write formatted text to a socket, in same format as printf */
 
 void Write(int sock,char *text, ...)
 {
-  if (!text)
-  {
-       log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
-       return;
-  }
-  char textbuffer[MAXBUF];
-  va_list argsPtr;
-  char tb[MAXBUF];
-
-  va_start (argsPtr, text);
-  vsnprintf(textbuffer, MAXBUF, text, argsPtr);
-  va_end(argsPtr);
-  sprintf(tb,"%s\r\n",textbuffer);
-  chop(tb);
-  if (sock != -1)
-  {
-       write(sock,tb,strlen(tb));
-       update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
-  }
+       if (!text)
+       {
+               log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
+               return;
+       }
+       char textbuffer[MAXBUF];
+       va_list argsPtr;
+       char tb[MAXBUF];
+       
+       va_start (argsPtr, text);
+       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       va_end(argsPtr);
+       sprintf(tb,"%s\r\n",textbuffer);
+       chop(tb);
+       if (sock != -1)
+       {
+               write(sock,tb,strlen(tb));
+               update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
+       }
 }
 
 /* write a server formatted numeric response to a single socket */
 
 void WriteServ(int sock, char* text, ...)
 {
-  if (!text)
-  {
-       log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
-       return;
-  }
-  char textbuffer[MAXBUF],tb[MAXBUF];
-  va_list argsPtr;
-  va_start (argsPtr, text);
-
-  vsnprintf(textbuffer, MAXBUF, text, argsPtr);
-  va_end(argsPtr);
-  sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
-  chop(tb);
-  if (sock != -1)
-  {
-       write(sock,tb,strlen(tb));
-       update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
-  }
+       if (!text)
+       {
+               log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
+               return;
+       }
+       char textbuffer[MAXBUF],tb[MAXBUF];
+       va_list argsPtr;
+       va_start (argsPtr, text);
+       
+       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       va_end(argsPtr);
+       sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
+       chop(tb);
+       if (sock != -1)
+       {
+               write(sock,tb,strlen(tb));
+               update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
+       }
 }
 
 /* write text from an originating user to originating user */
 
 void WriteFrom(int sock, userrec *user,char* text, ...)
 {
-  if ((!text) || (!user))
-  {
-       log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
-       return;
-  }
-  char textbuffer[MAXBUF],tb[MAXBUF];
-  va_list argsPtr;
-  va_start (argsPtr, text);
-
-  vsnprintf(textbuffer, MAXBUF, text, argsPtr);
-  va_end(argsPtr);
-  sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
-  chop(tb);
-  if (sock != -1)
-  {
-       write(sock,tb,strlen(tb));
-       update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
-  }
+       if ((!text) || (!user))
+       {
+               log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
+               return;
+       }
+       char textbuffer[MAXBUF],tb[MAXBUF];
+       va_list argsPtr;
+       va_start (argsPtr, text);
+       
+       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       va_end(argsPtr);
+       sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
+       chop(tb);
+       if (sock != -1)
+       {
+               write(sock,tb,strlen(tb));
+               update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
+       }
 }
 
 /* write text to an destination user from a source user (e.g. user privmsg) */
@@ -1437,7 +1443,7 @@ chanrec* add_channel(userrec *user, const char* cn, const char* key, bool overri
                        {
                                // use the stamdard J token with no privilages.
                                char buffer[MAXBUF];
-                               snprintf(buffer,MAXBUF,"J %s :%s",user->nick,Ptr->name);
+                               snprintf(buffer,MAXBUF,"J %s %s",user->nick,Ptr->name);
                                NetSendToAll(buffer);
                        }
 
@@ -1567,7 +1573,7 @@ void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
                WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
                return;
        }
-       if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
+       if (((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr))) && (!is_uline(src->server)))
        {
                if (cstatus(src,Ptr) == STATUS_HOP)
                {
@@ -1834,6 +1840,56 @@ void kill_link(userrec *user,const char* r)
        }
 }
 
+void kill_link_silent(userrec *user,const char* r)
+{
+       user_hash::iterator iter = clientlist.find(user->nick);
+       
+       char reason[MAXBUF];
+       
+       strncpy(reason,r,MAXBUF);
+
+       if (strlen(reason)>MAXQUIT)
+       {
+               reason[MAXQUIT-1] = '\0';
+       }
+
+       log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
+       Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
+       log(DEBUG,"closing fd %d",user->fd);
+
+       /* bugfix, cant close() a nonblocking socket (sux!) */
+       if (user->registered == 7) {
+               FOREACH_MOD OnUserQuit(user);
+               WriteCommonExcept(user,"QUIT :%s",reason);
+
+               // Q token must go to ALL servers!!!
+               char buffer[MAXBUF];
+               snprintf(buffer,MAXBUF,"Q %s :%s",user->nick,reason);
+               NetSendToAll(buffer);
+       }
+
+       /* push the socket on a stack of sockets due to be closed at the next opportunity
+        * 'Client exited' is an exception to this as it means the client side has already
+        * closed the socket, we don't need to do it.
+        */
+       fd_reap.push_back(user->fd);
+       
+       bool do_purge = false;
+       
+       if (iter != clientlist.end())
+       {
+               log(DEBUG,"deleting user hash value %d",iter->second);
+               if ((iter->second) && (user->registered == 7)) {
+                       delete iter->second;
+               }
+               clientlist.erase(iter);
+       }
+
+       if (user->registered == 7) {
+               purge_empty_chans();
+       }
+}
+
 
 
 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
@@ -2014,7 +2070,7 @@ void AddWhoWas(userrec* u)
 
 
 /* add a client connection to the sockets list */
-void AddClient(int socket, char* host, int port, bool iscached)
+void AddClient(int socket, char* host, int port, bool iscached, char* ip)
 {
        int i;
        int blocking = 1;
@@ -2053,6 +2109,7 @@ void AddClient(int socket, char* host, int port, bool iscached)
        clientlist[tempnick]->nping = time(NULL)+240;
        clientlist[tempnick]->lastping = 1;
        clientlist[tempnick]->port = port;
+       strncpy(clientlist[tempnick]->ip,ip,32);
 
        if (iscached)
        {
@@ -2095,6 +2152,14 @@ void AddClient(int socket, char* host, int port, bool iscached)
 
        if (clientlist.size() == MAXCLIENTS)
                kill_link(clientlist[tempnick],"No more connections allowed in this class");
+               
+       char* r = matches_zline(ip);
+       if (r)
+       {
+               char reason[MAXBUF];
+               snprintf(reason,MAXBUF,"Z-Lined: %s",r);
+               kill_link(clientlist[tempnick],reason);
+       }
 }
 
 
@@ -2218,13 +2283,33 @@ void ConnectUser(userrec *user)
                return;
        }
 
+       char match_against[MAXBUF];
+       snprintf(match_against,MAXBUF,"%s@%s",user->ident,user->host);
+       char* r = matches_gline(match_against);
+       if (r)
+       {
+               char reason[MAXBUF];
+               snprintf(reason,MAXBUF,"G-Lined: %s",r);
+               kill_link_silent(user,reason);
+               return;
+       }
+
+       r = matches_kline(user->host);
+       if (r)
+       {
+               char reason[MAXBUF];
+               snprintf(reason,MAXBUF,"K-Lined: %s",r);
+               kill_link_silent(user,reason);
+               return;
+       }
+
        WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
        WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
        WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
        WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
-       WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
-       WriteServ(user->fd,"005 %s :MAP KNOCK SAFELIST HCN MAXCHANNELS=20 MAXBANS=60 NICKLEN=30 TOPICLEN=307 KICKLEN=307 MAXTARGETS=20 AWAYLEN=307 :are supported by this server",user->nick);
-       WriteServ(user->fd,"005 %s :WALLCHOPS WATCH=128 SILENCE=5 MODES=13 CHANTYPES=# PREFIX=(ohv)@%c+ CHANMODES=ohvbeqa,kfL,l,psmntirRcOAQKVHGCuzN NETWORK=%s :are supported by this server",user->nick,'%',Network);
+       WriteServ(user->fd,"004 %s %s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
+       WriteServ(user->fd,"005 %s MAP KNOCK SAFELIST HCN MAXCHANNELS=20 MAXBANS=60 NICKLEN=30 TOPICLEN=307 KICKLEN=307 MAXTARGETS=20 AWAYLEN=307 :are supported by this server",user->nick);
+       WriteServ(user->fd,"005 %s WALLCHOPS WATCH=128 SILENCE=5 MODES=13 CHANTYPES=# PREFIX=(ohv)@%c+ CHANMODES=ohvbeqa,kfL,l,psmntirRcOAQKVHGCuzN NETWORK=%s :are supported by this server",user->nick,'%',Network);
        ShowMOTD(user);
        FOREACH_MOD OnUserConnect(user);
        WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
@@ -2356,7 +2441,20 @@ long map_count(const char* s)
 void force_nickchange(userrec* user,const char* newnick)
 {
        char nick[MAXBUF];
+       int MOD_RESULT = 0;
+       
        strcpy(nick,"");
+
+       FOREACH_RESULT(OnUserPreNick(user,newnick));
+       if (MOD_RESULT) {
+               kill_link(user,"Nickname collision");
+               return;
+       }
+       if (matches_qline(newnick))
+       {
+               kill_link(user,"Nickname collision");
+               return;
+       }
        
        if (user)
        {
@@ -2729,6 +2827,10 @@ void SetupCommandTable(void)
        createcommand("MODULES",handle_modules,'o',0);
        createcommand("LINKS",handle_links,0,0);
        createcommand("MAP",handle_map,0,0);
+       createcommand("KLINE",handle_kline,'o',1);
+       createcommand("GLINE",handle_gline,'o',1);
+       createcommand("ZLINE",handle_zline,'o',1);
+       createcommand("QLINE",handle_qline,'o',1);
 }
 
 void process_buffer(const char* cmdbuf,userrec *user)
@@ -2749,6 +2851,8 @@ void process_buffer(const char* cmdbuf,userrec *user)
        {
                return;
        }
+       while ((cmdbuf[0] == ' ') && (strlen(cmdbuf)>0)) cmdbuf++; // strip leading spaces
+
        strncpy(cmd,cmdbuf,MAXBUF);
        if (!strcmp(cmd,""))
        {
@@ -2762,6 +2866,12 @@ void process_buffer(const char* cmdbuf,userrec *user)
        {
                cmd[strlen(cmd)-1] = '\0';
        }
+
+       while ((cmd[strlen(cmd)-1] == ' ') && (strlen(cmd)>0)) // strip trailing spaces
+       {
+               cmd[strlen(cmd)-1] = '\0';
+       }
+
        if (!strcmp(cmd,""))
        {
                return;
@@ -2774,510 +2884,51 @@ void process_buffer(const char* cmdbuf,userrec *user)
        }
 }
 
-void DoSync(serverrec* serv, char* udp_host)
+void DoSync(serverrec* serv, char* tcp_host)
 {
        char data[MAXBUF];
+       log(DEBUG,"Sending sync");
        // send start of sync marker: Y <timestamp>
        // at this point the ircd receiving it starts broadcasting this netburst to all ircds
        // except the ones its receiving it from.
        snprintf(data,MAXBUF,"Y %d",time(NULL));
-       serv->SendPacket(data,udp_host);
+       serv->SendPacket(data,tcp_host);
        // send users and channels
        for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
        {
                snprintf(data,MAXBUF,"N %d %s %s %s %s +%s %s :%s",u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,u->second->server,u->second->fullname);
-               serv->SendPacket(data,udp_host);
+               serv->SendPacket(data,tcp_host);
                if (strcmp(chlist(u->second),""))
                {
                        snprintf(data,MAXBUF,"J %s %s",u->second->nick,chlist(u->second));
-                       serv->SendPacket(data,udp_host);
+                       serv->SendPacket(data,tcp_host);
                }
        }
        // send channel modes, topics etc...
        for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
        {
                snprintf(data,MAXBUF,"M %s +%s",c->second->name,chanmodes(c->second));
-               serv->SendPacket(data,udp_host);
+               serv->SendPacket(data,tcp_host);
                if (strcmp(c->second->topic,""))
                {
                        snprintf(data,MAXBUF,"T %d %s %s :%s",c->second->topicset,c->second->setby,c->second->name,c->second->topic);
-                       serv->SendPacket(data,udp_host);
+                       serv->SendPacket(data,tcp_host);
                }
                // send current banlist
                
                for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
                {
                        snprintf(data,MAXBUF,"M %s +b %s",b->set_time,c->second->name,b->data);
-                       serv->SendPacket(data,udp_host);
+                       serv->SendPacket(data,tcp_host);
                }
        }
-       // send end of sync marker: E <timestamp>
        snprintf(data,MAXBUF,"F %d",time(NULL));
-       serv->SendPacket(data,udp_host);
+       serv->SendPacket(data,tcp_host);
+       log(DEBUG,"Sent sync");
        // ircd sends its serverlist after the end of sync here
 }
 
 
-void handle_V(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* src = strtok(params," ");
-       char* dest = strtok(NULL," :");
-       char* text = strtok(NULL,"\r\n");
-       text++;
-       
-       userrec* user = Find(src);
-       if (user)
-       {
-               userrec* dst = Find(dest);
-               
-               if (dst)
-               {
-                       WriteTo(user, dst, "NOTICE %s :%s", dst->nick, text);
-               }
-               else
-               {
-                       chanrec* d = FindChan(dest);
-                       if (d)
-                       {
-                               ChanExceptSender(d, user, "NOTICE %s :%s", d->name, text);
-                       }
-               }
-       }
-       
-}
-
-
-void handle_P(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* src = strtok(params," ");
-       char* dest = strtok(NULL," :");
-       char* text = strtok(NULL,"\r\n");
-       text++;
-       
-       userrec* user = Find(src);
-       if (user)
-       {
-               userrec* dst = Find(dest);
-               
-               if (dst)
-               {
-                       WriteTo(user, dst, "PRIVMSG %s :%s", dst->nick, text);
-               }
-               else
-               {
-                       chanrec* d = FindChan(dest);
-                       if (d)
-                       {
-                               ChanExceptSender(d, user, "PRIVMSG %s :%s", d->name, text);
-                       }
-               }
-       }
-       
-}
-
-void handle_i(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* nick = strtok(params," ");
-       char* from = strtok(NULL," ");
-       char* channel = strtok(NULL," ");
-       userrec* u = Find(nick);
-       userrec* user = Find(from);
-       chanrec* c = FindChan(channel);
-       if ((c) && (u) && (user))
-       {
-               u->InviteTo(c->name);
-               WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
-       }
-}
-
-void handle_t(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* setby = strtok(params," ");
-       char* channel = strtok(NULL," :");
-       char* topic = strtok(NULL,"\r\n");
-       topic++;
-       userrec* u = Find(setby);
-       chanrec* c = FindChan(channel);
-       if ((c) && (u))
-       {
-               WriteChannelLocal(c,u,"TOPIC %s :%s",c->name,topic);
-               strncpy(c->topic,topic,MAXTOPIC);
-               strncpy(c->setby,u->nick,NICKMAX);
-       }       
-}
-       
-
-void handle_T(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* tm = strtok(params," ");
-       char* setby = strtok(NULL," ");
-       char* channel = strtok(NULL," :");
-       char* topic = strtok(NULL,"\r\n");
-       topic++;
-       time_t TS = atoi(tm);
-       chanrec* c = FindChan(channel);
-       if (c)
-       {
-               // in the case of topics and TS, the *NEWER* 
-               if (TS <= c->topicset)
-               {
-                       WriteChannelLocal(c,NULL,"TOPIC %s :%s",c->name,topic);
-                       strncpy(c->topic,topic,MAXTOPIC);
-                       strncpy(c->setby,setby,NICKMAX);
-               }
-       }       
-}
-       
-void handle_M(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* pars[128];
-       char original[MAXBUF],target[MAXBUF];
-       strncpy(original,params,MAXBUF);
-       int index = 0;
-       char* parameter = strtok(params," ");
-       strncpy(target,parameter,MAXBUF);
-       while (parameter)
-       {
-               pars[index++] = parameter;
-               parameter = strtok(NULL," ");
-       }
-       merge_mode(pars,index);
-       if (FindChan(target))
-       {
-               WriteChannelLocal(FindChan(target), NULL, "MODE %s",original);
-       }
-       if (Find(target))
-       {
-               WriteTo(NULL,Find(target),"MODE %s",original);
-       }
-}
-
-// m is modes set by users only (not servers) valid targets are channels or users.
-
-void handle_m(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       // m blah #chatspike +b *!test@*4
-       char* pars[128];
-       char original[MAXBUF];
-       strncpy(original,params,MAXBUF);
-       
-       if (!strchr(params,' '))
-       {
-               WriteOpers("WARNING! 'm' token in data stream without any parameters! Something fishy is going on!");
-               return;
-       }
-       
-       int index = 0;
-       
-       char* src = strtok(params," ");
-       userrec* user = Find(src);
-       
-       if (user)
-       {
-               log(DEBUG,"Found user: %s",user->nick);
-               char* parameter = strtok(NULL," ");
-               while (parameter)
-               {
-                       pars[index++] = parameter;
-                       parameter = strtok(NULL," ");
-               }
-               
-               log(DEBUG,"Calling merge_mode2");
-               merge_mode2(pars,index,user);
-       }
-}
-
-
-void handle_L(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* nick = strtok(params," ");
-       char* channel = strtok(NULL," :");
-       char* reason = strtok(NULL,"\r\n");
-       userrec* user = Find(nick);
-       reason++;
-       if (user)
-       {
-               if (strcmp(reason,""))
-               {
-                       del_channel(user,channel,reason,true);
-               }
-               else
-               {
-                       del_channel(user,channel,NULL,true);
-               }
-       }
-}
-
-void handle_K(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* src = strtok(params," ");
-       char* nick = strtok(NULL," :");
-       char* reason = strtok(NULL,"\r\n");
-       char kreason[MAXBUF];
-       reason++;
-
-       userrec* u = Find(nick);
-       userrec* user = Find(src);
-       
-       if ((user) && (u))
-       {
-               WriteTo(user, u, "KILL %s :%s!%s!%s!%s (%s)", u->nick, source->name, ServerName, user->dhost,user->nick,reason);
-               WriteOpers("*** Remote kill from %s by %s: %s!%s@%s (%s)",source->name,user->nick,u->nick,u->ident,u->host,reason);
-               snprintf(kreason,MAXBUF,"[%s] Killed (%s (%s))",source->name,user->nick,reason);
-               kill_link(u,kreason);
-       }
-}
-
-void handle_Q(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* nick = strtok(params," :");
-       char* reason = strtok(NULL,"\r\n");
-       reason++;
-
-       userrec* user = Find(nick);
-       
-       if (user)
-       {
-               if (strlen(reason)>MAXQUIT)
-               {
-                       reason[MAXQUIT-1] = '\0';
-               }
-
-
-               WriteCommonExcept(user,"QUIT :%s",reason);
-
-               user_hash::iterator iter = clientlist.find(user->nick);
-       
-               if (iter != clientlist.end())
-               {
-                       log(DEBUG,"deleting user hash value %d",iter->second);
-                       if ((iter->second) && (user->registered == 7)) {
-                               delete iter->second;
-                       }
-                       clientlist.erase(iter);
-               }
-
-               purge_empty_chans();
-       }
-}
-
-void handle_n(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* oldnick = strtok(params," ");
-       char* newnick = strtok(NULL," ");
-       
-       userrec* user = Find(oldnick);
-       
-       if (user)
-       {
-               WriteCommon(user,"NICK %s",newnick);
-               user = ReHashNick(user->nick, newnick);
-               if (!user) return;
-               if (!user->nick) return;
-               strncpy(user->nick, newnick,NICKMAX);
-               log(DEBUG,"new nick set: %s",user->nick);
-       }
-}
-
-// k <SOURCE> <DEST> <CHANNEL> :<REASON>
-void handle_k(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* src = strtok(params," ");
-       char* dest = strtok(NULL," ");
-       char* channel = strtok(NULL," :");
-       char* reason = strtok(NULL,"\r\n");
-       reason++;
-       userrec* s = Find(src);
-       userrec* d = Find(dest);
-       chanrec* c = FindChan(channel);
-       if ((s) && (d) && (c))
-       {
-               kick_channel(s,d,c,reason);
-       }
-}
-
-void handle_AT(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* who = strtok(params," :");
-       char* text = strtok(NULL,"\r\n");
-       text++;
-       userrec* s = Find(who);
-       if (s)
-       {
-               WriteWallOps(s,true,text);
-       }
-}
-
-
-void handle_N(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* tm = strtok(params," ");
-       char* nick = strtok(NULL," ");
-       char* host = strtok(NULL," ");
-       char* dhost = strtok(NULL," ");
-       char* ident = strtok(NULL," ");
-       char* modes = strtok(NULL," ");
-       char* server = strtok(NULL," :");
-       char* gecos = strtok(NULL,"\r\n");
-       gecos++;
-       modes++;
-       time_t TS = atoi(tm);
-       user_hash::iterator iter = clientlist.find(nick);
-       if (iter != clientlist.end())
-       {
-               // nick collision
-               WriteOpers("Nickname collision: %s@%s != %s@%s",nick,server,iter->second->nick,iter->second->server);
-               char str[MAXBUF];
-               snprintf(str,MAXBUF,"Killed (Nick Collision (%s@%s < %s@%s))",nick,server,iter->second->nick,iter->second->server);
-               WriteServ(iter->second->fd, "KILL %s :%s",iter->second->nick,str);
-               kill_link(iter->second,str);
-       }
-       clientlist[nick] = new userrec();
-       // remote users have an fd of -1. This is so that our Write abstraction
-       // routines know to route any messages to this record away to whatever server
-       // theyre on.
-       clientlist[nick]->fd = -1;
-       strncpy(clientlist[nick]->nick, nick,NICKMAX);
-       strncpy(clientlist[nick]->host, host,160);
-       strncpy(clientlist[nick]->dhost, dhost,160);
-       strncpy(clientlist[nick]->server, server,256);
-       strncpy(clientlist[nick]->ident, ident,10); // +1 char to compensate for tilde
-       strncpy(clientlist[nick]->fullname, gecos,128);
-       clientlist[nick]->signon = TS;
-       clientlist[nick]->nping = 0; // this is ignored for a remote user anyway.
-       clientlist[nick]->lastping = 1;
-       clientlist[nick]->port = 0; // so is this...
-       clientlist[nick]->registered = 7; // this however we need to set for them to receive messages and appear online
-       clientlist[nick]->idle_lastmsg = time(NULL); // this is unrealiable and wont actually be used locally
-       for (int i = 0; i < MAXCHANS; i++)
-       {
-               clientlist[nick]->chans[i].channel = NULL;
-               clientlist[nick]->chans[i].uc_modes = 0;
-       }
-}
-
-void handle_F(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       long tdiff = time(NULL) - atoi(params);
-       if (tdiff)
-               WriteOpers("TS split for %s -> %s: %d",source->name,reply->name,tdiff);
-}
-
-void handle_a(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* nick = strtok(params," :");
-       char* gecos = strtok(NULL,"\r\n");
-       
-       userrec* user = Find(nick);
-
-       if (user)
-               strncpy(user->fullname,gecos,MAXBUF);
-}
-
-void handle_b(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* nick = strtok(params," ");
-       char* host = strtok(NULL," ");
-       
-       userrec* user = Find(nick);
-
-       if (user)
-               strncpy(user->dhost,host,160);
-}
-
-void handle_plus(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       // %s %s %d %d
-       // + test3.chatspike.net 7010 -2016508415
-       char* servername = strtok(params," ");
-       char* ipaddr = strtok(NULL," ");
-       char* ipport = strtok(NULL," ");
-       char* cookie = strtok(NULL," ");
-       log(DEBUG,"*** Connecting back to %s:%d",ipaddr,atoi(ipport));
-
-
-       bool conn_already = false;
-       for (int i = 0; i < 32; i++)
-       {
-               if (me[i] != NULL)
-               {
-                       for (int j = 0; j < me[i]->connectors.size(); j++)
-                       {
-                               if (!strcasecmp(me[i]->connectors[j].GetServerName().c_str(),servername))
-                               {
-                                       if (me[i]->connectors[j].GetServerPort() == atoi(ipport))
-                                       {
-                                               log(DEBUG,"Already got a connection to %s:%d, ignoring +",ipaddr,atoi(ipport));
-                                               conn_already = true;
-                                       }
-                               }
-                       }
-               }
-       }
-       if (!conn_already)
-               me[defaultRoute]->MeshCookie(ipaddr,atoi(ipport),atoi(cookie),servername);
-}
-
-void handle_R(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       char* server = strtok(params," ");
-       char* data = strtok(NULL,"\r\n");
-       log(DEBUG,"Forwarded packet '%s' to '%s'",data,server);
-       NetSendToOne(server,data);
-}
-
-void handle_J(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       // IMPORTANT NOTE
-       // The J token currently has no timestamp - this needs looking at
-       // because it will allow splitriding.
-       char* nick = strtok(params," ");
-       char* channel = strtok(NULL," ");
-       userrec* user = Find(nick);
-       while (channel)
-       {
-               if ((user != NULL) && (strcmp(channel,"")))
-               {
-                       char privilage = '\0';
-                       if (channel[0] != '#')
-                       {
-                               privilage = channel[0];
-                               channel++;
-                       }
-                       add_channel(user,channel,"",true);
-
-                       // now work out the privilages they should have on each channel
-                       // and send the appropriate servermodes.
-                       for (int i = 0; i != MAXCHANS; i++)
-                       {
-                               if (user->chans[i].channel)
-                               {
-                                       if (!strcasecmp(user->chans[i].channel->name,channel))
-                                       {
-                                               if (privilage == '@')
-                                               {
-                                                       user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_OP;
-                                                       WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +o %s",channel,user->nick);
-                                               }
-                                               if (privilage == '%')
-                                               {
-                                                       user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_HOP;
-                                                       WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +h %s",channel,user->nick);
-                                               }
-                                               if (privilage == '+')
-                                               {
-                                                       user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_VOICE;
-                                                       WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +v %s",channel,user->nick);
-                                               }
-                                       }
-                               }
-                       }
-
-               }
-               channel = strtok(NULL," ");
-       }
-}
-
 void NetSendMyRoutingTable()
 {
        // send out a line saying what is reachable to us.
@@ -3308,36 +2959,6 @@ void NetSendMyRoutingTable()
                NetSendToAll(buffer);
 }
 
-void handle_dollar(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       log(DEBUG,"Storing routing table...");
-       char* sourceserver = strtok(params," ");
-       char* server = strtok(NULL," ");
-       for (int i = 0; i < 32; i++)
-       {
-               if (me[i] != NULL)
-               {
-                       for (int j = 0; j < me[i]->connectors.size(); j++)
-                       {
-                               if (!strcasecmp(me[i]->connectors[j].GetServerName().c_str(),sourceserver))
-                               {
-                                       me[i]->connectors[j].routes.clear();
-                                       log(DEBUG,"Found entry for source server.");
-                                       while (server)
-                                       {
-                                               // store each route
-                                               me[i]->connectors[j].routes.push_back(server);
-                                               log(DEBUG,"*** Stored route: %s -> %s -> %s",ServerName,sourceserver,server);
-                                               server = strtok(NULL," ");
-                                       }
-                                       return;
-                               }
-                       }
-               }
-       }
-       log(DEBUG,"Warning! routing table received from nonexistent server!");
-}
-
 
 void DoSplit(const char* params)
 {
@@ -3413,472 +3034,6 @@ void RemoveServer(const char* name)
        }
 }
 
-void handle_amp(char token,char* params,serverrec* source,serverrec* reply, char* udp_host)
-{
-       log(DEBUG,"Netsplit! %s split from mesh, removing!",params);
-       bool go_again = true;
-       while (go_again)
-       {
-               go_again = false;
-               for (int i = 0; i < 32; i++)
-               {
-                       if (me[i] != NULL)
-                       {
-                               for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
-                               {
-                                       if (!strcasecmp(j->GetServerName().c_str(),params))
-                                       {
-                                               j->routes.clear();
-                                               j->CloseConnection();
-                                               me[i]->connectors.erase(j);
-                                               go_again = true;
-                                               break;
-                                       }
-                               }
-                       }
-               }
-       }
-       log(DEBUG,"Removed server. Will remove clients...");
-       // iterate through the userlist and remove all users on this server.
-       // because we're dealing with a mesh, we dont have to deal with anything
-       // "down-route" from this server (nice huh)
-       go_again = true;
-       char reason[MAXBUF];
-       snprintf(reason,MAXBUF,"%s %s",ServerName,params);
-       while (go_again)
-       {
-               go_again = false;
-               for (user_hash::const_iterator u = clientlist.begin(); u != clientlist.end(); u++)
-               {
-                       if (!strcasecmp(u->second->server,params))
-                       {
-                               kill_link(u->second,reason);
-                               go_again = true;
-                               break;
-                       }
-               }
-       }
-}
-
-long authcookie;
-
-
-void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,char* ipaddr,int port)
-{
-       char buffer[MAXBUF];
-
-       switch(token)
-       {
-               // Y <TS>
-               // start netburst
-               case 'Y':
-                       nb_start = time(NULL);
-                       WriteOpers("Server %s is starting netburst.",udp_host);
-                       // now broadcast this new servers address out to all servers that are linked to us,
-                       // except the newcomer. They'll all attempt to connect back to it.
-                       authcookie = rand()*rand();
-                       snprintf(buffer,MAXBUF,"~ %d",authcookie);
-                       NetSendToAll(buffer);
-               break;
-               // ~
-               // Store authcookie
-               // once stored, this authcookie permits other servers to log in
-               // without user or password, using it.
-               case '~':
-                       auth_cookies.push_back(atoi(params));
-                       log(DEBUG,"*** Stored auth cookie, will permit servers with auth-cookie %d",atoi(params));
-               break;
-               // connect back to a server using an authcookie
-               case '+':
-                       handle_plus(token,params,source,reply,udp_host);
-               break;
-               // routing table
-               case '$':
-                       handle_dollar(token,params,source,reply,udp_host);
-               break;
-               // node unreachable - we cant route to a server, sooooo we slit it off.
-               // servers can generate these for themselves for an squit.
-               case '&':
-                       handle_amp(token,params,source,reply,udp_host);
-               break;
-               // R <server> <data>
-               // redirect token, send all of <data> along to the given 
-               // server as this server has been found to still have a route to it
-               case 'R':
-                       handle_R(token,params,source,reply,udp_host);
-               break;
-               // ?
-               // ping
-               case '?':
-                       reply->SendPacket("!",udp_host);
-               break;
-               // ?
-               // pong
-               case '!':
-               break;
-               // *
-               // no operation
-               case '*':
-               break;
-               // N <TS> <NICK> <HOST> <DHOST> <IDENT> <MODES> <SERVER> :<GECOS>
-               // introduce remote client
-               case 'N':
-                       handle_N(token,params,source,reply,udp_host);
-               break;
-               // a <NICK> :<GECOS>
-               // change GECOS (SETNAME)
-               case 'a':
-                       handle_a(token,params,source,reply,udp_host);
-               break;
-               // b <NICK> :<HOST>
-               // change displayed host (SETHOST)
-               case 'b':
-                       handle_b(token,params,source,reply,udp_host);
-               break;
-               // t <NICK> <CHANNEL> :<TOPIC>
-               // change a channel topic
-               case 't':
-                       handle_t(token,params,source,reply,udp_host);
-               break;
-               // i <NICK> <CHANNEL>
-               // invite a user to a channel
-               case 'i':
-                       handle_i(token,params,source,reply,udp_host);
-               break;
-               // k <SOURCE> <DEST> <CHANNEL> :<REASON>
-               // kick a user from a channel
-               case 'k':
-                       handle_k(token,params,source,reply,udp_host);
-               break;
-               // n <NICK> <NEWNICK>
-               // change nickname of client -- a server should only be able to
-               // change the nicknames of clients that reside on it unless
-               // they are ulined.
-               case 'n':
-                       handle_n(token,params,source,reply,udp_host);
-               break;
-               // J <NICK> <CHANLIST>
-               // Join user to channel list, merge channel permissions
-               case 'J':
-                       handle_J(token,params,source,reply,udp_host);
-               break;
-               // T <TS> <CHANNEL> <TOPICSETTER> :<TOPIC>
-               // change channel topic (netburst only)
-               case 'T':
-                       handle_T(token,params,source,reply,udp_host);
-               break;
-               // M <TARGET> <MODES> [MODE-PARAMETERS]
-               // Server setting modes on an object
-               case 'M':
-                       handle_M(token,params,source,reply,udp_host);
-               break;
-               // m <SOURCE> <TARGET> <MODES> [MODE-PARAMETERS]
-               // User setting modes on an object
-               case 'm':
-                       handle_m(token,params,source,reply,udp_host);
-               break;
-               // P <SOURCE> <TARGET> :<TEXT>
-               // Send a private/channel message
-               case 'P':
-                       handle_P(token,params,source,reply,udp_host);
-               break;
-               // V <SOURCE> <TARGET> :<TEXT>
-               // Send a private/channel notice
-               case 'V':
-                       handle_V(token,params,source,reply,udp_host);
-               break;
-               // L <SOURCE> <CHANNEL> :<REASON>
-               // User parting a channel
-               case 'L':
-                       handle_L(token,params,source,reply,udp_host);
-               break;
-               // Q <SOURCE> :<REASON>
-               // user quitting
-               case 'Q':
-                       handle_Q(token,params,source,reply,udp_host);
-               break;
-               // K <SOURCE> <DEST> :<REASON>
-               // remote kill
-               case 'K':
-                       handle_K(token,params,source,reply,udp_host);
-               break;
-               // @ <SOURCE> :<TEXT>
-               // wallops
-               case '@':
-                       handle_AT(token,params,source,reply,udp_host);
-               break;
-               // F <TS>
-               // end netburst
-               case 'F':
-                       WriteOpers("Server %s has completed netburst. (%d secs)",udp_host,time(NULL)-nb_start);
-                       handle_F(token,params,source,reply,udp_host);
-                       nb_start = 0;
-                       // tell all the other servers to use this authcookie to connect back again
-                       // got '+ test3.chatspike.net 7010 -2016508415' from test.chatspike.net
-                       snprintf(buffer,MAXBUF,"+ %s %s %d %d",udp_host,ipaddr,port,authcookie);
-                       NetSendToAllExcept(udp_host,buffer);
-               break;
-               // X <reserved>
-               // Send netburst now
-               case 'X':
-                       WriteOpers("Sending my netburst to %s",udp_host);
-                       DoSync(source,udp_host);
-                       WriteOpers("Send of netburst to %s completed",udp_host);
-                       NetSendMyRoutingTable();
-               break;
-               // anything else
-               default:
-                       WriteOpers("WARNING! Unknown datagram type '%c'",token);
-               break;
-       }
-}
-
-
-void handle_link_packet(char* udp_msg, char* udp_host, serverrec *serv)
-{
-       char response[10240];
-       char token = udp_msg[0];
-       char* params = udp_msg + 2;
-       char finalparam[1024];
-       strcpy(finalparam," :xxxx");
-       if (strstr(udp_msg," :")) {
-               strncpy(finalparam,strstr(udp_msg," :"),1024);
-       }
-       if (token == '-') {
-               char* cookie = strtok(params," ");
-               char* servername = strtok(NULL," ");
-               char* serverdesc = finalparam+2;
-
-               WriteOpers("AuthCookie CONNECT from %s (%s)",servername,udp_host);
-
-               for (int u = 0; u < auth_cookies.size(); u++)
-               {
-                       if (auth_cookies[u] == atoi(cookie))
-                       {
-                               WriteOpers("Allowed cookie from %s, is now part of the mesh",servername);
-
-
-                               for (int j = 0; j < 32; j++)
-                               {
-                                       if (me[j] != NULL)
-                                       {
-                                               for (int k = 0; k < me[j]->connectors.size(); k++)
-                                               {
-                                                       if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),udp_host))
-                                                       {
-                                                               me[j]->connectors[k].SetServerName(servername);
-                                                               me[j]->connectors[k].SetDescription(serverdesc);
-                                                               me[j]->connectors[k].SetState(STATE_CONNECTED);
-                                                               NetSendMyRoutingTable();
-                                                               return;
-                                                       }
-                                               }
-                                       }
-                                       WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right now! Possible intrusion attempt!",udp_host);
-                                       return;
-                               }
-
-
-                               return;
-                       }
-               }
-               // bad cookie, bad bad! go sit in the corner!
-               WriteOpers("Bad cookie from %s!",servername);
-               return;
-       }
-       else
-       if (token == 'S') {
-               // S test.chatspike.net password portn :ChatSpike InspIRCd test server
-               char* servername = strtok(params," ");
-               char* password = strtok(NULL," ");
-               char* myport = strtok(NULL," ");
-               char* revision = strtok(NULL," ");
-               char* serverdesc = finalparam+2;
-
-               WriteOpers("CONNECT from %s (%s) (their port: %d)",servername,udp_host,atoi(myport));
-               
-               ircd_connector* cn = serv->FindHost(servername);
-               
-               if (cn)
-               {
-                       WriteOpers("CONNECT aborted: Server %s already exists from %s",servername,ServerName);
-                       char buffer[MAXBUF];
-                       sprintf(buffer,"E :Server %s already exists!",servername);
-                       serv->SendPacket(buffer,udp_host);
-                       RemoveServer(udp_host);
-                       return;
-               }
-
-               if (atoi(revision) != GetRevision())
-               {
-                       WriteOpers("CONNECT aborted: Could not link to %s, is an incompatible version %s, our version is %d",servername,revision,GetRevision());
-                       char buffer[MAXBUF];
-                       sprintf(buffer,"E :Version number mismatch");
-                       serv->SendPacket(buffer,udp_host);
-                       RemoveServer(udp_host);
-                       RemoveServer(servername);
-                       return;
-               }
-
-               for (int j = 0; j < serv->connectors.size(); j++)
-               {
-                       if (!strcasecmp(serv->connectors[j].GetServerName().c_str(),udp_host))
-                       {
-                               serv->connectors[j].SetServerName(servername);
-                               serv->connectors[j].SetDescription(serverdesc);
-                               serv->connectors[j].SetServerPort(atoi(myport));
-                       }
-               }
-               
-               
-               char Link_ServerName[1024];
-               char Link_IPAddr[1024];
-               char Link_Port[1024];
-               char Link_Pass[1024];
-               char Link_SendPass[1024];
-               int LinkPort = 0;
-               
-               // search for a corresponding <link> block in the config files
-               for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
-               {
-                       ConfValue("link","name",i,Link_ServerName,&config_f);
-                       ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
-                       ConfValue("link","port",i,Link_Port,&config_f);
-                       ConfValue("link","recvpass",i,Link_Pass,&config_f);
-                       ConfValue("link","sendpass",i,Link_SendPass,&config_f);
-                       log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
-                       LinkPort = atoi(Link_Port);
-                       if (!strcasecmp(Link_ServerName,servername))
-                       {
-                               // we have a matching link line -
-                               // send a 'diminutive' server message back...
-                               snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
-                               serv->SendPacket(response,servername);
-
-                               for (int t = 0; t < serv->connectors.size(); t++)
-                               {
-                                       if (!strcasecmp(serv->connectors[t].GetServerName().c_str(),servername))
-                                       {
-                                               serv->connectors[t].SetState(STATE_CONNECTED);
-                                       }
-                               }
-               
-                               return;
-                       }
-               }
-               char buffer[MAXBUF];
-               sprintf(buffer,"E :Access is denied (no matching link block)");
-               serv->SendPacket(buffer,udp_host);
-               WriteOpers("CONNECT from %s denied, no matching link block",servername);
-               RemoveServer(udp_host);
-               RemoveServer(servername);
-               return;
-       }
-       else
-       if (token == 's') {
-               // S test.chatspike.net password :ChatSpike InspIRCd test server
-               char* servername = strtok(params," ");
-               char* password = strtok(NULL," ");
-               char* serverdesc = finalparam+2;
-               
-               // TODO: we should do a check here to ensure that this server is one we recently initiated a
-               // link with, and didnt hear an 's' or 'E' back from yet (these are the only two valid responses
-               // to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
-               // a connect, so put out an oper alert!
-               
-               // for now, just accept all, we'll fix that later.
-               WriteOpers("%s accepted our link credentials ",servername);
-               
-               char Link_ServerName[1024];
-               char Link_IPAddr[1024];
-               char Link_Port[1024];
-               char Link_Pass[1024];
-               char Link_SendPass[1024];
-               int LinkPort = 0;
-               
-               // search for a corresponding <link> block in the config files
-               for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
-               {
-                       ConfValue("link","name",i,Link_ServerName,&config_f);
-                       ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
-                       ConfValue("link","port",i,Link_Port,&config_f);
-                       ConfValue("link","recvpass",i,Link_Pass,&config_f);
-                       ConfValue("link","sendpass",i,Link_SendPass,&config_f);
-                       log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
-                       LinkPort = atoi(Link_Port);
-                       if (!strcasecmp(Link_ServerName,servername))
-                       {
-                               // matching link at this end too, we're all done!
-                               // at this point we must begin key exchange and insert this
-                               // server into our 'active' table.
-                               for (int j = 0; j < 32; j++)
-                               {
-                                       if (me[j] != NULL)
-                                       {
-                                               for (int k = 0; k < me[j]->connectors.size(); k++)
-                                               {
-                                                       if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),udp_host))
-                                                       {
-                                                               char buffer[MAXBUF];
-                                                               me[j]->connectors[k].SetDescription(serverdesc);
-                                                               me[j]->connectors[k].SetState(STATE_CONNECTED);
-                                                               sprintf(buffer,"X 0");
-                                                               serv->SendPacket(buffer,udp_host);
-                                                               DoSync(me[j],udp_host);
-                                                               NetSendMyRoutingTable();
-                                                               return;
-                                                       }
-                                               }
-                                       }
-                                       WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
-                                       return;
-                               }
-                       }
-                       else {
-                               log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
-                       }
-               }
-               char buffer[MAXBUF];
-               sprintf(buffer,"E :Access is denied (no matching link block)");
-               serv->SendPacket(buffer,udp_host);
-               WriteOpers("CONNECT from %s denied, no matching link block",servername);
-               RemoveServer(udp_host);
-               RemoveServer(servername);
-               return;
-       }
-       else
-       if (token == 'E') {
-               char* error_message = finalparam+2;
-               WriteOpers("ERROR from %s: %s",udp_host,error_message);
-               return;
-       }
-       else {
-
-               serverrec* source_server = NULL;
-
-               for (int j = 0; j < 32; j++)
-               {
-                       if (me[j] != NULL)
-                       {
-                               for (int x = 0; x < me[j]->connectors.size(); x++)
-                               {
-                                       log(DEBUG,"Servers are: '%s' '%s'",udp_host,me[j]->connectors[x].GetServerName().c_str());
-                                       if (!strcasecmp(me[j]->connectors[x].GetServerName().c_str(),udp_host))
-                                       {
-                                               if (me[j]->connectors[x].GetState() == STATE_CONNECTED)
-                                               {
-                                                       // found a valid ircd_connector.
-                                                       process_restricted_commands(token,params,me[j],serv,udp_host,me[j]->connectors[x].GetServerIP(),me[j]->connectors[x].GetServerPort());
-                                                       return;
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s: %c",udp_host,token);
-       }
-}
 
 int reap_counter = 0;
 
@@ -4057,7 +3212,7 @@ int InspIRCd(void)
        
 
        length = sizeof (client);
-       char udp_msg[MAXBUF], udp_host[MAXBUF];
+       char udp_msg[MAXBUF], tcp_host[MAXBUF];
          
        /* main loop, this never returns */
        for (;;)
@@ -4065,6 +3220,8 @@ int InspIRCd(void)
 #ifdef _POSIX_PRIORITY_SCHEDULING
                sched_yield();
 #endif
+               // update the status of klines, etc
+               expire_lines();
 
                fd_set sfd;
                timeval tval;
@@ -4128,7 +3285,7 @@ int InspIRCd(void)
                {
                        std::deque<std::string> msgs;
                        msgs.clear();
-                       if (me[x]->RecvPacket(msgs, udp_host))
+                       if (me[x]->RecvPacket(msgs, tcp_host))
                        {
                                for (int ctr = 0; ctr < msgs.size(); ctr++)
                                {
@@ -4136,16 +3293,24 @@ int InspIRCd(void)
                                        strncpy(udp_msg,msgs[ctr].c_str(),MAXBUF);
                                        if (strlen(udp_msg)<1)
                                        {
-                                               log(DEBUG,"Invalid string from %s [route%d]",udp_host,x);
+                                               log(DEBUG,"Invalid string from %s [route%d]",tcp_host,x);
                                                break;
                                        }
                                        // during a netburst, send all data to all other linked servers
-                                       if ((nb_start>0) && (udp_msg[0] != 'Y') && (udp_msg[0] != 'X') && (udp_msg[0] != 'F'))
+                                       if ((((nb_start>0) && (udp_msg[0] != 'Y') && (udp_msg[0] != 'X') && (udp_msg[0] != 'F'))) || (is_uline(tcp_host)))
                                        {
-                                               NetSendToAllExcept(udp_host,udp_msg);
+                                               if (is_uline(tcp_host))
+                                               {
+                                                       if ((udp_msg[0] != 'Y') && (udp_msg[0] != 'X') && (udp_msg[0] != 'F'))
+                                                       {
+                                                               NetSendToAllExcept(tcp_host,udp_msg);
+                                                       }
+                                               }
+                                               else
+                                                       NetSendToAllExcept(tcp_host,udp_msg);
                                        }
                                        FOREACH_MOD OnPacketReceive(udp_msg);
-                                       handle_link_packet(udp_msg, udp_host, me[x]);
+                                       handle_link_packet(udp_msg, tcp_host, me[x]);
                                }
                                goto label;
                        }
@@ -4378,7 +3543,7 @@ int InspIRCd(void)
                                }
                                else
                                {
-                                       AddClient(incomingSockfd, resolved, ports[count], iscached);
+                                       AddClient(incomingSockfd, resolved, ports[count], iscached, target);
                                        log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
                                }
                                goto label;