]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree/treesocket2.cpp
Stability fixes, some DELETEs here that dont belong any more.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket2.cpp
index d287d4724f3f9ffdae14bdd492346b3d950d6eb4..dcfb2b4aad05bb60e94167d17792f0fad436ec76 100644 (file)
@@ -1,3 +1,17 @@
+/*       +------------------------------------+
+ *       | Inspire Internet Relay Chat Daemon |
+ *       +------------------------------------+
+ *
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ *            the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+#include "inspircd.h"
 #include "configreader.h"
 #include "users.h"
 #include "channels.h"
@@ -5,7 +19,6 @@
 #include "commands/cmd_whois.h"
 #include "commands/cmd_stats.h"
 #include "socket.h"
-#include "inspircd.h"
 #include "wildcard.h"
 #include "xline.h"
 #include "transport.h"
 
 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h */
 
+static std::map<std::string, std::string> warned;       /* Server names that have had protocol violation warnings displayed for them */
+
 int TreeSocket::WriteLine(std::string line)
 {
-       Instance->Log(DEBUG, "-> %s", line.c_str());
+       Instance->Log(DEBUG, "S[%d] -> %s", this->GetFd(), line.c_str());
        line.append("\r\n");
        return this->Write(line);
 }
 
+
 /* Handle ERROR command */
 bool TreeSocket::Error(std::deque<std::string> &params)
 {
        if (params.size() < 1)
                return false;
-       this->Instance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(InboundServerName != "" ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str());
+       this->Instance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(!InboundServerName.empty() ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str());
        /* we will return false to cause the socket to close. */
        return false;
 }
 
+bool TreeSocket::Modules(const std::string &prefix, std::deque<std::string> &params)
+{
+       if (params.empty())
+               return true;
+
+       if (!this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
+       {
+               /* Pass it on, not for us */
+               Utils->DoOneToOne(prefix, "MODULES", params, params[0]);
+               return true;
+       }
+
+       char strbuf[MAXBUF];
+       std::deque<std::string> par;
+       par.push_back(prefix);
+       par.push_back("");
+
+       userrec* source = this->Instance->FindNick(prefix);
+       if (!source)
+               return true;
+
+       for (unsigned int i = 0; i < Instance->Config->module_names.size(); i++)
+       {
+               Version V = Instance->modules[i]->GetVersion();
+               char modulename[MAXBUF];
+               char flagstate[MAXBUF];
+               *flagstate = 0;
+               if (V.Flags & VF_STATIC)
+                       strlcat(flagstate,", static",MAXBUF);
+               if (V.Flags & VF_VENDOR)
+                       strlcat(flagstate,", vendor",MAXBUF);
+               if (V.Flags & VF_COMMON)
+                       strlcat(flagstate,", common",MAXBUF);
+               if (V.Flags & VF_SERVICEPROVIDER)
+                       strlcat(flagstate,", service provider",MAXBUF);
+               if (!flagstate[0])
+                       strcpy(flagstate,"  <no flags>");
+               strlcpy(modulename,Instance->Config->module_names[i].c_str(),256);
+               if (*source->oper)
+               {
+                       snprintf(strbuf, MAXBUF, "::%s 900 %s :0x%08lx %d.%d.%d.%d %s (%s)",Instance->Config->ServerName,source->nick,(long unsigned int)Instance->modules[i],V.Major,V.Minor,V.Revision,V.Build,ServerConfig::CleanFilename(modulename),flagstate+2);
+               }
+               else
+               {
+                       snprintf(strbuf, MAXBUF, "::%s 900 %s :%s",Instance->Config->ServerName,source->nick,ServerConfig::CleanFilename(modulename));
+               }
+               par[1] = strbuf;
+               Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
+       }
+       snprintf(strbuf, MAXBUF, "::%s 901 %s :End of MODULES list", Instance->Config->ServerName, source->nick);
+       par[1] = strbuf;
+       Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
+       return true;
+}
+
 /** remote MOTD. leet, huh? */
 bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> &params)
 {
@@ -207,50 +278,19 @@ bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> &p
        return true;
 }
 
