2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
6 * Copyright (C) 2016, 2018-2020 Sadie Powell <sadie@witchery.services>
7 * Copyright (C) 2014 Adam <Adam@anope.org>
8 * Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
9 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
10 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
12 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
13 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
14 * Copyright (C) 2007, 2010 Craig Edwards <brain@inspircd.org>
16 * This file is part of InspIRCd. InspIRCd is free software: you can
17 * redistribute it and/or modify it under the terms of the GNU General Public
18 * License as published by the Free Software Foundation, version 2.
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "treeserver.h"
37 CommandMap::CommandMap(Module* Creator)
38 : Command(Creator, "MAP", 0, 1)
40 allow_empty_last_param = false;
44 static inline bool IsHidden(User* user, TreeServer* server)
50 if (Utils->HideULines && server->IsULine())
57 // Calculate the map depth the servers go, and the longest server name
58 static void GetDepthAndLen(TreeServer* current, unsigned int depth, unsigned int& max_depth, unsigned int& max_len, unsigned int& max_version)
60 if (depth > max_depth)
63 if (current->GetName().length() > max_len)
64 max_len = current->GetName().length();
66 if (current->GetRawVersion().length() > max_version)
67 max_version = current->GetRawVersion().length();
69 const TreeServer::ChildServers& servers = current->GetChildren();
70 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
72 TreeServer* child = *i;
73 GetDepthAndLen(child, depth + 1, max_depth, max_len, max_version);
77 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int max_version_len, unsigned int depth)
81 const user_hash& users = ServerInstance->Users->GetUsers();
84 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
85 percent = current->UserCount * 100.0 / users.size();
88 std::string buffer = current->GetName();
91 buffer += " (" + current->GetId();
93 const std::string& cur_vers = current->GetRawVersion();
94 if (!cur_vers.empty())
95 buffer += " " + cur_vers;
99 buffer.append(max_version_len - current->GetRawVersion().length(), ' ');
102 // Pad with spaces until its at max len, max_len must always be >= my names length
103 buffer.append(max_len - current->GetName().length(), ' ');
105 buffer += InspIRCd::Format("%5d [%5.2f%%]", current->UserCount, percent);
109 time_t secs_up = ServerInstance->Time() - current->age;
110 buffer += " [Up: " + InspIRCd::DurationString(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
113 std::vector<std::string> map;
114 map.push_back(buffer);
116 const TreeServer::ChildServers& servers = current->GetChildren();
117 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
119 TreeServer* child = *i;
121 if (IsHidden(user, child))
125 for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
126 if (!IsHidden(user, *j))
129 unsigned int next_len;
131 if (user->IsOper() || !Utils->FlatLinks)
133 // This child is indented by us, so remove the depth from the max length to align the users properly
134 next_len = max_len - 2;
138 // This user can not see depth, so max_len remains constant
142 // Build the map for this child
143 std::vector<std::string> child_map = GetMap(user, child, next_len, max_version_len, depth + 1);
145 for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
149 if (user->IsOper() || !Utils->FlatLinks)
151 // If this server is not the root child
152 if (j != child_map.begin())
154 // If this child is not my last child, then add |
155 // to be able to "link" the next server in my list to me, and to indent this child's servers
158 // Otherwise this is my last child, so just use a space as there's nothing else linked to me below this
162 // If we get here, this server must be the root child
165 // If this is the last child, it gets a `-
168 // Otherwise this isn't the last child, so it gets |-
174 // User can't see depth, so use no prefix
177 // Add line to the map
178 map.push_back(prefix + *j);
185 CmdResult CommandMap::Handle(User* user, const Params& parameters)
187 if (parameters.size() > 0)
189 // Remote MAP, the target server is the 1st parameter
190 TreeServer* s = Utils->FindServerMask(parameters[0]);
193 user->WriteNumeric(ERR_NOSUCHSERVER, parameters[0], "No such server");
201 // Max depth and max server name length
202 unsigned int max_depth = 0;
203 unsigned int max_len = 0;
204 unsigned int max_version = 0;
205 GetDepthAndLen(Utils->TreeRoot, 0, max_depth, max_len, max_version);
208 if (user->IsOper() || !Utils->FlatLinks)
210 // Each level of the map is indented by 2 characters, making the max possible line (max_depth * 2) + max_len
211 max = (max_depth * 2) + max_len;
215 // This user can't see any depth
221 std::vector<std::string> map = GetMap(user, Utils->TreeRoot, max, max_version, 0);
222 for (std::vector<std::string>::const_iterator i = map.begin(); i != map.end(); ++i)
223 user->WriteRemoteNumeric(RPL_MAP, *i);
225 size_t totusers = ServerInstance->Users->GetUsers().size();
226 float avg_users = (float) totusers / Utils->serverlist.size();
228 user->WriteRemoteNumeric(RPL_MAPUSERS, InspIRCd::Format("%u server%s and %u user%s, average %.2f users per server",
229 (unsigned int)Utils->serverlist.size(), (Utils->serverlist.size() > 1 ? "s" : ""), (unsigned int)totusers, (totusers > 1 ? "s" : ""), avg_users));
230 user->WriteRemoteNumeric(RPL_ENDMAP, "End of /MAP");
235 RouteDescriptor CommandMap::GetRouting(User* user, const Params& parameters)
237 if (!parameters.empty())
238 return ROUTE_UNICAST(parameters[0]);
239 return ROUTE_LOCALONLY;