]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/commands.cpp
Changed the type of struct whowas records are stored in (no need to waste a big heavy...
[user/henk/code/inspircd.git] / src / commands.cpp
index f8d4c208d4a4414766c3d1abdc4d3c76aaab3869..18329e9b37b15940759ded0605ba86a70474fb5d 100644 (file)
 #include <sys/errno.h>
 #include <sys/ioctl.h>
 #include <sys/utsname.h>
+#ifdef USE_KQUEUE
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+#endif
 #include <cstdio>
 #include <time.h>
 #include <string>
 #include <errno.h>
 #include <unistd.h>
 #include <sched.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#ifndef RUSAGE_SELF
+#define   RUSAGE_SELF     0
+#define   RUSAGE_CHILDREN     -1
+#endif
 #include "connection.h"
 #include "users.h"
 #include "servers.h"
 
 using namespace std;
 
+#ifdef USE_KQUEUE
+extern int kq;
+#endif
+
 extern int MODCOUNT;
 extern std::vector<Module*> modules;
 extern std::vector<ircd_module*> factory;
@@ -98,7 +114,7 @@ extern std::vector<std::string> module_names;
 extern char MyExecutable[1024];
 extern int boundPortCount;
 extern int portCount;
-extern int UDPportCount;
+extern int SERVERportCount;
 extern int ports[MAXSOCKS];
 extern int defaultRoute;
 
@@ -185,17 +201,26 @@ struct InAddr_HashComp
 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
+typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, StrHashComp> whowas_hash;
 typedef std::deque<command_t> command_table;
 
 
 extern user_hash clientlist;
 extern chan_hash chanlist;
-extern user_hash whowas;
+extern whowas_hash whowas;
 extern command_table cmdlist;
 extern file_cache MOTD;
 extern file_cache RULES;
 extern address_cache IP;
 
+extern std::vector<userrec*> all_opers;
+
+// This table references users by file descriptor.
+// its an array to make it VERY fast, as all lookups are referenced
+// by an integer, meaning there is no need for a scan/search operation.
+extern userrec* fd_ref_table[65536];
+
+extern int statsAccept,statsRefused,statsUnknown,statsCollisions,statsDns,statsDnsGood,statsDnsBad,statsConnects,statsSent,statsRecv;
 
 void handle_join(char **parameters, int pcnt, userrec *user)
 {
@@ -391,6 +416,8 @@ void handle_kill(char **parameters, int pcnt, userrec *user)
                        {
                                purge_empty_chans(u);
                        }
+                       if (u->fd > -1)
+                               fd_ref_table[u->fd] = NULL;
                        delete u;
                }
                else
@@ -447,7 +474,7 @@ void handle_invite(char **parameters, int pcnt, userrec *user)
                }
                else
                {
-                       if (c->inviteonly)
+                       if (c->binarymodes & CM_INVITEONLY)
                        {
                                WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
                        }
@@ -456,7 +483,7 @@ void handle_invite(char **parameters, int pcnt, userrec *user)
                return;
        }
 
