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