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"
26 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h */
28 void ModuleSpanningTree::OnPostCommand(Command* command, const std::vector<std::string>& parameters, LocalUser* user, CmdResult result, const std::string& original_line)
30 if (result == CMD_SUCCESS)
31 Utils->RouteCommand(NULL, command, parameters, user);
34 void SpanningTreeUtilities::RouteCommand(TreeServer* origin, Command* thiscmd, const parameterlist& parameters, User* user)
36 const std::string& command = thiscmd->name;
37 RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
39 std::string sent_cmd = command;
42 if (routing.type == ROUTE_TYPE_LOCALONLY)
44 /* Broadcast when it's a core command with the default route descriptor and the source is a
45 * remote user or a remote server
48 Version ver = thiscmd->creator->GetVersion();
49 if ((!(ver.Flags & VF_CORE)) || (IS_LOCAL(user)) || (IS_SERVER(user) == ServerInstance->FakeClient))
52 routing = ROUTE_BROADCAST;
54 else if (routing.type == ROUTE_TYPE_OPT_BCAST)
56 params.push_back("*");
57 params.push_back(command);
60 else if (routing.type == ROUTE_TYPE_OPT_UCAST)
62 TreeServer* sdest = FindServer(routing.serverdest);
65 ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Trying to route ENCAP to nonexistant server %s",
66 routing.serverdest.c_str());
69 params.push_back(sdest->GetID());
70 params.push_back(command);
75 Module* srcmodule = thiscmd->creator;
76 Version ver = srcmodule->GetVersion();
78 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
80 ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Routed command %s from non-VF_COMMON module %s",
81 command.c_str(), srcmodule->ModuleSourceFile.c_str());
86 std::string output_text;
87 ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
89 params.push_back(output_text);
91 if (routing.type == ROUTE_TYPE_MESSAGE)
94 std::string dest = routing.serverdest;
95 if (ServerInstance->Modes->FindPrefix(dest[0]))
98 dest = dest.substr(1);
102 Channel* c = ServerInstance->FindChan(dest);
106 // TODO OnBuildExemptList hook was here
107 GetListOfServersForChannel(c,list,pfx, CUList());
108 std::string data = ":" + user->uuid + " " + sent_cmd;
109 for (unsigned int x = 0; x < params.size(); x++)
110 data += " " + params[x];
111 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
113 TreeSocket* Sock = (*i)->GetSocket();
114 if (origin && origin->GetSocket() == Sock)
117 Sock->WriteLine(data);
120 else if (dest[0] == '$')
123 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
125 DoOneToMany(user->uuid, sent_cmd, params);
130 User* d = ServerInstance->FindNick(dest);
133 TreeServer* tsd = BestRouteTo(d->server);
135 // huh? no routing stuff around in a circle, please.
137 DoOneToOne(user->uuid, sent_cmd, params, d->server);
140 else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
143 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
145 DoOneToMany(user->uuid, sent_cmd, params);
147 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
149 if (origin && routing.serverdest == origin->GetName())
151 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);