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