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)
46 else if (routing.type == ROUTE_TYPE_OPT_BCAST)
48 params.push_back("*");
49 params.push_back(command);
52 else if (routing.type == ROUTE_TYPE_OPT_UCAST)
54 TreeServer* sdest = FindServer(routing.serverdest);
57 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Trying to route ENCAP to nonexistant server %s",
58 routing.serverdest.c_str());
61 params.push_back(sdest->GetID());
62 params.push_back(command);
67 Module* srcmodule = thiscmd->creator;
68 Version ver = srcmodule->GetVersion();
70 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
72 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Routed command %s from non-VF_COMMON module %s",
73 command.c_str(), srcmodule->ModuleSourceFile.c_str());
78 std::string output_text = CommandParser::TranslateUIDs(thiscmd->translation, parameters, true, thiscmd);
80 params.push_back(output_text);
82 if (routing.type == ROUTE_TYPE_MESSAGE)
85 std::string dest = routing.serverdest;
86 if (ServerInstance->Modes->FindPrefix(dest[0]))
89 dest = dest.substr(1);
93 Channel* c = ServerInstance->FindChan(dest);
97 // TODO OnBuildExemptList hook was here
98 GetListOfServersForChannel(c,list,pfx, CUList());
99 std::string data = ":" + user->uuid + " " + sent_cmd;
100 for (unsigned int x = 0; x < params.size(); x++)
101 data += " " + params[x];
102 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
104 TreeSocket* Sock = (*i)->GetSocket();
105 if (origin && origin->GetSocket() == Sock)
108 Sock->WriteLine(data);
111 else if (dest[0] == '$')
114 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
116 DoOneToMany(user->uuid, sent_cmd, params);
121 User* d = ServerInstance->FindNick(dest);
124 TreeServer* tsd = BestRouteTo(d->server);
126 // huh? no routing stuff around in a circle, please.
128 DoOneToOne(user->uuid, sent_cmd, params, d->server);
131 else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
134 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
136 DoOneToMany(user->uuid, sent_cmd, params);
138 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
140 if (origin && routing.serverdest == origin->GetName())
142 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);