]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree.cpp
Fix test client error cheecking on result types
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
index 71ae7de52f9954ae1d73996ee69297fa292138f8..22c9e92461acb262156cff05da6f2b3806363209 100644 (file)
@@ -23,15 +23,13 @@ using namespace std;
 #include <deque>
 #include "globals.h"
 #include "inspircd_config.h"
-#ifdef GCC3
-#include <ext/hash_map>
-#else
-#include <hash_map>
-#endif
+#include "hash_map.h"
+#include "configreader.h"
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
 #include "commands.h"
+#include "commands/cmd_whois.h"
 #include "socket.h"
 #include "helperfuncs.h"
 #include "inspircd.h"
@@ -43,11 +41,7 @@ using namespace std;
 #include "cull_list.h"
 #include "aes.h"
 
-#ifdef GCC3
 #define nspace __gnu_cxx
-#else
-#define nspace std
-#endif
 
 /*
  * The server list in InspIRCd is maintained as two structures
@@ -156,7 +150,7 @@ extern std::vector<ELine> pelines;
  * are created and destroyed.
  */
 
-class TreeServer
+class TreeServer : public classbase
 {
        TreeServer* Parent;                     /* Parent entry */
        TreeServer* Route;                      /* Route entry */
@@ -169,7 +163,6 @@ class TreeServer
        TreeSocket* Socket;                     /* For directly connected servers this points at the socket object */
        time_t NextPing;                        /* After this time, the server should be PINGed*/
        bool LastPingWasGood;                   /* True if the server responded to the last PING with a PONG */
-       std::map<userrec*,userrec*> Users;      /* Users on this server */
        
  public:
 
@@ -266,36 +259,26 @@ class TreeServer
                this->AddHashEntry();
        }
 
-       void AddUser(userrec* user)
-       {
-               log(DEBUG,"Add user %s to server %s",user->nick,this->ServerName.c_str());
-               std::map<userrec*,userrec*>::iterator iter;
-               iter = Users.find(user);
-               if (iter == Users.end())
-                       Users[user] = user;
-       }
-
-       void DelUser(userrec* user)
-       {
-               log(DEBUG,"Remove user %s from server %s",user->nick,this->ServerName.c_str());
-               std::map<userrec*,userrec*>::iterator iter;
-               iter = Users.find(user);
-               if (iter != Users.end())
-                       Users.erase(iter);
-       }
-
        int QuitUsers(const std::string &reason)
        {
-               int x = Users.size();
                log(DEBUG,"Removing all users from server %s",this->ServerName.c_str());
                const char* reason_s = reason.c_str();
-               for (std::map<userrec*,userrec*>::iterator n = Users.begin(); n != Users.end(); n++)
+               std::vector<userrec*> time_to_die;
+               for (user_hash::iterator n = clientlist.begin(); n != clientlist.end(); n++)
+               {
+                       if (!strcmp(n->second->server, this->ServerName.c_str()))
+                       {
+                               time_to_die.push_back(n->second);
+                       }
+               }
+               for (std::vector<userrec*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
                {
-                       log(DEBUG,"Kill %s",n->second->nick);
-                       kill_link(n->second,reason_s);
+                       userrec* a = (userrec*)*n;
+                       log(DEBUG,"Kill %s fd=%d",a->nick,a->fd);
+                       if (!IS_LOCAL(a))
+                               kill_link(a,reason_s);
                }
-               Users.clear();
-               return x;
+               return time_to_die.size();
        }
 
        /* This method is used to add the structure to the
@@ -457,7 +440,7 @@ class TreeServer
                                TreeServer* s = (TreeServer*)*a;
                                s->Tidy();
                                Children.erase(a);
-                               delete s;
+                               DELETE(s);
                                stillchildren = true;
                                break;
                        }
@@ -479,7 +462,7 @@ class TreeServer
  * of them, and populate the list on rehash/load.
  */
 
-class Link
+class Link : public classbase
 {
  public:
         irc::string Name;
@@ -572,9 +555,9 @@ class cmd_rconnect : public command_t
        cmd_rconnect (Module* Callback) : command_t("RCONNECT", 'o', 2), Creator(Callback)
        {
                this->source = "m_spanningtree.so";
-       }                
+       }
 
-       void Handle (char **parameters, int pcnt, userrec *user)
+       void Handle (const char** parameters, int pcnt, userrec *user)
        {
                WriteServ(user->fd,"NOTICE %s :*** RCONNECT: Sending remote connect to \002%s\002 to connect server \002%s\002.",user->nick,parameters[0],parameters[1]);
                /* Is this aimed at our server? */
@@ -582,7 +565,7 @@ class cmd_rconnect : public command_t
                {
                        /* Yes, initiate the given connect */
                        WriteOpers("*** Remote CONNECT from %s matching \002%s\002, connecting server \002%s\002",user->nick,parameters[0],parameters[1]);
-                       char* para[1];
+                       const char* para[1];
                        para[0] = parameters[1];
                        Creator->OnPreCommand("CONNECT", para, 1, user, true);
                }
@@ -660,9 +643,9 @@ class TreeSocket : public InspSocket
        ~TreeSocket()
        {
                if (ctx_in)
-                       delete ctx_in;
+                       DELETE(ctx_in);
                if (ctx_out)
-                       delete ctx_out;
+                       DELETE(ctx_out);
        }
 
        void InitAES(std::string key,std::string SName)
@@ -740,6 +723,10 @@ class TreeSocket : public InspSocket
                 * dirty work is done in OnClose() (see below)
                 * which is still called on error conditions too.
                 */
+               if (e == I_ERR_CONNECT)
+               {
+                       Srv->SendOpers("*** Connection failed: Connection refused");
+               }
        }
 
        virtual int OnDisconnect()
@@ -885,7 +872,7 @@ class TreeSocket : public InspSocket
                        SquitServer(from, Current);
                        Current->Tidy();
                        Current->GetParent()->DelChild(Current);
-                       delete Current;
+                       DELETE(Current);
                        WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
                }
                else
