2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "treeserver.h"
25 #include "commandbuilder.h"
27 void ModuleSpanningTree::OnPostCommand(Command* command, const std::vector<std::string>& parameters, LocalUser* user, CmdResult result, const std::string& original_line)
29 if (result == CMD_SUCCESS)
30 Utils->RouteCommand(NULL, command, parameters, user);
33 void SpanningTreeUtilities::RouteCommand(TreeServer* origin, CommandBase* thiscmd, const parameterlist& parameters, User* user)
35 const std::string& command = thiscmd->name;
36 RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
37 if (routing.type == ROUTE_TYPE_LOCALONLY)
40 const bool encap = ((routing.type == ROUTE_TYPE_OPT_BCAST) || (routing.type == ROUTE_TYPE_OPT_UCAST));
41 CmdBuilder params(user, encap ? "ENCAP" : command.c_str());
42 TreeServer* sdest = NULL;
44 if (routing.type == ROUTE_TYPE_OPT_BCAST)
47 params.push_back(command);
49 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
51 sdest = static_cast<TreeServer*>(routing.server);
54 sdest = FindServer(routing.serverdest);
57 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Trying to route %s%s to nonexistant server %s", (encap ? "ENCAP " : ""), command.c_str(), routing.serverdest.c_str());
64 params.push_back(sdest->GetID());
65 params.push_back(command);
70 Module* srcmodule = thiscmd->creator;
71 Version ver = srcmodule->GetVersion();
73 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
75 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Routed command %s from non-VF_COMMON module %s",
76 command.c_str(), srcmodule->ModuleSourceFile.c_str());
81 std::string output_text = CommandParser::TranslateUIDs(thiscmd->translation, parameters, true, thiscmd);
83 params.push_back(output_text);
85 if (routing.type == ROUTE_TYPE_MESSAGE)
88 std::string dest = routing.serverdest;
89 if (ServerInstance->Modes->FindPrefix(dest[0]))
92 dest = dest.substr(1);
96 Channel* c = ServerInstance->FindChan(dest);
99 // TODO OnBuildExemptList hook was here
101 SendChannelMessage(user->uuid, c, parameters[1], pfx, exempts, command.c_str(), origin ? origin->GetSocket() : NULL);
103 else if (dest[0] == '$')
105 params.Forward(origin);
110 User* d = ServerInstance->FindNick(dest);
111 if (!d || IS_LOCAL(d))
113 TreeServer* tsd = TreeServer::Get(d)->GetRoute();
115 // huh? no routing stuff around in a circle, please.
120 else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
122 params.Forward(origin);
124 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
126 params.Unicast(sdest->ServerUser);