2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5 * Copyright (C) 2016, 2018-2019 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)
42 static inline bool IsHidden(User* user, TreeServer* server)
48 if (Utils->HideULines && server->IsULine())
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)
58 if (depth > max_depth)
60 if (current->GetName().length() > max_len)
61 max_len = current->GetName().length();
63 const TreeServer::ChildServers& servers = current->GetChildren();
64 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
66 TreeServer* child = *i;
67 GetDepthAndLen(child, depth + 1, max_depth, max_len);
71 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int depth)
75 const user_hash& users = ServerInstance->Users->GetUsers();
78 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
79 percent = current->UserCount * 100.0 / users.size();
82 std::string buffer = current->GetName();
85 buffer += " (" + current->GetId();
87 const std::string& cur_vers = current->GetRawVersion();
88 if (!cur_vers.empty())
89 buffer += " " + cur_vers;
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(), ' ');
97 buffer += InspIRCd::Format("%5d [%5.2f%%]", current->UserCount, percent);
101 time_t secs_up = ServerInstance->Time() - current->age;
102 buffer += " [Up: " + InspIRCd::DurationString(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
105 std::vector<std::string> map;
106 map.push_back(buffer);
108 const TreeServer::ChildServers& servers = current->GetChildren();
109 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
111 TreeServer* child = *i;
113 if (IsHidden(user, child))
117 for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
118 if (!IsHidden(user, *j))
121 unsigned int next_len;
123 if (user->IsOper() || !Utils->FlatLinks)
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;
130 // This user can not see depth, so max_len remains constant
134 // Build the map for this child
135 std::vector<std::string> child_map = GetMap(user, child, next_len, depth + 1);
137 for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
141 if (user->IsOper() || !Utils->FlatLinks)
143 // If this server is not the root child
144 if (j != child_map.begin())
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 childs servers
150 // Otherwise this is my last child, so just use a space as theres nothing else linked to me below this
154 // If we get here, this server must be the root child
157 // If this is the last child, it gets a `-
160 // Otherwise this isn't the last child, so it gets |-
166 // User can't see depth, so use no prefix
169 // Add line to the map
170 map.push_back(prefix + *j);
177 CmdResult CommandMap::Handle(User* user, const Params& parameters)
179 if (parameters.size() > 0)
181 // Remote MAP, the target server is the 1st parameter
182 TreeServer* s = Utils->FindServerMask(parameters[0]);
185 user->WriteNumeric(ERR_NOSUCHSERVER, parameters[0], "No such server");
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);
199 if (user->IsOper() || !Utils->FlatLinks)
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;
206 // This user can't see any depth
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);
214 size_t totusers = ServerInstance->Users->GetUsers().size();
215 float avg_users = (float) totusers / Utils->serverlist.size();
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");
224 RouteDescriptor CommandMap::GetRouting(User* user, const Params& parameters)
226 if (!parameters.empty())
227 return ROUTE_UNICAST(parameters[0]);
228 return ROUTE_LOCALONLY;