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
30 static std::map<char, char const*> const &entities;
39 std::string Sanitize(const std::string &str)
42 ret.reserve(str.length() * 2);
44 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
46 std::map<char, char const*>::const_iterator it = entities.find(*x);
48 if (it != entities.end())
54 else if (*x == 0x09 || *x == 0x0A || *x == 0x0D || ((*x >= 0x20) && (*x <= 0x7e)))
56 // The XML specification defines the following characters as valid inside an XML document:
57 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
62 // If we reached this point then the string contains characters which can
63 // not be represented in XML, even using a numeric escape. Therefore, we
64 // Base64 encode the entire string and wrap it in a CDATA.
67 ret += BinToBase64(str);
75 void DumpMeta(std::stringstream& data, Extensible* ext)
78 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
80 ExtensionItem* item = i->first;
81 std::string value = item->serialize(FORMAT_USER, ext, i->second);
83 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
84 else if (!item->name.empty())
85 data << "<meta name=\"" << item->name << "\"/>";
87 data << "</metadata>";
90 void OnEvent(Event& event) CXX11_OVERRIDE
92 std::stringstream data("");
94 if (event.id == "httpd_url")
96 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
97 HTTPRequest* http = (HTTPRequest*)&event;
99 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
101 data << "<inspircdstats><server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
102 << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
103 << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
106 data << "<usercount>" << ServerInstance->Users->GetUsers().size() << "</usercount>";
107 data << "<channelcount>" << ServerInstance->GetChans().size() << "</channelcount>";
108 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
109 data << "<socketcount>" << (SocketEngine::GetUsedFds()) << "</socketcount><socketmax>" << SocketEngine::GetMaxFds() << "</socketmax><socketengine>" INSPIRCD_SOCKETENGINE_NAME "</socketengine>";
111 time_t current_time = 0;
112 current_time = ServerInstance->Time();
113 time_t server_uptime = current_time - ServerInstance->startup_time;
115 stime = gmtime(&server_uptime);
116 data << "<uptime><days>" << stime->tm_yday << "</days><hours>" << stime->tm_hour << "</hours><mins>" << stime->tm_min << "</mins><secs>" << stime->tm_sec << "</secs><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
118 data << "<isupport>";
119 const std::vector<std::string>& isupport = ServerInstance->ISupport.GetLines();
120 for (std::vector<std::string>::const_iterator it = isupport.begin(); it != isupport.end(); it++)
122 data << Sanitize(*it) << std::endl;
124 data << "</isupport></general><xlines>";
125 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
126 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
128 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
132 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
134 data << "<xline type=\"" << it->c_str() << "\"><mask>"
135 << Sanitize(i->second->Displayable()) << "</mask><settime>"
136 << i->second->set_time << "</settime><duration>" << i->second->duration
137 << "</duration><reason>" << Sanitize(i->second->reason)
138 << "</reason></xline>";
142 data << "</xlines><modulelist>";
143 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
145 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
147 Version v = i->second->GetVersion();
148 data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
150 data << "</modulelist><channellist>";
152 const chan_hash& chans = ServerInstance->GetChans();
153 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
155 Channel* c = i->second;
158 data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
159 data << "<channeltopic>";
160 data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
161 data << "<setby>" << Sanitize(c->setby) << "</setby>";
162 data << "<settime>" << c->topicset << "</settime>";
163 data << "</channeltopic>";
164 data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
166 const Channel::MemberMap& ulist = c->GetUsers();
167 for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
169 Membership* memb = x->second;
170 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
171 << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
172 << memb->modes << "</modes>";
173 DumpMeta(data, memb);
174 data << "</channelmember>";
179 data << "</channel>";
182 data << "</channellist><userlist>";
184 const user_hash& users = ServerInstance->Users->GetUsers();
185 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
190 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
191 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
192 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
194 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
196 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
197 data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
198 LocalUser* lu = IS_LOCAL(u);
200 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
201 << lu->server_sa.str() << "</servaddr>";
202 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
209 data << "</userlist><serverlist>";
211 ProtocolInterface::ServerList sl;
212 ServerInstance->PI->GetServerList(sl);
214 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
217 data << "<servername>" << b->servername << "</servername>";
218 data << "<parentname>" << b->parentname << "</parentname>";
219 data << "<gecos>" << b->gecos << "</gecos>";
220 data << "<usercount>" << b->usercount << "</usercount>";
221 // This is currently not implemented, so, commented out.
222 // data << "<opercount>" << b->opercount << "</opercount>";
223 data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
227 data << "</serverlist></inspircdstats>";
229 /* Send the document back to m_httpd */
230 HTTPDocumentResponse response(this, *http, &data, 200);
231 response.headers.SetHeader("X-Powered-By", MODNAME);
232 response.headers.SetHeader("Content-Type", "text/xml");
233 API->SendResponse(response);
238 Version GetVersion() CXX11_OVERRIDE
240 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
244 static std::map<char, char const*> const &init_entities()
246 static std::map<char, char const*> entities;
247 entities['<'] = "lt";
248 entities['>'] = "gt";
249 entities['&'] = "amp";
250 entities['"'] = "quot";
254 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
256 MODULE_INIT(ModuleHttpStats)