]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_config.cpp
Merge pull request #959 from Alef-Burzmali/master+fixcloaking
[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 "modules/httpd.h"
23
24 class ModuleHttpConfig : public Module
25 {
26         HTTPdAPI API;
27
28  public:
29         ModuleHttpConfig()
30                 : API(this)
31         {
32         }
33
34         std::string Sanitize(const std::string &str)
35         {
36                 std::string ret;
37
38                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
39                 {
40                         switch (*x)
41                         {
42                                 case '<':
43                                         ret += "&lt;";
44                                 break;
45                                 case '>':
46                                         ret += "&gt;";
47                                 break;
48                                 case '&':
49                                         ret += "&amp;";
50                                 break;
51                                 case '"':
52                                         ret += "&quot;";
53                                 break;
54                                 default:
55                                         if (*x < 32 || *x > 126)
56                                         {
57                                                 int n = *x;
58                                                 ret += ("&#" + ConvToStr(n) + ";");
59                                         }
60                                         else
61                                                 ret += *x;
62                                 break;
63                         }
64                 }
65                 return ret;
66         }
67
68         void OnEvent(Event& event) CXX11_OVERRIDE
69         {
70                 std::stringstream data("");
71
72                 if (event.id == "httpd_url")
73                 {
74                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
75                         HTTPRequest* http = (HTTPRequest*)&event;
76
77                         if ((http->GetURI() == "/config") || (http->GetURI() == "/config/"))
78                         {
79                                 data << "<html><head><title>InspIRCd Configuration</title></head><body>";
80                                 data << "<h1>InspIRCd Configuration</h1><p>";
81
82                                 for (ConfigDataHash::iterator x = ServerInstance->Config->config_data.begin(); x != ServerInstance->Config->config_data.end(); ++x)
83                                 {
84                                         data << "&lt;" << x->first << " ";
85                                         ConfigTag* tag = x->second;
86                                         for (std::vector<KeyVal>::const_iterator j = tag->getItems().begin(); j != tag->getItems().end(); j++)
87                                         {
88                                                 data << Sanitize(j->first) << "=&quot;" << Sanitize(j->second) << "&quot; ";
89                                         }
90                                         data << "&gt;<br>";
91                                 }
92
93                                 data << "</body></html>";
94                                 /* Send the document back to m_httpd */
95                                 HTTPDocumentResponse response(this, *http, &data, 200);
96                                 response.headers.SetHeader("X-Powered-By", MODNAME);
97                                 response.headers.SetHeader("Content-Type", "text/html");
98                                 API->SendResponse(response);
99                         }
100                 }
101         }
102
103         Version GetVersion() CXX11_OVERRIDE
104         {
105                 return Version("Allows for the server configuration to be viewed over HTTP via m_httpd.so", VF_VENDOR);
106         }
107 };
108
109 MODULE_INIT(ModuleHttpConfig)