+ std::ostream& Channels(std::ostream& data)
+ {
+ data << "<channellist>";
+
+ const chan_hash& chans = ServerInstance->GetChans();
+ for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
+ {
+ Channel* c = i->second;
+
+ data << "<channel>";
+ data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
+ data << "<channeltopic>";
+ data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
+ data << "<setby>" << Sanitize(c->setby) << "</setby>";
+ data << "<settime>" << c->topicset << "</settime>";
+ data << "</channeltopic>";
+ data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
+
+ const Channel::MemberMap& ulist = c->GetUsers();
+ for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
+ {
+ Membership* memb = x->second;
+ data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
+ << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
+ << memb->modes << "</modes>";
+ DumpMeta(data, memb);
+ data << "</channelmember>";
+ }
+
+ DumpMeta(data, c);
+
+ data << "</channel>";
+ }
+
+ return data << "</channellist>";
+ }
+
+ std::ostream& Users(std::ostream& data)
+ {
+ data << "<userlist>";
+ const user_hash& users = ServerInstance->Users->GetUsers();
+ for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
+ {
+ User* u = i->second;
+
+ data << "<user>";
+ data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
+ << u->GetRealHost() << "</realhost><displayhost>" << u->GetDisplayedHost() << "</displayhost><realname>"
+ << Sanitize(u->GetRealName()) << "</realname><server>" << u->server->GetName() << "</server>";
+ if (u->IsAway())
+ data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
+ if (u->IsOper())
+ data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
+ data << "<modes>" << u->GetModeLetters().substr(1) << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
+ LocalUser* lu = IS_LOCAL(u);
+ if (lu)
+ data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
+ << lu->server_sa.str() << "</servaddr>";
+ data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
+
+ DumpMeta(data, u);
+
+ data << "</user>";
+ }
+ return data << "</userlist>";
+ }
+
+ std::ostream& Servers(std::ostream& data)
+ {
+ data << "<serverlist>";
+
+ ProtocolInterface::ServerList sl;
+ ServerInstance->PI->GetServerList(sl);
+
+ for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
+ {
+ data << "<server>";
+ data << "<servername>" << b->servername << "</servername>";
+ data << "<parentname>" << b->parentname << "</parentname>";
+ data << "<description>" << Sanitize(b->description) << "</description>";
+ data << "<usercount>" << b->usercount << "</usercount>";
+// This is currently not implemented, so, commented out.
+// data << "<opercount>" << b->opercount << "</opercount>";
+ data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
+ data << "</server>";
+ }
+
+ return data << "</serverlist>";
+ }
+
+ std::ostream& Commands(std::ostream& data)
+ {
+ data << "<commandlist>";
+
+ const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
+ for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
+ {
+ data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
+ }
+ return data << "</commandlist>";
+ }
+}
+
+class ModuleHttpStats : public Module, public HTTPRequestEventListener
+{
+ HTTPdAPI API;
+
+ public:
+ ModuleHttpStats()
+ : HTTPRequestEventListener(this)
+ , API(this)
+ {
+ }
+
+ ModResult HandleRequest(HTTPRequest* http)
+ {
+ std::string uri = http->GetURI();
+
+ if (uri != "/stats" && uri.substr(0, 7) != "/stats/")
+ return MOD_RES_PASSTHRU;
+
+ if (uri[uri.size() - 1] == '/')
+ uri.erase(uri.size() - 1, 1);
+
+ ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
+
+ bool found = true;
+ std::stringstream data;
+ data << "<inspircdstats>";
+
+ if (uri == "/stats")
+ {
+ data << Stats::ServerInfo << Stats::General
+ << Stats::XLines << Stats::Modules
+ << Stats::Channels << Stats::Users
+ << Stats::Servers << Stats::Commands;
+ }
+ else if (uri == "/stats/general")
+ {
+ data << Stats::General;
+ }
+ else if (uri == "/stats/users")
+ {
+ data << Stats::Users;
+ }
+ else
+ {
+ found = false;
+ }
+
+ if (found)
+ {
+ data << "</inspircdstats>";
+ }
+ else
+ {
+ data.clear();
+ data.str(std::string());
+ }
+
+ /* Send the document back to m_httpd */
+ HTTPDocumentResponse response(this, *http, &data, found ? 200 : 404);
+ response.headers.SetHeader("X-Powered-By", MODNAME);
+ response.headers.SetHeader("Content-Type", "text/xml");
+ API->SendResponse(response);
+ return MOD_RES_DENY; // Handled
+ }