-/*
- * Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
- * then let that server do the dirty work (squit it!). Example:
- * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t
- */
-bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> &params)
+bool TreeSocket::OperQuit(const std::string &prefix, std::deque<std::string> &params)
 {
-       /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */
-       if (params.size() < 2)
+       if (params.size() < 1)
                return true;
 
-       TreeServer* s = Utils->FindServerMask(params[0]);
-
-       if (s)
-       {
-               if (s == Utils->TreeRoot)
-               {
-                       this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str());
-                       return true;
-               }
-
-               TreeSocket* sock = s->GetSocket();
+       userrec* u = this->Instance->FindNick(prefix);
 
-               if (sock)
-               {
-                       /* it's locally connected, KILL IT! */
-                       Instance->SNO->WriteToSnoMask('l',"RSQUIT: Server \002%s\002 removed from network by %s: %s", params[0].c_str(), prefix.c_str(), params[1].c_str());
-                       sock->Squit(s,"Server quit by " + prefix + ": " + params[1]);
-                       Instance->SE->DelFd(sock);
-                       sock->Close();
-                       delete sock;
-               }
-               else
-               {
-                       /* route the rsquit */
-                       params[1] = ":" + params[1];
-                       Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]);
-               }
-       }
-       else
+       if (u)
        {
-               /* mother fucker! it doesn't exist */
+               u->SetOperQuit(params[0]);
+               params[0] = ":" + params[0];
+               Utils->DoOneToAllButSender(prefix,"OPERQUIT",params,prefix);
        }
-
        return true;
 }
 
@@ -265,7 +305,7 @@ bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string>
        {
                /* only join if it's local, otherwise just pass it on! */
                if (IS_LOCAL(u))
-                       chanrec::JoinUser(this->Instance, u, params[1].c_str(), false);
+                       chanrec::JoinUser(this->Instance, u, params[1].c_str(), false, "", Instance->Time());
                Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix);
        }
        return true;
@@ -290,30 +330,25 @@ bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string>
 }
 
 bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> &params)
-{
+{       
        if (params.size() != 2)
                return true;
 
-       std::string nick = params[0];
-       userrec* u = this->Instance->FindNick(prefix);
-       userrec* who = this->Instance->FindNick(nick);
+       userrec* who = this->Instance->FindNick(params[0]);
 
        if (who)
        {
-               /* Prepend kill source, if we don't have one */
-               std::string sourceserv = prefix;
-               if (u)
-               {
-                       sourceserv = u->server;
-               }
+               /* Prepend kill source, if we don't have one */          
                if (*(params[1].c_str()) != '[')
                {
-                       params[1] = "[" + sourceserv + "] Killed (" + params[1] +")";
+                       params[1] = "[" + prefix + "] Killed (" + params[1] +")";
                }
                std::string reason = params[1];
                params[1] = ":" + params[1];
-               Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv);
-               who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
+               Utils->DoOneToAllButSender(prefix,"KILL",params,prefix);
+               // NOTE: This is safe with kill hiding on, as RemoteKill is only reached if we have a server prefix.
+               // in short this is not executed for USERS.
+               who->Write(":%s KILL %s :%s (%s)", prefix.c_str(), who->nick, prefix.c_str(), reason.c_str());
                userrec::QuitUser(this->Instance,who,reason);
        }
        return true;
@@ -330,6 +365,7 @@ bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> &p
                if (ServerSource)
                {
                        ServerSource->SetPingFlag();
+                       ServerSource->rtt = Instance->Time() - ServerSource->LastPing;
                }
        }
        else
@@ -363,11 +399,13 @@ bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> &pa
 {
        if (params.size() < 2)
                return true;
-       else
+       else if (params.size() < 3)
                params.push_back("");
        TreeServer* ServerSource = Utils->FindServer(prefix);
        if (ServerSource)
        {
+               Utils->SetRemoteBursting(ServerSource, false);
+
                if (params[0] == "*")
                {
                        FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2]));