@@ -901,7 +888,7 @@ class TreeSocket : public InspSocket
                        return true;
                userrec* who = new userrec();
                who->fd = FD_MAGIC_NUMBER;
-               char* modelist[64];
+               const char* modelist[64];
                memset(&modelist,0,sizeof(modelist));
                for (unsigned int q = 0; q < params.size(); q++)
                {
@@ -909,7 +896,7 @@ class TreeSocket : public InspSocket
                }
                Srv->SendMode(modelist,params.size(),who);
                DoOneToAllButSender(source,"FMODE",params,source);
-               delete who;
+               DELETE(who);
                return true;
        }
 
@@ -939,7 +926,7 @@ class TreeSocket : public InspSocket
                                        userrec* user = Srv->FindNick(source);
                                        if (!user)
                                        {
-                                               WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
+                                               WriteChannelWithServ(source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
                                        }
                                        else
                                        {
@@ -969,7 +956,7 @@ class TreeSocket : public InspSocket
                memset(&mode_users,0,sizeof(mode_users));
                mode_users[0] = first;
                mode_users[1] = modestring;
-               strcpy(mode_users[1],"+");
+               strcpy(first,"+");
                unsigned int modectr = 2;
                
                userrec* who = NULL;
@@ -1041,7 +1028,7 @@ class TreeSocket : public InspSocket
                                                {
                                                        /* We also always let u-lined clients win, no matter what the TS value */
                                                        log(DEBUG,"Our our channel newer than theirs, accepting their modes");
-                                                       Srv->SendMode(mode_users,modectr,who);
+                                                       Srv->SendMode((const char**)mode_users,modectr,who);
                                                }
                                                else
                                                {
@@ -1071,7 +1058,7 @@ class TreeSocket : public InspSocket
                        if (ourTS >= TS)
                        {
                                log(DEBUG,"Our our channel newer than theirs, accepting their modes");
-                               Srv->SendMode(mode_users,modectr,who);
+                               Srv->SendMode((const char**)mode_users,modectr,who);
                        }
                        else
                        {
@@ -1088,6 +1075,26 @@ class TreeSocket : public InspSocket
                return true;
        }
 
+       bool SyncChannelTS(std::string source, std::deque<std::string> &params)
+       {
+               if (params.size() == 2)
+               {
+                       chanrec* c = Srv->FindChannel(params[0]);
+                       if (c)
+                       {
+                               time_t theirTS = atoi(params[1].c_str());
+                               time_t ourTS = c->age;
+                               if (ourTS >= theirTS)
+                               {
+                                       log(DEBUG,"Updating timestamp for %s, our timestamp was %lu and theirs is %lu",c->name,ourTS,theirTS);
+                                       c->age = theirTS;
+                               }
+                       }
+               }
+               DoOneToAllButSender(Srv->GetServerName(),"SYNCTS",params,source);
+               return true;
+       }
+
        /* NICK command */
        bool IntroduceClient(std::string source, std::deque<std::string> &params)
        {
@@ -1101,18 +1108,17 @@ class TreeSocket : public InspSocket
                // NICK age nick host dhost ident +modes ip :gecos
                //   0   123  4 56   7
                time_t age = atoi(params[0].c_str());
-               std::string modes = params[5];
-               while (*(modes.c_str()) == '+')
-               {
-                       char* m = (char*)modes.c_str();
-                       m++;
-                       modes = m;
-               }
-               char* tempnick = (char*)params[1].c_str();
+               
+               /* This used to have a pretty craq'y loop doing the same thing,
+                * now we just let the STL do the hard work (more efficiently)
+                */
+               params[5] = params[5].substr(params[5].find_first_not_of('+'));
+               
+               const char* tempnick = params[1].c_str();
                log(DEBUG,"Introduce client %s!%s@%s",tempnick,params[4].c_str(),params[2].c_str());
                
-               user_hash::iterator iter;
-               iter = clientlist.find(tempnick);
+               user_hash::iterator iter = clientlist.find(tempnick);
+               
                if (iter != clientlist.end())
                {
                        // nick collision
@@ -1126,32 +1132,19 @@ class TreeSocket : public InspSocket
                strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX-1);
                strlcpy(clientlist[tempnick]->host, params[2].c_str(),63);
                strlcpy(clientlist[tempnick]->dhost, params[3].c_str(),63);
-               clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
+               clientlist[tempnick]->server = FindServerNamePtr(source.c_str());
                strlcpy(clientlist[tempnick]->ident, params[4].c_str(),IDENTMAX);
                strlcpy(clientlist[tempnick]->fullname, params[7].c_str(),MAXGECOS);
                clientlist[tempnick]->registered = 7;
                clientlist[tempnick]->signon = age;
-               strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
-               for (char *v = clientlist[tempnick]->modes; *v; v++)
+               
+               for (std::string::iterator v = params[5].begin(); v != params[5].end(); v++)
                {
-                       switch (*v)
-                       {
-                               case 'i':
-                                       clientlist[tempnick]->modebits |= UM_INVISIBLE;
-                               break;
-                               case 'w':
-                                       clientlist[tempnick]->modebits |= UM_WALLOPS;
-                               break;
-                               case 's':
-                                       clientlist[tempnick]->modebits |= UM_SERVERNOTICE;
-                               break;
-                               default:
-                               break;
-                       }
+                       clientlist[tempnick]->modes[(*v)-65] = 1;
                }
                inet_aton(params[6].c_str(),&clientlist[tempnick]->ip4);
 
-               WriteOpers("*** Client connecting at %s: %s!%s@%s [%s]",clientlist[tempnick]->server,clientlist[tempnick]->nick,clientlist[tempnick]->ident,clientlist[tempnick]->host,(char*)inet_ntoa(clientlist[tempnick]->ip4));
+               WriteOpers("*** Client connecting at %s: %s!%s@%s [%s]",clientlist[tempnick]->server,clientlist[tempnick]->nick,clientlist[tempnick]->ident,clientlist[tempnick]->host, inet_ntoa(clientlist[tempnick]->ip4));
 
                params[7] = ":" + params[7];
                DoOneToAllButSender(source,"NICK",params,source);
@@ -1160,7 +1153,7 @@ class TreeSocket : public InspSocket
                TreeServer* SourceServer = FindServer(source);
                if (SourceServer)
                {
-                       SourceServer->AddUser(clientlist[tempnick]);
+                       log(DEBUG,"Found source server of %s",clientlist[tempnick]->nick);
                        SourceServer->AddUserCount();
                }
 
@@ -1248,6 +1241,7 @@ class TreeSocket : public InspSocket
                                this->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" +h "+specific_halfop[y]->nick);
                        }
                }
+               this->WriteLine(":"+Srv->GetServerName()+" SYNCTS "+c->name+" "+ConvToStr(c->age));
        }
 
        /* Send G, Q, Z and E lines */
