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)
51 else if (routing.type == ROUTE_TYPE_OPT_BCAST)
53 params.push_back("*");
54 params.push_back(command);
57 else if (routing.type == ROUTE_TYPE_OPT_UCAST)
59 TreeServer* sdest = FindServer(routing.serverdest);
62 ServerInstance->Logs->Log("m_spanningtree",ERROR,"Trying to route ENCAP to nonexistant server %s",
63 routing.serverdest.c_str());
66 params.push_back(sdest->GetID());
67 params.push_back(command);
72 Module* srcmodule = thiscmd->creator;
73 Version ver = srcmodule->GetVersion();
75 if (!(ver.Flags & (VF_COMMON | VF_CORE)))
77 ServerInstance->Logs->Log("m_spanningtree",ERROR,"Routed command %s from non-VF_COMMON module %s",
78 command.c_str(), srcmodule->ModuleSourceFile.c_str());
83 std::string output_text;
84 ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
86 params.push_back(output_text);
88 if (routing.type == ROUTE_TYPE_MESSAGE)
91 std::string dest = routing.serverdest;
92 if (ServerInstance->Modes->FindPrefix(dest[0]))
95 dest = dest.substr(1);
99 Channel* c = ServerInstance->FindChan(dest);
103 // TODO OnBuildExemptList hook was here
104 GetListOfServersForChannel(c,list,pfx, CUList());
105 std::string data = ":" + user->uuid + " " + sent_cmd;
106 for (unsigned int x = 0; x < params.size(); x++)
107 data += " " + params[x];
108 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
110 TreeSocket* Sock = i->second->GetSocket();
111 if (origin && origin->GetSocket() == Sock)
114 Sock->WriteLine(data);
117 else if (dest[0] == '$')
120 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
122 DoOneToMany(user->uuid, sent_cmd, params);
127 User* d = ServerInstance->FindNick(dest);
130 TreeServer* tsd = BestRouteTo(d->server);
132 // huh? no routing stuff around in a circle, please.
134 DoOneToOne(user->uuid, sent_cmd, params, d->server);
137 else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
140 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
142 DoOneToMany(user->uuid, sent_cmd, params);
144 else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
146 if (origin && routing.serverdest == origin->GetName())
148 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);