]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_config.cpp
Merge pull request #92 from Robby-/insp20-headers
[user/henk/code/inspircd.git] / src / modules / m_httpd_config.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "httpd.h"
23 #include "protocol.h"
24
25 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
26
27 class ModuleHttpConfig : public Module
28 {
29  public:
30         ModuleHttpConfig()      {
31                 Implementation eventlist[] = { I_OnEvent };
32                 ServerInstance->Modules->Attach(eventlist, this, 1);
33         }
34
35         std::string Sanitize(const std::string &str)
36         {
37                 std::string ret;
38
39                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
40                 {
41                         switch (*x)
42                         {
43                                 case '<':
44                                         ret += "&lt;";
45                                 break;
46                                 case '>':
47                                         ret += "&gt;";
48                                 break;
49                                 case '&':
50                                         ret += "&amp;";
51                                 break;
52                                 case '"':
53                                         ret += "&quot;";
54                                 break;
55                                 default:
56                                         if (*x < 32 || *x > 126)
57                                         {
58                                                 int n = *x;
59                                                 ret += ("&#" + ConvToStr(n) + ";");
60                                         }
61                                         else
62                                                 ret += *x;
63                                 break;
64                         }
65                 }
66                 return ret;
67         }
68
69         void OnEvent(Event& event)
70         {
71                 std::stringstream data("");
72
73                 if (event.id == "httpd_url")
74                 {
75                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
76                         HTTPRequest* http = (HTTPRequest*)&event;
77
78                         if ((http->GetURI() == "/config") || (http->GetURI() == "/config/"))
79                         {
80                                 data << "<html><head><title>InspIRCd Configuration</title></head><body>";
81                                 data << "<h1>InspIRCd Configuration</h1><p>";
82
83                                 for (ConfigDataHash::iterator x = ServerInstance->Config->config_data.begin(); x != ServerInstance->Config->config_data.end(); ++x)
84                                 {
85                                         data << "&lt;" << x->first << " ";
86                                         ConfigTag* tag = x->second;
87                                         for (std::vector<KeyVal>::const_iterator j = tag->getItems().begin(); j != tag->getItems().end(); j++)
88                                         {
89                                                 data << Sanitize(j->first) << "=&quot;" << Sanitize(j->second) << "&quot; ";
90                                         }
91                                         data << "&gt;<br>";
92                                 }
93
94                                 data << "</body></html>";
95                                 /* Send the document back to m_httpd */
96                                 HTTPDocumentResponse response(this, *http, &data, 200);
97                                 response.headers.SetHeader("X-Powered-By", "m_httpd_config.so");
98                                 response.headers.SetHeader("Content-Type", "text/html");
99                                 response.Send();
100                         }
101                 }
102         }
103
104         virtual ~ModuleHttpConfig()
105         {
106         }
107
108         virtual Version GetVersion()
109         {
110                 return Version("Provides configuration over HTTP via m_httpd.so", VF_VENDOR);
111         }
112 };
113
114 MODULE_INIT(ModuleHttpConfig)