]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_config.cpp
0763ad11dac452322d4c67f52510933175db57fe
[user/henk/code/inspircd.git] / src / modules / m_httpd_config.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "httpd.h"
16 #include "protocol.h"
17
18 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
19 /* $ModDep: httpd.h */
20
21 class ModuleHttpStats : public Module
22 {
23
24         std::string stylesheet;
25         bool changed;
26
27  public:
28
29         void ReadConfig()
30         {
31                 ConfigReader c;
32                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
33         }
34
35         ModuleHttpStats()       {
36                 ReadConfig();
37                 this->changed = true;
38                 Implementation eventlist[] = { I_OnEvent };
39                 ServerInstance->Modules->Attach(eventlist, this, 1);
40         }
41
42         std::string Sanitize(const std::string &str)
43         {
44                 std::string ret;
45
46                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
47                 {
48                         switch (*x)
49                         {
50                                 case '<':
51                                         ret += "&lt;";
52                                 break;
53                                 case '>':
54                                         ret += "&gt;";
55                                 break;
56                                 case '&':
57                                         ret += "&amp;";
58                                 break;
59                                 case '"':
60                                         ret += "&quot;";
61                                 break;
62                                 default:
63                                         if (*x < 32 || *x > 126)
64                                         {
65                                                 int n = *x;
66                                                 ret += ("&#" + ConvToStr(n) + ";");
67                                         }
68                                         else
69                                                 ret += *x;
70                                 break;
71                         }
72                 }
73                 return ret;
74         }
75
76         void OnEvent(Event& event)
77         {
78                 std::stringstream data("");
79
80                 if (event.id == "httpd_url")
81                 {
82                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
83                         HTTPRequest* http = (HTTPRequest*)&event;
84
85                         if ((http->GetURI() == "/config") || (http->GetURI() == "/config/"))
86                         {
87                                 data << "<html><head><title>InspIRCd Configuration</title></head><body>";
88                                 data << "<h1>InspIRCd Configuration</h1><p>";
89
90                                 for (ConfigDataHash::iterator x = ServerInstance->Config->config_data.begin(); x != ServerInstance->Config->config_data.end(); ++x)
91                                 {
92                                         data << "&lt;" << x->first << " ";
93                                         ConfigTag* tag = x->second;
94                                         for (std::vector<KeyVal>::const_iterator j = tag->getItems().begin(); j != tag->getItems().end(); j++)
95                                         {
96                                                 data << Sanitize(j->first) << "=&quot;" << Sanitize(j->second) << "&quot; ";
97                                         }
98                                         data << "&gt;<br>";
99                                 }
100
101                                 data << "</body></html>";
102                                 /* Send the document back to m_httpd */
103                                 HTTPDocumentResponse response(this, *http, &data, 200);
104                                 response.headers.SetHeader("X-Powered-By", "m_httpd_config.so");
105                                 response.headers.SetHeader("Content-Type", "text/html");
106                                 response.Send();
107                         }
108                 }
109         }
110
111         virtual ~ModuleHttpStats()
112         {
113         }
114
115         virtual Version GetVersion()
116         {
117                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR);
118         }
119 };
120
121 MODULE_INIT(ModuleHttpStats)