1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $ModDesc: Provides a spanning tree server link protocol */
22 #include "treeserver.h"
23 #include "treesocket.h"
25 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
27 void ModuleSpanningTree::OnPostCommand(const std::string &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, const std::string &command, const parameterlist& parameters, User *user)
35 if (!ServerInstance->IsValidModuleCommand(command, parameters.size(), user))
38 /* We know it's non-null because IsValidModuleCommand returned true */
39 Command* thiscmd = ServerInstance->Parser->GetHandler(command);
41 RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
43 std::string sent_cmd = command;
46 if (routing.type == ROUTE_TYPE_LOCALONLY)
48 Module* srcmodule = thiscmd->creator;
49 Version ver = srcmodule->GetVersion();
51 if ((ver.Flags & VF_CORE) && !IS_LOCAL(user))
52 routing = ROUTE_BROADCAST;
55 if (IS_SERVER(user) && user != ServerInstance->FakeClient)
58 else if (routing.type == ROUTE_TYPE_OPT_BCAST)
60 params.push_back("*");
61 params.push_back(command);
64 else if (routing.type == ROUTE_TYPE_OPT_UCAST)
66 TreeServer* sdest = FindServer(routing.serverdest);
69 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Trying to route ENCAP to nonexistant server %s",
70 routing.serverdest.c_str());
73 params.push_back(sdest->GetID());
74 params.push_back(command);
79 Module* srcmodule = thiscmd->creator;
80 Version ver = srcmodule->GetVersion();
82 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
84 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Routed command %s from non-VF_COMMON module %s",
85 command.c_str(), srcmodule->ModuleSourceFile.c_str());
90 std::string output_text;
91 ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
93 params.push_back(output_text);
95 if (routing.type == ROUTE_TYPE_MESSAGE)
98 std::string dest = routing.serverdest;
99 if (ServerInstance->Modes->FindPrefix(dest[0]))
102 dest = dest.substr(1);
106 Channel* c = ServerInstance->FindChan(dest);
110 // TODO OnBuildExemptList hook was here
111 GetListOfServersForChannel(c,list,pfx, CUList());
112 std::string data = ":" + user->uuid + " " + sent_cmd;
113 for (unsigned int x = 0; x < params.size(); x++)
114 data += " " + params[x];
115 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
117 TreeSocket* Sock = i->second->GetSocket();
118 if (origin && origin->GetSocket() == Sock)
121 Sock->WriteLine(data);
124 else if (dest[0] == '$')
127 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
129 DoOneToMany(user->uuid, sent_cmd, params);
134 User* d = ServerInstance->FindNick(dest);
137 TreeServer* tsd = BestRouteTo(d->server);
139 // huh? no routing stuff around in a circle, please.
141 DoOneToOne(user->uuid, sent_cmd, params, d->server);
144 else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
147 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
149 DoOneToMany(user->uuid, sent_cmd, params);
151 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
153 if (origin && routing.serverdest == origin->GetName())
155 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);