]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
Change command name parameter of OnPostCommand to be a Command*
[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                 /* Broadcast when it's a core command with the default route descriptor and the source is a
45                  * remote user or a remote server
46                  */
47
48                 Version ver = thiscmd->creator->GetVersion();
49                 if ((!(ver.Flags & VF_CORE)) || (IS_LOCAL(user)) || (IS_SERVER(user) == ServerInstance->FakeClient))
50                         return;
51
52                 routing = ROUTE_BROADCAST;
53         }
54         else if (routing.type == ROUTE_TYPE_OPT_BCAST)
55         {
56                 params.push_back("*");
57                 params.push_back(command);
58                 sent_cmd = "ENCAP";
59         }
60         else if (routing.type == ROUTE_TYPE_OPT_UCAST)
61         {
62                 TreeServer* sdest = FindServer(routing.serverdest);
63                 if (!sdest)
64                 {
65                         ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Trying to route ENCAP to nonexistant server %s",
66                                 routing.serverdest.c_str());
67                         return;
68                 }
69                 params.push_back(sdest->GetID());
70                 params.push_back(command);
71                 sent_cmd = "ENCAP";
72         }
73         else
74         {
75                 Module* srcmodule = thiscmd->creator;
76                 Version ver = srcmodule->GetVersion();
77
78                 if (!(ver.Flags & (VF_COMMON | VF_CORE)) && srcmodule != Creator)
79                 {
80                         ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Routed command %s from non-VF_COMMON module %s",
81                                 command.c_str(), srcmodule->ModuleSourceFile.c_str());
82                         return;
83                 }
84         }
85
86         std::string output_text;
87         ServerInstance->Parser->TranslateUIDs(thiscmd->translation, parameters, output_text, true, thiscmd);
88
89         params.push_back(output_text);
90
91         if (routing.type == ROUTE_TYPE_MESSAGE)
92         {
93                 char pfx = 0;
94                 std::string dest = routing.serverdest;
95                 if (ServerInstance->Modes->FindPrefix(dest[0]))
96                 {
97                         pfx = dest[0];
98                         dest = dest.substr(1);
99                 }
100                 if (dest[0] == '#')
101                 {
102                         Channel* c = ServerInstance->FindChan(dest);
103                         if (!c)
104                                 return;
105                         TreeServerList list;
106                         // TODO OnBuildExemptList hook was here
107                         GetListOfServersForChannel(c,list,pfx, CUList());
108                         std::string data = ":" + user->uuid + " " + sent_cmd;
109                         for (unsigned int x = 0; x < params.size(); x++)
110                                 data += " " + params[x];
111                         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
112                         {
113                                 TreeSocket* Sock = (*i)->GetSocket();
114                                 if (origin && origin->GetSocket() == Sock)
115                                         continue;
116                                 if (Sock)
117                                         Sock->WriteLine(data);
118                         }
119                 }
120                 else if (dest[0] == '$')
121                 {
122                         if (origin)
123                                 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
124                         else
125                                 DoOneToMany(user->uuid, sent_cmd, params);
126                 }
127                 else
128                 {
129                         // user target?
130                         User* d = ServerInstance->FindNick(dest);
131                         if (!d)
132                                 return;
133                         TreeServer* tsd = BestRouteTo(d->server);
134                         if (tsd == origin)
135                                 // huh? no routing stuff around in a circle, please.
136                                 return;
137                         DoOneToOne(user->uuid, sent_cmd, params, d->server);
138                 }
139         }
140         else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
141         {
142                 if (origin)
143                         DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
144                 else
145                         DoOneToMany(user->uuid, sent_cmd, params);
146         }
147         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
148         {
149                 if (origin && routing.serverdest == origin->GetName())
150                         return;
151                 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);
152         }
153 }