2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014 Adam <Adam@anope.org>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "treeserver.h"
30 CommandMap::CommandMap(Module* Creator)
31 : Command(Creator, "MAP", 0, 1)
36 static inline bool IsHidden(User* user, TreeServer* server)
42 if (Utils->HideULines && server->IsULine())
49 // Calculate the map depth the servers go, and the longest server name
50 static void GetDepthAndLen(TreeServer* current, unsigned int depth, unsigned int& max_depth, unsigned int& max_len)
52 if (depth > max_depth)
54 if (current->GetName().length() > max_len)
55 max_len = current->GetName().length();
57 const TreeServer::ChildServers& servers = current->GetChildren();
58 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
60 TreeServer* child = *i;
61 GetDepthAndLen(child, depth + 1, max_depth, max_len);
65 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int depth)
69 const user_hash& users = ServerInstance->Users->GetUsers();
72 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
73 percent = current->UserCount * 100.0 / users.size();
76 std::string buffer = current->GetName();
79 buffer += " (" + current->GetID();
81 const std::string& cur_vers = current->GetRawVersion();
82 if (!cur_vers.empty())
83 buffer += " " + cur_vers;
88 // Pad with spaces until its at max len, max_len must always be >= my names length
89 buffer.append(max_len - current->GetName().length(), ' ');
91 buffer += InspIRCd::Format("%5d [%5.2f%%]", current->UserCount, percent);
95 time_t secs_up = ServerInstance->Time() - current->age;
96 buffer += " [Up: " + ModuleSpanningTree::TimeToStr(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
99 std::vector<std::string> map;
100 map.push_back(buffer);
102 const TreeServer::ChildServers& servers = current->GetChildren();
103 for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
105 TreeServer* child = *i;
107 if (IsHidden(user, child))
111 for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
112 if (!IsHidden(user, *j))
115 unsigned int next_len;
117 if (user->IsOper() || !Utils->FlatLinks)
119 // This child is indented by us, so remove the depth from the max length to align the users properly
120 next_len = max_len - 2;
124 // This user can not see depth, so max_len remains constant
128 // Build the map for this child
129 std::vector<std::string> child_map = GetMap(user, child, next_len, depth + 1);
131 for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
135 if (user->IsOper() || !Utils->FlatLinks)
137 // If this server is not the root child
138 if (j != child_map.begin())
140 // If this child is not my last child, then add |
141 // to be able to "link" the next server in my list to me, and to indent this childs servers
144 // Otherwise this is my last child, so just use a space as theres nothing else linked to me below this
148 // If we get here, this server must be the root child
151 // If this is the last child, it gets a `-
154 // Otherwise this isn't the last child, so it gets |-
160 // User can't see depth, so use no prefix
163 // Add line to the map
164 map.push_back(prefix + *j);
171 CmdResult CommandMap::Handle(User* user, const Params& parameters)
173 if (parameters.size() > 0)
175 // Remote MAP, the target server is the 1st parameter
176 TreeServer* s = Utils->FindServerMask(parameters[0]);
179 user->WriteNumeric(ERR_NOSUCHSERVER, parameters[0], "No such server");
187 // Max depth and max server name length
188 unsigned int max_depth = 0;
189 unsigned int max_len = 0;
190 GetDepthAndLen(Utils->TreeRoot, 0, max_depth, max_len);
193 if (user->IsOper() || !Utils->FlatLinks)
195 // Each level of the map is indented by 2 characters, making the max possible line (max_depth * 2) + max_len
196 max = (max_depth * 2) + max_len;
200 // This user can't see any depth
204 std::vector<std::string> map = GetMap(user, Utils->TreeRoot, max, 0);
205 for (std::vector<std::string>::const_iterator i = map.begin(); i != map.end(); ++i)
206 user->WriteRemoteNumeric(RPL_MAP, *i);
208 size_t totusers = ServerInstance->Users->GetUsers().size();
209 float avg_users = (float) totusers / Utils->serverlist.size();
211 user->WriteRemoteNumeric(RPL_MAPUSERS, InspIRCd::Format("%u server%s and %u user%s, average %.2f users per server",
212 (unsigned int)Utils->serverlist.size(), (Utils->serverlist.size() > 1 ? "s" : ""), (unsigned int)totusers, (totusers > 1 ? "s" : ""), avg_users));
213 user->WriteRemoteNumeric(RPL_ENDMAP, "End of /MAP");
218 RouteDescriptor CommandMap::GetRouting(User* user, const Params& parameters)
220 if (!parameters.empty())
221 return ROUTE_UNICAST(parameters[0]);
222 return ROUTE_LOCALONLY;