]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Change httpd modules to use the MODNAME constant in headers.
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
9  *
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.
13  *
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
17  * details.
18  *
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/>.
21  */
22
23
24 #include "inspircd.h"
25 #include "modules/httpd.h"
26 #include "xline.h"
27 #include "protocol.h"
28
29 class ModuleHttpStats : public Module
30 {
31         static std::map<char, char const*> const &entities;
32         HTTPdAPI API;
33
34  public:
35         ModuleHttpStats()
36                 : API(this)
37         {
38         }
39
40         void init() CXX11_OVERRIDE
41         {
42                 Implementation eventlist[] = { I_OnEvent };
43                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
44         }
45
46         std::string Sanitize(const std::string &str)
47         {
48                 std::string ret;
49                 ret.reserve(str.length() * 2);
50
51                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
52                 {
53                         std::map<char, char const*>::const_iterator it = entities.find(*x);
54
55                         if (it != entities.end())
56                         {
57                                 ret += '&';
58                                 ret += it->second;
59                                 ret += ';';
60                         }
61                         else if (*x == 0x9 ||  *x == 0xA || *x == 0xD || *x >= 0x20)
62                         {
63                                 // The XML specification defines the following characters as valid inside an XML document:
64                                 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
65                                 ret += *x;
66                         }
67                         else
68                         {
69                                 // If we reached this point then the string contains characters which can
70                                 // not be represented in XML, even using a numeric escape. Therefore, we
71                                 // Base64 encode the entire string and wrap it in a CDATA.
72                                 ret.clear();
73                                 ret += "<![CDATA[";
74                                 ret += BinToBase64(str);
75                                 ret += "]]>";
76                                 break;
77                         }
78                 }
79                 return ret;
80         }
81
82         void DumpMeta(std::stringstream& data, Extensible* ext)
83         {
84                 data << "<metadata>";
85                 for(Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
86                 {
87                         ExtensionItem* item = i->first;
88                         std::string value = item->serialize(FORMAT_USER, ext, i->second);
89                         if (!value.empty())
90                                 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
91                         else if (!item->name.empty())
92                                 data << "<meta name=\"" << item->name << "\"/>";
93                 }
94                 data << "</metadata>";
95         }
96
97         void OnEvent(Event& event) CXX11_OVERRIDE
98         {
99                 std::stringstream data("");
100
101                 if (event.id == "httpd_url")
102                 {
103                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
104                         HTTPRequest* http = (HTTPRequest*)&event;
105
106                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
107                         {
108                                 data << "<inspircdstats><server><name>" << ServerInstance->Config->ServerName << "</name><gecos>"
109                                         << Sanitize(ServerInstance->Config->ServerDesc) << "</gecos><version>"
110                                         << Sanitize(ServerInstance->GetVersionString()) << "</version></server>";
111
112                                 data << "<general>";
113                                 data << "<usercount>" << ServerInstance->Users->clientlist->size() << "</usercount>";
114                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
115                                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
116                                 data << "<socketcount>" << (ServerInstance->SE->GetUsedFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() << "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "</socketengine>";
117
118                                 time_t current_time = 0;
119                                 current_time = ServerInstance->Time();
120                                 time_t server_uptime = current_time - ServerInstance->startup_time;
121                                 struct tm* stime;
122                                 stime = gmtime(&server_uptime);
123                                 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>";
124
125                                 data << "<isupport>";
126                                 const std::vector<std::string>& isupport = ServerInstance->ISupport.GetLines();
127                                 for (std::vector<std::string>::const_iterator it = isupport.begin(); it != isupport.end(); it++)
128                                 {
129                                         data << Sanitize(*it) << std::endl;
130                                 }
131                                 data << "</isupport></general><xlines>";
132                                 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
133                                 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
134                                 {
135                                         XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
136
137                                         if (!lookup)
138                                                 continue;
139                                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
140                                         {
141                                                 data << "<xline type=\"" << it->c_str() << "\"><mask>"
142                                                         << Sanitize(i->second->Displayable()) << "</mask><settime>"
143                                                         << i->second->set_time << "</settime><duration>" << i->second->duration
144                                                         << "</duration><reason>" << Sanitize(i->second->reason)
145                                                         << "</reason></xline>";
146                                         }
147                                 }
148
149                                 data << "</xlines><modulelist>";
150                                 std::vector<std::string> module_names = ServerInstance->Modules->GetAllModuleNames(0);
151
152                                 for (std::vector<std::string>::iterator i = module_names.begin(); i != module_names.end(); ++i)
153                                 {
154                                         Module* m = ServerInstance->Modules->Find(i->c_str());
155                                         Version v = m->GetVersion();
156                                         data << "<module><name>" << *i << "</name><description>" << Sanitize(v.description) << "</description></module>";
157                                 }
158                                 data << "</modulelist><channellist>";
159
160                                 for (chan_hash::const_iterator a = ServerInstance->chanlist->begin(); a != ServerInstance->chanlist->end(); ++a)
161                                 {
162                                         Channel* c = a->second;
163
164                                         data << "<channel>";
165                                         data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
166                                         data << "<channeltopic>";
167                                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
168                                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
169                                         data << "<settime>" << c->topicset << "</settime>";
170                                         data << "</channeltopic>";
171                                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
172                                         const UserMembList* ulist = c->GetUsers();
173
174                                         for (UserMembCIter x = ulist->begin(); x != ulist->end(); ++x)
175                                         {
176                                                 Membership* memb = x->second;
177                                                 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
178                                                         << Sanitize(c->GetAllPrefixChars(x->first)) << "</privs><modes>"
179                                                         << memb->modes << "</modes>";
180                                                 DumpMeta(data, memb);
181                                                 data << "</channelmember>";
182                                         }
183
184                                         DumpMeta(data, c);
185
186                                         data << "</channel>";
187                                 }
188
189                                 data << "</channellist><userlist>";
190
191                                 for (user_hash::const_iterator a = ServerInstance->Users->clientlist->begin(); a != ServerInstance->Users->clientlist->end(); ++a)
192                                 {
193                                         User* u = a->second;
194
195                                         data << "<user>";
196                                         data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
197                                                 << u->host << "</realhost><displayhost>" << u->dhost << "</displayhost><gecos>"
198                                                 << Sanitize(u->fullname) << "</gecos><server>" << u->server << "</server>";
199                                         if (u->IsAway())
200                                                 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
201                                         if (u->IsOper())
202                                                 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
203                                         data << "<modes>" << u->FormatModes() << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
204                                         LocalUser* lu = IS_LOCAL(u);
205                                         if (lu)
206                                                 data << "<port>" << lu->GetServerPort() << "</port><servaddr>"
207                                                         << irc::sockets::satouser(lu->server_sa) << "</servaddr>";
208                                         data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
209
210                                         DumpMeta(data, u);
211
212                                         data << "</user>";
213                                 }
214
215                                 data << "</userlist><serverlist>";
216
217                                 ProtoServerList sl;
218                                 ServerInstance->PI->GetServerList(sl);
219
220                                 for (ProtoServerList::iterator b = sl.begin(); b != sl.end(); ++b)
221                                 {
222                                         data << "<server>";
223                                         data << "<servername>" << b->servername << "</servername>";
224                                         data << "<parentname>" << b->parentname << "</parentname>";
225                                         data << "<gecos>" << b->gecos << "</gecos>";
226                                         data << "<usercount>" << b->usercount << "</usercount>";
227 // This is currently not implemented, so, commented out.
228 //                                      data << "<opercount>" << b->opercount << "</opercount>";
229                                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
230                                         data << "</server>";
231                                 }
232
233                                 data << "</serverlist></inspircdstats>";
234
235                                 /* Send the document back to m_httpd */
236                                 HTTPDocumentResponse response(this, *http, &data, 200);
237                                 response.headers.SetHeader("X-Powered-By", MODNAME);
238                                 response.headers.SetHeader("Content-Type", "text/xml");
239                                 API->SendResponse(response);
240                         }
241                 }
242         }
243
244         Version GetVersion() CXX11_OVERRIDE
245         {
246                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
247         }
248 };
249
250 static std::map<char, char const*> const &init_entities()
251 {
252         static std::map<char, char const*> entities;
253         entities['<'] = "lt";
254         entities['>'] = "gt";
255         entities['&'] = "amp";
256         entities['"'] = "quot";
257         return entities;
258 }
259
260 std::map<char, char const*> const &ModuleHttpStats::entities = init_entities ();
261
262 MODULE_INIT(ModuleHttpStats)