2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7 * Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "modules/httpd.h"
28 class ModuleHttpStats : public Module, public HTTPRequestEventListener
30 static const insp::flat_map<char, char const*>& entities;
35 : HTTPRequestEventListener(this)
40 std::string Sanitize(const std::string &str)
43 ret.reserve(str.length() * 2);
45 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
47 insp::flat_map<char, char const*>::const_iterator it = entities.find(*x);
49 if (it != entities.end())
55 else if (*x == 0x09 || *x == 0x0A || *x == 0x0D || ((*x >= 0x20) && (*x <= 0x7e)))
57 // The XML specification defines the following characters as valid inside an XML document:
58 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
63 // If we reached this point then the string contains characters which can
64 // not be represented in XML, even using a numeric escape. Therefore, we
65 // Base64 encode the entire string and wrap it in a CDATA.
68 ret += BinToBase64(str);
76 void DumpMeta(std::stringstream& data, Extensible* ext)
79 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
81 ExtensionItem* item = i->first;
82 std::string value = item->serialize(FORMAT_USER, ext, i->second);
84 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
85 else if (!item->name.empty())
86 data << "<meta name=\"" << item->name << "\"/>";
88 data << "</metadata>";
91 ModResult HandleRequest(HTTPRequest* http)
93 std::stringstream data("");
96 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
98 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
100 data << "<inspircdstats><server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
101 << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
102 << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
105 data << "<usercount>" << ServerInstance->Users->GetUsers().size() << "</usercount>";
106 data << "<channelcount>" << ServerInstance->GetChans().size() << "</channelcount>";
107 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
108 data << "<socketcount>" << (SocketEngine::GetUsedFds()) << "</socketcount><socketmax>" << SocketEngine::GetMaxFds() << "</socketmax><socketengine>" INSPIRCD_SOCKETENGINE_NAME "</socketengine>";
109 data << "<uptime><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
111 data << "<isupport>";
112 const std::vector<std::string>& isupport = ServerInstance->ISupport.GetLines();
113 for (std::vector<std::string>::const_iterator it = isupport.begin(); it != isupport.end(); it++)
115 data << Sanitize(*it) << std::endl;
117 data << "</isupport></general><xlines>";
118 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
119 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
121 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
125 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
127 data << "<xline type=\"" << it->c_str() << "\"><mask>"
128 << Sanitize(i->second->Displayable()) << "</mask><settime>"
129 << i->second->set_time << "</settime><duration>" << i->second->duration
130 << "</duration><reason>" << Sanitize(i->second->reason)
131 << "</reason></xline>";
135 data << "</xlines><modulelist>";
136 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
138 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
140 Version v = i->second->GetVersion();
141 data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
143 data << "</modulelist><channellist>";
145 const chan_hash& chans = ServerInstance->GetChans();
146 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
148 Channel* c = i->second;
151 data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
152 data << "<channeltopic>";
153 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
154 data << "<setby>" << Sanitize(c->setby) << "</setby>";
155 data << "<settime>" << c->topicset << "</settime>";
156 data << "</channeltopic>";
157 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
159 const Channel::MemberMap& ulist = c->GetUsers();
160 for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
162 Membership* memb = x->second;
163 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
164 << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
165 << memb->modes << "</modes>";
166 DumpMeta(data, memb);
167 data << "</channelmember>";
172 data << "</channel>";
175 data << "</channellist><userlist>";
177 const user_hash& users = ServerInstance->Users->GetUsers();
178 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
183 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
184 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
185 << Sanitize(u->fullname) << "</gecos><server>" << u->server->GetName() << "</server>";
187 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
189 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
190 data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
191 LocalUser* lu = IS_LOCAL(u);
193 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
194 << lu->server_sa.str() << "</servaddr>";
195 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
202 data << "</userlist><serverlist>";
204 ProtocolInterface::ServerList sl;
205 ServerInstance->PI->GetServerList(sl);
207 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
210 data << "<servername>" << b->servername << "</servername>";
211 data << "<parentname>" << b->parentname << "</parentname>";
212 data << "<gecos>" << b->gecos << "</gecos>";
213 data << "<usercount>" << b->usercount << "</usercount>";
214 // This is currently not implemented, so, commented out.
215 // data << "<opercount>" << b->opercount << "</opercount>";
216 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
220 data << "</serverlist><commandlist>";
222 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
223 for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
225 data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
228 data << "</commandlist></inspircdstats>";
230 /* Send the document back to m_httpd */
231 HTTPDocumentResponse response(this, *http, &data, 200);
232 response.headers.SetHeader("X-Powered-By", MODNAME);
233 response.headers.SetHeader("Content-Type", "text/xml");
234 API->SendResponse(response);
235 return MOD_RES_DENY; // Handled
238 return MOD_RES_PASSTHRU;
241 ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE
243 return HandleRequest(&req);
246 Version GetVersion() CXX11_OVERRIDE
248 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
252 static const insp::flat_map<char, char const*>& init_entities()
254 static insp::flat_map<char, char const*> entities;
255 entities['<'] = "lt";
256 entities['>'] = "gt";
257 entities['&'] = "amp";
258 entities['"'] = "quot";
262 const insp::flat_map<char, char const*>& ModuleHttpStats::entities = init_entities();
264 MODULE_INIT(ModuleHttpStats)