]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
b49417f8a9220de06aecf6546d9987662499d9b9
[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://wiki.inspircd.org/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 "main.h"
19 #include "utils.h"
20 #include "treeserver.h"
21 #include "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) + (Current->rtt == 0 ? "]" : " Lag: " + ConvToStr(Current->rtt) + "ms]");
29 }
30
31 void ModuleSpanningTree::ShowMap(TreeServer* Current, User* user, int depth, int &line, char* names, int &maxnamew, char* stats)
32 {
33         ServerInstance->Logs->Log("map",DEBUG,"ShowMap depth %d on line %d", depth, line);
34         float percent;
35
36         if (ServerInstance->Users->clientlist->size() == 0)
37         {
38                 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
39                 percent = 0;
40         }
41         else
42         {
43                 percent = Current->GetUserCount() * 100.0 / ServerInstance->Users->clientlist->size();
44         }
45
46         const std::string operdata = IS_OPER(user) ? MapOperInfo(Current) : "";
47
48         char* myname = names + 100 * line;
49         char* mystat = stats + 50 * line;
50         memset(myname, ' ', depth);
51         int w = depth + snprintf(myname + depth, 99 - depth, "%s (%s)", Current->GetName().c_str(), Current->GetID().c_str());
52         memset(myname + w, ' ', 100 - w);
53         if (w > maxnamew)
54                 maxnamew = w;
55         snprintf(mystat, 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, line, 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         // These arrays represent 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 totservers = this->CountServs();
115         int maxnamew = 0;
116         int line = 0;
117         char* names = new char[totservers * 100];
118         char* stats = new char[totservers * 50];
119
120         // The only recursive bit is called here.
121         ShowMap(Utils->TreeRoot,user,0,line,names,maxnamew,stats);
122
123         // Process each line one by one.
124         for (int l = 1; l < line; l++)
125         {
126                 char* myname = names + 100 * l;
127                 // scan across the line looking for the start of the
128                 // servername (the recursive part of the algorithm has placed
129                 // the servers at indented positions depending on what they
130                 // are related to)
131                 int first_nonspace = 0;
132
133                 while (myname[first_nonspace] == ' ')
134                 {
135                         first_nonspace++;
136                 }
137
138                 first_nonspace--;
139
140                 // Draw the `- (corner) section: this may be overwritten by
141                 // another L shape passing along the same vertical pane, becoming
142                 // a |- (branch) section instead.
143
144                 myname[first_nonspace] = '-';
145                 myname[first_nonspace-1] = '`';
146                 int l2 = l - 1;
147
148                 // Draw upwards until we hit the parent server, causing possibly
149                 // other corners (`-) to become branches (|-)
150                 while ((names[l2 * 100 + first_nonspace-1] == ' ') || (names[l2 * 100 + first_nonspace-1] == '`'))
151                 {
152                         names[l2 * 100 + first_nonspace-1] = '|';
153                         l2--;
154                 }
155         }
156
157         float avg_users = totusers * 1.0 / totservers;
158
159         // dump the whole lot to the user.
160         if (IS_LOCAL(user))
161         {
162                 ServerInstance->Logs->Log("map",DEBUG,"local");
163                 for (int t = 0; t < line; t++)
164                 {
165                         // terminate the string at maxnamew characters
166                         names[100 * t + maxnamew] = '\0';
167                         user->WriteNumeric(RPL_MAP, "%s :%s %s",user->nick.c_str(),names + 100 * t, stats + 50 * t);
168                 }
169                 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);
170                 user->WriteNumeric(RPL_ENDMAP, "%s :End of /MAP",user->nick.c_str());
171         }
172         else
173         {
174                 ServerInstance->Logs->Log("map", DEBUG, "remote dump lines=%d", line);
175
176                 // XXX: annoying that we have to use hardcoded numerics here..
177                 for (int t = 0; t < line; t++)
178                 {
179                         // terminate the string at maxnamew characters
180                         char* name = names + 100 * t;
181                         char* stat = stats + 50 * t;
182                         name[maxnamew] = '\0';
183                         ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 006 " + user->nick + " :" + name + " " + stat);
184                 }
185
186                 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");
187                 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 007 " + user->nick + " :End of /MAP");
188         }
189         delete[] names;
190         delete[] stats;
191
192         return 1;
193 }
194