@@ -1262,73 +1256,41 @@ class TreeSocket : public InspSocket
                {
                        snprintf(data,MAXBUF,":%s ADDLINE Z %s %s %lu %lu :%s",sn,i->ipaddr,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
                for (std::vector<QLine>::iterator i = qlines.begin(); i != qlines.end(); i++, iterations++)
                {
                        snprintf(data,MAXBUF,":%s ADDLINE Q %s %s %lu %lu :%s",sn,i->nick,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
                for (std::vector<GLine>::iterator i = glines.begin(); i != glines.end(); i++, iterations++)
                {
                        snprintf(data,MAXBUF,":%s ADDLINE G %s %s %lu %lu :%s",sn,i->hostmask,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
                for (std::vector<ELine>::iterator i = elines.begin(); i != elines.end(); i++, iterations++)
                {
                        snprintf(data,MAXBUF,":%s ADDLINE E %s %s %lu %lu :%s",sn,i->hostmask,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
                for (std::vector<ZLine>::iterator i = pzlines.begin(); i != pzlines.end(); i++, iterations++)
                {
                        snprintf(data,MAXBUF,":%s ADDLINE Z %s %s %lu %lu :%s",sn,i->ipaddr,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
                for (std::vector<QLine>::iterator i = pqlines.begin(); i != pqlines.end(); i++, iterations++)
                {
                        snprintf(data,MAXBUF,":%s ADDLINE Q %s %s %lu %lu :%s",sn,i->nick,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
                for (std::vector<GLine>::iterator i = pglines.begin(); i != pglines.end(); i++, iterations++)
                {
                        snprintf(data,MAXBUF,":%s ADDLINE G %s %s %lu %lu :%s",sn,i->hostmask,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
                for (std::vector<ELine>::iterator i = pelines.begin(); i != pelines.end(); i++, iterations++)
                {
                        snprintf(data,MAXBUF,":%s ADDLINE E %s %s %lu %lu :%s",sn,i->hostmask,i->source,(unsigned long)i->set_time,(unsigned long)i->duration,i->reason);
                        this->WriteLine(data);
-                       if ((iterations % 10) == 0)
-                       {
-                               ServerInstance->DoOneIteration(false);
-                       }
                }
        }
 
@@ -1375,7 +1337,7 @@ class TreeSocket : public InspSocket
                {
                        if (u->second->registered == 7)
                        {
-                               snprintf(data,MAXBUF,":%s NICK %lu %s %s %s %s +%s %s :%s",u->second->server,(unsigned long)u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,(char*)inet_ntoa(u->second->ip4),u->second->fullname);
+                               snprintf(data,MAXBUF,":%s NICK %lu %s %s %s %s +%s %s :%s",u->second->server,(unsigned long)u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->FormatModes(),inet_ntoa(u->second->ip4),u->second->fullname);
                                this->WriteLine(data);
                                if (*u->second->oper)
                                {
@@ -1403,7 +1365,7 @@ class TreeSocket : public InspSocket
         */
        void DoBurst(TreeServer* s)
        {
-               /* The calls here to ServerInstance->DoOneIteration(false); yield the processing
+               /* The calls here to ServerInstance-> yield the processing
                 * back to the core so that a large burst is split into at least 6 sections
                 * (possibly more)
                 */
@@ -1413,26 +1375,16 @@ class TreeSocket : public InspSocket
                std::string name = s->GetName();
                Srv->SendOpers("*** Bursting to \2"+name+"\2.");
                this->WriteLine(burst);
-               ServerInstance->DoOneIteration(false);
                /* send our version string */
                this->WriteLine(":"+Srv->GetServerName()+" VERSION :"+Srv->GetVersion());
                /* Send server tree */
-               if (FindServer(name))
-                       this->SendServers(TreeRoot,s,1);
-               ServerInstance->DoOneIteration(false);
+               this->SendServers(TreeRoot,s,1);
                /* Send users and their oper status */
-               if (FindServer(name))
-                       this->SendUsers(s);
-               ServerInstance->DoOneIteration(false);
+               this->SendUsers(s);
                /* Send everything else (channel modes, xlines etc) */
-               if (FindServer(name))
-                       this->SendChannelModes(s);
-               ServerInstance->DoOneIteration(false);
-               if (FindServer(name))
-                       this->SendXLines(s);
-               ServerInstance->DoOneIteration(false);
+               this->SendChannelModes(s);
+               this->SendXLines(s);            
                FOREACH_MOD(I_OnSyncOtherMetaData,OnSyncOtherMetaData((Module*)TreeProtocolModule,(void*)this));
-               ServerInstance->DoOneIteration(false);
                this->WriteLine(endburst);
                Srv->SendOpers("*** Finished bursting to \2"+name+"\2.");
        }
@@ -1447,7 +1399,6 @@ class TreeSocket : public InspSocket
         */
        virtual bool OnDataReady()
        {
-               int iterations = 0;
                char* data = this->Read();
                /* Check that the data read is a valid pointer and it has some content */
                if (data && *data)
@@ -1458,11 +1409,6 @@ class TreeSocket : public InspSocket
                         */
                        while (in_buffer.find("\n") != std::string::npos)
                        {
-                               iterations++;
-                               if ((iterations % 10) == 0)
-                               {
-                                       ServerInstance->DoOneIteration(false);
-                               }
                                std::string ret = in_buffer.substr(0,in_buffer.find("\n")-1);
                                in_buffer = in_buffer.substr(in_buffer.find("\n")+1,in_buffer.length()-in_buffer.find("\n"));
                                if (ret.find("\r") != std::string::npos)
@@ -1555,11 +1501,8 @@ class TreeSocket : public InspSocket
                userrec* u = Srv->FindNick(prefix);
                if (u)
                {
+                       u->modes[UM_OPERATOR] = 1;
                        strlcpy(u->oper,opertype.c_str(),NICKMAX-1);
-                       if (!strchr(u->modes,'o'))
-                       {
-                               strcat(u->modes,"o");
-                       }
                        DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server);
                }
                return true;
@@ -1582,7 +1525,9 @@ class TreeSocket : public InspSocket
                        {
                                std::deque<std::string> par;
                                par.push_back(params[1]);
-                               DoOneToMany(u->nick,"NICK",par);
+                               /* This is not required as one is sent in OnUserPostNick below
+                                */
+                               //DoOneToMany(u->nick,"NICK",par);
                                Srv->ChangeUserNick(u,params[1]);
                                u->age = atoi(params[2].c_str());
                        }
@@ -1646,6 +1591,7 @@ class TreeSocket : public InspSocket
                        std::string reason = params[1];
                        params[1] = ":" + params[1];
                        DoOneToAllButSender(prefix,"KILL",params,sourceserv);
+                       ::Write(who->fd, ":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
                        Srv->QuitUser(who,reason);
                }
                return true;
@@ -1770,19 +1716,19 @@ class TreeSocket : public InspSocket
                {
                        case 'Z':
                                propogate = add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               zline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'Q':
                                propogate = add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               qline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'E':
                                propogate = add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               eline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'G':
                                propogate = add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
-                               gline_set_creation_time((char*)params[1].c_str(), atoi(params[3].c_str()));
+                               gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
                        break;
                        case 'K':
                                propogate = add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
@@ -1881,7 +1827,7 @@ class TreeSocket : public InspSocket
                                        unsigned long signon = atoi(params[1].c_str());
                                        unsigned long idle = atoi(params[2].c_str());
                                        if ((who_to_send_to) && (who_to_send_to->fd > -1))
-                                               do_whois(who_to_send_to,u,signon,idle,(char*)nick_whoised.c_str());
+                                               do_whois(who_to_send_to,u,signon,idle,nick_whoised.c_str());
                                }
                                else
                                {
@@ -1900,6 +1846,9 @@ class TreeSocket : public InspSocket
 
                userrec* u = Srv->FindNick(params[0]);
 
+               if (!u)
+                       return true;
+
                if (IS_LOCAL(u))
                {
                        // push the raw to the user
@@ -2020,8 +1969,8 @@ class TreeSocket : public InspSocket
                TreeServer* CheckDupe = FindServer(servername);
                if (CheckDupe)
                {
-                       this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
-                       Srv->SendOpers("*** Server connection from \2"+servername+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
+                       this->WriteLine("ERROR :Server "+servername+" already exists!");
+                       Srv->SendOpers("*** Server connection from \2"+servername+"\2 denied, already exists");
                        return false;
                }
                TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
@@ -2207,37 +2156,28 @@ class TreeSocket : public InspSocket
 
        bool ProcessLine(std::string line)
        {
-               char* l = (char*)line.c_str();
-               for (char* x = l; *x; x++)
-               {
-                       if ((*x == '\r') || (*x == '\n'))
-                               *x = 0;
-               }
-               if (!*l)
-                       return true;
-
-               log(DEBUG,"IN: %s",l);
-
                std::deque<std::string> params;
-               this->Split(l,true,params);
-               irc::string command = "";
-               std::string prefix = "";
-               if (((params[0].c_str())[0] == ':') && (params.size() > 1))
-               {
-                       prefix = params[0];
-                       command = params[1].c_str();
-                       char* pref = (char*)prefix.c_str();
-                       prefix = ++pref;
-                       params.pop_front();
-                       params.pop_front();
-               }
-               else
+               irc::string command;
+               std::string prefix;
+               
+               if (line.empty())
+                       return true;
+               
+               line = line.substr(0, line.find_first_of("\r\n"));
+               
+               log(DEBUG,"IN: %s", line.c_str());
+               
+               this->Split(line.c_str(),true,params);
+                       
+               if ((params[0][0] == ':') && (params.size() > 1))
                {
-                       prefix = "";
-                       command = params[0].c_str();
+                       prefix = params[0].substr(1);
                        params.pop_front();
                }
 
+               command = params[0].c_str();
+               params.pop_front();
+
                if ((!this->ctx_in) && (command == "AES"))
                {
                        std::string sserv = params[0];
@@ -2420,6 +2360,10 @@ class TreeSocket : public InspSocket
                                {
                                        return this->ForceJoin(prefix,params);
                                }
+                               else if (command == "SYNCTS")
+                               {
+                                       return this->SyncChannelTS(prefix,params);
+                               }
                                else if (command == "SERVER")
                                {
                                        return this->RemoteServer(prefix,params);
@@ -2583,22 +2527,14 @@ class TreeSocket : public InspSocket
                                        }
                                        if (who)
                                        {
-                                               if (command == "QUIT")
-                                               {
-                                                       TreeServer* s = FindServer(who->server);
-                                                       if (s)
-                                                       {
-                                                               s->DelUser(who);
-                                                       }
-                                               }
-                                               else if ((command == "NICK") && (params.size() > 0))
+                                               if ((command == "NICK") && (params.size() > 0))
                                                {
                                                        /* On nick messages, check that the nick doesnt
                                                         * already exist here. If it does, kill their copy,
                                                         * and our copy.
                                                         */
                                                        userrec* x = Srv->FindNick(params[0]);
-                                                       if (x)
+                                                       if ((x) && (x != who))
                                                        {
                                                                std::deque<std::string> p;
                                                                p.push_back(params[0]);
@@ -2619,10 +2555,10 @@ class TreeSocket : public InspSocket
                                                }
                                                // its a user
                                                target = who->server;
-                                               char* strparams[127];
+                                               const char* strparams[127];
                                                for (unsigned int q = 0; q < params.size(); q++)
                                                {
-                                                       strparams[q] = (char*)params[q].c_str();
+                                                       strparams[q] = params[q].c_str();
                                                }
                                                if (!Srv->CallCommandHandler(command.c_str(), strparams, params.size(), who))
                                                {
@@ -2690,6 +2626,38 @@ class TreeSocket : public InspSocket
 
        virtual int OnIncomingConnection(int newsock, char* ip)
        {
+               /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port,
+                * or discovering if this port is the server port, we don't allow connections from any
+                * IPs for which we don't have a link block.
+                */
+               bool found = false;
+               char resolved_host[MAXBUF];
+               vector<Link>::iterator i;
+               for (i = LinkBlocks.begin(); i != LinkBlocks.end(); i++)
+               {
+                       if (i->IPAddr == ip)
+                       {
+                               found = true;
+                               break;
+                       }
+                       /* XXX: Fixme: blocks for a very short amount of time,
+                        * we should cache these on rehash/startup
+                        */
+                       if (CleanAndResolve(resolved_host,i->IPAddr.c_str(),true))
+                       {
+                               if (std::string(resolved_host) == ip)
+                               {
+                                       found = true;
+                                       break;
+                               }
+                       }
+               }
+               if (!found)
+               {
+                       WriteOpers("Server connection from %s denied (no link blocks with that IP address)", ip);
+                       close(newsock);
+                       return false;
+               }
                TreeSocket* s = new TreeSocket(newsock, ip);
                Srv->AddSocket(s);
                return true;
@@ -2885,7 +2853,7 @@ void ReadConfiguration(bool rebind)
                                {
                                        log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
                                        listener->Close();
-                                       delete listener;
+                                       DELETE(listener);
                                }
                        }
                }
@@ -2935,7 +2903,7 @@ void ReadConfiguration(bool rebind)
                        }
                }
        }
-       delete Conf;
+       DELETE(Conf);
 }
 
 
@@ -3002,14 +2970,14 @@ class ModuleSpanningTree : public Module
                return serverlist.size();
        }
 
-       void HandleLinks(char** parameters, int pcnt, userrec* user)
+       void HandleLinks(const char** parameters, int pcnt, userrec* user)
        {
                ShowLinks(TreeRoot,user,0);
                WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
                return;
        }
 
-       void HandleLusers(char** parameters, int pcnt, userrec* user)
+       void HandleLusers(const char** parameters, int pcnt, userrec* user)
        {
                unsigned int n_users = usercnt();
 
@@ -3094,7 +3062,7 @@ class ModuleSpanningTree : public Module
        // (a character matrix), then draw the branches as a series of "L" shapes
        // from the nodes. This is not only friendlier on CPU it uses less stack.
 
-       void HandleMap(char** parameters, int pcnt, userrec* user)
+       void HandleMap(const char** parameters, int pcnt, userrec* user)
        {
                // This array represents a virtual screen which we will
                // "scratch" draw to, as the console device of an irc
@@ -3150,7 +3118,7 @@ class ModuleSpanningTree : public Module
                return;
        }
 
-       int HandleSquit(char** parameters, int pcnt, userrec* user)
+       int HandleSquit(const char** parameters, int pcnt, userrec* user)
        {
                TreeServer* s = FindServerMask(parameters[0]);
                if (s)
@@ -3180,7 +3148,7 @@ class ModuleSpanningTree : public Module
                return 1;
        }
 
-       int HandleTime(char** parameters, int pcnt, userrec* user)
+       int HandleTime(const char** parameters, int pcnt, userrec* user)
        {
                if ((user->fd > -1) && (pcnt))
                {
@@ -3204,7 +3172,7 @@ class ModuleSpanningTree : public Module
                return 1;
        }
 
-       int HandleRemoteWhois(char** parameters, int pcnt, userrec* user)
+       int HandleRemoteWhois(const char** parameters, int pcnt, userrec* user)
        {
                if ((user->fd > -1) && (pcnt > 1))
                {
@@ -3268,21 +3236,21 @@ class ModuleSpanningTree : public Module
                                        // an autoconnected server is not connected. Check if its time to connect it
                                        WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
                                        TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name.c_str());
-                                       if (newsocket->GetState() != I_ERROR)
+                                       if (newsocket->GetFd() > -1)
                                        {
                                                Srv->AddSocket(newsocket);
                                        }
                                        else
                                        {
-                                               WriteOpers("*** AUTOCONNECT: Error autoconnecting \002%s\002.",x->Name.c_str());
-                                               delete newsocket;
+                                               WriteOpers("*** AUTOCONNECT: Error autoconnecting \002%s\002: %s.",x->Name.c_str(),strerror(errno));
+                                               DELETE(newsocket);
                                        }
                                }
                        }
                }
        }
 
-       int HandleVersion(char** parameters, int pcnt, userrec* user)
+       int HandleVersion(const char** parameters, int pcnt, userrec* user)
        {
                // we've already checked if pcnt > 0, so this is safe
                TreeServer* found = FindServerMask(parameters[0]);
@@ -3319,7 +3287,7 @@ class ModuleSpanningTree : public Module
                return 1;
        }
        
-       int HandleConnect(char** parameters, int pcnt, userrec* user)
+       int HandleConnect(const char** parameters, int pcnt, userrec* user)
        {
                for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
                {
@@ -3330,14 +3298,14 @@ class ModuleSpanningTree : public Module
                                {
                                        WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",user->nick,x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
                                        TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name.c_str());
-                                       if (newsocket->GetState() != I_ERROR)
+                                       if (newsocket->GetFd() > -1)
                                        {
                                                Srv->AddSocket(newsocket);
                                        }
                                        else
                                        {
-                                               WriteServ(user->fd,"NOTICE %s :*** CONNECT: Error connecting \002%s\002.",user->nick,x->Name.c_str());
-                                               delete newsocket;
+                                               WriteServ(user->fd,"NOTICE %s :*** CONNECT: Error connecting \002%s\002: %s.",user->nick,x->Name.c_str(),strerror(errno));
+                                               DELETE(newsocket);
                                        }
                                        return 1;
                                }
@@ -3368,7 +3336,7 @@ class ModuleSpanningTree : public Module
                return 0;
        }
 
-       virtual int OnPreCommand(const std::string &command, char **parameters, int pcnt, userrec *user, bool validated)
+       virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
        {
                /* If the command doesnt appear to be valid, we dont want to mess with it. */
                if (!validated)
@@ -3564,11 +3532,7 @@ class ModuleSpanningTree : public Module
                        std::deque<std::string> params;
                        params.clear();
                        params.push_back(channel->name);
-                       if (*channel->key)
-                       {
-                               // if the channel has a key, force the join by emulating the key.
-                               params.push_back(channel->key);
-                       }
+
                        if (channel->GetUserCounter() > 1)
                        {
                                // not the first in the channel
@@ -3633,7 +3597,7 @@ class ModuleSpanningTree : public Module
                        params.push_back(user->host);
                        params.push_back(user->dhost);
                        params.push_back(user->ident);
-                       params.push_back("+"+std::string(user->modes));
+                       params.push_back("+"+std::string(user->FormatModes()));
                        params.push_back((char*)inet_ntoa(user->ip4));
                        params.push_back(":"+std::string(user->fullname));
                        DoOneToMany(Srv->GetServerName(),"NICK",params);
@@ -3892,6 +3856,13 @@ class ModuleSpanningTree : public Module
                        (*params)[2] = ":" + (*params)[2];
                        DoOneToMany(Srv->GetServerName(),"METADATA",*params);
                }
+               else if (event->GetEventID() == "send_mode")
+               {
+                       std::deque<std::string>* params = (std::deque<std::string>*)event->GetData();
+                       if (params->size() < 2)
+                               return;
+                       DoOneToMany(Srv->GetServerName(),"FMODE",*params);
+               }
        }
 
        virtual ~ModuleSpanningTree()