]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Channel user count stuff
[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                         a[channel->name] = 1;
144                 }
145         }
146
147         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
148         {
149                 StatsIter a = sh->find(channel->name);
150                 if (a != sh->end())
151                 {
152                         a->second--;
153                 }
154         }
155
156         void OnUserQuit(userrec* user, const std::string &message)
157         {
158                 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
159                 {
160                         if (((ucrec*)(*v))->channel)
161                         {
162                                 chanrec* c = ((ucrec*)(*v))->channel;
163                                 StatsIter a = sh->find(c->name);
164                                 if (a != sh->end())
165                                 {
166                                         a->second--;
167                                 }
168                         }
169                 }
170         }
171
172         char* OnRequest(Request* request)
173         {
174                 return NULL;
175         }
176
177         void Implements(char* List)
178         {
179                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
180         }
181
182         virtual ~ModuleHttpStats()
183         {
184                 delete sh;
185         }
186
187         virtual Version GetVersion()
188         {
189                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
190         }
191 };
192
193
194 class ModuleHttpStatsFactory : public ModuleFactory
195 {
196  public:
197         ModuleHttpStatsFactory()
198         {
199         }
200         
201         ~ModuleHttpStatsFactory()
202         {
203         }
204         
205         virtual Module * CreateModule(Server* Me)
206         {
207                 return new ModuleHttpStats(Me);
208         }
209 };
210
211
212 extern "C" void * init_module( void )
213 {
214         return new ModuleHttpStatsFactory;
215 }