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 */
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 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);
51 int w = depth + snprintf(myname + depth, 99 - depth, "%s (%s)", Current->GetName().c_str(), Current->GetID().c_str());
52 memset(myname + w, ' ', 100 - w);
55 snprintf(mystat, 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, line, 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 // These arrays represent 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();
114 int totservers = this->CountServs();
117 char* names = new char[totservers * 100];
118 char* stats = new char[totservers * 50];
120 // The only recursive bit is called here.
121 ShowMap(Utils->TreeRoot,user,0,line,names,maxnamew,stats);
123 // Process each line one by one.
124 for (int l = 1; l < line; l++)
126 char* myname = names + 100 * l;
127 // scan across the line looking for the start of the
128 // servername (the recursive part of the algorithm has placed
129 // the servers at indented positions depending on what they
131 int first_nonspace = 0;
133 while (myname[first_nonspace] == ' ')
140 // Draw the `- (corner) section: this may be overwritten by
141 // another L shape passing along the same vertical pane, becoming
142 // a |- (branch) section instead.
144 myname[first_nonspace] = '-';
145 myname[first_nonspace-1] = '`';
148 // Draw upwards until we hit the parent server, causing possibly
149 // other corners (`-) to become branches (|-)
150 while ((names[l2 * 100 + first_nonspace-1] == ' ') || (names[l2 * 100 + first_nonspace-1] == '`'))
152 names[l2 * 100 + first_nonspace-1] = '|';
157 float avg_users = totusers * 1.0 / totservers;
159 // dump the whole lot to the user.
162 ServerInstance->Logs->Log("map",DEBUG,"local");
163 for (int t = 0; t < line; t++)
165 // terminate the string at maxnamew characters
166 names[100 * t + maxnamew] = '\0';
167 user->WriteNumeric(RPL_MAP, "%s :%s %s",user->nick.c_str(),names + 100 * t, stats + 50 * t);
169 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);
170 user->WriteNumeric(RPL_ENDMAP, "%s :End of /MAP",user->nick.c_str());
174 ServerInstance->Logs->Log("map", DEBUG, "remote dump lines=%d", line);
176 // XXX: annoying that we have to use hardcoded numerics here..
177 for (int t = 0; t < line; t++)
179 // terminate the string at maxnamew characters
180 char* name = names + 100 * t;
181 char* stat = stats + 50 * t;
182 name[maxnamew] = '\0';
183 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 006 " + user->nick + " :" + name + " " + stat);
186 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");
187 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 007 " + user->nick + " :End of /MAP");