]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
Change modules to use the MODNAME constant when logging.
[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(MODNAME, 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(MODNAME, 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 = CommandParser::TranslateUIDs(thiscmd->translation, parameters, true, thiscmd);
79
80         params.push_back(output_text);
81
82         if (routing.type == ROUTE_TYPE_MESSAGE)
83         {
84                 char pfx = 0;
85                 std::string dest = routing.serverdest;
86                 if (ServerInstance->Modes->FindPrefix(dest[0]))
87                 {
88                         pfx = dest[0];
89                         dest = dest.substr(1);
90                 }
91                 if (dest[0] == '#')
92                 {
93                         Channel* c = ServerInstance->FindChan(dest);
94                         if (!c)
95                                 return;
96                         TreeServerList list;
97                         // TODO OnBuildExemptList hook was here
98                         GetListOfServersForChannel(c,list,pfx, CUList());
99                         std::string data = ":" + user->uuid + " " + sent_cmd;
100                         for (unsigned int x = 0; x < params.size(); x++)
101                                 data += " " + params[x];
102                         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
103                         {
104                                 TreeSocket* Sock = (*i)->GetSocket();
105                                 if (origin && origin->GetSocket() == Sock)
106                                         continue;
107                                 if (Sock)
108                                         Sock->WriteLine(data);
109                         }
110                 }
111                 else if (dest[0] == '$')
112                 {
113                         if (origin)
114                                 DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
115                         else
116                                 DoOneToMany(user->uuid, sent_cmd, params);
117                 }
118                 else
119                 {
120                         // user target?
121                         User* d = ServerInstance->FindNick(dest);
122                         if (!d)
123                                 return;
124                         TreeServer* tsd = BestRouteTo(d->server);
125                         if (tsd == origin)
126                                 // huh? no routing stuff around in a circle, please.
127                                 return;
128                         DoOneToOne(user->uuid, sent_cmd, params, d->server);
129                 }
130         }
131         else if (routing.type == ROUTE_TYPE_BROADCAST || routing.type == ROUTE_TYPE_OPT_BCAST)
132         {
133                 if (origin)
134                         DoOneToAllButSender(user->uuid, sent_cmd, params, origin->GetName());
135                 else
136                         DoOneToMany(user->uuid, sent_cmd, params);
137         }
138         else if (routing.type == ROUTE_TYPE_UNICAST || routing.type == ROUTE_TYPE_OPT_UCAST)
139         {
140                 if (origin && routing.serverdest == origin->GetName())
141                         return;
142                 DoOneToOne(user->uuid, sent_cmd, params, routing.serverdest);
143         }
144 }