]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
5122fd0edb71edbf81d7e43b7193608c1e8b6a94
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / override_map.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5  *   Copyright (C) 2016, 2018-2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2014 Adam <Adam@anope.org>
7  *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
12  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
13  *   Copyright (C) 2007, 2010 Craig Edwards <brain@inspircd.org>
14  *
15  * This file is part of InspIRCd.  InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26  */
27
28
29 #include "inspircd.h"
30
31 #include "main.h"
32 #include "utils.h"
33 #include "treeserver.h"
34 #include "commands.h"
35
36 CommandMap::CommandMap(Module* Creator)
37         : Command(Creator, "MAP", 0, 1)
38 {
39         Penalty = 2;
40 }
41
42 static inline bool IsHidden(User* user, TreeServer* server)
43 {
44         if (!user->IsOper())
45         {
46                 if (server->Hidden)
47                         return true;
48                 if (Utils->HideULines && server->IsULine())
49                         return true;
50         }
51
52         return false;
53 }
54
55 // Calculate the map depth the servers go, and the longest server name
56 static void GetDepthAndLen(TreeServer* current, unsigned int depth, unsigned int& max_depth, unsigned int& max_len, unsigned int& max_version)
57 {
58         if (depth > max_depth)
59                 max_depth = depth;
60
61         if (current->GetName().length() > max_len)
62                 max_len = current->GetName().length();
63
64         if (current->GetRawVersion().length() > max_version)
65                 max_version = current->GetRawVersion().length();
66
67         const TreeServer::ChildServers& servers = current->GetChildren();
68         for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
69         {
70                 TreeServer* child = *i;
71                 GetDepthAndLen(child, depth + 1, max_depth, max_len, max_version);
72         }
73 }
74
75 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int max_version_len, unsigned int depth)
76 {
77         float percent = 0;
78
79         const user_hash& users = ServerInstance->Users->GetUsers();
80         if (!users.empty())
81         {
82                 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
83                 percent = current->UserCount * 100.0 / users.size();
84         }
85
86         std::string buffer = current->GetName();
87         if (user->IsOper())
88         {
89                 buffer += " (" + current->GetId();
90
91                 const std::string& cur_vers = current->GetRawVersion();
92                 if (!cur_vers.empty())
93                         buffer += " " + cur_vers;
94
95                 buffer += ")";
96
97                 buffer.append(max_version_len - current->GetRawVersion().length(), ' ');
98         }
99
100         // Pad with spaces until its at max len, max_len must always be >= my names length
101         buffer.append(max_len - current->GetName().length(), ' ');
102
103         buffer += InspIRCd::Format("%5d [%5.2f%%]", current->UserCount, percent);
104
105         if (user->IsOper())
106         {
107                 time_t secs_up = ServerInstance->Time() - current->age;
108                 buffer += " [Up: " + InspIRCd::DurationString(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
109         }
110
111         std::vector<std::string> map;
112         map.push_back(buffer);
113
114         const TreeServer::ChildServers& servers = current->GetChildren();
115         for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
116         {
117                 TreeServer* child = *i;
118
119                 if (IsHidden(user, child))
120                         continue;
121
122                 bool last = true;
123                 for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
124                         if (!IsHidden(user, *j))
125                                 last = false;
126
127                 unsigned int next_len;
128
129                 if (user->IsOper() || !Utils->FlatLinks)
130                 {
131                         // This child is indented by us, so remove the depth from the max length to align the users properly
132                         next_len = max_len - 2;
133                 }
134                 else
135                 {
136                         // This user can not see depth, so max_len remains constant
137                         next_len = max_len;
138                 }
139
140                 // Build the map for this child
141                 std::vector<std::string> child_map = GetMap(user, child, next_len, max_version_len, depth + 1);
142
143                 for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
144                 {
145                         const char* prefix;
146
147                         if (user->IsOper() || !Utils->FlatLinks)
148                         {
149                                 // If this server is not the root child
150                                 if (j != child_map.begin())
151                                 {
152                                         // If this child is not my last child, then add |
153                                         // to be able to "link" the next server in my list to me, and to indent this child's servers
154                                         if (!last)
155                                                 prefix = "| ";
156                                         // Otherwise this is my last child, so just use a space as there's nothing else linked to me below this
157                                         else
158                                                 prefix = "  ";
159                                 }
160                                 // If we get here, this server must be the root child
161                                 else
162                                 {
163                                         // If this is the last child, it gets a `-
164                                         if (last)
165                                                 prefix = "`-";
166                                         // Otherwise this isn't the last child, so it gets |-
167                                         else
168                                                 prefix = "|-";
169                                 }
170                         }
171                         else
172                                 // User can't see depth, so use no prefix
173                                 prefix = "";
174
175                         // Add line to the map
176                         map.push_back(prefix + *j);
177                 }
178         }
179
180         return map;
181 }
182
183 CmdResult CommandMap::Handle(User* user, const Params& parameters)
184 {
185         if (parameters.size() > 0)
186         {
187                 // Remote MAP, the target server is the 1st parameter
188                 TreeServer* s = Utils->FindServerMask(parameters[0]);
189                 if (!s)
190                 {
191                         user->WriteNumeric(ERR_NOSUCHSERVER, parameters[0], "No such server");
192                         return CMD_FAILURE;
193                 }
194
195                 if (!s->IsRoot())
196                         return CMD_SUCCESS;
197         }
198
199         // Max depth and max server name length
200         unsigned int max_depth = 0;
201         unsigned int max_len = 0;
202         unsigned int max_version = 0;
203         GetDepthAndLen(Utils->TreeRoot, 0, max_depth, max_len, max_version);
204
205         unsigned int max;
206         if (user->IsOper() || !Utils->FlatLinks)
207         {
208                 // Each level of the map is indented by 2 characters, making the max possible line (max_depth * 2) + max_len
209                 max = (max_depth * 2) + max_len;
210         }
211         else
212         {
213                 // This user can't see any depth
214                 max = max_len;
215                 if (!user->IsOper())
216                         max_version = 0;
217         }
218
219         std::vector<std::string> map = GetMap(user, Utils->TreeRoot, max, max_version, 0);
220         for (std::vector<std::string>::const_iterator i = map.begin(); i != map.end(); ++i)
221                 user->WriteRemoteNumeric(RPL_MAP, *i);
222
223         size_t totusers = ServerInstance->Users->GetUsers().size();
224         float avg_users = (float) totusers / Utils->serverlist.size();
225
226         user->WriteRemoteNumeric(RPL_MAPUSERS, InspIRCd::Format("%u server%s and %u user%s, average %.2f users per server",
227                 (unsigned int)Utils->serverlist.size(), (Utils->serverlist.size() > 1 ? "s" : ""), (unsigned int)totusers, (totusers > 1 ? "s" : ""), avg_users));
228         user->WriteRemoteNumeric(RPL_ENDMAP, "End of /MAP");
229
230         return CMD_SUCCESS;
231 }
232
233 RouteDescriptor CommandMap::GetRouting(User* user, const Params& parameters)
234 {
235         if (!parameters.empty())
236                 return ROUTE_UNICAST(parameters[0]);
237         return ROUTE_LOCALONLY;
238 }