]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree/treesocket2.cpp
m_spanningtree Remove duplicate code for sending channel messages from RouteCommand()
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket2.cpp
index 1b3b9dae2e8bba907a04097e4f0367cbf099ee29..5181dfeeac05c48bb174f1d6210dd6ea4f26b2ed 100644 (file)
@@ -80,7 +80,7 @@ void TreeSocket::ProcessLine(std::string &line)
        std::string command;
        parameterlist params;
 
-       ServerInstance->Logs->Log("m_spanningtree", LOG_RAWIO, "S[%d] I %s", this->GetFd(), line.c_str());
+       ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] I %s", this->GetFd(), line.c_str());
 
        Split(line, prefix, command, params);
 
@@ -172,7 +172,7 @@ void TreeSocket::ProcessLine(std::string &line)
 
                                linkID = capab->name;
 
-                               MyRoot = new TreeServer(Utils, capab->name, capab->description, capab->sid, Utils->TreeRoot, this, capab->hidden);
+                               MyRoot = new TreeServer(capab->name, capab->description, capab->sid, Utils->TreeRoot, this, capab->hidden);
                                Utils->TreeRoot->AddChild(MyRoot);
 
                                MyRoot->bursting = true;
@@ -184,8 +184,8 @@ void TreeSocket::ProcessLine(std::string &line)
                                sparams.push_back("0");
                                sparams.push_back(MyRoot->GetID());
                                sparams.push_back(":" + MyRoot->GetDesc());
-                               Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(), "SERVER", sparams, MyRoot->GetName());
-                               Utils->DoOneToAllButSender(MyRoot->GetID(), "BURST", params, MyRoot->GetName());
+                               Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(), "SERVER", sparams, MyRoot);
+                               Utils->DoOneToAllButSender(MyRoot->GetID(), "BURST", params, MyRoot);
                        }
                        else if (command == "ERROR")
                        {
@@ -267,7 +267,7 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
                        }
                        else
                        {
-                               ServerInstance->Logs->Log("m_spanningtree", LOG_DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.",
+                               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.",
                                        command.c_str(), prefix.c_str());
                                return;
                        }
@@ -298,216 +298,68 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
        if ((!route_back_again) || (route_back_again->GetSocket() != this))
        {
                if (route_back_again)
-                       ServerInstance->Logs->Log("m_spanningtree", LOG_DEBUG, "Protocol violation: Fake direction '%s' from connection '%s'",
+                       ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Protocol violation: Fake direction '%s' from connection '%s'",
                                prefix.c_str(),linkID.c_str());
                return;
        }
 
-       /*
-        * First up, check for any malformed commands (e.g. MODE without a timestamp)
-        * and rewrite commands where necessary (SVSMODE -> MODE for services). -- w
-        */
-       if (command == "SVSMODE") // This isn't in an "else if" so we still force FMODE for changes on channels.
-               command = "MODE";
-
+       // Translate commands coming from servers using an older protocol
        if (proto_version < ProtocolVersion)
        {
                if (!PreProcessOldProtocolMessage(who, command, params))
                        return;
        }
 
-       // TODO move all this into Commands
-       if (command == "MAP")
-       {
-               Utils->Creator->HandleMap(params, who);
-       }
-       else if (command == "SERVER")
-       {
-               this->RemoteServer(prefix,params);
-       }
-       else if (command == "ERROR")
-       {
-               this->Error(params);
-       }
-       else if (command == "AWAY")
-       {
-               this->Away(prefix,params);
-       }
-       else if (command == "PING")
-       {
-               this->LocalPing(prefix,params);
-       }
-       else if (command == "PONG")
-       {
-               TreeServer *s = Utils->FindServer(prefix);
-               if (s && s->bursting)
-               {
-                       ServerInstance->SNO->WriteGlobalSno('l',"Server \002%s\002 has not finished burst, forcing end of burst (send ENDBURST!)", prefix.c_str());
-                       s->FinishBurst();
-               }
-               this->LocalPong(prefix,params);
-       }
-       else if (command == "VERSION")
-       {
-               this->ServerVersion(prefix,params);
-       }
-       else if (command == "ADDLINE")
-       {
-               this->AddLine(prefix,params);
-       }
-       else if (command == "DELLINE")
+       ServerCommand* scmd = Utils->Creator->CmdManager.GetHandler(command);
+       CommandBase* cmdbase = scmd;
+       Command* cmd;
+       if (!scmd)
        {
-               this->DelLine(prefix,params);
-       }
-       else if (command == "SAVE")
-       {
-               this->ForceNick(prefix,params);
-       }
-       else if (command == "OPERQUIT")
-       {
-               this->OperQuit(prefix,params);
-       }
-       else if (command == "IDLE")
-       {
-               this->Whois(prefix,params);
-       }
-       else if (command == "PUSH")
-       {
-               this->Push(prefix,params);
-       }
-       else if (command == "SQUIT")
-       {
-               if (params.size() == 2)
-               {
-                       this->Squit(Utils->FindServer(params[0]),params[1]);
-               }
-       }
-       else if (command == "SNONOTICE")
-       {
-               if (params.size() >= 2)
-               {
-                       ServerInstance->SNO->WriteToSnoMask(params[0][0], "From " + who->nick + ": "+ params[1]);
-                       params[1] = ":" + params[1];
-                       Utils->DoOneToAllButSender(prefix, command, params, prefix);
-               }
-       }
-       else if (command == "BURST")
-       {
-               // Set prefix server as bursting
-               TreeServer* ServerSource = Utils->FindServer(prefix);
-               if (!ServerSource)
+               // Not a special server-to-server command
+               cmd = ServerInstance->Parser->GetHandler(command);
+               if (!cmd)
                {
-                       ServerInstance->SNO->WriteGlobalSno('l', "WTF: Got BURST from a non-server(?): %s", prefix.c_str());
+                       irc::stringjoiner pmlist(params);
+                       ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Unrecognised S2S command :%s %s %s",
+                               who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
+                       SendError("Unrecognised command '" + command + "' -- possibly loaded mismatched modules");
                        return;
                }
-
-               ServerSource->bursting = true;
-               Utils->DoOneToAllButSender(prefix, command, params, prefix);
+               cmdbase = cmd;
        }