@@ -472,7 +510,8 @@ bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> &par
        {
                if (atoi(params[4].c_str()))
                {
-                       this->Instance->SNO->WriteToSnoMask('x',"%s Added %cLINE on %s to expire in %lu seconds (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),atoi(params[4].c_str()),params[5].c_str());
+                       time_t c_requires_crap = ConvToInt(params[4]) + Instance->Time();
+                       this->Instance->SNO->WriteToSnoMask('x',"%s Added %cLINE on %s to expire on %s (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),Instance->TimeString(c_requires_crap).c_str(),params[5].c_str());
                }
                else
                {
@@ -519,19 +558,20 @@ bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> &param
                                userrec* x = this->Instance->FindNick(params[0]);
                                char signon[MAXBUF];
                                char idle[MAXBUF];
-                               snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon);
-                               snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true)));
+                               snprintf(signon, MAXBUF, "%lu", (unsigned long)x->signon);
+                               snprintf(idle, MAXBUF, "%lu", (unsigned long)abs((x->idle_lastmsg) - Instance->Time(true)));
                                std::deque<std::string> par;
                                par.push_back(prefix);
                                par.push_back(signon);
                                par.push_back(idle);
                                // ours, we're done, pass it BACK
-                               Utils->DoOneToOne(params[0],"IDLE",par,u->server);
+                               Utils->DoOneToOne(params[0], "IDLE", par, u->server);
                        }
                        else
                        {
                                // not ours pass it on
-                               Utils->DoOneToOne(prefix,"IDLE",params,x->server);
+                               if (x)
+                                       Utils->DoOneToOne(prefix, "IDLE", params, x->server);
                        }
                }
                else if (params.size() == 3)
@@ -545,12 +585,15 @@ bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> &param
                                unsigned long signon = atoi(params[1].c_str());
                                unsigned long idle = atoi(params[2].c_str());
                                if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
-                                       do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str());
+                               {
+                                       do_whois(this->Instance, who_to_send_to, u, signon, idle, nick_whoised.c_str());
+                               }
                        }
                        else
                        {
                                // not ours, pass it on
-                               Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server);
+                               if (who_to_send_to)
+                                       Utils->DoOneToOne(prefix, "IDLE", params, who_to_send_to->server);
                        }
                }
        }
@@ -587,28 +630,17 @@ bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string
        if ((params.size() == 2) && (params[1] == "FORCE"))
                force = true;
 
-       time_t rts = atoi(params[0].c_str());
-       time_t us = Instance->Time(true);
-
-       if (rts == us)
-       {
-               Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
-       }
-       else if (force || (rts < us))
-       {
-               int old = Instance->SetTimeDelta(rts - us);
-               Instance->Log(DEBUG, "%s TS (diff %d) from %s applied (old delta was %d)", (force) ? "Forced" : "Lower", rts - us, prefix.c_str(), old);
+       time_t them = atoi(params[0].c_str());
+       time_t us = Instance->Time(false);
 
-               Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
-       }
-       else
-       {
-               Instance->Log(DEBUG, "Higher TS (diff %d) from %s overridden", us - rts, prefix.c_str());
+       time_t diff = them - us;
 
-               std::deque<std::string> oparams;
-               oparams.push_back(ConvToStr(us));
+       Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
 
-               Utils->DoOneToMany(prefix, "TIMESET", oparams);
+       if (force || (them != us))
+       {
+               time_t old = Instance->SetTimeDelta(diff);
+               Instance->Log(DEBUG, "TS (diff %d) from %s applied (old delta was %d)", diff, prefix.c_str(), old);
        }
 
        return true;
@@ -721,24 +753,44 @@ bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string>
        TreeServer* ParentOfThis = Utils->FindServer(prefix);
        if (!ParentOfThis)
        {
-               this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
+               this->SendError("Protocol error - Introduced remote server from unknown server "+prefix);
                return false;
        }
        TreeServer* CheckDupe = Utils->FindServer(servername);
        if (CheckDupe)
        {
-               this->WriteLine("ERROR :Server "+servername+" already exists!");
-               this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+servername+"\2 denied, already exists");
+               this->SendError("Server "+servername+" already exists!");
+               this->Instance->SNO->WriteToSnoMask('l',"Server \2"+servername+"\2 being introduced from \2" + prefix + "\2 denied, already exists. Closing link with " + prefix);
                return false;
        }
