]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/override_map.cpp
Fire every debug.
[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-2008 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 matrix[250][250], float &totusers, float &totservers)
33 {
34         ServerInstance->Logs->Log("map",DEBUG,"ShowMap depth %d totusers %0.2f totservers %0.2f", depth, totusers, totservers);
35         if (line < 250)
36         {
37                 for (int t = 0; t < depth; t++)
38                 {
39                         matrix[line][t] = ' ';
40                 }
41
42                 // For Aligning, we need to work out exactly how deep this thing is, and produce
43                 // a 'Spacer' String to compensate.
44                 char spacer[80];
45                 memset(spacer,' ',sizeof(spacer));
46                 if ((80 - Current->GetName().length() - depth) > 1) {
47                         spacer[80 - Current->GetName().length() - depth] = '\0';
48                 }
49                 else
50                 {
51                         spacer[5] = '\0';
52                 }
53
54                 float percent;
55                 char text[250];
56                 /* Neat and tidy default values, as we're dealing with a matrix not a simple string */
57                 memset(text, 0, sizeof(text));
58
59                 if (ServerInstance->Users->clientlist->size() == 0)
60                 {
61                         // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
62                         percent = 0;
63                 }
64                 else
65                 {
66                         percent = ((float)Current->GetUserCount() / (float)ServerInstance->Users->clientlist->size()) * 100;
67                 }
68
69                 const std::string operdata = IS_OPER(user) ? MapOperInfo(Current) : "";
70                 snprintf(text, 249, "%s (%s)%s%5d [%5.2f%%]%s", Current->GetName().c_str(), Current->GetID().c_str(), spacer, Current->GetUserCount(), percent, operdata.c_str());
71                 totusers += Current->GetUserCount();
72                 totservers++;
73                 strlcpy(&matrix[line][depth], text, 249);
74                 line++;
75
76                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
77                 {
78                         if ((Current->GetChild(q)->Hidden) || ((Utils->HideULines) && (ServerInstance->ULine(Current->GetChild(q)->GetName().c_str()))))
79                         {
80                                 if (IS_OPER(user))
81                                 {
82                                         ShowMap(Current->GetChild(q),user,(Utils->FlatLinks && (!IS_OPER(user))) ? depth : depth+2,matrix,totusers,totservers);
83                                 }
84                         }
85                         else
86                         {
87                                 ShowMap(Current->GetChild(q),user,(Utils->FlatLinks && (!IS_OPER(user))) ? depth : depth+2,matrix,totusers,totservers);
88                         }
89                 }
90         }
91 }
92
93
94 // Ok, prepare to be confused.
95 // After much mulling over how to approach this, it struck me that
96 // the 'usual' way of doing a /MAP isnt the best way. Instead of
97 // keeping track of a ton of ascii characters, and line by line
98 // under recursion working out where to place them using multiplications
99 // and divisons, we instead render the map onto a backplane of characters
100 // (a character matrix), then draw the branches as a series of "L" shapes
101 // from the nodes. This is not only friendlier on CPU it uses less stack.
102 int ModuleSpanningTree::HandleMap(const std::vector<std::string>& parameters, User* user)
103 {
104         if (parameters.size() > 0)
105         {
106                 /* Remote MAP, the server is within the 1st parameter */
107                 TreeServer* s = Utils->FindServerMask(parameters[0]);
108                 bool ret = false;
109                 if (!s)
110                 {
111                         user->WriteNumeric(ERR_NOSUCHSERVER, "%s %s :No such server", user->nick.c_str(), parameters[0].c_str());
112                         ret = true;
113                 }
114                 else if (s && s != Utils->TreeRoot)
115                 {
116                         std::deque<std::string> params;
117                         params.push_back(parameters[0]);
118
119                         params[0] = s->GetName();
120                         Utils->DoOneToOne(user->uuid, "MAP", params, s->GetName());
121                         ret = true;
122                 }
123
124                 // Don't return if s == Utils->TreeRoot (us)
125                 if (ret)
126                         return 1;
127         }
128
129         // This array represents a virtual screen which we will
130         // "scratch" draw to, as the console device of an irc
131         // client does not provide for a proper terminal.
132         float totusers = 0;
133         float totservers = 0;
134         static char matrix[250][250];
135
136         for (unsigned int t = 0; t < 250; t++)
137         {
138                 matrix[t][0] = '\0';
139         }
140
141         line = 0;
142
143         // The only recursive bit is called here.
144         ShowMap(Utils->TreeRoot,user,0,matrix,totusers,totservers);
145
146         // Process each line one by one. The algorithm has a limit of
147         // 250 servers (which is far more than a spanning tree should have
148         // anyway, so we're ok). This limit can be raised simply by making
149         // the character matrix deeper, 250 rows taking 100k of memory.
150         for (int l = 1; l < line; l++)
151         {
152                 // scan across the line looking for the start of the
153                 // servername (the recursive part of the algorithm has placed
154                 // the servers at indented positions depending on what they
155                 // are related to)
156                 int first_nonspace = 0;
157
158                 while (matrix[l][first_nonspace] == ' ')
159                 {
160                         first_nonspace++;
161                 }
162
163                 first_nonspace--;
164
165                 // Draw the `- (corner) section: this may be overwritten by
166                 // another L shape passing along the same vertical pane, becoming
167                 // a |- (branch) section instead.
168
169                 matrix[l][first_nonspace] = '-';
170                 matrix[l][first_nonspace-1] = '`';
171                 int l2 = l - 1;
172
173                 // Draw upwards until we hit the parent server, causing possibly
174                 // other corners (`-) to become branches (|-)
175                 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
176                 {
177                         matrix[l2][first_nonspace-1] = '|';
178                         l2--;
179                 }
180         }
181
182         float avg_users = totusers / totservers;
183
184         // dump the whole lot to the user.
185         if (IS_LOCAL(user))
186         {
187                 ServerInstance->Logs->Log("map",DEBUG,"local");
188                 for (int t = 0; t < line; t++)
189                 {
190                         user->WriteNumeric(RPL_MAP, "%s :%s",user->nick.c_str(),&matrix[t][0]);
191                 }
192                 user->WriteNumeric(RPL_MAPUSERS, "%s :%.0f server%s and %.0f user%s, average %.2f users per server",user->nick.c_str(),totservers,(totservers > 1 ? "s" : ""),totusers,(totusers > 1 ? "s" : ""),avg_users);
193                 user->WriteNumeric(RPL_ENDMAP, "%s :End of /MAP",user->nick.c_str());
194         }
195         else
196         {
197                 ServerInstance->Logs->Log("map", DEBUG, "remote dump lines=%d", line);
198
199                 // XXX: annoying that we have to use hardcoded numerics here..
200                 for (int t = 0; t < line; t++)
201                 {
202                         ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 006 " + user->nick + " :" + &matrix[t][0]);
203                 }
204
205                 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");
206                 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 007 " + user->nick + " :End of /MAP");
207         }
208
209         return 1;
210 }
211