]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
Merge pull request #590 from SaberUK/master+module-logging
[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
28 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h */
29
30 const std::string ModuleSpanningTree::MapOperInfo(TreeServer* Current)
31 {
32         time_t secs_up = ServerInstance->Time() - Current->age;
33         return " [Up: " + TimeToStr(secs_up) + (Current->rtt == 0 ? "]" : " Lag: " + ConvToStr(Current->rtt) + "ms]");
34 }
35
36 void ModuleSpanningTree::ShowMap(TreeServer* Current, User* user, int depth, int &line, char* names, int &maxnamew, char* stats)
37 {
38         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "ShowMap depth %d on line %d", depth, line);
39         float percent;
40
41         if (ServerInstance->Users->clientlist->size() == 0)
42         {
43                 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
44                 percent = 0;
45         }
46         else
47         {
48                 percent = Current->UserCount * 100.0 / ServerInstance->Users->clientlist->size();
49         }
50
51         const std::string operdata = user->IsOper() ? MapOperInfo(Current) : "";
52
53         char* myname = names + 100 * line;
54         char* mystat = stats + 50 * line;
55         memset(myname, ' ', depth);
56         int w = depth;
57
58         if (user->IsOper())
59         {
60                 w += snprintf(myname + depth, 99 - depth, "%s (%s)", Current->GetName().c_str(), Current->GetID().c_str());
61         }
62         else
63         {
64                 w += snprintf(myname + depth, 99 - depth, "%s", Current->GetName().c_str());
65         }
66         memset(myname + w, ' ', 100 - w);
67         if (w > maxnamew)
68                 maxnamew = w;
69         snprintf(mystat, 49, "%5d [%5.2f%%]%s", Current->UserCount, percent, operdata.c_str());
70
71         line++;
72
73         if (user->IsOper() || !Utils->FlatLinks)
74                 depth = depth + 2;
75         for (unsigned int q = 0; q < Current->ChildCount(); q++)
76         {
77                 TreeServer* child = Current->GetChild(q);
78                 if (!user->IsOper()) {
79                         if (child->Hidden)
80                                 continue;
81                         if ((Utils->HideULines) && (ServerInstance->ULine(child->GetName())))
82                                 continue;
83                 }
84                 ShowMap(child, user, depth, line, names, maxnamew, stats);
85         }
86 }
87
88
89 // Ok, prepare to be confused.
90 // After much mulling over how to approach this, it struck me that
91 // the 'usual' way of doing a /MAP isnt the best way. Instead of
92 // keeping track of a ton of ascii characters, and line by line
93 // under recursion working out where to place them using multiplications
94 // and divisons, we instead render the map onto a backplane of characters
95 // (a character matrix), then draw the branches as a series of "L" shapes
96 // from the nodes. This is not only friendlier on CPU it uses less stack.
97 bool ModuleSpanningTree::HandleMap(const std::vector<std::string>& parameters, User* user)
98 {
99         if (parameters.size() > 0)
100         {
101                 /* Remote MAP, the server is within the 1st parameter */
102                 TreeServer* s = Utils->FindServerMask(parameters[0]);
103                 bool ret = false;
104                 if (!s)
105                 {
106                         user->WriteNumeric(ERR_NOSUCHSERVER, "%s %s :No such server", user->nick.c_str(), parameters[0].c_str());
107                         ret = true;
108                 }
109                 else if (s && s != Utils->TreeRoot)
110                 {
111                         parameterlist params;
112                         params.push_back(parameters[0]);
113
114                         params[0] = s->GetName();
115                         Utils->DoOneToOne(user->uuid, "MAP", params, s->GetName());
116                         ret = true;
117                 }
118
119                 // Don't return if s == Utils->TreeRoot (us)
120                 if (ret)
121                         return true;
122         }
123
124         // These arrays represent a virtual screen which we will
125         // "scratch" draw to, as the console device of an irc
126         // client does not provide for a proper terminal.
127         int totusers = ServerInstance->Users->clientlist->size();
128         int totservers = this->CountServs();
129         int maxnamew = 0;
130         int line = 0;
131         char* names = new char[totservers * 100];
132         char* stats = new char[totservers * 50];
133
134         // The only recursive bit is called here.
135         ShowMap(Utils->TreeRoot,user,0,line,names,maxnamew,stats);
136
137         // Process each line one by one.
138         for (int l = 1; l < line; l++)
139         {
140                 char* myname = names + 100 * l;
141                 // scan across the line looking for the start of the
142                 // servername (the recursive part of the algorithm has placed
143                 // the servers at indented positions depending on what they
144                 // are related to)
145                 int first_nonspace = 0;
146
147                 while (myname[first_nonspace] == ' ')
148                 {
149                         first_nonspace++;
150                 }
151
152                 first_nonspace--;
153
154                 // Draw the `- (corner) section: this may be overwritten by
155                 // another L shape passing along the same vertical pane, becoming
156                 // a |- (branch) section instead.
157
158                 myname[first_nonspace] = '-';
159                 myname[first_nonspace-1] = '`';
160                 int l2 = l - 1;
161
162                 // Draw upwards until we hit the parent server, causing possibly
163                 // other corners (`-) to become branches (|-)
164                 while ((names[l2 * 100 + first_nonspace-1] == ' ') || (names[l2 * 100 + first_nonspace-1] == '`'))
165                 {
166                         names[l2 * 100 + first_nonspace-1] = '|';
167                         l2--;
168                 }
169         }
170
171         float avg_users = totusers * 1.0 / line;
172
173         for (int t = 0; t < line; t++)
174         {
175                 // terminate the string at maxnamew characters
176                 names[100 * t + maxnamew] = '\0';
177                 user->SendText(":%s %03d %s :%s %s", ServerInstance->Config->ServerName.c_str(),
178                         RPL_MAP, user->nick.c_str(), names + 100 * t, stats + 50 * t);
179         }
180         user->SendText(":%s %03d %s :%d server%s and %d user%s, average %.2f users per server",
181                 ServerInstance->Config->ServerName.c_str(), RPL_MAPUSERS, user->nick.c_str(),
182                 line, (line > 1 ? "s" : ""), totusers, (totusers > 1 ? "s" : ""), avg_users);
183         user->SendText(":%s %03d %s :End of /MAP", ServerInstance->Config->ServerName.c_str(),
184                 RPL_ENDMAP, user->nick.c_str());
185
186         delete[] names;
187         delete[] stats;
188
189         return true;
190 }
191