-       TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL);
+       Link* lnk = Utils->FindLink(servername);
+       TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL, lnk ? lnk->Hidden : false);
        ParentOfThis->AddChild(Node);
        params[3] = ":" + params[3];
+       Utils->SetRemoteBursting(Node, true);
        Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
        this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
        return true;
 }
 
+bool TreeSocket::ComparePass(const std::string &ours, const std::string &theirs)
+{
+       if ((!strncmp(ours.c_str(), "HMAC-SHA256:", 12)) || (!strncmp(theirs.c_str(), "HMAC-SHA256:", 12)))
+       {
+               /* One or both of us specified hmac sha256, but we don't have sha256 module loaded!
+                * We can't allow this password as valid.
+                */
+               if (!Instance->FindModule("m_sha256.so") || !Utils->ChallengeResponse)
+                               return false;
+               else
+                       /* Straight string compare of hashes */
+                       return ours == theirs;
+       }
+       else
+               /* Straight string compare of plaintext */
+               return ours == theirs;
+}
+
 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
 {
        if (params.size() < 4)
@@ -747,23 +799,30 @@ bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
        irc::string servername = params[0].c_str();
        std::string sname = params[0];
        std::string password = params[1];
+       std::string description = params[3];
        int hops = atoi(params[2].c_str());
 
+       this->InboundServerName = sname;
+       this->InboundDescription = description;
+
+       if (!sentcapab)
+               this->SendCapabilities();
+
        if (hops)
        {
-               this->WriteLine("ERROR :Server too far away for authentication");
+               this->SendError("Server too far away for authentication");
                this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
                return false;
        }
-       std::string description = params[3];
+
        for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
        {
-               if ((x->Name == servername) && (x->RecvPass == password))
+               if ((x->Name == servername) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password)) || (x->RecvPass == password && (this->GetTheirChallenge().empty()))))
                {
                        TreeServer* CheckDupe = Utils->FindServer(sname);
                        if (CheckDupe)
                        {
-                               this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
+                               this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
                                this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
                                return false;
                        }
@@ -775,7 +834,7 @@ bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
                        // we should add the details of this server now
                        // to the servers tree, as a child of the root
                        // node.
-                       TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this);
+                       TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this,x->Hidden);
                        Utils->TreeRoot->AddChild(Node);
                        params[3] = ":" + params[3];
                        Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname);
@@ -784,7 +843,7 @@ bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> &params)
                        return true;
                }
        }
-       this->WriteLine("ERROR :Invalid credentials");
+       this->SendError("Invalid credentials");
        this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
        return false;
 }
@@ -796,23 +855,43 @@ bool TreeSocket::Inbound_Server(std::deque<std::string> &params)
        irc::string servername = params[0].c_str();
        std::string sname = params[0];
        std::string password = params[1];
+       std::string description = params[3];
        int hops = atoi(params[2].c_str());
 
+       this->InboundServerName = sname;
+       this->InboundDescription = description;
+
+       if (!sentcapab)
+               this->SendCapabilities();
+
        if (hops)
        {
-               this->WriteLine("ERROR :Server too far away for authentication");
+               this->SendError("Server too far away for authentication");
                this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
                return false;
        }
