1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 names[MaxMapHeight][100], int &maxnamew, char stats[MaxMapHeight][50])
34 ServerInstance->Logs->Log("map",DEBUG,"ShowMap depth %d on line %d", depth, line);
35 if (line >= MaxMapHeight)
39 if (ServerInstance->Users->clientlist->size() == 0)
41 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
46 percent = ((float)Current->GetUserCount() / (float)ServerInstance->Users->clientlist->size()) * 100;
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);
55 snprintf(stats[line], 49, "%5d [%5.2f%%]%s", Current->GetUserCount(), percent, operdata.c_str());
59 if (IS_OPER(user) || !Utils->FlatLinks)
61 for (unsigned int q = 0; q < Current->ChildCount(); q++)
63 TreeServer* child = Current->GetChild(q);
67 if ((Utils->HideULines) && (ServerInstance->ULine(child->GetName().c_str())))
70 ShowMap(child, user, depth, names, maxnamew, stats);
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)
85 if (parameters.size() > 0)
87 /* Remote MAP, the server is within the 1st parameter */
88 TreeServer* s = Utils->FindServerMask(parameters[0]);
92 user->WriteNumeric(ERR_NOSUCHSERVER, "%s %s :No such server", user->nick.c_str(), parameters[0].c_str());
95 else if (s && s != Utils->TreeRoot)
97 std::deque<std::string> params;
98 params.push_back(parameters[0]);
100 params[0] = s->GetName();
101 Utils->DoOneToOne(user->uuid, "MAP", params, s->GetName());
105 // Don't return if s == Utils->TreeRoot (us)
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();
115 static char names[MaxMapHeight][100];
116 static char stats[MaxMapHeight][50];
118 for (int t = 0; t < MaxMapHeight; t++)
125 // The only recursive bit is called here.
126 ShowMap(Utils->TreeRoot,user,0,names,maxnamew,stats);
128 int totservers = line;
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++)
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
140 int first_nonspace = 0;
142 while (names[l][first_nonspace] == ' ')
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.
153 names[l][first_nonspace] = '-';
154 names[l][first_nonspace-1] = '`';
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] == '`'))
161 names[l2][first_nonspace-1] = '|';
166 float avg_users = ((float)totusers) / ((float)totservers);
168 // dump the whole lot to the user.
171 ServerInstance->Logs->Log("map",DEBUG,"local");
172 for (int t = 0; t < line; t++)
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]);
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());
183 ServerInstance->Logs->Log("map", DEBUG, "remote dump lines=%d", line);
185 // XXX: annoying that we have to use hardcoded numerics here..
186 for (int t = 0; t < line; t++)
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]);
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");