]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
216fd4d66d53f030babff73c1f274a0483ee0e90
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / override_map.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #include "inspircd.h"
24
25 #include "main.h"
26 #include "utils.h"
27 #include "treeserver.h"
28 #include "commands.h"
29
30 CommandMap::CommandMap(Module* Creator)
31         : Command(Creator, "MAP", 0, 1)
32 {
33         Penalty = 2;
34 }
35
36 static inline bool IsHidden(User* user, TreeServer* server)
37 {
38         if (!user->IsOper())
39         {
40                 if (server->Hidden)
41                         return true;
42                 if (Utils->HideULines && server->IsULine())
43                         return true;
44         }
45
46         return false;
47 }
48
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)
51 {
52         if (depth > max_depth)
53                 max_depth = depth;
54         if (current->GetName().length() > max_len)
55                 max_len = current->GetName().length();
56
57         const TreeServer::ChildServers& servers = current->GetChildren();
58         for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
59         {
60                 TreeServer* child = *i;
61                 GetDepthAndLen(child, depth + 1, max_depth, max_len);
62         }
63 }
64
65 static std::vector<std::string> GetMap(User* user, TreeServer* current, unsigned int max_len, unsigned int depth)
66 {
67         float percent = 0;
68
69         if (!ServerInstance->Users->clientlist->empty())
70         {
71                 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
72                 percent = current->UserCount * 100.0 / ServerInstance->Users->clientlist->size();
73         }
74
75         std::string buffer = current->GetName();
76         if (user->IsOper())
77         {
78                 buffer += " (" + current->GetID() + ")";
79         }
80
81         // Pad with spaces until its at max len, max_len must always be >= my names length
82         buffer.append(max_len - current->GetName().length(), ' ');
83
84         char buf[16];
85         snprintf(buf, sizeof(buf), "%5d [%5.2f%%]", current->UserCount, percent);
86         buffer += buf;
87
88         if (user->IsOper())
89         {
90                 time_t secs_up = ServerInstance->Time() - current->age;
91                 buffer += " [Up: " + ModuleSpanningTree::TimeToStr(secs_up) + (current->rtt == 0 ? "]" : " Lag: " + ConvToStr(current->rtt) + "ms]");
92         }
93
94         std::vector<std::string> map;
95         map.push_back(buffer);
96
97         const TreeServer::ChildServers& servers = current->GetChildren();
98         for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
99         {
100                 TreeServer* child = *i;
101
102                 if (IsHidden(user, child))
103                         continue;
104
105                 bool last = true;
106                 for (TreeServer::ChildServers::const_iterator j = i + 1; last && j != servers.end(); ++j)
107                         if (!IsHidden(user, *j))
108                                 last = false;
109
110                 unsigned int next_len;
111
112                 if (user->IsOper() || !Utils->FlatLinks)
113                 {
114                         // This child is indented by us, so remove the depth from the max length to align the users properly
115                         next_len = max_len - 2;
116                 }
117                 else
118                 {
119                         // This user can not see depth, so max_len remains constant
120                         next_len = max_len;
121                 }
122
123                 // Build the map for this child
124                 std::vector<std::string> child_map = GetMap(user, child, next_len, depth + 1);
125
126                 for (std::vector<std::string>::const_iterator j = child_map.begin(); j != child_map.end(); ++j)
127                 {
128                         const char* prefix;
129
130                         if (user->IsOper() || !Utils->FlatLinks)
131                         {
132                                 // If this server is not the root child
133                                 if (j != child_map.begin())
134                                 {
135                                         // If this child is not my last child, then add |
136                                         // to be able to "link" the next server in my list to me, and to indent this childs servers
137                                         if (!last)
138                                                 prefix = "| ";
139                                         // Otherwise this is my last child, so just use a space as theres nothing else linked to me below this
140                                         else
141                                                 prefix = "  ";
142                                 }
143                                 // If we get here, this server must be the root child
144                                 else
145                                 {
146                                         // If this is the last child, it gets a `-
147                                         if (last)
148                                                 prefix = "`-";
149                                         // Otherwise this isn't the last child, so it gets |-
150                                         else
151                                                 prefix = "|-";
152                                 }
153                         }
154                         else
155                                 // User can't see depth, so use no prefix
156                                 prefix = "";
157
158                         // Add line to the map
159                         map.push_back(prefix + *j);
160                 }
161         }
162
163         return map;
164 }
165
166 CmdResult CommandMap::Handle(const std::vector<std::string>& parameters, User* user)
167 {
168         if (parameters.size() > 0)
169         {
170                 /* Remote MAP, the server is within the 1st parameter */
171                 TreeServer* s = Utils->FindServerMask(parameters[0]);
172                 if (!s)
173                 {
174                         user->WriteNumeric(ERR_NOSUCHSERVER, "%s :No such server", parameters[0].c_str());
175                         return CMD_FAILURE;
176                 }
177
178                 if (!s->IsRoot())
179                         return CMD_SUCCESS;
180         }
181
182         // Max depth and max server name length
183         unsigned int max_depth = 0;
184         unsigned int max_len = 0;
185         GetDepthAndLen(Utils->TreeRoot, 0, max_depth, max_len);
186
187         unsigned int max;
188         if (user->IsOper() || !Utils->FlatLinks)
189         {
190                 // Each level of the map is indented by 2 characters, making the max possible line (max_depth * 2) + max_len
191                 max = (max_depth * 2) + max_len;
192         }
193         else
194         {
195                 // This user can't see any depth
196                 max = max_len;
197         }
198
199         std::vector<std::string> map = GetMap(user, Utils->TreeRoot, max, 0);
200         for (std::vector<std::string>::const_iterator i = map.begin(); i != map.end(); ++i)
201                 user->SendText(":%s %03d %s :%s", ServerInstance->Config->ServerName.c_str(),
202                         RPL_MAP, user->nick.c_str(), i->c_str());
203
204         size_t totusers = ServerInstance->Users->clientlist->size();
205         float avg_users = (float) totusers / Utils->serverlist.size();
206
207         user->SendText(":%s %03d %s :%u server%s and %u user%s, average %.2f users per server",
208                 ServerInstance->Config->ServerName.c_str(), RPL_MAPUSERS, user->nick.c_str(),
209                 (unsigned int)Utils->serverlist.size(), (Utils->serverlist.size() > 1 ? "s" : ""), (unsigned int)totusers, (totusers > 1 ? "s" : ""), avg_users);
210         user->SendText(":%s %03d %s :End of /MAP", ServerInstance->Config->ServerName.c_str(),
211                 RPL_ENDMAP, user->nick.c_str());
212
213         return CMD_SUCCESS;
214 }
215
216 RouteDescriptor CommandMap::GetRouting(User* user, const std::vector<std::string>& parameters)
217 {
218         if (!parameters.empty())
219                 return ROUTE_UNICAST(parameters[0]);
220         return ROUTE_LOCALONLY;
221 }