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