]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
Rework /MAP output to not be overly wide
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / override_map.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *        the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Provides a spanning tree server link protocol */
15
16 #include "inspircd.h"
17
18 #include "m_spanningtree/main.h"
19 #include "m_spanningtree/utils.h"
20 #include "m_spanningtree/treeserver.h"
21 #include "m_spanningtree/treesocket.h"
22
23 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
24
25 const std::string ModuleSpanningTree::MapOperInfo(TreeServer* Current)
26 {
27         time_t secs_up = ServerInstance->Time() - Current->age;
28         return (" [Up: " + TimeToStr(secs_up) + " Lag: " + (Current->rtt == 0 ? "<1" : ConvToStr(Current->rtt)) + "ms]");
29 }
30
31 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
32 void ModuleSpanningTree::ShowMap(TreeServer* Current, User* user, int depth, char names[MaxMapHeight][100], int &maxnamew, char stats[MaxMapHeight][50])
33 {
34         ServerInstance->Logs->Log("map",DEBUG,"ShowMap depth %d on line %d", depth, line);
35         if (line >= MaxMapHeight)
36                 return;
37         float percent;
38
39         if (ServerInstance->Users->clientlist->size() == 0)
40         {
41                 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
42                 percent = 0;
43         }
44         else
45         {
46                 percent = ((float)Current->GetUserCount() / (float)ServerInstance->Users->clientlist->size()) * 100;
47         }
48
49         const std::string operdata = IS_OPER(user) ? MapOperInfo(Current) : "";
50         memset(names[line], ' ', depth);
51         int w = depth + snprintf(names[line] + depth, 99 - depth, "%s (%s)", Current->GetName().c_str(), Current->GetID().c_str());
52         memset(names[line] + w, ' ', 100 - w);
53         if (w > maxnamew)
54                 maxnamew = w;
55         snprintf(stats[line], 49, "%5d [%5.2f%%]%s", Current->GetUserCount(), percent, operdata.c_str());
56
57         line++;
58
59         if (IS_OPER(user) || !Utils->FlatLinks)
60                 depth = depth + 2;
61         for (unsigned int q = 0; q < Current->ChildCount(); q++)
62         {
63                 TreeServer* child = Current->GetChild(q);
64                 if (!IS_OPER(user)) {
65                         if (child->Hidden)
66                                 continue;
67                         if ((Utils->HideULines) && (ServerInstance->ULine(child->GetName().c_str())))
68                                 continue;
69                 }
70                 ShowMap(child, user, depth, names, maxnamew, stats);
71         }
72 }
73
74
75 // Ok, prepare to be confused.
76 // After much mulling over how to approach this, it struck me that
77 // the 'usual' way of doing a /MAP isnt the best way. Instead of
78 // keeping track of a ton of ascii characters, and line by line
79 // under recursion working out where to place them using multiplications
80 // and divisons, we instead render the map onto a backplane of characters
81 // (a character matrix), then draw the branches as a series of "L" shapes
82 // from the nodes. This is not only friendlier on CPU it uses less stack.
83 int ModuleSpanningTree::HandleMap(const std::vector<std::string>& parameters, User* user)
84 {
85         if (parameters.size() > 0)
86         {
87                 /* Remote MAP, the server is within the 1st parameter */
88                 TreeServer* s = Utils->FindServerMask(parameters[0]);
89                 bool ret = false;
90                 if (!s)
91                 {
92                         user->WriteNumeric(ERR_NOSUCHSERVER, "%s %s :No such server", user->nick.c_str(), parameters[0].c_str());
93                         ret = true;
94                 }
95                 else if (s && s != Utils->TreeRoot)
96                 {
97                         std::deque<std::string> params;
98                         params.push_back(parameters[0]);
99
100                         params[0] = s->GetName();
101                         Utils->DoOneToOne(user->uuid, "MAP", params, s->GetName());
102                         ret = true;
103                 }
104
105                 // Don't return if s == Utils->TreeRoot (us)
106                 if (ret)
107                         return 1;
108         }
109
110         // This array represents a virtual screen which we will
111         // "scratch" draw to, as the console device of an irc
112         // client does not provide for a proper terminal.
113         int totusers = ServerInstance->Users->clientlist->size();
114         int maxnamew = 0;
115         static char names[MaxMapHeight][100];
116         static char stats[MaxMapHeight][50];
117
118         for (int t = 0; t < MaxMapHeight; t++)
119         {
120                 names[t][0] = '\0';
121         }
122
123         line = 0;
124
125         // The only recursive bit is called here.
126         ShowMap(Utils->TreeRoot,user,0,names,maxnamew,stats);
127
128         int totservers = line;
129
130         // Process each line one by one. The algorithm has a limit of
131         // MaxMapHeight=250 servers (which is far more than a spanning tree
132         // should have anyway, so we're ok). This limit can be raised simply by
133         // making the character matrix deeper, 250 rows taking ~38k of memory.
134         for (int l = 1; l < line; l++)
135         {
136                 // scan across the line looking for the start of the
137                 // servername (the recursive part of the algorithm has placed
138                 // the servers at indented positions depending on what they
139                 // are related to)
140                 int first_nonspace = 0;
141
142                 while (names[l][first_nonspace] == ' ')
143                 {
144                         first_nonspace++;
145                 }
146
147                 first_nonspace--;
148
149                 // Draw the `- (corner) section: this may be overwritten by
150                 // another L shape passing along the same vertical pane, becoming
151                 // a |- (branch) section instead.
152
153                 names[l][first_nonspace] = '-';
154                 names[l][first_nonspace-1] = '`';
155                 int l2 = l - 1;
156
157                 // Draw upwards until we hit the parent server, causing possibly
158                 // other corners (`-) to become branches (|-)
159                 while ((names[l2][first_nonspace-1] == ' ') || (names[l2][first_nonspace-1] == '`'))
160                 {
161                         names[l2][first_nonspace-1] = '|';
162                         l2--;
163                 }
164         }
165
166         float avg_users = ((float)totusers) / ((float)totservers);
167
168         // dump the whole lot to the user.
169         if (IS_LOCAL(user))
170         {
171                 ServerInstance->Logs->Log("map",DEBUG,"local");
172                 for (int t = 0; t < line; t++)
173                 {
174                         // terminate the string at maxnamew characters
175                         names[t][maxnamew] = '\0';
176                         user->WriteNumeric(RPL_MAP, "%s :%s %s",user->nick.c_str(),names[t],stats[t]);
177                 }
178                 user->WriteNumeric(RPL_MAPUSERS, "%s :%d server%s and %d user%s, average %.2f users per server",user->nick.c_str(),totservers,(totservers > 1 ? "s" : ""),totusers,(totusers > 1 ? "s" : ""),avg_users);
179                 user->WriteNumeric(RPL_ENDMAP, "%s :End of /MAP",user->nick.c_str());
180         }
181         else
182         {
183                 ServerInstance->Logs->Log("map", DEBUG, "remote dump lines=%d", line);
184
185                 // XXX: annoying that we have to use hardcoded numerics here..
186                 for (int t = 0; t < line; t++)
187                 {
188                         // terminate the string at maxnamew characters
189                         names[t][maxnamew] = '\0';
190                         ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 006 " + user->nick + " :" + names[t] + " " + stats[t]);
191                 }
192
193                 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 270 " + user->nick + " :" + ConvToStr(totservers) + " server"+(totservers > 1 ? "s" : "") + " and " + ConvToStr(totusers) + " user"+(totusers > 1 ? "s" : "") + ", average " + ConvToStr(avg_users) + " users per server");
194                 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 007 " + user->nick + " :End of /MAP");
195         }
196
197         return 1;
198 }
199