]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
FIX channel user count stuff. next to do, order the user count list
[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 "configreader.h"
23 #include "modules.h"
24 #include "inspsocket.h"
25 #include "helperfuncs.h"
26 #include "httpd.h"
27 #include "inspircd.h"
28
29 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
30
31 extern user_hash clientlist;
32 extern chan_hash chanlist;
33 extern std::vector<userrec*> all_opers;
34 extern InspIRCd* ServerInstance;
35 extern ServerConfig* Config;
36
37 extern int MODCOUNT;
38
39 typedef std::map<irc::string,int> StatsHash;
40 typedef StatsHash::iterator StatsIter;
41 static StatsHash* sh = new StatsHash();
42
43 class ModuleHttpStats : public Module
44 {
45         Server* Srv;
46         std::string stylesheet;
47
48  public:
49
50         void ReadConfig()
51         {
52                 ConfigReader c;
53                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
54         }
55
56         ModuleHttpStats(Server* Me) : Module::Module(Me)
57         {
58                 Srv = Me;
59                 ReadConfig();
60         }
61
62         void OnEvent(Event* event)
63         {
64                 std::stringstream data("");
65
66                 if (event->GetEventID() == "httpd_url")
67                 {
68                         HTTPRequest* http = (HTTPRequest*)event->GetData();
69
70                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
71                         {
72                                 log(DEBUG,"HTTP URL!");
73
74                                 data << "<HTML><HEAD>";
75                                 data << "<TITLE>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</TITLE>";
76                                 data << "</HEAD><BODY>";
77                                 data << "<H1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</H1>";
78
79                                 data << "<DIV ID='TOTALS'>";
80                                 data << "<H2>Totals</H2>";
81                                 data << "<TABLE>";
82                                 data << "<TR><TD>Users</TD><TD>" << clientlist.size() << "</TD></TR>";
83                                 data << "<TR><TD>Channels</TD><TD>" << chanlist.size() << "</TD></TR>";
84                                 data << "<TR><TD>Opers</TD><TD>" << all_opers.size() << "</TD></TR>";
85                                 data << "<TR><TD>Sockets</TD><TD>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</TD></TR>";
86                                 data << "</TABLE>";
87                                 data << "</DIV>";
88
89                                 data << "<DIV ID='MODULES'>";
90                                 data << "<H2>Modules</H2>";
91                                 data << "<TABLE>";
92                                 for (int i = 0; i <= MODCOUNT; i++)
93                                 {
94                                         if (Config->module_names[i] != "")
95                                                 data << "<TR><TD>" << Config->module_names[i] << "</TD></TR>";
96                                 }
97                                 data << "</TABLE>";
98                                 data << "</DIV>";
99
100                                 data << "<DIV ID='CHANNELS'>";
101                                 data << "<H2>Channels</H2>";
102                                 data << "<TABLE>";
103                                 data << "<TR><TH>Users</TH><TH>Count</TH></TR>";
104                                 for (StatsIter a = sh->begin(); a != sh->end(); a++)
105                                 {
106                                         data << "<TR><TD>" << a->second << "</TD><TD>" << a->first << "</TD></TR>";
107                                 }
108                                 data << "</TABLE>";
109                                 data << "</DIV>";
110
111                                 
112                                 data << "</BODY>";
113                                 data << "</HTML>";
114
115                                 /* Send the document back to m_httpd */
116                                 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html\r\n");
117                                 Request req((char*)&response, (Module*)this, event->GetSource());
118                                 req.Send();
119
120                                 log(DEBUG,"Sent");
121                         }
122                 }
123         }
124
125         void OnChannelDelete(chanrec* chan)
126         {
127                 StatsIter a = sh->find(chan->name);
128                 if (a != sh->end())
129                 {
130                         sh->erase(a);
131                 }
132         }
133
134         void OnUserJoin(userrec* user, chanrec* channel)
135         {
136                 StatsIter a = sh->find(channel->name);
137                 if (a != sh->end())
138                 {
139                         a->second++;
140                 }
141                 else
142                 {
143                         irc::string name = channel->name;
144                         sh->insert(std::pair<irc::string,int>(name,1));
145                 }
146         }
147
148         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
149         {
150                 StatsIter a = sh->find(channel->name);
151                 if (a != sh->end())
152                 {
153                         a->second--;
154                 }
155         }
156
157         void OnUserQuit(userrec* user, const std::string &message)
158         {
159                 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
160                 {
161                         if (((ucrec*)(*v))->channel)
162                         {
163                                 chanrec* c = ((ucrec*)(*v))->channel;
164                                 StatsIter a = sh->find(c->name);
165                                 if (a != sh->end())
166                                 {
167                                         a->second--;
168                                 }
169                         }
170                 }
171         }
172
173         char* OnRequest(Request* request)
174         {
175                 return NULL;
176         }
177
178         void Implements(char* List)
179         {
180                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
181         }
182
183         virtual ~ModuleHttpStats()
184         {
185                 delete sh;
186         }
187
188         virtual Version GetVersion()
189         {
190                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
191         }
192 };
193
194
195 class ModuleHttpStatsFactory : public ModuleFactory
196 {
197  public:
198         ModuleHttpStatsFactory()
199         {
200         }
201         
202         ~ModuleHttpStatsFactory()
203         {
204         }
205         
206         virtual Module * CreateModule(Server* Me)
207         {
208                 return new ModuleHttpStats(Me);
209         }
210 };
211
212
213 extern "C" void * init_module( void )
214 {
215         return new ModuleHttpStatsFactory;
216 }