-       if (c->inviteonly)
+       if (c->binarymodes & CM_INVITEONLY)
        {
                if (cstatus(user,c) < STATUS_HOP)
                {
@@ -502,7 +529,7 @@ void handle_topic(char **parameters, int pcnt, userrec *user)
                        Ptr = FindChan(parameters[0]);
                        if (Ptr)
                        {
-                               if (((Ptr) && (!has_channel(user,Ptr))) && (Ptr->secret))
+                               if (((Ptr) && (!has_channel(user,Ptr))) && (Ptr->binarymodes & CM_SECRET))
                                {
                                        WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
                                        return;
@@ -536,7 +563,7 @@ void handle_topic(char **parameters, int pcnt, userrec *user)
                                        WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
                                        return;
                                }
-                               if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
+                               if ((Ptr->binarymodes & CM_TOPICLOCK) && (cstatus(user,Ptr)<STATUS_HOP))
                                {
                                        WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel", user->nick, Ptr->name);
                                        return;
@@ -590,7 +617,7 @@ void handle_names(char **parameters, int pcnt, userrec *user)
        c = FindChan(parameters[0]);
        if (c)
        {
-                if (((c) && (!has_channel(user,c))) && (c->secret))
+                if (((c) && (!has_channel(user,c))) && (c->binarymodes & CM_SECRET))
                 {
                       WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, c->name);
                       return;
@@ -618,12 +645,12 @@ void handle_privmsg(char **parameters, int pcnt, userrec *user)
                chan = FindChan(parameters[0]);
                if (chan)
                {
-                       if ((chan->noexternal) && (!has_channel(user,chan)))
+                       if ((chan->binarymodes & CM_NOEXTERNAL) && (!has_channel(user,chan)))
                        {
                                WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
                                return;
                        }
-                       if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
+                       if ((chan->binarymodes & CM_MODERATED) && (cstatus(user,chan)<STATUS_VOICE))
                        {
                                WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
                                return;
@@ -637,6 +664,12 @@ void handle_privmsg(char **parameters, int pcnt, userrec *user)
                                return;
                        }
                        parameters[1] = (char*)temp.c_str();
+
+                       if (temp == "")
+                       {
+                               WriteServ(user->fd,"412 %s No text to send", user->nick);
+                               return;
+                       }
                        
                        ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
                        
@@ -706,12 +739,12 @@ void handle_notice(char **parameters, int pcnt, userrec *user)
                chan = FindChan(parameters[0]);
                if (chan)
                {
-                       if ((chan->noexternal) && (!has_channel(user,chan)))
+                       if ((chan->binarymodes & CM_NOEXTERNAL) && (!has_channel(user,chan)))
                        {
                                WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
                                return;
                        }
-                       if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
+                       if ((chan->binarymodes & CM_MODERATED) && (cstatus(user,chan)<STATUS_VOICE))
                        {
                                WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
                                return;
@@ -726,6 +759,12 @@ void handle_notice(char **parameters, int pcnt, userrec *user)
                        }
                        parameters[1] = (char*)temp.c_str();
 
+                        if (temp == "")
+                        {
+                                WriteServ(user->fd,"412 %s No text to send", user->nick);
+                                return;
+                        }
+
                        ChanExceptSender(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
 
                        // if any users of this channel are on remote servers, broadcast the packet
@@ -843,11 +882,12 @@ void handle_whois(char **parameters, int pcnt, userrec *user)
                        WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
                        if ((user == dest) || (strchr(user->modes,'o')))
                        {
-                               WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
+                               WriteServ(user->fd,"378 %s %s :is connecting from *@%s %s",user->nick, dest->nick, dest->host, dest->ip);
                        }
-                       if (strcmp(chlist(dest),""))
+                       char* cl = chlist(dest,user);
+                       if (strcmp(cl,""))
                        {
-                               WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
+                               WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, cl);
                        }
                        WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, GetServerDescription(dest->server).c_str());
                        if (strcmp(dest->awaymsg,""))
@@ -871,12 +911,14 @@ void handle_whois(char **parameters, int pcnt, userrec *user)
                else
                {
                        WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
+                       WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, parameters[0]);
                }
        }
        else
        {
                /* no such nick/channel */
                WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
+               WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, parameters[0]);
        }
 }
 
@@ -928,8 +970,17 @@ void handle_quit(char **parameters, int pcnt, userrec *user)
        /* push the socket on a stack of sockets due to be closed at the next opportunity */
        if (user->fd > -1)
        {
-               shutdown(user->fd,2);
-               close(user->fd);
+#ifdef USE_KQUEUE
+                struct kevent ke;
+                EV_SET(&ke, user->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
+                int i = kevent(kq, &ke, 1, 0, 0, NULL);
+                if (i == -1)
+                {
+                        log(DEBUG,"kqueue: Failed to remove user from queue!");
+                }
+#endif
+                shutdown(user->fd,2);
+                close(user->fd);
        }
        
        if (iter != clientlist.end())
@@ -940,6 +991,8 @@ void handle_quit(char **parameters, int pcnt, userrec *user)
        if (user->registered == 7) {
                purge_empty_chans(user);
        }
+        if (user->fd > -1)
+                fd_ref_table[user->fd] = NULL;
        delete user;
 }
 
@@ -965,11 +1018,11 @@ void handle_who(char **parameters, int pcnt, userrec *user)
                                                // Bug Fix #29
                                                strcpy(tmp, "");
                                                if (strcmp(i->second->awaymsg, "")) {
-                                                       strncat(tmp, "G", 9);
+                                                       strlcat(tmp, "G", 9);
                                                } else {
-                                                       strncat(tmp, "H", 9);
+                                                       strlcat(tmp, "H", 9);
                                                }
-                                               if (strchr(i->second->modes,'o')) { strncat(tmp, "*", 9); }
+                                               if (strchr(i->second->modes,'o')) { strlcat(tmp, "*", 9); }
                                                WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr ? Ptr->name : "*", i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
                                                n_list++;
                                                if (n_list > MaxWhoResults)
@@ -999,12 +1052,12 @@ void handle_who(char **parameters, int pcnt, userrec *user)
                                                // Fix Bug #29 - Part 2..
                                                strcpy(tmp, "");
                                                if (strcmp(i->second->awaymsg, "")) {
-                                                       strncat(tmp, "G", 9);
+                                                       strlcat(tmp, "G", 9);
                                                } else {
-                                                       strncat(tmp, "H", 9);
+                                                       strlcat(tmp, "H", 9);
                                                }
-                                               if (strchr(i->second->modes,'o')) { strncat(tmp, "*", 9); }
-                                               strcat(tmp, cmode(i->second, Ptr));
+                                               if (strchr(i->second->modes,'o')) { strlcat(tmp, "*", 9); }
+                                               strlcat(tmp, cmode(i->second, Ptr),5);
                                                WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
                                        }
                                }
@@ -1023,12 +1076,13 @@ void handle_who(char **parameters, int pcnt, userrec *user)
                                // Bug Fix #29 -- Part 29..
                                strcpy(tmp, "");
                                if (strcmp(u->awaymsg, "")) {
-                                       strncat(tmp, "G" ,9);
+                                       strlcat(tmp, "G" ,9);
                                } else {
-                                       strncat(tmp, "H" ,9);
+                                       strlcat(tmp, "H" ,9);
                                }
-                               if (strchr(u->modes,'o')) { strncat(tmp, "*" ,9); }
-                               WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, u->nick, u->ident, u->dhost, u->server, u->nick, tmp, u->fullname);
+                               if (strchr(u->modes,'o')) { strlcat(tmp, "*" ,9); }
+                               WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, u->chans[0].channel ? u->chans[0].channel->name
+                                : "*", u->ident, u->dhost, u->server, u->nick, tmp, u->fullname);
                        }
                        WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
                }
