1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 */
19 #include "../transport.h"
23 #include "treeserver.h"
24 #include "treesocket.h"
26 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
28 void ModuleSpanningTree::OnPostCommand(const std::string &command, const std::vector<std::string>& parameters, User *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->IsValidModuleCommand(command, parameters.size(), user))
39 /* We know it's non-null because IsValidModuleCommand 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 Module* srcmodule = thiscmd->creator;
50 Version ver = srcmodule->GetVersion();
52 if ((ver.Flags & VF_CORE) && !IS_LOCAL(user))
53 routing = ROUTE_BROADCAST;
56 if (user == ServerUser)
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",ERROR,"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)))
85 ServerInstance->Logs->Log("m_spanningtree",ERROR,"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->second->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);