1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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 */
20 #include "treeserver.h"
21 #include "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) + (Current->rtt == 0 ? "]" : " Lag: " + ConvToStr(Current->rtt) + "ms]");
31 void ModuleSpanningTree::ShowMap(TreeServer* Current, User* user, int depth, int &line, char* names, int &maxnamew, char* stats)
33 ServerInstance->Logs->Log("map",DEBUG,"ShowMap depth %d on line %d", depth, line);
36 if (ServerInstance->Users->clientlist->size() == 0)
38 // If there are no users, WHO THE HELL DID THE /MAP?!?!?!
43 percent = Current->GetUserCount() * 100.0 / ServerInstance->Users->clientlist->size();
46 const std::string operdata = IS_OPER(user) ? MapOperInfo(Current) : "";
48 char* myname = names + 100 * line;
49 char* mystat = stats + 50 * line;
50 memset(myname, ' ', depth);
55 w += snprintf(myname + depth, 99 - depth, "%s (%s)", Current->GetName().c_str(), Current->GetID().c_str());
59 w += snprintf(myname + depth, 99 - depth, "%s", Current->GetName().c_str());
61 memset(myname + w, ' ', 100 - w);
64 snprintf(mystat, 49, "%5d [%5.2f%%]%s", Current->GetUserCount(), percent, operdata.c_str());
68 if (IS_OPER(user) || !Utils->FlatLinks)
70 for (unsigned int q = 0; q < Current->ChildCount(); q++)
72 TreeServer* child = Current->GetChild(q);
76 if ((Utils->HideULines) && (ServerInstance->ULine(child->GetName().c_str())))
79 ShowMap(child, user, depth, line, names, maxnamew, stats);
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)
94 if (parameters.size() > 0)
96 /* Remote MAP, the server is within the 1st parameter */
97 TreeServer* s = Utils->FindServerMask(parameters[0]);
101 user->WriteNumeric(ERR_NOSUCHSERVER, "%s %s :No such server", user->nick.c_str(), parameters[0].c_str());
104 else if (s && s != Utils->TreeRoot)
106 parameterlist params;
107 params.push_back(parameters[0]);
109 params[0] = s->GetName();
110 Utils->DoOneToOne(user->uuid, "MAP", params, s->GetName());
114 // Don't return if s == Utils->TreeRoot (us)
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();
126 char* names = new char[totservers * 100];
127 char* stats = new char[totservers * 50];
129 // The only recursive bit is called here.
130 ShowMap(Utils->TreeRoot,user,0,line,names,maxnamew,stats);
132 // Process each line one by one.
133 for (int l = 1; l < line; l++)
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
140 int first_nonspace = 0;
142 while (myname[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 myname[first_nonspace] = '-';
154 myname[first_nonspace-1] = '`';
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] == '`'))
161 names[l2 * 100 + first_nonspace-1] = '|';
166 float avg_users = totusers * 1.0 / line;
168 ServerInstance->Logs->Log("map",DEBUG,"local");
169 for (int t = 0; t < line; t++)
171 // terminate the string at maxnamew characters
172 names[100 * t + maxnamew] = '\0';
173 ServerInstance->DumpText(user, ":%s %d %s :%s %s", ServerInstance->Config->ServerName,
174 RPL_MAP, user->nick.c_str(), names + 100 * t, stats + 50 * t);
176 ServerInstance->DumpText(user, ":%s %d %s :%d server%s and %d user%s, average %.2f users per server",
177 ServerInstance->Config->ServerName, RPL_MAPUSERS, user->nick.c_str(),
178 line, (line > 1 ? "s" : ""), totusers, (totusers > 1 ? "s" : ""), avg_users);
179 ServerInstance->DumpText(user, ":%s %d %s :End of /MAP", ServerInstance->Config->ServerName,
180 RPL_ENDMAP, user->nick.c_str());