1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
15  *
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.
19  *
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
23  * details.
24  *
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/>.
27  */
28 
29 
30 #include "inspircd.h"
31 
32 #include "main.h"
33 #include "utils.h"
34 #include "treeserver.h"
35 #include "commands.h"
36 
CommandMap(Module * Creator)37 CommandMap::CommandMap(Module* Creator)
38 	: Command(Creator, "MAP", 0, 1)
39 {
40 	allow_empty_last_param = false;
41 	Penalty = 2;
42 }
43 
IsHidden(User * user,TreeServer * server)44 static inline bool IsHidden(User* user, TreeServer* server)
45 {
46 	if (!user->IsOper())
47 	{
48 		if (server->Hidden)
49 			return true;
50 		if (Utils->HideULines && server->IsULine())
51 			return true;
52 	}
53 
54 	return false;
55 }
56 
57 // Calculate the map depth the servers go, and the longest server name
GetDepthAndLen(TreeServer * current,unsigned int depth,unsigned int & max_depth,unsigned int & max_len,unsigned int & max_version)58 static void GetDepthAndLen(TreeServer* current, unsigned int depth, unsigned int& max_depth, unsigned int& max_len, unsigned int& max_version)
59 {
60 	if (depth > max_depth)
61 		max_depth = depth;
62 
63 	if (current->GetName().length() > max_len)
64 		max_len = current->GetName().length();
65 
66 	if (current->GetRawVersion().length() > max_version)
67 		max_version = current->GetRawVersion().length();
68 
69 	const TreeServer::ChildServers& servers = current->GetChildren();
70 	for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
71 	{
72 		TreeServer* child = *i;
73 		GetDepthAndLen(child, depth + 1, max_depth, max_len, max_version);
74 	}
75 }
76 
GetMap(User * user,TreeServer * current,unsigned int max_len,unsigned int max_version_len,unsigned int depth)77 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int max_version_len, unsigned int depth)
78 {
79 	float percent = 0;
80 
81 	const user_hash& users = ServerInstance->Users->GetUsers();
82 	if (!users.empty())
83 	{
84 		// If there are no users, WHO THE HELL DID THE /MAP?!?!?!
85 		percent = current->UserCount * 100.0 / users.size();
86 	}
87 
88 	std::string buffer = current->GetName();
89 	if (user->IsOper())
90 	{
91 		buffer += " (" + current->GetId();
92 
93 		const std::string& cur_vers = current->GetRawVersion();
94 		if (!cur_vers.empty())
95 			buffer += " " + cur_vers;
96 
97 		buffer += ")";
98 
99 		buffer.append(max_version_len - current->GetRawVersion().length(), ' ');
100 	}
101 
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(), ' ');
104 
105 	buffer += InspIRCd::Format("%5d [%5.2f%%]", current->UserCount, percent);
106 
107 	if (user->IsOper())
108 	{
109 		time_t secs_up = ServerInstance->Time() - current->age;
110 		buffer += " [Up: " + InspIRCd::DurationString(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
111 	}
112 
113 	std::vector<std::string> map;
114 	map.push_back(buffer);
115 
116 	const TreeServer::ChildServers& servers = current->GetChildren();
117 	for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
118 	{
119 		TreeServer* child = *i;
120 
121 		if (IsHidden(user, child))
122 			continue;
123 
124 		bool last = true;
125 		for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
126 			if (!IsHidden(user, *j))
127 				last = false;
128 
129 		unsigned int next_len;
130 
131 		if (user->IsOper() || !Utils->FlatLinks)
132 		{
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;
135 		}
136 		else
137 		{
138 			// This user can not see depth, so max_len remains constant
139 			next_len = max_len;
140 		}
141 
142 		// Build the map for this child
143 		std::vector<std::string> child_map = GetMap(user, child, next_len, max_version_len, depth + 1);
144 
145 		for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
146 		{
147 			const char* prefix;
148 
149 			if (user->IsOper() || !Utils->FlatLinks)
150 			{
151 				// If this server is not the root child
152 				if (j != child_map.begin())
153 				{
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
156 					if (!last)
157 						prefix = "| ";
158 					// Otherwise this is my last child, so just use a space as there's nothing else linked to me below this
159 					else
160 						prefix = "  ";
161 				}
162 				// If we get here, this server must be the root child
163 				else
164 				{
165 					// If this is the last child, it gets a `-
166 					if (last)
167 						prefix = "`-";
168 					// Otherwise this isn't the last child, so it gets |-
169 					else
170 						prefix = "|-";
171 				}
172 			}
173 			else
174 				// User can't see depth, so use no prefix
175 				prefix = "";
176 
177 			// Add line to the map
178 			map.push_back(prefix + *j);
179 		}
180 	}
181 
182 	return map;
183 }
184 
Handle(User * user,const Params & parameters)185 CmdResult CommandMap::Handle(User* user, const Params& parameters)
186 {
187 	if (parameters.size() > 0)
188 	{
189 		// Remote MAP, the target server is the 1st parameter
190 		TreeServer* s = Utils->FindServerMask(parameters[0]);
191 		if (!s)
192 		{
193 			user->WriteNumeric(ERR_NOSUCHSERVER, parameters[0], "No such server");
194 			return CMD_FAILURE;
195 		}
196 
197 		if (!s->IsRoot())
198 			return CMD_SUCCESS;
199 	}
200 
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);
206 
207 	unsigned int max;
208 	if (user->IsOper() || !Utils->FlatLinks)
209 	{
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;
212 	}
213 	else
214 	{
215 		// This user can't see any depth
216 		max = max_len;
217 		if (!user->IsOper())
218 			max_version = 0;
219 	}
220 
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);
224 
225 	size_t totusers = ServerInstance->Users->GetUsers().size();
226 	float avg_users = (float) totusers / Utils->serverlist.size();
227 
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");
231 
232 	return CMD_SUCCESS;
233 }
234 
GetRouting(User * user,const Params & parameters)235 RouteDescriptor CommandMap::GetRouting(User* user, const Params& parameters)
236 {
237 	if (!parameters.empty())
238 		return ROUTE_UNICAST(parameters[0]);
239 	return ROUTE_LOCALONLY;
240 }
241