]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/postcommand.cpp
Fix routing for normal core commands like QUIT
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / postcommand.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *        the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Provides a spanning tree server link protocol */
15
16 #include "inspircd.h"
17 #include "socket.h"
18 #include "xline.h"
19 #include "../transport.h"
20
21 #include "main.h"
22 #include "utils.h"
23 #include "treeserver.h"
24 #include "treesocket.h"
25
26 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
27
28 void ModuleSpanningTree::OnPostCommand(const std::string &command, const std::vector<std::string>& parameters, User *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->IsValidModuleCommand(command, parameters.size(), user))
37                 return;
38
39         /* We know it's non-null because IsValidModuleCommand 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                 Module* srcmodule = thiscmd->creator;
50                 Version ver = srcmodule->GetVersion();
51
52                 if ((ver.Flags & VF_CORE) && !IS_LOCAL(user))
53                         routing = ROUTE_BROADCAST;
54                 else
55                         return;
56                 if (user == ServerUser)
57                         return;
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",ERROR,"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)))
84                 {
85                         ServerInstance->Logs->Log("m_spanningtree",ERROR,"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->second->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 }