]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
m_spanningtree Move all server-to-server command handlers into handler classes
[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                         TreeServerList list;
95                         // TODO OnBuildExemptList hook was here
96                         GetListOfServersForChannel(c,list,pfx, CUList());
97                         std::string data = ":" + user->uuid + " " + sent_cmd;
98                         for (unsigned int x = 0; x < params.size(); x++)
99                                 data += " " + params[x];
100                         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
101                         {
102                                 TreeSocket* Sock = (*i)->GetSocket();
103                                 if (origin && origin->GetSocket() == Sock)
104                                         continue;
105                                 if (Sock)
106                                         Sock->WriteLine(data);
107                         }
108                 }
109                 else if (dest[0] == '$')
110                 {
111                         if (origin)
112                                 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
113                         else
114                                 DoOneToMany(user->uuid, sent_cmd, params);
115                 }
116                 else
117                 {
118                         // user target?
119                         User* d = ServerInstance->FindNick(dest);
120                         if (!d)
121                                 return;
122                         TreeServer* tsd = BestRouteTo(d->server);
123                         if (tsd == origin)
124                                 // huh? no routing stuff around in a circle, please.
125                                 return;
126                         DoOneToOne(user->uuid, sent_cmd, params, d->server);
127                 }
128         }
129         else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
130         {
131                 if (origin)
132                         DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
133                 else
134                         DoOneToMany(user->uuid, sent_cmd, params);
135         }
136         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
137         {
138                 if (origin && routing.serverdest == origin->GetName())
139                         return;
140                 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);
141         }
142 }