-       std::string description = params[3];
+
        for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
        {
-               if ((x->Name == servername) && (x->RecvPass == password))
+               if ((x->Name == servername) && ((ComparePass(this->MakePass(x->RecvPass,this->GetOurChallenge()),password) || x->RecvPass == password && (this->GetTheirChallenge().empty()))))
                {
+                       /* First check for instances of the server that are waiting between the inbound and outbound SERVER command */
+                       TreeSocket* CheckDupeSocket = Utils->FindBurstingServer(sname);
+                       if (CheckDupeSocket)
+                       {
+                               /* If we find one, we abort the link to prevent a race condition */
+                               this->SendError("Negotiation collision");
+                               this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists in a negotiating state.");
+                               CheckDupeSocket->SendError("Negotiation collision");
+                               Instance->SE->DelFd(CheckDupeSocket);
+                               CheckDupeSocket->Close();
+                               return false;
+                       }
+                       /* Now check for fully initialized instances of the server */
                        TreeServer* CheckDupe = Utils->FindServer(sname);
                        if (CheckDupe)
                        {
-                               this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
+                               this->SendError("Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
                                this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
                                return false;
                        }
@@ -823,17 +902,17 @@ bool TreeSocket::Inbound_Server(std::deque<std::string> &params)
                                this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
                        }
 
-                       this->InboundServerName = sname;
-                       this->InboundDescription = description;
+                       Utils->AddBurstingServer(sname,this);
+
                        // this is good. Send our details: Our server name and description and hopcount of 0,
                        // along with the sendpass from this block.
-                       this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+x->SendPass+" 0 :"+this->Instance->Config->ServerDesc);
+                       this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(x->SendPass, this->GetTheirChallenge())+" 0 :"+this->Instance->Config->ServerDesc);
                        // move to the next state, we are now waiting for THEM.
                        this->LinkState = WAIT_AUTH_2;
                        return true;
                }
        }
-       this->WriteLine("ERROR :Invalid credentials");
+       this->SendError("Invalid credentials");
        this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
        return false;
 }
@@ -843,8 +922,11 @@ void TreeSocket::Split(const std::string &line, std::deque<std::string> &n)
        n.clear();
        irc::tokenstream tokens(line);
        std::string param;
-       while ((param = tokens.GetToken()) != "")
-               n.push_back(param);
+       while (tokens.GetToken(param))
+       {
+               if (!param.empty())
+                       n.push_back(param);
+       }
        return;
 }
 
@@ -859,10 +941,13 @@ bool TreeSocket::ProcessLine(std::string &line)
        if (line.empty())
                return true;
 
-       Instance->Log(DEBUG, "<- %s", line.c_str());
+       Instance->Log(DEBUG, "S[%d] <- %s", this->GetFd(), line.c_str());
 
        this->Split(line.c_str(),params);
-
+       
+       if (params.empty())
+               return true;
+       
        if ((params[0][0] == ':') && (params.size() > 1))
        {
                prefix = params[0].substr(1);
@@ -894,7 +979,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                        }
                        else if (command == "USER")
                        {
-                               this->WriteLine("ERROR :Client connections to this port are prohibited.");
+                               this->SendError("Client connections to this port are prohibited.");
                                return false;
                        }
                        else if (command == "CAPAB")
