]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
Make connect class debug logging more complete and consistent.
[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)
57 {
58         if (depth > max_depth)
59                 max_depth = depth;
60         if (current->GetName().length() > max_len)
61                 max_len = current->GetName().length();
62
63         const TreeServer::ChildServers& servers = current->GetChildren();
64         for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
65         {
66                 TreeServer* child = *i;
67                 GetDepthAndLen(child, depth + 1, max_depth, max_len);
68         }
69 }
70
71 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int depth)
72 {
73         float percent = 0;
74
75         const user_hash& users = ServerInstance->Users->GetUsers();
76         if (!users.empty())
77         {
78                 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
79                 percent = current->UserCount * 100.0 / users.size();
80         }
81
82         std::string buffer = current->GetName();
83         if (user->IsOper())
84         {
85                 buffer += " (" + current->GetId();
86
87                 const std::string& cur_vers = current->GetRawVersion();
88                 if (!cur_vers.empty())
89                         buffer += " " + cur_vers;
90
91                 buffer += ")";
92         }
93
94         // Pad with spaces until its at max len, max_len must always be >= my names length
95         buffer.append(max_len - current->GetName().length(), ' ');
96
97         buffer += InspIRCd::Format("%5d [%5.2f%%]", current->UserCount, percent);
98
99         if (user->IsOper())
100         {
101                 time_t secs_up = ServerInstance->Time() - current->age;
102                 buffer += " [Up: " + InspIRCd::DurationString(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
103         }
104
105         std::vector<std::string> map;
106         map.push_back(buffer);
107
108         const TreeServer::ChildServers& servers = current->GetChildren();
109         for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
110         {
111                 TreeServer* child = *i;
112
113                 if (IsHidden(user, child))
114                         continue;
115
116                 bool last = true;
117                 for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
118                         if (!IsHidden(user, *j))
119                                 last = false;
120
121                 unsigned int next_len;
122
123                 if (user->IsOper() || !Utils->FlatLinks)
124                 {
125                         // This child is indented by us, so remove the depth from the max length to align the users properly
126                         next_len = max_len - 2;
127                 }
128                 else
129                 {
130                         // This user can not see depth, so max_len remains constant
131                         next_len = max_len;
132                 }
133
134                 // Build the map for this child
135                 std::vector<std::string> child_map = GetMap(user, child, next_len, depth + 1);
136
137                 for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
138                 {
139                         const char* prefix;
140
141                         if (user->IsOper() || !Utils->FlatLinks)
142                         {
143                                 // If this server is not the root child
144                                 if (j != child_map.begin())
145                                 {
146                                         // If this child is not my last child, then add |
147                                         // to be able to "link" the next server in my list to me, and to indent this child's servers
148                                         if (!last)
149                                                 prefix = "| ";
150                                         // Otherwise this is my last child, so just use a space as there's nothing else linked to me below this
151                                         else
152                                                 prefix = "  ";
153                                 }
154                                 // If we get here, this server must be the root child
155                                 else
156                                 {
157                                         // If this is the last child, it gets a `-
158                                         if (last)
159                                                 prefix = "`-";
160                                         // Otherwise this isn't the last child, so it gets |-
161                                         else
162                                                 prefix = "|-";
163                                 }
164                         }
165                         else
166                                 // User can't see depth, so use no prefix
167                                 prefix = "";
168
169                         // Add line to the map
170                         map.push_back(prefix + *j);
171                 }
172         }
173
174         return map;
175 }
176
177 CmdResult CommandMap::Handle(User* user, const Params& parameters)
178 {
179         if (parameters.size() > 0)
180         {
181                 // Remote MAP, the target server is the 1st parameter
182                 TreeServer* s = Utils->FindServerMask(parameters[0]);
183                 if (!s)
184                 {
185                         user->WriteNumeric(ERR_NOSUCHSERVER, parameters[0], "No such server");
186                         return CMD_FAILURE;
187                 }
188
189                 if (!s->IsRoot())
190                         return CMD_SUCCESS;
191         }
192
193         // Max depth and max server name length
194         unsigned int max_depth = 0;
195         unsigned int max_len = 0;
196         GetDepthAndLen(Utils->TreeRoot, 0, max_depth, max_len);
197
198         unsigned int max;
199         if (user->IsOper() || !Utils->FlatLinks)
200         {
201                 // Each level of the map is indented by 2 characters, making the max possible line (max_depth * 2) + max_len
202                 max = (max_depth * 2) + max_len;
203         }
204         else
205         {
206                 // This user can't see any depth
207                 max = max_len;
208         }
209
210         std::vector<std::string> map = GetMap(user, Utils->TreeRoot, max, 0);
211         for (std::vector<std::string>::const_iterator i = map.begin(); i != map.end(); ++i)
212                 user->WriteRemoteNumeric(RPL_MAP, *i);
213
214         size_t totusers = ServerInstance->Users->GetUsers().size();
215         float avg_users = (float) totusers / Utils->serverlist.size();
216
217         user->WriteRemoteNumeric(RPL_MAPUSERS, InspIRCd::Format("%u server%s and %u user%s, average %.2f users per server",
218                 (unsigned int)Utils->serverlist.size(), (Utils->serverlist.size() > 1 ? "s" : ""), (unsigned int)totusers, (totusers > 1 ? "s" : ""), avg_users));
219         user->WriteRemoteNumeric(RPL_ENDMAP, "End of /MAP");
220
221         return CMD_SUCCESS;
222 }
223
224 RouteDescriptor CommandMap::GetRouting(User* user, const Params& parameters)
225 {
226         if (!parameters.empty())
227                 return ROUTE_UNICAST(parameters[0]);
228         return ROUTE_LOCALONLY;
229 }