]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
Merge insp20
[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 std::vector<std::string>& parameters, LocalUser* user, CmdResult result, const std::string& original_line)
28 {
29         if (result == CMD_SUCCESS)
30                 Utils->RouteCommand(NULL, command, parameters, user);
31 }
32
33 void SpanningTreeUtilities::RouteCommand(TreeServer* origin, CommandBase* thiscmd, const parameterlist& 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
43         if (routing.type == ROUTE_TYPE_OPT_BCAST)
44         {
45                 params.push('*');
46                 params.push_back(command);
47         }
48         else if (routing.type == ROUTE_TYPE_OPT_UCAST)
49         {
50                 TreeServer* sdest = FindServer(routing.serverdest);
51                 if (!sdest)
52                 {
53                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Trying to route ENCAP to nonexistant server %s",
54                                 routing.serverdest.c_str());
55                         return;
56                 }
57                 params.push_back(sdest->GetID());
58                 params.push_back(command);
59         }
60         else
61         {
62                 Module* srcmodule = thiscmd->creator;
63                 Version ver = srcmodule->GetVersion();
64
65                 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
66                 {
67                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Routed command %s from non-VF_COMMON module %s",
68                                 command.c_str(), srcmodule->ModuleSourceFile.c_str());
69                         return;
70                 }
71         }
72
73         std::string output_text = CommandParser::TranslateUIDs(thiscmd->translation, parameters, true, thiscmd);
74
75         params.push_back(output_text);
76
77         if (routing.type == ROUTE_TYPE_MESSAGE)
78         {
79                 char pfx = 0;
80                 std::string dest = routing.serverdest;
81                 if (ServerInstance->Modes->FindPrefix(dest[0]))
82                 {
83                         pfx = dest[0];
84                         dest = dest.substr(1);
85                 }
86                 if (dest[0] == '#')
87                 {
88                         Channel* c = ServerInstance->FindChan(dest);
89                         if (!c)
90                                 return;
91                         // TODO OnBuildExemptList hook was here
92                         CUList exempts;
93                         SendChannelMessage(user->uuid, c, parameters[1], pfx, exempts, command.c_str(), origin ? origin->GetSocket() : NULL);
94                 }
95                 else if (dest[0] == '$')
96                 {
97                         params.Forward(origin);
98                 }
99                 else
100                 {
101                         // user target?
102                         User* d = ServerInstance->FindNick(dest);
103                         if (!d)
104                                 return;
105                         TreeServer* tsd = BestRouteTo(d->server);
106                         if (tsd == origin)
107                                 // huh? no routing stuff around in a circle, please.
108                                 return;
109                         params.Unicast(d);
110                 }
111         }
112         else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
113         {
114                 params.Forward(origin);
115         }
116         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
117         {
118                 if (origin && routing.serverdest == origin->GetName())
119                         return;
120                 params.Unicast(routing.serverdest);
121         }
122 }