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(const std::string &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, const std::string &command, const parameterlist& parameters, User *user)
36 if (!ServerInstance->Parser->IsValidCommand(command, parameters.size(), user))
39 /* We know it's non-null because IsValidCommand returned true */
40 Command* thiscmd = ServerInstance->Parser->GetHandler(command);
42 RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
44 std::string sent_cmd = command;
47 if (routing.type == ROUTE_TYPE_LOCALONLY)
49 /* Broadcast when it's a core command with the default route descriptor and the source is a
50 * remote user or a remote server
53 Version ver = thiscmd->creator->GetVersion();
54 if ((!(ver.Flags & VF_CORE)) || (IS_LOCAL(user)) || (IS_SERVER(user) == ServerInstance->FakeClient))
57 routing = ROUTE_BROADCAST;
59 else if (routing.type == ROUTE_TYPE_OPT_BCAST)
61 params.push_back("*");
62 params.push_back(command);
65 else if (routing.type == ROUTE_TYPE_OPT_UCAST)
67 TreeServer* sdest = FindServer(routing.serverdest);
70 ServerInstance->Logs->Log("m_spanningtree",LOG_DEFAULT,"Trying to route ENCAP to nonexistant server %s",
71 routing.serverdest.c_str());
74 params.push_back(sdest->GetID());
75 params.push_back(command);
80 Module* srcmodule = thiscmd->creator;
81 Version ver = srcmodule->GetVersion();
83 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
85 ServerInstance->Logs->Log("m_spanningtree",LOG_DEFAULT,"Routed command %s from non-VF_COMMON module %s",
86 command.c_str(), srcmodule->ModuleSourceFile.c_str());
91 std::string output_text;
92 ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
94 params.push_back(output_text);
96 if (routing.type == ROUTE_TYPE_MESSAGE)
99 std::string dest = routing.serverdest;
100 if (ServerInstance->Modes->FindPrefix(dest[0]))
103 dest = dest.substr(1);
107 Channel* c = ServerInstance->FindChan(dest);
111 // TODO OnBuildExemptList hook was here
112 GetListOfServersForChannel(c,list,pfx, CUList());
113 std::string data = ":" + user->uuid + " " + sent_cmd;
114 for (unsigned int x = 0; x < params.size(); x++)
115 data += " " + params[x];
116 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
118 TreeSocket* Sock = (*i)->GetSocket();
119 if (origin && origin->GetSocket() == Sock)
122 Sock->WriteLine(data);
125 else if (dest[0] == '$')
128 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
130 DoOneToMany(user->uuid, sent_cmd, params);
135 User* d = ServerInstance->FindNick(dest);
138 TreeServer* tsd = BestRouteTo(d->server);
140 // huh? no routing stuff around in a circle, please.
142 DoOneToOne(user->uuid, sent_cmd, params, d->server);
145 else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
148 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
150 DoOneToMany(user->uuid, sent_cmd, params);
152 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
154 if (origin && routing.serverdest == origin->GetName())
156 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);