@@ -903,14 +988,13 @@ bool TreeSocket::ProcessLine(std::string &line)
                        }
                        else if ((command == "U") || (command == "S"))
                        {
-                               this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
+                               this->SendError("Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
                                return false;
                        }
                        else
                        {
-                               std::string error("ERROR :Invalid command in negotiation phase: ");
-                               error.append(command.c_str());
-                               this->WriteLine(error);
+                               irc::string error = "Invalid command in negotiation phase: " + command;
+                               this->SendError(assign(error));
                                return false;
                        }
                break;
@@ -925,41 +1009,38 @@ bool TreeSocket::ProcessLine(std::string &line)
                        }
                        else if ((command == "U") || (command == "S"))
                        {
-                               this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
+                               this->SendError("Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
                                return false;
                        }
                        else if (command == "BURST")
                        {
                                if (params.size() && Utils->EnableTimeSync)
                                {
-                                       /* If a time stamp is provided, apply synchronization */
-                                       bool force = false;
+                                       bool we_have_delta = (Instance->Time(false) != Instance->Time(true));
                                        time_t them = atoi(params[0].c_str());
-                                       time_t us = Instance->Time(false);
-                                       int delta = them - us;
-                                       if ((params.size() == 2) && (params[1] == "FORCE"))
-                                               force = true;
-                                       if ((delta < -600) || (delta > 600))
+                                       time_t delta = them - Instance->Time(false);
+                                       if ((delta < -300) || (delta > 300))
                                        {
-                                               this->Instance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than ten minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs(delta));
-                                               this->WriteLine("ERROR :Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than ten minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
+                                               Instance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than five minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs(delta));
+                                               SendError("Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than five minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
                                                return false;
                                        }
+                                       else if ((delta < -30) || (delta > 30))
+                                       {
+                                               Instance->SNO->WriteToSnoMask('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs(delta));
+                                       }
 
-                                       if (force || (us > them))
+                                       if (!Utils->MasterTime && !we_have_delta)
                                        {
-                                               this->Instance->SetTimeDelta(them - us);
+                                               this->Instance->SetTimeDelta(delta);
                                                // Send this new timestamp to any other servers
                                                Utils->DoOneToMany(Utils->TreeRoot->GetName(), "TIMESET", params);
                                        }
-                                       else
-                                       {
-                                               // Override the timestamp
-                                               this->WriteLine(":" + Utils->TreeRoot->GetName() + " TIMESET " + ConvToStr(us));
-                                       }
                                }
                                this->LinkState = CONNECTED;
-                               Node = new TreeServer(this->Utils,this->Instance,InboundServerName,InboundDescription,Utils->TreeRoot,this);
+                               Link* lnk = Utils->FindLink(InboundServerName);
+                               Node = new TreeServer(this->Utils,this->Instance, InboundServerName, InboundDescription, Utils->TreeRoot, this, lnk ? lnk->Hidden : false);
+                               Utils->DelBurstingServer(this);
                                Utils->TreeRoot->AddChild(Node);
                                params.clear();
                                params.push_back(InboundServerName);
@@ -981,7 +1062,7 @@ bool TreeSocket::ProcessLine(std::string &line)
 
                break;
                case LISTENER:
-                       this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
+                       this->SendError("Internal error -- listening socket accepted its own descriptor!!!");
                        return false;
                break;
                case CONNECTING:
@@ -998,13 +1079,17 @@ bool TreeSocket::ProcessLine(std::string &line)
                        {
                                return this->Error(params);
                        }
+                       else if (command == "CAPAB")
+                       {
+                               return this->Capab(params);
+                       }
                break;
                case CONNECTED:
                        // This is the 'authenticated' state, when all passwords
                        // have been exchanged and anything past this point is taken
                        // as gospel.
 
-                       if (prefix != "")
+                       if (!prefix.empty())
                        {
                                std::string direction = prefix;
                                userrec* t = this->Instance->FindNick(prefix);
@@ -1026,6 +1111,28 @@ bool TreeSocket::ProcessLine(std::string &line)
                                route_back_again->SetNextPingTime(time(NULL) + 60);
                                route_back_again->SetPingFlag();
                        }
+                       else
+                       {
+                               prefix = this->GetName();
+                       }
+
+                       if ((command == "MODE") && (params.size() >= 2))
+                       {
+                               chanrec* channel = Instance->FindChan(params[0]);
+                               if (channel)
+                               {
+                                       userrec* x = Instance->FindNick(prefix);
+                                       if (x)
+                                       {
+                                               if (warned.find(x->server) == warned.end())
+                                               {
+                                                       Instance->Log(DEFAULT,"WARNING: I revceived modes '%s' from another server '%s'. This is not compliant with InspIRCd. Please check that server for bugs.", params[1].c_str(), x->server);
+                                                       Instance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending nonstandard modes: '%s MODE %s' where FMODE should be used, and may cause desyncs.", x->server, x->nick, params[1].c_str());
+                                                       warned[x->server] = x->nick;
+                                               }
+                                       }
+                               }
+                       }
 
                        if (command == "SVSMODE")
                        {
@@ -1035,16 +1142,19 @@ bool TreeSocket::ProcessLine(std::string &line)
                                 */
                                command = "MODE";
                        }
-                       std::string target = "";
+                       std::string target;
                        /* Yes, know, this is a mess. Its reasonably fast though as we're
                         * working with std::string here.
                         */
-                       if ((command == "NICK") && (params.size() > 1))
+                       if ((command == "NICK") && (params.size() >= 8))
                        {
                                return this->IntroduceClient(prefix,params);
                        }
                        else if (command == "FJOIN")
                        {
+                               TreeServer* ServerSource = Utils->FindServer(prefix);
+                               if (ServerSource)
+                                       Utils->SetRemoteBursting(ServerSource, false);
                                return this->ForceJoin(prefix,params);
                        }
                        else if (command == "STATS")
@@ -1055,6 +1165,14 @@ bool TreeSocket::ProcessLine(std::string &line)
                        {
                                return this->Motd(prefix, params);
                        }
+                       else if (command == "KILL" && Utils->IsServer(prefix))
+                       {
+                               return this->RemoteKill(prefix,params);
+                       }
+                       else if (command == "MODULES")
+                       {
+                               return this->Modules(prefix, params);
+                       }
                        else if (command == "ADMIN")
                        {
                                return this->Admin(prefix, params);
@@ -1073,12 +1191,11 @@ bool TreeSocket::ProcessLine(std::string &line)
                        }
                        else if (command == "FMODE")
                        {
+                               TreeServer* ServerSource = Utils->FindServer(prefix);
+                               if (ServerSource)
+                                       Utils->SetRemoteBursting(ServerSource, false);
                                return this->ForceMode(prefix,params);
                        }
-                       else if (command == "KILL")
-                       {
-                               return this->RemoteKill(prefix,params);
-                       }
                        else if (command == "FTOPIC")
                        {
                                return this->ForceTopic(prefix,params);
@@ -1097,40 +1214,46 @@ bool TreeSocket::ProcessLine(std::string &line)
                        }
                        else if (command == "PING")
                        {
+                               if (prefix.empty())
+                                       prefix = this->GetName();
                                /*
                                 * We just got a ping from a server that's bursting.
                                 * This can't be right, so set them to not bursting, and
                                 * apply their lines.
                                 */
+                               TreeServer* ServerSource = Utils->FindServer(prefix);
+                               if (ServerSource)
+                                       Utils->SetRemoteBursting(ServerSource, false);
+
                                if (this->bursting)
                                {
                                        this->bursting = false;
                                        Instance->XLines->apply_lines(Utils->lines_to_apply);
                                        Utils->lines_to_apply = 0;
                                }
-                               if (prefix == "")
-                               {
-                                       prefix = this->GetName();
-                               }
+
                                return this->LocalPing(prefix,params);
                        }
                        else if (command == "PONG")
                        {
+                               if (prefix.empty())
+                                       prefix = this->GetName();
                                /*
                                 * We just got a pong from a server that's bursting.
                                 * This can't be right, so set them to not bursting, and
                                 * apply their lines.
                                 */
+                               TreeServer* ServerSource = Utils->FindServer(prefix);
+                               if (ServerSource)
+                                       Utils->SetRemoteBursting(ServerSource, false);
+
                                if (this->bursting)
                                {
                                        this->bursting = false;
                                        Instance->XLines->apply_lines(Utils->lines_to_apply);
                                        Utils->lines_to_apply = 0;
                                }
-                               if (prefix == "")
-                               {
-                                       prefix = this->GetName();
-                               }
+
                                return this->LocalPong(prefix,params);
                        }
                        else if (command == "VERSION")
@@ -1147,19 +1270,22 @@ bool TreeSocket::ProcessLine(std::string &line)
                        }
                        else if (command == "ADDLINE")
                        {
+                               TreeServer* ServerSource = Utils->FindServer(prefix);
+                               if (ServerSource)
+                                       Utils->SetRemoteBursting(ServerSource, false);
                                return this->AddLine(prefix,params);
                        }
                        else if (command == "SVSNICK")
                        {
-                               if (prefix == "")
+                               if (prefix.empty())
                                {
                                        prefix = this->GetName();
                                }
                                return this->ForceNick(prefix,params);
                        }
-                       else if (command == "RSQUIT")
+                       else if (command == "OPERQUIT")
                        {
-                               return this->RemoteSquit(prefix, params);
+                               return this->OperQuit(prefix,params);
                        }
                        else if (command == "IDLE")
                        {
@@ -1191,7 +1317,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                                                        delete chan;
                                        }
                                }
-                               if (this->InboundServerName != "")
+                               if (!this->InboundServerName.empty())
                                {
                                        sourceserv = this->InboundServerName;
                                }
@@ -1199,7 +1325,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                        }
                        else if (command == "SVSJOIN")
                        {
-                               if (prefix == "")
+                               if (prefix.empty())
                                {
                                        prefix = this->GetName();
                                }
@@ -1216,7 +1342,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                        else if (command == "OPERNOTICE")
                        {
                                std::string sourceserv = this->myhost;
-                               if (this->InboundServerName != "")
+                               if (!this->InboundServerName.empty())
                                        sourceserv = this->InboundServerName;
                                if (params.size() >= 1)
                                        Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]);
