1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $ModDesc: Provides a spanning tree server link protocol */
18 #include "m_spanningtree/main.h"
19 #include "m_spanningtree/utils.h"
20 #include "m_spanningtree/treeserver.h"
21 #include "m_spanningtree/treesocket.h"
23 /* $ModDep: m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
25 const std::string ModuleSpanningTree::MapOperInfo(TreeServer* Current)
27 time_t secs_up = ServerInstance->Time() - Current->age;
28 return (" [Up: " + TimeToStr(secs_up) + " Lag: " + (Current->rtt == 0 ? "<1" : ConvToStr(Current->rtt)) + "ms]");
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)
34 ServerInstance->Logs->Log("map",DEBUG,"ShowMap depth %d totusers %0.2f totservers %0.2f", depth, totusers, totservers);
37 for (int t = 0; t < depth; t++)
39 matrix[line][t] = ' ';
42 // For Aligning, we need to work out exactly how deep this thing is, and produce
43 // a 'Spacer' String to compensate.
45 memset(spacer,' ',sizeof(spacer));
46 if ((80 - Current->GetName().length() - depth) > 1) {
47 spacer[80 - Current->GetName().length() - depth] = '\0';
56 /* Neat and tidy default values, as we're dealing with a matrix not a simple string */
57 memset(text, 0, sizeof(text));
59 if (ServerInstance->Users->clientlist->size() == 0)
61 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
66 percent = ((float)Current->GetUserCount() / (float)ServerInstance->Users->clientlist->size()) * 100;
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();
73 strlcpy(&matrix[line][depth], text, 249);
76 for (unsigned int q = 0; q < Current->ChildCount(); q++)
78 if ((Current->GetChild(q)->Hidden) || ((Utils->HideULines) && (ServerInstance->ULine(Current->GetChild(q)->GetName().c_str()))))
82 ShowMap(Current->GetChild(q),user,(Utils->FlatLinks && (!IS_OPER(user))) ? depth : depth+2,matrix,totusers,totservers);
87 ShowMap(Current->GetChild(q),user,(Utils->FlatLinks && (!IS_OPER(user))) ? depth : depth+2,matrix,totusers,totservers);
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)
104 if (parameters.size() > 0)
106 /* Remote MAP, the server is within the 1st parameter */
107 TreeServer* s = Utils->FindServerMask(parameters[0]);
111 user->WriteNumeric(ERR_NOSUCHSERVER, "%s %s :No such server", user->nick.c_str(), parameters[0].c_str());
114 else if (s && s != Utils->TreeRoot)
116 std::deque<std::string> params;
117 params.push_back(parameters[0]);
119 params[0] = s->GetName();
120 Utils->DoOneToOne(user->uuid, "MAP", params, s->GetName());
124 // Don't return if s == Utils->TreeRoot (us)
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.
133 float totservers = 0;
134 static char matrix[250][250];
136 for (unsigned int t = 0; t < 250; t++)
143 // The only recursive bit is called here.
144 ShowMap(Utils->TreeRoot,user,0,matrix,totusers,totservers);
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++)
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
156 int first_nonspace = 0;
158 while (matrix[l][first_nonspace] == ' ')
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.
169 matrix[l][first_nonspace] = '-';
170 matrix[l][first_nonspace-1] = '`';
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] == '`'))
177 matrix[l2][first_nonspace-1] = '|';
182 float avg_users = totusers / totservers;
184 // dump the whole lot to the user.
187 ServerInstance->Logs->Log("map",DEBUG,"local");
188 for (int t = 0; t < line; t++)
190 user->WriteNumeric(RPL_MAP, "%s :%s",user->nick.c_str(),&matrix[t][0]);
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());
197 ServerInstance->Logs->Log("map", DEBUG, "remote dump lines=%d", line);
199 // XXX: annoying that we have to use hardcoded numerics here..
200 for (int t = 0; t < line; t++)
202 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 006 " + user->nick + " :" + &matrix[t][0]);
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");