]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_config.cpp
5f902eb2998e43a2528ebd6b9ae6fe6ef4c1267e
[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(ServerInstance);
32                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
33         }
34
35         ModuleHttpStats(InspIRCd* Me) : Module(Me)
36         {
37                 ReadConfig();
38                 this->changed = true;
39                 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
40                 ServerInstance->Modules->Attach(eventlist, this, 2);
41         }
42
43         std::string Sanitize(const std::string &str)
44         {
45                 std::string ret;
46
47                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
48                 {
49                         switch (*x)
50                         {
51                                 case '<':
52                                         ret += "&lt;";
53                                 break;
54                                 case '>':
55                                         ret += "&gt;";
56                                 break;
57                                 case '&':
58                                         ret += "&amp;";
59                                 break;
60                                 case '"':
61                                         ret += "&quot;";
62                                 break;
63                                 default:
64                                         if (*x < 32 || *x > 126)
65                                         {
66                                                 int n = *x;
67                                                 ret += ("&#" + ConvToStr(n) + ";");
68                                         }
69                                         else
70                                                 ret += *x;
71                                 break;
72                         }
73                 }
74                 return ret;
75         }
76
77         void OnEvent(Event* event)
78         {
79                 std::stringstream data("");
80
81                 if (event->GetEventID() == "httpd_url")
82                 {
83                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
84                         HTTPRequest* http = (HTTPRequest*)event->GetData();
85
86                         if ((http->GetURI() == "/config") || (http->GetURI() == "/config/"))
87                         {
88                                 data << "<html><head><title>InspIRCd Configuration</title></head><body>";
89                                 data << "<h1>InspIRCd Configuration</h1><p>";
90
91                                 for (ConfigDataHash::iterator x = ServerInstance->Config->config_data.begin(); x != ServerInstance->Config->config_data.end(); ++x)
92                                 {
93                                         data << "&lt;" << x->first << " ";
94                                         for (KeyValList::iterator j = x->second.begin(); j != x->second.end(); j++)
95                                         {
96                                                 data << j->first << "=&quot;" << j->second << "&quot; ";
97                                         }
98                                         data << "&gt;<br>";
99                                 }
100
101                                 data << "</body></html>";
102                                 /* Send the document back to m_httpd */
103                                 HTTPDocument response(http->sock, &data, 200);
104                                 response.headers.SetHeader("X-Powered-By", "m_httpd_config.so");
105                                 response.headers.SetHeader("Content-Type", "text/html");
106                                 Request req((char*)&response, (Module*)this, event->GetSource());
107                                 req.Send();
108                         }
109                 }
110         }
111
112         const char* OnRequest(Request* request)
113         {
114                 return NULL;
115         }
116
117
118         virtual ~ModuleHttpStats()
119         {
120         }
121
122         virtual Version GetVersion()
123         {
124                 return Version("Provides statistics over HTTP via m_httpd.so", VF_VENDOR, API_VERSION);
125         }
126 };
127
128 MODULE_INIT(ModuleHttpStats)