]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
Fix error message on OPTCOMMON mismatch
[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-2010 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;
52
53         if (IS_OPER(user))
54         {
55                 w += snprintf(myname + depth, 99 - depth, "%s (%s)", Current->GetName().c_str(), Current->GetID().c_str());
56         }
57         else
58         {
59                 w += snprintf(myname + depth, 99 - depth, "%s", Current->GetName().c_str());
60         }
61         memset(myname + w, ' ', 100 - w);
62         if (w > maxnamew)
63                 maxnamew = w;
64         snprintf(mystat, 49, "%5d [%5.2f%%]%s", Current->GetUserCount(), percent, operdata.c_str());
65
66         line++;
67
68         if (IS_OPER(user) || !Utils->FlatLinks)
69                 depth = depth + 2;
70         for (unsigned int q = 0; q < Current->ChildCount(); q++)
71         {
72                 TreeServer* child = Current->GetChild(q);
73                 if (!IS_OPER(user)) {
74                         if (child->Hidden)
75                                 continue;
76                         if ((Utils->HideULines) && (ServerInstance->ULine(child->GetName().c_str())))
77                                 continue;
78                 }
79                 ShowMap(child, user, depth, line, names, maxnamew, stats);
80         }
81 }
82
83
84 // Ok, prepare to be confused.
85 // After much mulling over how to approach this, it struck me that
86 // the 'usual' way of doing a /MAP isnt the best way. Instead of
87 // keeping track of a ton of ascii characters, and line by line
88 // under recursion working out where to place them using multiplications
89 // and divisons, we instead render the map onto a backplane of characters
90 // (a character matrix), then draw the branches as a series of "L" shapes
91 // from the nodes. This is not only friendlier on CPU it uses less stack.
92 bool ModuleSpanningTree::HandleMap(const std::vector<std::string>& parameters, User* user)
93 {
94         if (parameters.size() > 0)
95         {
96                 /* Remote MAP, the server is within the 1st parameter */
97                 TreeServer* s = Utils->FindServerMask(parameters[0]);
98                 bool ret = false;
99                 if (!s)
100                 {
101                         user->WriteNumeric(ERR_NOSUCHSERVER, "%s %s :No such server", user->nick.c_str(), parameters[0].c_str());
102                         ret = true;
103                 }
104                 else if (s && s != Utils->TreeRoot)
105                 {
106                         parameterlist params;
107                         params.push_back(parameters[0]);
108
109                         params[0] = s->GetName();
110                         Utils->DoOneToOne(user->uuid, "MAP", params, s->GetName());
111                         ret = true;
112                 }
113
114                 // Don't return if s == Utils->TreeRoot (us)
115                 if (ret)
116                         return true;
117         }
118
119         // These arrays represent a virtual screen which we will
120         // "scratch" draw to, as the console device of an irc
121         // client does not provide for a proper terminal.
122         int totusers = ServerInstance->Users->clientlist->size();
123         int totservers = this->CountServs();
124         int maxnamew = 0;
125         int line = 0;
126         char* names = new char[totservers * 100];
127         char* stats = new char[totservers * 50];
128
129         // The only recursive bit is called here.
130         ShowMap(Utils->TreeRoot,user,0,line,names,maxnamew,stats);
131
132         // Process each line one by one.
133         for (int l = 1; l < line; l++)
134         {
135                 char* myname = names + 100 * l;
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 (myname[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                 myname[first_nonspace] = '-';
154                 myname[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 * 100 + first_nonspace-1] == ' ') || (names[l2 * 100 + first_nonspace-1] == '`'))
160                 {
161                         names[l2 * 100 + first_nonspace-1] = '|';
162                         l2--;
163                 }
164         }
165
166         float avg_users = totusers * 1.0 / line;
167
168         ServerInstance->Logs->Log("map",DEBUG,"local");
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 %d %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 %d %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 %d %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 true;
186 }
187