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