]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_config.cpp
25d2f54bf369431b986d82139c8b83419aba9e86
[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, public HTTPRequestEventListener
25 {
26         HTTPdAPI API;
27
28  public:
29         ModuleHttpConfig()
30                 : HTTPRequestEventListener(this)
31                 , API(this)
32         {
33         }
34
35         ModResult OnHTTPRequest(HTTPRequest& request) CXX11_OVERRIDE
36         {
37                 if ((request.GetPath() != "/config") && (request.GetPath() != "/config/"))
38                         return MOD_RES_PASSTHRU;
39
40                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling request for the HTTP /config route");
41                 std::stringstream buffer;
42
43                 ConfigDataHash& config = ServerInstance->Config->config_data;
44                 for (ConfigDataHash::const_iterator citer = config.begin(); citer != config.end(); ++citer)
45                 {
46                         // Show the location of the tag in a comment.
47                         ConfigTag* tag = citer->second;
48                         buffer << "# " << tag->getTagLocation() << std::endl
49                                 << '<' << tag->tag << ' ';
50
51                         // Print out the tag with all keys aligned vertically.
52                         const std::string indent(tag->tag.length() + 2, ' ');
53                         const ConfigItems& items = tag->getItems();
54                         for (ConfigItems::const_iterator kiter = items.begin(); kiter != items.end(); )
55                         {
56                                 ConfigItems::const_iterator curr = kiter++;
57                                 buffer << curr->first << "=\"" << ServerConfig::Escape(curr->second) << '"';
58                                 if (kiter != items.end())
59                                         buffer << std::endl << indent;
60                         }
61                         buffer << '>' << std::endl << std::endl;
62                 }
63
64                 HTTPDocumentResponse response(this, request, &buffer, 200);
65                 response.headers.SetHeader("X-Powered-By", MODNAME);
66                 response.headers.SetHeader("Content-Type", "text/plain");
67                 API->SendResponse(response);
68                 return MOD_RES_DENY;
69         }
70
71         Version GetVersion() CXX11_OVERRIDE
72         {
73                 return Version("Allows for the server configuration to be viewed over HTTP via m_httpd", VF_VENDOR);
74         }
75 };
76
77 MODULE_INIT(ModuleHttpConfig)