]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
a9b68eb9b18b238ed5569b9b83907fdf08ec8f8c
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / override_map.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 #include "main.h"
25 #include "utils.h"
26 #include "treeserver.h"
27 #include "commands.h"
28
29 CommandMap::CommandMap(Module* Creator)
30         : Command(Creator, "MAP", 0, 1)
31 {
32         Penalty = 2;
33 }
34
35 std::string CommandMap::MapOperInfo(TreeServer* Current)
36 {
37         time_t secs_up = ServerInstance->Time() - Current->age;
38         return " [Up: " + ModuleSpanningTree::TimeToStr(secs_up) + (Current->rtt == 0 ? "]" : " Lag: " + ConvToStr(Current->rtt) + "ms]");
39 }
40
41 void CommandMap::ShowMap(TreeServer* Current, User* user, int depth, int &line, char* names, int &maxnamew, char* stats)
42 {
43         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "ShowMap depth %d on line %d", depth, line);
44         float percent;
45
46         if (ServerInstance->Users->clientlist->size() == 0)
47         {
48                 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
49                 percent = 0;
50         }
51         else
52         {
53                 percent = Current->UserCount * 100.0 / ServerInstance->Users->clientlist->size();
54         }
55
56         const std::string operdata = user->IsOper() ? MapOperInfo(Current) : "";
57
58         char* myname = names + 100 * line;
59         char* mystat = stats + 50 * line;
60         memset(myname, ' ', depth);
61         int w = depth;
62
63         if (user->IsOper())
64         {
65                 w += snprintf(myname + depth, 99 - depth, "%s (%s)", Current->GetName().c_str(), Current->GetID().c_str());
66         }
67         else
68         {
69                 w += snprintf(myname + depth, 99 - depth, "%s", Current->GetName().c_str());
70         }
71         memset(myname + w, ' ', 100 - w);
72         if (w > maxnamew)
73                 maxnamew = w;
74         snprintf(mystat, 49, "%5d [%5.2f%%]%s", Current->UserCount, percent, operdata.c_str());
75
76         line++;
77
78         if (user->IsOper() || !Utils->FlatLinks)
79                 depth = depth + 2;
80
81         const TreeServer::ChildServers& servers = Current->GetChildren();
82         for (TreeServer::ChildServers::const_iterator i = servers.begin(); i != servers.end(); ++i)
83         {
84                 TreeServer* child = *i;
85                 if (!user->IsOper()) {
86                         if (child->Hidden)
87                                 continue;
88                         if ((Utils->HideULines) && (child->IsULine()))
89                                 continue;
90                 }
91                 ShowMap(child, user, depth, line, names, maxnamew, stats);
92         }
93 }
94
95
96 // Ok, prepare to be confused.
97 // After much mulling over how to approach this, it struck me that
98 // the 'usual' way of doing a /MAP isnt the best way. Instead of
99 // keeping track of a ton of ascii characters, and line by line
100 // under recursion working out where to place them using multiplications
101 // and divisons, we instead render the map onto a backplane of characters
102 // (a character matrix), then draw the branches as a series of "L" shapes
103 // from the nodes. This is not only friendlier on CPU it uses less stack.
104 CmdResult CommandMap::Handle(const std::vector<std::string>& parameters, User* user)
105 {
106         if (parameters.size() > 0)
107         {
108                 /* Remote MAP, the server is within the 1st parameter */
109                 TreeServer* s = Utils->FindServerMask(parameters[0]);
110                 if (!s)
111                 {
112                         user->WriteNumeric(ERR_NOSUCHSERVER, "%s :No such server", parameters[0].c_str());
113                         return CMD_FAILURE;
114                 }
115
116                 if (!s->IsRoot())
117                         return CMD_SUCCESS;
118         }
119
120         // These arrays represent a virtual screen which we will
121         // "scratch" draw to, as the console device of an irc
122         // client does not provide for a proper terminal.
123         int totusers = ServerInstance->Users->clientlist->size();
124         int totservers = Utils->serverlist.size();
125         int maxnamew = 0;
126         int line = 0;
127         char* names = new char[totservers * 100];
128         char* stats = new char[totservers * 50];
129
130         // The only recursive bit is called here.
131         ShowMap(Utils->TreeRoot,user,0,line,names,maxnamew,stats);
132
133         // Process each line one by one.
134         for (int l = 1; l < line; l++)
135         {
136                 char* myname = names + 100 * l;
137                 // scan across the line looking for the start of the
138                 // servername (the recursive part of the algorithm has placed
139                 // the servers at indented positions depending on what they
140                 // are related to)
141                 int first_nonspace = 0;
142
143                 while (myname[first_nonspace] == ' ')
144                 {
145                         first_nonspace++;
146                 }
147
148                 first_nonspace--;
149
150                 // Draw the `- (corner) section: this may be overwritten by
151                 // another L shape passing along the same vertical pane, becoming
152                 // a |- (branch) section instead.
153
154                 myname[first_nonspace] = '-';
155                 myname[first_nonspace-1] = '`';
156                 int l2 = l - 1;
157
158                 // Draw upwards until we hit the parent server, causing possibly
159                 // other corners (`-) to become branches (|-)
160                 while ((names[l2 * 100 + first_nonspace-1] == ' ') || (names[l2 * 100 + first_nonspace-1] == '`'))
161                 {
162                         names[l2 * 100 + first_nonspace-1] = '|';
163                         l2--;
164                 }
165         }
166
167         float avg_users = totusers * 1.0 / line;
168
169         for (int t = 0; t < line; t++)
170         {
171                 // terminate the string at maxnamew characters
172                 names[100 * t + maxnamew] = '\0';
173                 user->SendText(":%s %03d %s :%s %s", ServerInstance->Config->ServerName.c_str(),
174                         RPL_MAP, user->nick.c_str(), names + 100 * t, stats + 50 * t);
175         }
176         user->SendText(":%s %03d %s :%d server%s and %d user%s, average %.2f users per server",
177                 ServerInstance->Config->ServerName.c_str(), RPL_MAPUSERS, user->nick.c_str(),
178                 line, (line > 1 ? "s" : ""), totusers, (totusers > 1 ? "s" : ""), avg_users);
179         user->SendText(":%s %03d %s :End of /MAP", ServerInstance->Config->ServerName.c_str(),
180                 RPL_ENDMAP, user->nick.c_str());
181
182         delete[] names;
183         delete[] stats;
184
185         return CMD_SUCCESS;
186 }
187
188 RouteDescriptor CommandMap::GetRouting(User* user, const std::vector<std::string>& parameters)
189 {
190         if (!parameters.empty())
191                 return ROUTE_UNICAST(parameters[0]);
192         return ROUTE_LOCALONLY;
193 }