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/>.
21 /* $ModDesc: Provides a spanning tree server link protocol */
29 #include "treeserver.h"
30 #include "treesocket.h"
32 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
34 void ModuleSpanningTree::OnPostCommand(const std::string &command, const std::vector<std::string>& parameters, LocalUser *user, CmdResult result, const std::string &original_line)
36 if (result == CMD_SUCCESS)
37 Utils->RouteCommand(NULL, command, parameters, user);
40 void SpanningTreeUtilities::RouteCommand(TreeServer* origin, const std::string &command, const parameterlist& parameters, User *user)
42 if (!ServerInstance->Parser->IsValidCommand(command, parameters.size(), user))
45 /* We know it's non-null because IsValidCommand returned true */
46 Command* thiscmd = ServerInstance->Parser->GetHandler(command);
48 RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
50 std::string sent_cmd = command;
53 if (routing.type == ROUTE_TYPE_LOCALONLY)
55 /* Broadcast when it's a core command with the default route descriptor and the source is a
56 * remote user or a remote server
59 Version ver = thiscmd->creator->GetVersion();
60 if ((!(ver.Flags & VF_CORE)) || (IS_LOCAL(user)) || (IS_SERVER(user) == ServerInstance->FakeClient))
63 routing = ROUTE_BROADCAST;
65 else if (routing.type == ROUTE_TYPE_OPT_BCAST)
67 params.push_back("*");
68 params.push_back(command);
71 else if (routing.type == ROUTE_TYPE_OPT_UCAST)
73 TreeServer* sdest = FindServer(routing.serverdest);
76 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Trying to route ENCAP to nonexistant server %s",
77 routing.serverdest.c_str());
80 params.push_back(sdest->GetID());
81 params.push_back(command);
86 Module* srcmodule = thiscmd->creator;
87 Version ver = srcmodule->GetVersion();
89 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
91 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Routed command %s from non-VF_COMMON module %s",
92 command.c_str(), srcmodule->ModuleSourceFile.c_str());
97 std::string output_text;
98 ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
100 params.push_back(output_text);
102 if (routing.type == ROUTE_TYPE_MESSAGE)
105 std::string dest = routing.serverdest;
106 if (ServerInstance->Modes->FindPrefix(dest[0]))
109 dest = dest.substr(1);
113 Channel* c = ServerInstance->FindChan(dest);
117 // TODO OnBuildExemptList hook was here
118 GetListOfServersForChannel(c,list,pfx, CUList());
119 std::string data = ":" + user->uuid + " " + sent_cmd;
120 for (unsigned int x = 0; x < params.size(); x++)
121 data += " " + params[x];
122 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
124 TreeSocket* Sock = i->second->GetSocket();
125 if (origin && origin->GetSocket() == Sock)
128 Sock->WriteLine(data);
131 else if (dest[0] == '$')
134 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
136 DoOneToMany(user->uuid, sent_cmd, params);
141 User* d = ServerInstance->FindNick(dest);
144 TreeServer* tsd = BestRouteTo(d->server);
146 // huh? no routing stuff around in a circle, please.
148 DoOneToOne(user->uuid, sent_cmd, params, d->server);
151 else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
154 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
156 DoOneToMany(user->uuid, sent_cmd, params);
158 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
160 if (origin && routing.serverdest == origin->GetName())
162 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);