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