]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
448bb3b543f5827d553365a6d1dea7ed5b88c08a
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / postcommand.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20 #include "inspircd.h"
21
22 #include "main.h"
23 #include "utils.h"
24 #include "treeserver.h"
25 #include "commandbuilder.h"
26
27 void ModuleSpanningTree::OnPostCommand(Command* command, const CommandBase::Params& parameters, LocalUser* user, CmdResult result, bool loop)
28 {
29         if (result == CMD_SUCCESS)
30                 Utils->RouteCommand(NULL, command, parameters, user);
31 }
32
33 void SpanningTreeUtilities::RouteCommand(TreeServer* origin, CommandBase* thiscmd, const CommandBase::Params& parameters, User* user)
34 {
35         const std::string& command = thiscmd->name;
36         RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
37         if (routing.type == ROUTE_TYPE_LOCALONLY)
38                 return;
39
40         const bool encap = ((routing.type == ROUTE_TYPE_OPT_BCAST) || (routing.type == ROUTE_TYPE_OPT_UCAST));
41         CmdBuilder params(user, encap ? "ENCAP" : command.c_str());
42         params.push_tags(parameters.GetTags());
43         TreeServer* sdest = NULL;
44
45         if (routing.type == ROUTE_TYPE_OPT_BCAST)
46         {
47                 params.push('*');
48                 params.push_back(command);
49         }
50         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
51         {
52                 sdest = static_cast<TreeServer*>(routing.server);
53                 if (!sdest)
54                 {
55                         // Assume the command handler already validated routing.serverdest and have only returned success if the target is something that the
56                         // user executing the command is allowed to look up e.g. target is not an uuid if user is local.
57                         sdest = FindRouteTarget(routing.serverdest);
58                         if (!sdest)
59                         {
60                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Trying to route %s%s to nonexistent server %s", (encap ? "ENCAP " : ""), command.c_str(), routing.serverdest.c_str());
61                                 return;
62                         }
63                 }
64
65                 if (encap)
66                 {
67                         params.push_back(sdest->GetID());
68                         params.push_back(command);
69                 }
70         }
71         else
72         {
73                 Module* srcmodule = thiscmd->creator;
74                 Version ver = srcmodule->GetVersion();
75
76                 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
77                 {
78                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Routed command %s from non-VF_COMMON module %s",
79                                 command.c_str(), srcmodule->ModuleSourceFile.c_str());
80                         return;
81                 }
82         }
83
84         std::string output_text = CommandParser::TranslateUIDs(thiscmd->translation, parameters, true, thiscmd);
85
86         params.push_back(output_text);
87
88         if (routing.type == ROUTE_TYPE_MESSAGE)
89         {
90                 char pfx = 0;
91                 std::string dest = routing.serverdest;
92                 if (ServerInstance->Modes->FindPrefix(dest[0]))
93                 {
94                         pfx = dest[0];
95                         dest.erase(dest.begin());
96                 }
97                 if (dest[0] == '#')
98                 {
99                         Channel* c = ServerInstance->FindChan(dest);
100                         if (!c)
101                                 return;
102                         // TODO OnBuildExemptList hook was here
103                         CUList exempts;
104                         SendChannelMessage(user->uuid, c, parameters[1], pfx, parameters.GetTags(), exempts, command.c_str(), origin ? origin->GetSocket() : NULL);
105                 }
106                 else if (dest[0] == '$')
107                 {
108                         params.Forward(origin);
109                 }
110                 else
111                 {
112                         // user target?
113                         User* d = ServerInstance->FindNick(dest);
114                         if (!d || IS_LOCAL(d))
115                                 return;
116                         TreeServer* tsd = TreeServer::Get(d)->GetRoute();
117                         if (tsd == origin)
118                                 // huh? no routing stuff around in a circle, please.
119                                 return;
120                         params.Unicast(d);
121                 }
122         }
123         else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
124         {
125                 params.Forward(origin);
126         }
127         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
128         {
129                 params.Unicast(sdest->ServerUser);
130         }
131 }