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"
32 static const insp::flat_map<char, char const*>& entities;
35 static const insp::flat_map<char, char const*>& init_entities()
37 static insp::flat_map<char, char const*> entities;
40 entities['&'] = "amp";
41 entities['"'] = "quot";
45 const insp::flat_map<char, char const*>& Entities::entities = init_entities();
47 std::string Sanitize(const std::string& str)
50 ret.reserve(str.length() * 2);
52 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
54 insp::flat_map<char, char const*>::const_iterator it = Entities::entities.find(*x);
56 if (it != Entities::entities.end())
62 else if (*x == 0x09 || *x == 0x0A || *x == 0x0D || ((*x >= 0x20) && (*x <= 0x7e)))
64 // The XML specification defines the following characters as valid inside an XML document:
65 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
70 // If we reached this point then the string contains characters which can
71 // not be represented in XML, even using a numeric escape. Therefore, we
72 // Base64 encode the entire string and wrap it in a CDATA.
75 ret += BinToBase64(str);
83 void DumpMeta(std::ostream& data, Extensible* ext)
86 for (Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
88 ExtensionItem* item = i->first;
89 std::string value = item->serialize(FORMAT_USER, ext, i->second);
91 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
92 else if (!item->name.empty())
93 data << "<meta name=\"" << item->name << "\"/>";
95 data << "</metadata>";
98 std::ostream& ServerInfo(std::ostream& data)
100 return data << "<server><name>" << ServerInstance->Config->ServerName << "</name><description>"
101 << Sanitize(ServerInstance->Config->ServerDesc) << "</description><version>"
102 << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
105 std::ostream& ISupport(std::ostream& data)
107 data << "<isupport>";
108 const std::vector<Numeric::Numeric>& isupport = ServerInstance->ISupport.GetLines();
109 for (std::vector<Numeric::Numeric>::const_iterator i = isupport.begin(); i != isupport.end(); ++i)
111 const Numeric::Numeric& num = *i;
112 for (std::vector<std::string>::const_iterator j = num.GetParams().begin(); j != num.GetParams().end() - 1; ++j)
113 data << "<token>" << Sanitize(*j) << "</token>";
115 return data << "</isupport>";
118 std::ostream& General(std::ostream& data)
121 data << "<usercount>" << ServerInstance->Users->GetUsers().size() << "</usercount>";
122 data << "<localusercount>" << ServerInstance->Users->GetLocalUsers().size() << "</localusercount>";
123 data << "<channelcount>" << ServerInstance->GetChans().size() << "</channelcount>";
124 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
125 data << "<socketcount>" << (SocketEngine::GetUsedFds()) << "</socketcount><socketmax>" << SocketEngine::GetMaxFds() << "</socketmax>";
126 data << "<uptime><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
129 return data << "</general>";
132 std::ostream& XLines(std::ostream& data)
135 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
136 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
138 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
142 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
144 data << "<xline type=\"" << it->c_str() << "\"><mask>"
145 << Sanitize(i->second->Displayable()) << "</mask><settime>"
146 << i->second->set_time << "</settime><duration>" << i->second->duration
147 << "</duration><reason>" << Sanitize(i->second->reason)
148 << "</reason></xline>";
151 return data << "</xlines>";
154 std::ostream& Modules(std::ostream& data)
156 data << "<modulelist>";
157 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
159 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
161 Version v = i->second->GetVersion();
162 data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
164 return data << "</modulelist>";
167 std::ostream& Channels(std::ostream& data)
169 data << "<channellist>";
171 const chan_hash& chans = ServerInstance->GetChans();
172 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
174 Channel* c = i->second;
177 data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
178 data << "<channeltopic>";
179 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
180 data << "<setby>" << Sanitize(c->setby) << "</setby>";
181 data << "<settime>" << c->topicset << "</settime>";
182 data << "</channeltopic>";
183 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
185 const Channel::MemberMap& ulist = c->GetUsers();
186 for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
188 Membership* memb = x->second;
189 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
190 << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
191 << memb->modes << "</modes>";
192 DumpMeta(data, memb);
193 data << "</channelmember>";
198 data << "</channel>";
201 return data << "</channellist>";
204 std::ostream& Users(std::ostream& data)
206 data << "<userlist>";
207 const user_hash& users = ServerInstance->Users->GetUsers();
208 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
213 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
214 << u->GetRealHost() << "</realhost><displayhost>" << u->GetDisplayedHost() << "</displayhost><realname>"
215 << Sanitize(u->GetRealName()) << "</realname><server>" << u->server->GetName() << "</server>";
217 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
219 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
220 data << "<modes>" << u->GetModeLetters().substr(1) << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
221 LocalUser* lu = IS_LOCAL(u);
223 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
224 << lu->server_sa.str() << "</servaddr>";
225 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
231 return data << "</userlist>";
234 std::ostream& Servers(std::ostream& data)
236 data << "<serverlist>";
238 ProtocolInterface::ServerList sl;
239 ServerInstance->PI->GetServerList(sl);
241 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
244 data << "<servername>" << b->servername << "</servername>";
245 data << "<parentname>" << b->parentname << "</parentname>";
246 data << "<description>" << Sanitize(b->description) << "</description>";
247 data << "<usercount>" << b->usercount << "</usercount>";
248 // This is currently not implemented, so, commented out.
249 // data << "<opercount>" << b->opercount << "</opercount>";
250 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
254 return data << "</serverlist>";
257 std::ostream& Commands(std::ostream& data)
259 data << "<commandlist>";
261 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
262 for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
264 data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
266 return data << "</commandlist>";
270 class ModuleHttpStats : public Module, public HTTPRequestEventListener
276 : HTTPRequestEventListener(this)
281 ModResult HandleRequest(HTTPRequest* http)
283 std::string uri = http->GetURI();
285 if (uri != "/stats" && uri.substr(0, 7) != "/stats/")
286 return MOD_RES_PASSTHRU;
288 if (uri[uri.size() - 1] == '/')
289 uri.erase(uri.size() - 1, 1);
291 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
294 std::stringstream data;
295 data << "<inspircdstats>";
299 data << Stats::ServerInfo << Stats::General
300 << Stats::XLines << Stats::Modules
301 << Stats::Channels << Stats::Users
302 << Stats::Servers << Stats::Commands;
304 else if (uri == "/stats/general")
306 data << Stats::General;
308 else if (uri == "/stats/users")
310 data << Stats::Users;
319 data << "</inspircdstats>";
324 data.str(std::string());
327 /* Send the document back to m_httpd */
328 HTTPDocumentResponse response(this, *http, &data, found ? 200 : 404);
329 response.headers.SetHeader("X-Powered-By", MODNAME);
330 response.headers.SetHeader("Content-Type", "text/xml");
331 API->SendResponse(response);
332 return MOD_RES_DENY; // Handled
335 ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE
337 return HandleRequest(&req);
340 Version GetVersion() CXX11_OVERRIDE
342 return Version("Provides statistics over HTTP via m_httpd", VF_VENDOR);
346 MODULE_INIT(ModuleHttpStats)