2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
33 #include "treeserver.h"
36 CommandMap::CommandMap(Module* Creator)
37 : Command(Creator, "MAP", 0, 1)
39 allow_empty_last_param = false;
43 static inline bool IsHidden(User* user, TreeServer* server)
49 if (Utils->HideULines && server->IsULine())
56 // Calculate the map depth the servers go, and the longest server name
57 static void GetDepthAndLen(TreeServer* current, unsigned int depth, unsigned int& max_depth, unsigned int& max_len, unsigned int& max_version)
59 if (depth > max_depth)
62 if (current->GetName().length() > max_len)
63 max_len = current->GetName().length();
65 if (current->GetRawVersion().length() > max_version)
66 max_version = current->GetRawVersion().length();
68 const TreeServer::ChildServers& servers = current->GetChildren();
69 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
71 TreeServer* child = *i;
72 GetDepthAndLen(child, depth + 1, max_depth, max_len, max_version);
76 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int max_version_len, unsigned int depth)
80 const user_hash& users = ServerInstance->Users->GetUsers();
83 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
84 percent = current->UserCount * 100.0 / users.size();
87 std::string buffer = current->GetName();
90 buffer += " (" + current->GetId();
92 const std::string& cur_vers = current->GetRawVersion();
93 if (!cur_vers.empty())
94 buffer += " " + cur_vers;
98 buffer.append(max_version_len - current->GetRawVersion().length(), ' ');
101 // Pad with spaces until its at max len, max_len must always be >= my names length
102 buffer.append(max_len - current->GetName().length(), ' ');
104 buffer += InspIRCd::Format("%5d [%5.2f%%]", current->UserCount, percent);
108 time_t secs_up = ServerInstance->Time() - current->age;
109 buffer += " [Up: " + InspIRCd::DurationString(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
112 std::vector<std::string> map;
113 map.push_back(buffer);
115 const TreeServer::ChildServers& servers = current->GetChildren();
116 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
118 TreeServer* child = *i;
120 if (IsHidden(user, child))
124 for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
125 if (!IsHidden(user, *j))
128 unsigned int next_len;
130 if (user->IsOper() || !Utils->FlatLinks)
132 // This child is indented by us, so remove the depth from the max length to align the users properly
133 next_len = max_len - 2;
137 // This user can not see depth, so max_len remains constant
141 // Build the map for this child
142 std::vector<std::string> child_map = GetMap(user, child, next_len, max_version_len, depth + 1);
144 for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
148 if (user->IsOper() || !Utils->FlatLinks)
150 // If this server is not the root child
151 if (j != child_map.begin())
153 // If this child is not my last child, then add |
154 // to be able to "link" the next server in my list to me, and to indent this child's servers
157 // Otherwise this is my last child, so just use a space as there's nothing else linked to me below this
161 // If we get here, this server must be the root child
164 // If this is the last child, it gets a `-
167 // Otherwise this isn't the last child, so it gets |-
173 // User can't see depth, so use no prefix
176 // Add line to the map
177 map.push_back(prefix + *j);
184 CmdResult CommandMap::Handle(User* user, const Params& parameters)
186 if (parameters.size() > 0)
188 // Remote MAP, the target server is the 1st parameter
189 TreeServer* s = Utils->FindServerMask(parameters[0]);
192 user->WriteNumeric(ERR_NOSUCHSERVER, parameters[0], "No such server");
200 // Max depth and max server name length
201 unsigned int max_depth = 0;
202 unsigned int max_len = 0;
203 unsigned int max_version = 0;
204 GetDepthAndLen(Utils->TreeRoot, 0, max_depth, max_len, max_version);
207 if (user->IsOper() || !Utils->FlatLinks)
209 // Each level of the map is indented by 2 characters, making the max possible line (max_depth * 2) + max_len
210 max = (max_depth * 2) + max_len;
214 // This user can't see any depth
220 std::vector<std::string> map = GetMap(user, Utils->TreeRoot, max, max_version, 0);
221 for (std::vector<std::string>::const_iterator i = map.begin(); i != map.end(); ++i)
222 user->WriteRemoteNumeric(RPL_MAP, *i);
224 size_t totusers = ServerInstance->Users->GetUsers().size();
225 float avg_users = (float) totusers / Utils->serverlist.size();
227 user->WriteRemoteNumeric(RPL_MAPUSERS, InspIRCd::Format("%u server%s and %u user%s, average %.2f users per server",
228 (unsigned int)Utils->serverlist.size(), (Utils->serverlist.size() > 1 ? "s" : ""), (unsigned int)totusers, (totusers > 1 ? "s" : ""), avg_users));
229 user->WriteRemoteNumeric(RPL_ENDMAP, "End of /MAP");
234 RouteDescriptor CommandMap::GetRouting(User* user, const Params& parameters)
236 if (!parameters.empty())
237 return ROUTE_UNICAST(parameters[0]);
238 return ROUTE_LOCALONLY;