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<Numeric::Numeric>& isupport = ServerInstance->ISupport.GetLines();
113 for (std::vector<Numeric::Numeric>::const_iterator i = isupport.begin(); i != isupport.end(); ++i)
115 const Numeric::Numeric& num = *i;
116 for (std::vector<std::string>::const_iterator j = num.GetParams().begin(); j != num.GetParams().end()-1; ++j)
117 data << "<token>" << Sanitize(*j) << "</token>" << std::endl;
119 data << "</isupport></general><xlines>";
120 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
121 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
123 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
127 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
129 data << "<xline type=\"" << it->c_str() << "\"><mask>"
130 << Sanitize(i->second->Displayable()) << "</mask><settime>"
131 << i->second->set_time << "</settime><duration>" << i->second->duration
132 << "</duration><reason>" << Sanitize(i->second->reason)
133 << "</reason></xline>";
137 data << "</xlines><modulelist>";
138 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
140 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
142 Version v = i->second->GetVersion();
143 data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
145 data << "</modulelist><channellist>";
147 const chan_hash& chans = ServerInstance->GetChans();
148 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
150 Channel* c = i->second;
153 data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
154 data << "<channeltopic>";
155 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
156 data << "<setby>" << Sanitize(c->setby) << "</setby>";
157 data << "<settime>" << c->topicset << "</settime>";
158 data << "</channeltopic>";
159 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
161 const Channel::MemberMap& ulist = c->GetUsers();
162 for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
164 Membership* memb = x->second;
165 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
166 << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
167 << memb->modes << "</modes>";
168 DumpMeta(data, memb);
169 data << "</channelmember>";
174 data << "</channel>";
177 data << "</channellist><userlist>";
179 const user_hash& users = ServerInstance->Users->GetUsers();
180 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
185 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
186 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
187 << Sanitize(u->fullname) << "</gecos><server>" << u->server->GetName() << "</server>";
189 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
191 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
192 data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
193 LocalUser* lu = IS_LOCAL(u);
195 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
196 << lu->server_sa.str() << "</servaddr>";
197 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
204 data << "</userlist><serverlist>";
206 ProtocolInterface::ServerList sl;
207 ServerInstance->PI->GetServerList(sl);
209 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
212 data << "<servername>" << b->servername << "</servername>";
213 data << "<parentname>" << b->parentname << "</parentname>";
214 data << "<gecos>" << b->gecos << "</gecos>";
215 data << "<usercount>" << b->usercount << "</usercount>";
216 // This is currently not implemented, so, commented out.
217 // data << "<opercount>" << b->opercount << "</opercount>";
218 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
222 data << "</serverlist><commandlist>";
224 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
225 for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
227 data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
230 data << "</commandlist></inspircdstats>";
232 /* Send the document back to m_httpd */
233 HTTPDocumentResponse response(this, *http, &data, 200);
234 response.headers.SetHeader("X-Powered-By", MODNAME);
235 response.headers.SetHeader("Content-Type", "text/xml");
236 API->SendResponse(response);
237 return MOD_RES_DENY; // Handled
240 return MOD_RES_PASSTHRU;
243 ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE
245 return HandleRequest(&req);
248 Version GetVersion() CXX11_OVERRIDE
250 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
254 static const insp::flat_map<char, char const*>& init_entities()
256 static insp::flat_map<char, char const*> entities;
257 entities['<'] = "lt";
258 entities['>'] = "gt";
259 entities['&'] = "amp";
260 entities['"'] = "quot";
264 const insp::flat_map<char, char const*>& ModuleHttpStats::entities = init_entities();
266 MODULE_INIT(ModuleHttpStats)