The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_httpd_config.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *          the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 #include "httpd.h"
00016 #include "protocol.h"
00017 
00018 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
00019 /* $ModDep: httpd.h */
00020 
00021 class ModuleHttpStats : public Module
00022 {
00023 
00024         std::string stylesheet;
00025         bool changed;
00026 
00027  public:
00028 
00029         void ReadConfig()
00030         {
00031                 ConfigReader c(ServerInstance);
00032                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
00033         }
00034 
00035         ModuleHttpStats(InspIRCd* Me) : Module(Me)
00036         {
00037                 ReadConfig();
00038                 this->changed = true;
00039                 Implementation eventlist[] = { I_OnEvent, I_OnRequest };
00040                 ServerInstance->Modules->Attach(eventlist, this, 2);
00041         }
00042 
00043         std::string Sanitize(const std::string &str)
00044         {
00045                 std::string ret;
00046 
00047                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
00048                 {
00049                         switch (*x)
00050                         {
00051                                 case '<':
00052                                         ret += "&lt;";
00053                                 break;
00054                                 case '>':
00055                                         ret += "&gt;";
00056                                 break;
00057                                 case '&':
00058                                         ret += "&amp;";
00059                                 break;
00060                                 case '"':
00061                                         ret += "&quot;";
00062                                 break;
00063                                 default:
00064                                         if (*x < 32 || *x > 126)
00065                                         {
00066                                                 int n = *x;
00067                                                 ret += ("&#" + ConvToStr(n) + ";");
00068                                         }
00069                                         else
00070                                                 ret += *x;
00071                                 break;
00072                         }
00073                 }
00074                 return ret;
00075         }
00076 
00077         void OnEvent(Event* event)
00078         {
00079                 std::stringstream data("");
00080 
00081                 if (event->GetEventID() == "httpd_url")
00082                 {
00083                         ServerInstance->Logs->Log("m_http_stats", DEBUG,"Handling httpd event");
00084                         HTTPRequest* http = (HTTPRequest*)event->GetData();
00085 
00086                         if ((http->GetURI() == "/config") || (http->GetURI() == "/config/"))
00087                         {
00088                                 data << "<html><head><title>InspIRCd Configuration</title></head><body>";
00089                                 data << "<h1>InspIRCd Configuration</h1><p>";
00090 
00091                                 for (ConfigDataHash::iterator x = ServerInstance->Config->config_data.begin(); x != ServerInstance->Config->config_data.end(); ++x)
00092                                 {
00093                                         data << "&lt;" << x->first << " ";
00094                                         for (KeyValList::iterator j = x->second.begin(); j != x->second.end(); j++)
00095                                         {
00096                                                 data << j->first << "=&quot;" << j->second << "&quot; ";
00097                                         }
00098                                         data << "&gt;<br>";
00099                                 }
00100 
00101                                 data << "</body></html>";
00102                                 /* Send the document back to m_httpd */
00103                                 HTTPDocument response(http->sock, &data, 200);
00104                                 response.headers.SetHeader("X-Powered-By", "m_httpd_config.so");
00105                                 response.headers.SetHeader("Content-Type", "text/html");
00106                                 Request req((char*)&response, (Module*)this, event->GetSource());
00107                                 req.Send();
00108                         }
00109                 }
00110         }
00111 
00112         const char* OnRequest(Request* request)
00113         {
00114                 return NULL;
00115         }
00116 
00117 
00118         virtual ~ModuleHttpStats()
00119         {
00120         }
00121 
00122         virtual Version GetVersion()
00123         {
00124                 return Version("$Id: m_httpd_config.cpp 10291 2008-08-25 20:35:51Z w00t $", VF_VENDOR, API_VERSION);
00125         }
00126 };
00127 
00128 MODULE_INIT(ModuleHttpStats)