]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Check for uri and only claim /stats and /stats/
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include "inspsocket.h"
24 #include "helperfuncs.h"
25 #include "httpd.h"
26
27 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
28
29 class ModuleHttpStats : public Module
30 {
31         Server* Srv;
32         std::string stylesheet;
33
34  public:
35
36         void ReadConfig()
37         {
38                 ConfigReader c;
39                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
40         }
41
42         ModuleHttpStats(Server* Me) : Module::Module(Me)
43         {
44                 Srv = Me;
45                 ReadConfig();
46         }
47
48         void OnEvent(Event* event)
49         {
50                 std::stringstream data("");
51
52                 if (event->GetEventID() == "httpd_url")
53                 {
54                         HTTPRequest* http = (HTTPRequest*)event->GetData();
55
56                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
57                         {
58                                 log(DEBUG,"HTTP URL!");
59
60                                 data << "<HTML><HEAD>";
61                                 data << "<TITLE>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</TITLE>";
62                                 data << "</HEAD><BODY>";
63                                 data << "<H1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</H1>";
64                                 
65                                 data << "</BODY>";
66                                 data << "</HTML>";
67
68                                 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html\r\n");
69                                 Request req((char*)&response, (Module*)this, event->GetSource());
70                                 req.Send();
71
72                                 log(DEBUG,"Sent");
73                         }
74                 }
75         }
76
77         char* OnRequest(Request* request)
78         {
79                 return NULL;
80         }
81
82         void Implements(char* List)
83         {
84                 List[I_OnEvent] = List[I_OnRequest] = 1;
85         }
86
87         virtual ~ModuleHttpStats()
88         {
89         }
90
91         virtual Version GetVersion()
92         {
93                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
94         }
95 };
96
97
98 class ModuleHttpStatsFactory : public ModuleFactory
99 {
100  public:
101         ModuleHttpStatsFactory()
102         {
103         }
104         
105         ~ModuleHttpStatsFactory()
106         {
107         }
108         
109         virtual Module * CreateModule(Server* Me)
110         {
111                 return new ModuleHttpStats(Me);
112         }
113 };
114
115
116 extern "C" void * init_module( void )
117 {
118         return new ModuleHttpStatsFactory;
119 }