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