-       else if (command == "ENDBURST")
-       {
-               TreeServer* ServerSource = Utils->FindServer(prefix);
-               if (!ServerSource)
-               {
-                       ServerInstance->SNO->WriteGlobalSno('l', "WTF: Got ENDBURST from a non-server(?): %s", prefix.c_str());
-                       return;
-               }
 
-               ServerSource->FinishBurst();
-               Utils->DoOneToAllButSender(prefix, command, params, prefix);
-       }
-       else if (command == "ENCAP")
+       if (params.size() < cmdbase->min_params)
        {
-               this->Encap(who, params);
+               irc::stringjoiner pmlist(params);
+               ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Insufficient parameters for S2S command :%s %s %s",
+                       who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
+               SendError("Insufficient parameters for command '" + command + "'");
+               return;
        }
-       else if (command == "NICK")
-       {
-               if (params.size() != 2)
-               {
-                       SendError("Protocol violation: Wrong number of parameters for NICK message");
-                       return;
-               }
-
-               if (IS_SERVER(who))
-               {
-                       SendError("Protocol violation: Server changing nick");
-                       return;
-               }
 
-               if ((isdigit(params[0][0])) && (params[0] != who->uuid))
-               {
-                       SendError("Protocol violation: User changing nick to an invalid UID - " + params[0]);
-                       return;
-               }
-
-               /* Update timestamp on user when they change nicks */
-               who->age = ConvToInt(params[1]);
-
-               /*
-                * On nick messages, check that the nick doesnt already exist here.
-                * If it does, perform collision logic.
-                */
-               User* x = ServerInstance->FindNickOnly(params[0]);
-               if ((x) && (x != who))
-               {
-                       int collideret = 0;
-                       /* x is local, who is remote */
-                       collideret = this->DoCollision(x, who->age, who->ident, who->GetIPString(), who->uuid);
-                       if (collideret != 1)
-                       {
-                               /*
-                                * Remote client lost, or both lost, parsing or passing on this
-                                * nickchange would be pointless, as the incoming client's server will
-                                * soon recieve SVSNICK to change its nick to its UID. :) -- w00t
-                                */
-                               return;
-                       }
-               }
-               who->ForceNickChange(params[0]);
-               Utils->RouteCommand(route_back_again, command, params, who);
-       }
-       else
+       if ((!params.empty()) && (params.back().empty()) && (!cmdbase->allow_empty_last_param))
        {
-               Command* cmd = ServerInstance->Parser->GetHandler(command);
-
-               if (!cmd)
-               {
-                       irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
-                       ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Unrecognised S2S command :%s %s %s",
-                               who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
-                       SendError("Unrecognised command '" + command + "' -- possibly loaded mismatched modules");
-                       return;
-               }
-
-               if (params.size() < cmd->min_params)
-               {
-                       irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
-                       ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Insufficient parameters for S2S command :%s %s %s",
-                               who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
-                       SendError("Insufficient parameters for command '" + command + "'");
+               // the last param is empty and the command handler doesn't allow that, check if there will be enough params if we drop the last
+               if (params.size()-1 < cmdbase->min_params)
                        return;
-               }
-
-               if ((!params.empty()) && (params.back().empty()) && (!cmd->allow_empty_last_param))
-               {
-                       // the last param is empty and the command handler doesn't allow that, check if there will be enough params if we drop the last
-                       if (params.size()-1 < cmd->min_params)
-                               return;
-                       params.pop_back();
-               }
+               params.pop_back();
+       }
 
-               CmdResult res = cmd->Handle(params, who);
+       CmdResult res;
+       if (scmd)
+               res = scmd->Handle(who, params);
+       else
+               res = cmd->Handle(params, who);
 
-               if (res == CMD_INVALID)
-               {
-                       irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
-                       ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Error handling S2S command :%s %s %s",
-                               who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
-                       SendError("Error handling '" + command + "' -- possibly loaded mismatched modules");
-               }
-               else if (res == CMD_SUCCESS)
-                       Utils->RouteCommand(route_back_again, command, params, who);
+       if (res == CMD_INVALID)
+       {
+               irc::stringjoiner pmlist(params);
+               ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "Error handling S2S command :%s %s %s",
+                       who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
+               SendError("Error handling '" + command + "' -- possibly loaded mismatched modules");
        }
+       else if (res == CMD_SUCCESS)
+               Utils->RouteCommand(route_back_again, cmdbase, params, who);
 }
 
 void TreeSocket::OnTimeout()
@@ -536,7 +388,7 @@ void TreeSocket::Close()
                time_t server_uptime = ServerInstance->Time() - this->age;
                if (server_uptime)
                {
-                       std::string timestr = Utils->Creator->TimeToStr(server_uptime);
+                       std::string timestr = ModuleSpanningTree::TimeToStr(server_uptime);
                        ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' was established for %s", linkID.c_str(), timestr.c_str());
                }
        }