@@ -1225,7 +1351,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                        else if (command == "MODENOTICE")
                        {
                                std::string sourceserv = this->myhost;
-                               if (this->InboundServerName != "")
+                               if (!this->InboundServerName.empty())
                                        sourceserv = this->InboundServerName;
                                if (params.size() >= 2)
                                {
@@ -1236,7 +1362,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                        else if (command == "SNONOTICE")
                        {
                                std::string sourceserv = this->myhost;
-                               if (this->InboundServerName != "")
+                               if (!this->InboundServerName.empty())
                                        sourceserv = this->InboundServerName;
                                if (params.size() >= 2)
                                {
@@ -1250,10 +1376,8 @@ bool TreeSocket::ProcessLine(std::string &line)
                                Instance->XLines->apply_lines(Utils->lines_to_apply);
                                Utils->lines_to_apply = 0;
                                std::string sourceserv = this->myhost;
-                               if (this->InboundServerName != "")
-                               {
+                               if (!this->InboundServerName.empty())
                                        sourceserv = this->InboundServerName;
-                               }
                                this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str());
 
                                Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server");
@@ -1268,7 +1392,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                                // this saves us having a huge ugly parser.
                                userrec* who = this->Instance->FindNick(prefix);
                                std::string sourceserv = this->myhost;
-                               if (this->InboundServerName != "")
+                               if (!this->InboundServerName.empty())
                                {
                                        sourceserv = this->InboundServerName;
                                }
@@ -1326,7 +1450,7 @@ bool TreeSocket::ProcessLine(std::string &line)
                                        switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who))
                                        {
                                                case CMD_INVALID:
-                                                       this->WriteLine("ERROR :Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules");
+                                                       this->SendError("Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules");
                                                        return false;
                                                break;
                                                case CMD_FAILURE:
@@ -1357,7 +1481,7 @@ bool TreeSocket::ProcessLine(std::string &line)
 std::string TreeSocket::GetName()
 {
        std::string sourceserv = this->myhost;
-       if (this->InboundServerName != "")
+       if (!this->InboundServerName.empty())
        {
                sourceserv = this->InboundServerName;
        }
@@ -1381,7 +1505,7 @@ void TreeSocket::OnClose()
        // If the connection is fully up (state CONNECTED)
        // then propogate a netsplit to all peers.
        std::string quitserver = this->myhost;
-       if (this->InboundServerName != "")
+       if (!this->InboundServerName.empty())
        {
                quitserver = this->InboundServerName;
        }
@@ -1391,8 +1515,13 @@ void TreeSocket::OnClose()
                Squit(s,"Remote host closed the connection");
        }
 
-       if (quitserver != "")
+       if (!quitserver.empty())
+       {
                this->Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' failed.",quitserver.c_str());
+               time_t server_uptime = Instance->Time() - this->age;    
+               if (server_uptime)
+                       Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' was established for %s", quitserver.c_str(), Utils->Creator->TimeToStr(server_uptime).c_str());
+       }
 }
 
 int TreeSocket::OnIncomingConnection(int newsock, char* ip)