]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
Get rid of the OnRemoteKill hook, make use of GetRouting() and TR_CUSTOM to route...
[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 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h */
27
28 void ModuleSpanningTree::OnPostCommand(Command* command, const std::vector<std::string>& parameters, LocalUser* user, CmdResult result, const std::string& original_line)
29 {
30         if (result == CMD_SUCCESS)
31                 Utils->RouteCommand(NULL, command, parameters, user);
32 }
33
34 void SpanningTreeUtilities::RouteCommand(TreeServer* origin, Command* thiscmd, const parameterlist& parameters, User* user)
35 {
36         const std::string& command = thiscmd->name;
37         RouteDescriptor routing = thiscmd->GetRouting(user, parameters);
38
39         std::string sent_cmd = command;
40         parameterlist params;
41
42         if (routing.type == ROUTE_TYPE_LOCALONLY)
43         {
44                 return;
45         }
46         else if (routing.type == ROUTE_TYPE_OPT_BCAST)
47         {
48                 params.push_back("*");
49                 params.push_back(command);
50                 sent_cmd = "ENCAP";
51         }
52         else if (routing.type == ROUTE_TYPE_OPT_UCAST)
53         {
54                 TreeServer* sdest = FindServer(routing.serverdest);
55                 if (!sdest)
56                 {
57                         ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Trying to route ENCAP to nonexistant server %s",
58                                 routing.serverdest.c_str());
59                         return;
60                 }
61                 params.push_back(sdest->GetID());
62                 params.push_back(command);
63                 sent_cmd = "ENCAP";
64         }
65         else
66         {
67                 Module* srcmodule = thiscmd->creator;
68                 Version ver = srcmodule->GetVersion();
69
70                 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
71                 {
72                         ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Routed command %s from non-VF_COMMON module %s",
73                                 command.c_str(), srcmodule->ModuleSourceFile.c_str());
74                         return;
75                 }
76         }
77
78         std::string output_text;
79         ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
80
81         params.push_back(output_text);
82
83         if (routing.type == ROUTE_TYPE_MESSAGE)
84         {
85                 char pfx = 0;
86                 std::string dest = routing.serverdest;
87                 if (ServerInstance->Modes->FindPrefix(dest[0]))
88                 {
89                         pfx = dest[0];
90                         dest = dest.substr(1);
91                 }
92                 if (dest[0] == '#')
93                 {
94                         Channel* c = ServerInstance->FindChan(dest);
95                         if (!c)
96                                 return;
97                         TreeServerList list;
98                         // TODO OnBuildExemptList hook was here
99                         GetListOfServersForChannel(c,list,pfx, CUList());
100                         std::string data = ":" + user->uuid + " " + sent_cmd;
101                         for (unsigned int x = 0; x < params.size(); x++)
102                                 data += " " + params[x];
103                         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
104                         {
105                                 TreeSocket* Sock = (*i)->GetSocket();
106                                 if (origin && origin->GetSocket() == Sock)
107                                         continue;
108                                 if (Sock)
109                                         Sock->WriteLine(data);
110                         }
111                 }
112                 else if (dest[0] == '$')
113                 {
114                         if (origin)
115                                 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
116                         else
117                                 DoOneToMany(user->uuid, sent_cmd, params);
118                 }
119                 else
120                 {
121                         // user target?
122                         User* d = ServerInstance->FindNick(dest);
123                         if (!d)
124                                 return;
125                         TreeServer* tsd = BestRouteTo(d->server);
126                         if (tsd == origin)
127                                 // huh? no routing stuff around in a circle, please.
128                                 return;
129                         DoOneToOne(user->uuid, sent_cmd, params, d->server);
130                 }
131         }
132         else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
133         {
134                 if (origin)
135                         DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
136                 else
137                         DoOneToMany(user->uuid, sent_cmd, params);
138         }
139         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
140         {
141                 if (origin && routing.serverdest == origin->GetName())
142                         return;
143                 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);
144         }
145 }