@@ -1037,24 +1091,19 @@ void handle_who(char **parameters, int pcnt, userrec *user)
        {
                 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
                 {
-                       for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
+                       for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
                         {
-                                if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
-                                {
-                                        if (strchr(i->second->modes,'o'))
-                                        {
-                                               // If i were a rich man.. I wouldn't need to me making these bugfixes..
-                                               // But i'm a poor bastard with nothing better to do.
-                                               strcpy(tmp, "");
-                                               if (strcmp(i->second->awaymsg, "")) {
-                                                       strncat(tmp, "G" ,9);
-                                               } else {
-                                                       strncat(tmp, "H" ,9);
-                                               }
-
-                                                WriteServ(user->fd,"352 %s %s %s %s %s %s %s* :0 %s",user->nick, user->nick, i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
-                                        }
-                                }
+                               // If i were a rich man.. I wouldn't need to me making these bugfixes..
+                               // But i'm a poor bastard with nothing better to do.
+                               userrec* oper = *i;
+                               strcpy(tmp, "");
+                               if (strcmp(oper->awaymsg, "")) {
+                                       strlcat(tmp, "G" ,9);
+                               } else {
+                                       strlcat(tmp, "H" ,9);
+                               }
+                                WriteServ(user->fd,"352 %s %s %s %s %s %s %s* :0 %s", user->nick, oper->chans[0].channel ? oper->chans[0].channel->name 
+                               : "*", oper->ident, oper->dhost, oper->server, oper->nick, tmp, oper->fullname);
                         }
                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
                         return;
@@ -1072,7 +1121,8 @@ void handle_list(char **parameters, int pcnt, userrec *user)
        WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
        for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
        {
-               if ((!i->second->c_private) && (!i->second->secret))
+               // if the channel is not private/secret, OR the user is on the channel anyway
+               if (((!(i->second->binarymodes & CM_PRIVATE)) && (!(i->second->binarymodes & CM_SECRET))) || (has_channel(user,i->second)))
                {
                        WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
                }
@@ -1215,7 +1265,7 @@ void handle_away(char **parameters, int pcnt, userrec *user)
 
 void handle_whowas(char **parameters, int pcnt, userrec* user)
 {
-       user_hash::iterator i = whowas.find(parameters[0]);
+       whowas_hash::iterator i = whowas.find(parameters[0]);
 
        if (i == whowas.end())
        {
@@ -1292,6 +1342,7 @@ void handle_modules(char **parameters, int pcnt, userrec *user)
                        WriteServ(user->fd,"900 %s :%s",user->nick,CleanFilename(modulename));
                }
        }
+       WriteServ(user->fd,"901 %s :End of MODULES list",user->nick);
 }
 
 void handle_stats(char **parameters, int pcnt, userrec *user)
@@ -1411,6 +1462,7 @@ void handle_stats(char **parameters, int pcnt, userrec *user)
        /* stats z (debug and memory info) */
        if (*parameters[0] == 'z')
        {
+               rusage R;
                WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
                WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
                WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
@@ -1419,6 +1471,24 @@ void handle_stats(char **parameters, int pcnt, userrec *user)
                WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
                WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
                WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
+               if (!getrusage(RUSAGE_SELF,&R))
+               {
+                       WriteServ(user->fd,"249 %s :Total allocation: %luK (0x%lx)",user->nick,R.ru_maxrss,R.ru_maxrss);
+                       WriteServ(user->fd,"249 %s :Signals:          %lu  (0x%lx)",user->nick,R.ru_nsignals,R.ru_nsignals);
+                       WriteServ(user->fd,"249 %s :Page faults:      %lu  (0x%lx)",user->nick,R.ru_majflt,R.ru_majflt);
+                       WriteServ(user->fd,"249 %s :Swaps:            %lu  (0x%lx)",user->nick,R.ru_nswap,R.ru_nswap);
+                       WriteServ(user->fd,"249 %s :Context Switches: %lu  (0x%lx)",user->nick,R.ru_nvcsw+R.ru_nivcsw,R.ru_nvcsw+R.ru_nivcsw);
+               }
+       }
+
+       if (*parameters[0] == 'T')
+       {
+               WriteServ(user->fd,"249 Brain :accepts %d refused %d",statsAccept,statsRefused);
+               WriteServ(user->fd,"249 Brain :unknown commands %d",statsUnknown);
+               WriteServ(user->fd,"249 Brain :nick collisions %d",statsCollisions);
+               WriteServ(user->fd,"249 Brain :dns requests %d succeeded %d failed %d",statsDns,statsDnsGood,statsDnsBad);
+               WriteServ(user->fd,"249 Brain :connections %d",statsConnects);
+               WriteServ(user->fd,"249 Brain :bytes sent %dK recv %dK",(statsSent / 1024),(statsRecv / 1024));
        }
        
        /* stats o */
@@ -1538,7 +1608,54 @@ void handle_squit(char **parameters, int pcnt, userrec *user)
        }
        else
        {
-               WriteServ(user->fd,"NOTICE :*** Remote SQUIT not supported yet.");
+               if (!strcasecmp("*",parameters[0]))
+               {
+                       WriteServ(user->fd,"NOTICE %s :*** You cannot issue an SQUIT this wide! If this is REALLY what you want, use a less wide mask.",user->nick);
+                       WriteOpers("*** WARNING! %s tried to SQUIT all servers at once!",user->nick);
+                       return;
+               }
+               if (!strcasecmp(ServerName,parameters[0]))
+               {
+                       WriteServ(user->fd,"NOTICE %s :*** To take the local server out of the mesh, just use /SQUIT with no parameters instead.",user->nick);
+                       return;
+               }
+               bool have_this_server = true;
+               int n_count = 0;
+               std::string server_to_squit = "";
+               while (have_this_server)
+               {
+                       have_this_server = false;
+                       for (int j = 0; j < 32; j++)
+                       {
+                               if (me[j] != NULL)
+                               {
+                                       for (int x = 0; x < me[j]->connectors.size(); x++)
+                                       {
+                                               if (match(me[j]->connectors[x].GetServerName().c_str(),parameters[0]))
+                                               {
+                                                       // found a valid ircd_connector.
+                                                       have_this_server = true;
+                                                       server_to_squit = me[j]->connectors[x].GetServerName().c_str();
+                                                       break;
+                                               }
+                                       }
+                                       }
+                               }
+                       if (have_this_server)
+                       {
+                               WriteOpers("SQUIT command issued by %s to remove %s from the mesh",user->nick,server_to_squit.c_str());
+                               WriteServ(user->fd,"NOTICE %s :*** Removing remote server %s.",user->nick,server_to_squit.c_str());
+                               char buffer[MAXBUF];
+                               snprintf(buffer,MAXBUF,"& %s",server_to_squit.c_str());
+                               NetSendToAll(buffer);
+                               DoSplit(server_to_squit.c_str());
+                               n_count++;
+                       }
+               }
+               if (!n_count)
+               {
+                       WriteServ(user->fd,"402 %s %s :Your pattern did not match any servers.",user->nick,parameters[0]);
+               }
        }
 }
 
@@ -1583,6 +1700,10 @@ void handle_map(char **parameters, int pcnt, userrec *user)
 
 bool is_uline(const char* server)
 {
+       if (!server)
+               return false;
+       if (!(*server))
+               return true;
        char ServName[MAXBUF];
        for (int i = 0; i < ConfValueEnum("uline",&config_f); i++)
        {
@@ -1797,29 +1918,67 @@ void handle_nick(char **parameters, int pcnt, userrec *user)
 }
 
 
+void handle_v(char token,char* params,serverrec* source,serverrec* reply, char* tcp_host)
+{
+       char* servername = strtok(params," ");
+       char* versionstr = strtok(NULL,"\r\n");
+
+        if ((!servername) || (!versionstr))
+                return;
+
+        for (int j = 0; j < 32; j++)
+        {
+                if (me[j] != NULL)
+                {
+                        for (int x = 0; x < me[j]->connectors.size(); x++)
+                        {
+                                if (!strcasecmp(me[j]->connectors[x].GetServerName().c_str(),servername))
+                                {
+                                       me[j]->connectors[x].SetVersionString(versionstr);
+                                       WriteOpers("Server '%s' (%d:%d) announces itself as version '%s'",me[j]->connectors[x].GetServerName().c_str(),j,x,me[j]->connectors[x].GetVersionString().c_str());
+                               }
+                       }
+               }
+       }
+}
 
 void handle_V(char token,char* params,serverrec* source,serverrec* reply, char* tcp_host)
 {
        char* src = strtok(params," ");
        char* dest = strtok(NULL," :");
        char* text = strtok(NULL,"\r\n");
+
+        if ((!src) || (!dest) || (!text))
+                return;
+
        text++;
        
        userrec* user = Find(src);
        if (user)
        {
-               userrec* dst = Find(dest);
-               
-               if (dst)
+               // notice all - only issuable by a server
+                if (!strcmp(dest,"*"))
+                {
+                        NoticeAll(user,true,"%s",text);
+                }
+               if (!strcmp(dest,"@*"))
                {
-                       WriteTo(user, dst, "NOTICE %s :%s", dst->nick, text);
+                       NoticeAllOpers(user,true,"%s",text);
                }
                else
                {
-                       chanrec* d = FindChan(dest);
-                       if (d)
+                       userrec* dst = Find(dest);      
+                       if (dst)
                        {
-                               ChanExceptSender(d, user, "NOTICE %s :%s", d->name, text);
+                               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);
+                               }
                        }
                }
        }
@@ -1832,6 +1991,10 @@ void handle_P(char token,char* params,serverrec* source,serverrec* reply, char*
        char* src = strtok(params," ");
        char* dest = strtok(NULL," :");
        char* text = strtok(NULL,"\r\n");
+
+        if ((!src) || (!dest) || (!text))
+                return;
+
        text++;
        
        userrec* user = Find(src);
@@ -1860,6 +2023,10 @@ void handle_i(char token,char* params,serverrec* source,serverrec* reply, char*
        char* nick = strtok(params," ");
        char* from = strtok(NULL," ");
        char* channel = strtok(NULL," ");
+
+        if ((!nick) || (!from) || (!channel))
+                return;
+
        userrec* u = Find(nick);
        userrec* user = Find(from);
        chanrec* c = FindChan(channel);
@@ -1875,6 +2042,10 @@ void handle_t(char token,char* params,serverrec* source,serverrec* reply, char*
        char* setby = strtok(params," ");
        char* channel = strtok(NULL," :");
        char* topic = strtok(NULL,"\r\n");
+
+        if ((!setby) || (!channel) || (!topic))
+                return;
+
        topic++;
        userrec* u = Find(setby);
        chanrec* c = FindChan(channel);
@@ -1894,6 +2065,10 @@ void handle_T(char token,char* params,serverrec* source,serverrec* reply, char*
        char* setby = strtok(NULL," ");
        char* channel = strtok(NULL," :");
        char* topic = strtok(NULL,"\r\n");
+
+        if ((!tm) || (!setby) || (!channel) || (!topic))
+                return;
+
        topic++;
        time_t TS = atoi(tm);
        chanrec* c = FindChan(channel);
@@ -1916,6 +2091,10 @@ void handle_M(char token,char* params,serverrec* source,serverrec* reply, char*
        strlcpy(original,params,MAXBUF);
        int index = 0;
        char* parameter = strtok(params," ");
+
+       if (!parameter)
+               return;
+
        strlcpy(target,parameter,MAXBUF);
        while (parameter)
        {
@@ -1924,15 +2103,17 @@ void handle_M(char token,char* params,serverrec* source,serverrec* reply, char*
                pars[index++] = parameter;
                parameter = strtok(NULL," ");
        }
-       log(DEBUG,"*** MODE: %s %s",pars[0],pars[1]);
+       log(DEBUG,"*** MODE: '%s %s'",pars[0],pars[1]);
        merge_mode(pars,index);
        if (FindChan(target))
        {
                WriteChannelLocal(FindChan(target), NULL, "MODE %s",original);
+               return;
        }
        if (Find(target))
        {
-               Write(Find(target)->fd,":%s MODE %s",ServerName,original);
+               if (original[0])
+                       Write(Find(target)->fd,":%s MODE %s",ServerName,original);
        }
 }
 
@@ -1943,6 +2124,10 @@ void handle_m(char token,char* params,serverrec* source,serverrec* reply, char*
        // m blah #chatspike +b *!test@*4
        char* pars[128];
        char original[MAXBUF];
+
+        if (!params)
+                return;
+
        strlcpy(original,params,MAXBUF);
        
        if (!strchr(params,' '))
@@ -1954,6 +2139,10 @@ void handle_m(char token,char* params,serverrec* source,serverrec* reply, char*
        int index = 0;
        
        char* src = strtok(params," ");
+
+        if (!src)
+                return;
+
        userrec* user = Find(src);
        
        if (user)
@@ -1967,7 +2156,8 @@ void handle_m(char token,char* params,serverrec* source,serverrec* reply, char*
                }
                
                log(DEBUG,"Calling merge_mode2");
-               merge_mode2(pars,index,user);
+               if (index)
+                       merge_mode2(pars,index,user);
        }
 }
 
@@ -1982,6 +2172,8 @@ void handle_L(char token,char* params,serverrec* source,serverrec* reply, char*
        {
                nick = strtok(params," ");
                channel = strtok(NULL,"\r\n");
+               if ((!nick) || (!channel))
+                       return;
                channel++;
                reason = "";
        }
@@ -1990,6 +2182,8 @@ void handle_L(char token,char* params,serverrec* source,serverrec* reply, char*
                 nick = strtok(params," ");
                 channel = strtok(NULL," :");
                 reason = strtok(NULL,"\r\n");
+               if ((!nick) || (!channel) || (!reason))
+                       return;
                 reason++;
         }
        userrec* user = Find(nick);
@@ -2011,6 +2205,8 @@ void handle_K(char token,char* params,serverrec* source,serverrec* reply, char*
        char* src = strtok(params," ");
        char* nick = strtok(NULL," :");
        char* reason = strtok(NULL,"\r\n");
+       if ((!src) || (!nick) || (!reason))
+               return;
        char kreason[MAXBUF];
        reason++;
 
@@ -2019,9 +2215,9 @@ void handle_K(char token,char* params,serverrec* source,serverrec* reply, char*
        
        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);
+               WriteTo(user, u, "KILL %s :%s!%s!%s!%s (%s)", u->nick, tcp_host, ServerName, user->dhost,user->nick,reason);
+               WriteOpers("*** Remote kill from %s by %s: %s!%s@%s (%s)",tcp_host,user->nick,u->nick,u->ident,u->host,reason);
+               snprintf(kreason,MAXBUF,"[%s] Killed (From: '%s' (%s))",tcp_host,user->nick,reason);
                kill_link(u,kreason);
        }
 }
@@ -2030,6 +2226,9 @@ void handle_Q(char token,char* params,serverrec* source,serverrec* reply, char*
 {
        char* nick = strtok(params," :");
        char* reason = strtok(NULL,"\r\n");
+
+       if ((!nick) || (!reason))
+               return;
        reason++;
 
        userrec* user = Find(nick);
@@ -2064,6 +2263,9 @@ void handle_n(char token,char* params,serverrec* source,serverrec* reply, char*
 {
        char* oldnick = strtok(params," ");
        char* newnick = strtok(NULL," ");
+
+       if ((!oldnick) || (!newnick))
+               return;
        
        userrec* user = Find(oldnick);
 
@@ -2075,11 +2277,13 @@ void handle_n(char token,char* params,serverrec* source,serverrec* reply, char*
                        FOREACH_RESULT(OnUserPreNick(user,newnick));
                        if (MOD_RESULT) {
                                // if a module returns true, the nick change couldnt be allowed
+                               statsCollisions++;
                                kill_link(user,"Nickname collision");
                                return;
                        }
                        if (matches_qline(newnick))
                        {
+                               statsCollisions++;
                                kill_link(user,"Nickname collision");
                                return;
                        }
@@ -2105,6 +2309,10 @@ void handle_k(char token,char* params,serverrec* source,serverrec* reply, char*
        char* dest = strtok(NULL," ");
        char* channel = strtok(NULL," :");
        char* reason = strtok(NULL,"\r\n");
+
+       if ((!src) || (!dest) || (!channel) || (!reason))
+               return;
+
        reason++;
        userrec* s = Find(src);
        userrec* d = Find(dest);
@@ -2127,6 +2335,10 @@ void handle_AT(char token,char* params,serverrec* source,serverrec* reply, char*
 {
        char* who = strtok(params," :");
        char* text = strtok(NULL,"\r\n");
+
+       if ((!who) || (!text))
+               return;
+
        text++;
        userrec* s = Find(who);
        if (s)
@@ -2140,6 +2352,10 @@ void handle_H(char token,char* params,serverrec* source,serverrec* reply, char*
        log(DEBUG,"Adding ULined server %s to my map",params);
        ircd_connector s;
        s.SetState(STATE_DISCONNECTED);
+
+       if (!params)
+               return;
+
        s.SetServerName(params);
 
        for (int j = 0; j < 32; j++)
@@ -2173,13 +2389,22 @@ void handle_N(char token,char* params,serverrec* source,serverrec* reply, char*
        char* ipaddr = strtok(NULL," ");
        char* server = strtok(NULL," :");
        char* gecos = strtok(NULL,"\r\n");
-       gecos++;
-       modes++;
+
+       if ((!tm) || (!nick) || (!host) || (!dhost) || (!ident) || (!modes) || (!ipaddr) || (!server) || (!gecos))
+               return;
+
+       if (*gecos == ':')
+               gecos++;
+
+       if (*modes == '+')
+               modes++;
+
        time_t TS = atoi(tm);
        user_hash::iterator iter = clientlist.find(nick);
        if (iter != clientlist.end())
        {
                // nick collision
+               statsCollisions++;
                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);
@@ -2197,7 +2422,7 @@ void handle_N(char token,char* params,serverrec* source,serverrec* reply, char*
        strlcpy(clientlist[nick]->server, server,256);
        strlcpy(clientlist[nick]->ident, ident,10); // +1 char to compensate for tilde
        strlcpy(clientlist[nick]->fullname, gecos,128);
-       strlcpy(clientlist[nick]->ip,ipaddr,32);
+       strlcpy(clientlist[nick]->ip,ipaddr,16);
        clientlist[nick]->signon = TS;
        clientlist[nick]->nping = 0; // this is ignored for a remote user anyway.
        clientlist[nick]->lastping = 1;
@@ -2218,6 +2443,8 @@ void handle_N(char token,char* params,serverrec* source,serverrec* reply, char*
 
 void handle_F(char token,char* params,serverrec* source,serverrec* reply, char* tcp_host)
 {
+       if (!params)
+               return;
        long tdiff = TIME - atoi(params);
        if (tdiff)
                WriteOpers("TS split for %s -> %s: %d",source->name,reply->name,tdiff);
@@ -2227,6 +2454,9 @@ void handle_a(char token,char* params,serverrec* source,serverrec* reply, char*
 {
        char* nick = strtok(params," :");
        char* gecos = strtok(NULL,"\r\n");
+
+       if ((!nick) || (!gecos))
+               return;
        
        userrec* user = Find(nick);
 
@@ -2238,6 +2468,9 @@ void handle_b(char token,char* params,serverrec* source,serverrec* reply, char*
 {
        char* nick = strtok(params," ");
        char* host = strtok(NULL," ");
+
+       if ((!nick) || (!host))
+               return;
        
        userrec* user = Find(nick);
 
@@ -2253,6 +2486,10 @@ void handle_plus(char token,char* params,serverrec* source,serverrec* reply, cha
        char* ipaddr = strtok(NULL," ");
        char* ipport = strtok(NULL," ");
        char* cookie = strtok(NULL," ");
+
+       if ((!servername) || (!ipaddr) || (!ipport) || (!cookie))
+               return;
+
        log(DEBUG,"*** Connecting back to %s:%d",ipaddr,atoi(ipport));
 
 
@@ -2282,6 +2519,7 @@ void handle_R(char token,char* params,serverrec* source,serverrec* reply, char*
 {
        char* server = strtok(params," ");
        char* data = strtok(NULL,"\r\n");
+
        if ((!data) || (!server))
        {
                log(DEBUG,"Someones playing silly buggers, attempting to send to a null server or send a null message (BUG?)");
@@ -2299,7 +2537,12 @@ void handle_J(char token,char* params,serverrec* source,serverrec* reply, char*
        // because it will allow splitriding.
        char* nick = strtok(params," ");
        char* channel = strtok(NULL," ");
+
+       if ((!nick) || (!channel))
+               return;
+
        userrec* user = Find(nick);
+
        while (channel)
        {
                if ((user != NULL) && (strcmp(channel,"")))
@@ -2349,6 +2592,10 @@ void handle_dollar(char token,char* params,serverrec* source,serverrec* reply, c
        log(DEBUG,"Storing routing table...");
        char* sourceserver = strtok(params," ");
        char* server = strtok(NULL," ");
+
+       if ((!sourceserver) || (!server))
+               return;
+
        for (int i = 0; i < 32; i++)
        {
                if (me[i] != NULL)
@@ -2376,6 +2623,9 @@ void handle_dollar(char token,char* params,serverrec* source,serverrec* reply, c
 
 void handle_amp(char token,char* params,serverrec* source,serverrec* reply, char* tcp_host)
 {
+       if (!params)
+               return;
+
        log(DEBUG,"Netsplit! %s split from mesh, removing!",params);
        WriteOpers("*** NOTICE - Controlled netsplit: %s split from %s",params,ServerName);
        bool go_again = true;
@@ -2433,6 +2683,10 @@ void handle_hash(char token,char* params,serverrec* source,serverrec* reply, cha
        char* create_time = strtok(NULL," ");
        char* duration = strtok(NULL," :");
        char* reason = strtok(NULL,"\r\n");
+
+       if ((!mask) || (!who) || (!create_time) || (!duration) || (!reason))
+               return;
+
        reason++;
        add_gline(atoi(duration),(const char*)who,(const char*)reason,(const char*)mask);
        // we must update the creation time on this gline
@@ -2454,6 +2708,10 @@ void handle_dot(char token,char* params,serverrec* source,serverrec* reply, char
        log(DEBUG,"Removing G-line");
        char* mask = strtok(params," ");
        char* who = strtok(NULL," ");
+
+       if ((!mask) || (!who))
+               return;
+
        if (mask)
        {
                if (del_gline((const char*)mask))
@@ -2475,6 +2733,10 @@ void handle_add_sqline(char token,char* params,serverrec* source,serverrec* repl
        char* create_time = strtok(NULL," ");
        char* duration = strtok(NULL," :");
        char* reason = strtok(NULL,"\r\n");
+
+        if ((!mask) || (!who) || (!create_time) || (!duration) || (!reason))
+                return;
+
        reason++;
        add_qline(atoi(duration),(const char*)who,(const char*)reason,(const char*)mask);
        // we must update the creation time on this gline
@@ -2497,6 +2759,10 @@ void handle_del_sqline(char token,char* params,serverrec* source,serverrec* repl
        log(DEBUG,"Removing Q-line");
        char* mask = strtok(params," ");
        char* who = strtok(NULL," ");
+
+        if ((!mask) || (!who))
+                return;
+
        if (mask)
        {
                if (del_qline((const char*)mask))
@@ -2518,6 +2784,10 @@ void handle_add_szline(char token,char* params,serverrec* source,serverrec* repl
        char* create_time = strtok(NULL," ");
        char* duration = strtok(NULL," :");
        char* reason = strtok(NULL,"\r\n");
+
+        if ((!mask) || (!who) || (!create_time) || (!duration) || (!reason))
+                return;
+
        reason++;
        add_zline(atoi(duration),(const char*)who,(const char*)reason,(const char*)mask);
        // we must update the creation time on this gline
@@ -2540,6 +2810,10 @@ void handle_del_szline(char token,char* params,serverrec* source,serverrec* repl
        log(DEBUG,"Removing Z-line");
        char* mask = strtok(params," ");
        char* who = strtok(NULL," ");
+
+        if ((!mask) || (!who))
+                return;
+
        if (mask)
        {
                if (del_zline((const char*)mask))
@@ -2556,6 +2830,10 @@ void handle_pipe(char token,char* params,serverrec* source,serverrec* reply, cha
 {
        char* nick = strtok(params," ");
        char* type = strtok(params," ");
+
+       if ((!nick) || (!type))
+               return;
+
        userrec* u = Find(nick);
        if (u)
        {
@@ -2622,6 +2900,9 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                // no operation
                case '*':
                break;
+               // no operation - reserved to prevent services issues
+               case ':':
+               break;
                // N <TS> <NICK> <HOST> <DHOST> <IDENT> <MODES> <SERVER> :<GECOS>
                // introduce remote client
                case 'N':
@@ -2689,6 +2970,10 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
                case 'V':
                        handle_V(token,params,source,reply,tcp_host);
                break;
+               // v <servername> <arbitary version string>
+               case 'v':
+                       handle_v(token,params,source,reply,tcp_host);
+               break;
                // L <SOURCE> <CHANNEL> :<REASON>
                // User parting a channel
                case 'L':
@@ -2817,9 +3102,9 @@ void process_restricted_commands(char token,char* params,serverrec* source,serve
 }
 
 
-void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
+void handle_link_packet(char* tcp_msg, char* tcp_host, serverrec *serv)
 {
-       if ((!strncmp(udp_msg,"USER ",5)) || (!strncmp(udp_msg,"NICK ",5)) || (!strncmp(udp_msg,"PASS ",5)) || (!strncmp(udp_msg,"SERVER ",7)))
+       if ((!strncmp(tcp_msg,"USER ",5)) || (!strncmp(tcp_msg,"NICK ",5)) || (!strncmp(tcp_msg,"PASS ",5)) || (!strncmp(tcp_msg,"SERVER ",7)))
        {
                // a user on a server port, just close their connection.
                RemoveServer(tcp_host);
@@ -2827,33 +3112,39 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
        }
 
        char response[10240];
-       char token = udp_msg[0];
-       char* old = udp_msg;
+       char token = tcp_msg[0];
+       char* old = tcp_msg;
+
+       if (!strncmp(tcp_msg,"PING",4))
+       {
+               // some muppet of a server is sending PING. We don't know what PING is. drop it silently.
+               return;
+       }
 
-       if ((token != ':') && (strlen(udp_msg)>1) && (udp_msg[1] != ' '))
+       if ((token != ':') && (strlen(tcp_msg)>1) && (tcp_msg[1] != ' '))
        {
-               WriteOpers("*** Discarded %d chars illegal data from %s",strlen(udp_msg),tcp_host);
+               WriteOpers("*** Discarded %d chars illegal data from %s",strlen(tcp_msg),tcp_host);
        }
 
        if (token == ':') // leading :servername or details - strip them off (services does this, sucky)
        {
-               char* src = udp_msg+1;
-               while (udp_msg[0] != ' ')
-                       udp_msg++;
-               udp_msg[0] = 0;
-               udp_msg++;
-               char* comd = udp_msg;
-               while (udp_msg[0] != ' ')
-                       udp_msg++;
-               udp_msg[0] = 0;
-               udp_msg++;
+               char* src = tcp_msg+1;
+               while (tcp_msg[0] != ' ')
+                       tcp_msg++;
+               tcp_msg[0] = 0;
+               tcp_msg++;
+               char* comd = tcp_msg;
+               while (tcp_msg[0] != ' ')
+                       tcp_msg++;
+               tcp_msg[0] = 0;
+               tcp_msg++;
                char data[MAXBUF];
                char source[MAXBUF];
                char command[MAXBUF];
-               strlcpy(data,udp_msg,512);
+               strlcpy(data,tcp_msg,512);
                strlcpy(source,src,MAXBUF);
                strlcpy(command,comd,MAXBUF);
-               udp_msg = old;
+               tcp_msg = old;
                
                // unused numeric:
                // :services-dev.chatspike.net 433 Craig Craig :Nickname is registered to someone else
@@ -2871,13 +3162,13 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                }
                if (!strcmp(command,"NOTICE"))
                {
-                       snprintf(udp_msg,MAXBUF,"V %s %s",source,data);
-                       log(DEBUG,"Rewrote NOTICE from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"V %s %s",source,data);
+                       log(DEBUG,"Rewrote NOTICE from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                if (!strcmp(command,"QUIT"))
                {
-                       if ((!udp_msg) || (!strcmp(data,"")) || (strcmp(data,":")))
+                       if ((!tcp_msg) || (!strcmp(data,"")) || (strcmp(data,":")))
                        {
                                strcpy(data,":No reason");
                        }
@@ -2885,70 +3176,70 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                        {
                                strcpy(data,":No reason");
                        }
-                       snprintf(udp_msg,MAXBUF,"Q %s %s",source,data);
-                       log(DEBUG,"Rewrote QUIT from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"Q %s %s",source,data);
+                       log(DEBUG,"Rewrote QUIT from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                if (!strcmp(command,"SQUIT"))
                {
-                       snprintf(udp_msg,MAXBUF,"& %s",source);
-                       log(DEBUG,"Rewrote SQUIT from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"& %s",source);
+                       log(DEBUG,"Rewrote SQUIT from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                if (!strcmp(command,"SVSMODE"))
                {
-                       snprintf(udp_msg,MAXBUF,"m %s %s",source,data);
-                       log(DEBUG,"Rewrote SVSMODE from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"m %s %s",source,data);
+                       log(DEBUG,"Rewrote SVSMODE from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                if (!strcmp(command,"SVS2MODE"))
                {
-                       snprintf(udp_msg,MAXBUF,"m %s %s",source,data);
-                       log(DEBUG,"Rewrote SVS2MODE from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"m %s %s",source,data);
+                       log(DEBUG,"Rewrote SVS2MODE from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                // todo: this wont work without u:lines
                // in give_ops etc allow nick on a u:lined serv to do just about anything
                if (!strcmp(command,"MODE"))
                {
-                       snprintf(udp_msg,MAXBUF,"m %s %s",source,data);
-                       log(DEBUG,"Rewrote MODE from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"m %s %s",source,data);
+                       log(DEBUG,"Rewrote MODE from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                if (!strcmp(command,"KICK"))
                {
-                       snprintf(udp_msg,MAXBUF,"k %s %s",source,data);
-                       log(DEBUG,"Rewrote KICK from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"k %s %s",source,data);
+                       log(DEBUG,"Rewrote KICK from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                if (!strcmp(command,"KILL"))
                {
-                       snprintf(udp_msg,MAXBUF,"K %s %s",source,data);
-                       log(DEBUG,"Rewrote KILL from services to: '%s'",udp_msg);
-                       token = udp_msg[0];
+                       snprintf(tcp_msg,MAXBUF,"K %s %s",source,data);
+                       log(DEBUG,"Rewrote KILL from services to: '%s'",tcp_msg);
+                       token = tcp_msg[0];
                }
                if (!strcmp(command,"SVSJOIN"))
                {
-                       snprintf(udp_msg,MAXBUF,"J %s",data);
-                       NetSendToOne(tcp_host,udp_msg);
+                       snprintf(tcp_msg,MAXBUF,"J %s",data);
+                       NetSendToOne(tcp_host,tcp_msg);
                        char* nick = strtok(data," ");
                        char* chan = strtok(NULL," ");
-                       log(DEBUG,"Rewrote SVSJOIN from services to: '%s'",udp_msg);
+                       log(DEBUG,"Rewrote SVSJOIN from services to: '%s'",tcp_msg);
                        userrec* u = Find(nick);
                        if (u)
                        {
                                add_channel(u,chan,"",true);
                        }
-                       token = udp_msg[0];
+                       token = tcp_msg[0];
                }
                
        }
 
-       char* params = udp_msg + 2;
+       char* params = tcp_msg + 2;
        char finalparam[1024];
        strcpy(finalparam," :xxxx");
-       if (strstr(udp_msg," :")) {
-               strlcpy(finalparam,strstr(udp_msg," :"),1024);
+       if (strstr(tcp_msg," :")) {
+               strlcpy(finalparam,strstr(tcp_msg," :"),1024);
        }
        
        
@@ -2957,6 +3248,10 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                char* servername = strtok(NULL," ");
                char* serverdesc = finalparam+2;
 
+               if ((!cookie) || (!servername) || (!serverdesc))
+                       return;
+
+
                WriteOpers("AuthCookie CONNECT from %s (%s)",servername,tcp_host);
 
                for (int u = 0; u < auth_cookies.size(); u++)
@@ -3003,7 +3298,8 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                char* revision = strtok(NULL," ");
                char* serverdesc = finalparam+2;
 
-               password = password;
+                if ((!servername) || (!password) || (!myport) || (!revision) || (!serverdesc))
+                        return;
 
                WriteOpers("CONNECT from %s (%s) (their port: %d)",servername,tcp_host,atoi(myport));
                
@@ -3023,7 +3319,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                {
                        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");
+                       snprintf(buffer,MAXBUF,"E :Version number mismatch");
                        serv->SendPacket(buffer,tcp_host);
                        RemoveServer(tcp_host);
                        RemoveServer(servername);
@@ -3077,7 +3373,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                        }
                }
                char buffer[MAXBUF];
-               sprintf(buffer,"E :Access is denied (no matching link block)");
+               snprintf(buffer,MAXBUF,"E :Access is denied (no matching link block)");
                serv->SendPacket(buffer,tcp_host);
                WriteOpers("CONNECT from %s denied, no matching link block",servername);
                RemoveServer(tcp_host);
@@ -3091,8 +3387,9 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                char* password = strtok(NULL," ");
                char* serverdesc = finalparam+2;
 
-               password = password;
-               
+               if ((!servername) || (!password) || (!serverdesc))
+                       return;
+
                // 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
@@ -3134,7 +3431,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                                                                char buffer[MAXBUF];
                                                                me[j]->connectors[k].SetDescription(serverdesc);
                                                                me[j]->connectors[k].SetState(STATE_CONNECTED);
-                                                               sprintf(buffer,"X 0");
+                                                               snprintf(buffer,MAXBUF,"X 0");
                                                                serv->SendPacket(buffer,tcp_host);
                                                                DoSync(me[j],tcp_host);
                                                                NetSendMyRoutingTable();
@@ -3149,7 +3446,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                        }
                }
                char buffer[MAXBUF];
-               sprintf(buffer,"E :Access is denied (no matching link block)");
+               snprintf(buffer,MAXBUF,"E :Access is denied (no matching link block)");
                serv->SendPacket(buffer,tcp_host);
                WriteOpers("CONNECT from %s denied, no matching link block",servername);
                RemoveServer(tcp_host);
@@ -3164,6 +3461,9 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                char* servername = strtok(params," ");
                char* password = strtok(NULL," ");
                char* serverdesc = finalparam+2;
+
+                if ((!password) || (!servername) || (!serverdesc))
+                        return;
                
                char Link_ServerName[1024];
                char Link_IPAddr[1024];
@@ -3184,7 +3484,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                                         if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),servername))
                                         {
                                                char buffer[MAXBUF];
-                                               sprintf(buffer,"E :Access is denied (Server exists in the mesh)");
+                                               snprintf(buffer,MAXBUF,"E :Access is denied (Server exists in the mesh)");
                                                serv->SendPacket(buffer,tcp_host);
                                                WriteOpers("CONNECT from %s denied, \"%s\" already exists!",tcp_host,servername);
                                                RemoveServer(tcp_host);
@@ -3222,9 +3522,9 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                                                                me[j]->connectors[k].SetDescription(serverdesc);
                                                                me[j]->connectors[k].SetServerName(servername);
                                                                me[j]->connectors[k].SetState(STATE_SERVICES);
-                                                               sprintf(buffer,"X 0");
+                                                               snprintf(buffer,MAXBUF,"X 0");
                                                                serv->SendPacket(buffer,servername);
-                                                               sprintf(buffer,"s %s %s %lu :%s",ServerName,Link_SendPass,LinkPort,ServerDesc);
+                                                               snprintf(buffer,MAXBUF,"s %s %s %lu :%s",ServerName,Link_SendPass,LinkPort,ServerDesc);
                                                                serv->SendPacket(buffer,servername);
                                                                DoSync(me[j],servername);
                                                                snprintf(buffer,MAXBUF,"H %s",servername);
@@ -3244,7 +3544,7 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                }
                log(DEBUG,"No matching link block found");
                char buffer[MAXBUF];
-               sprintf(buffer,"E :Access is denied (no matching link block)");
+               snprintf(buffer,MAXBUF,"E :Access is denied (no matching link block)");
                serv->SendPacket(buffer,tcp_host);
                WriteOpers("CONNECT from %s denied, no matching link block",servername);
                RemoveServer(tcp_host);
@@ -3254,6 +3554,10 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
        else
        if (token == 'E') {
                char* error_message = finalparam+2;
+
+               if (!error_message)
+                       return;
+
                WriteOpers("ERROR from %s: %s",tcp_host,error_message);
                return;
        }
@@ -3270,7 +3574,8 @@ void handle_link_packet(char* udp_msg, char* tcp_host, serverrec *serv)
                                                if ((me[j]->connectors[x].GetState() == STATE_CONNECTED) || (me[j]->connectors[x].GetState() == STATE_SERVICES))
                                                {
                                                        // found a valid ircd_connector.
-                                                       process_restricted_commands(token,params,me[j],serv,tcp_host,me[j]->connectors[x].GetServerIP(),me[j]->connectors[x].GetServerPort());
+                                                       if ((params) && (*params))
+                                                               process_restricted_commands(token,params,me[j],serv,tcp_host,me[j]->connectors[x].GetServerIP(),me[j]->connectors[x].GetServerPort());
                                                        return;
                                                }
                                        }