]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
WHEEEEE!!!!!
[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
42 typedef std::vector<std::pair<int,irc::string> > SortedList;
43 typedef SortedList::iterator SortedIter;
44
45 static StatsHash* sh = new StatsHash();
46 static SortedList* so = new SortedList();
47
48 class ModuleHttpStats : public Module
49 {
50         Server* Srv;
51         std::string stylesheet;
52         bool changed;
53
54  public:
55
56         void ReadConfig()
57         {
58                 ConfigReader c;
59                 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
60         }
61
62         ModuleHttpStats(Server* Me) : Module::Module(Me)
63         {
64                 Srv = Me;
65                 ReadConfig();
66                 this->changed = false;
67         }
68
69         void InsertOrder(irc::string channel, int count)
70         {
71                 /* This function figures out where in the sorted list to put an item from the hash */
72                 SortedIter a;
73                 for (a = so->begin(); a != so->end(); a++)
74                 {
75                         /* Found an item equal to or less than, we insert our item before it */
76                         if (a->first <= count)
77                         {
78                                 so->insert(a,std::pair<int,irc::string>(count,channel));
79                                 return;
80                         }
81                 }
82                 /* There are no items in the list yet, insert something at the beginning */
83                 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
84         }
85
86         void SortList()
87         {
88                 /* Sorts the hash into the sorted list using an insertion sort */
89                 so->clear();
90                 for (StatsIter a = sh->begin(); a != sh->end(); a++)
91                         InsertOrder(a->first, a->second);
92                 this->changed = false;
93         }
94
95         void OnEvent(Event* event)
96         {
97                 std::stringstream data("");
98
99                 if (event->GetEventID() == "httpd_url")
100                 {
101                         HTTPRequest* http = (HTTPRequest*)event->GetData();
102
103                         if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
104                         {
105                                 data << "<!DOCTYPE html PUBLIC \
106                                         \"-//W3C//DTD XHTML 1.1//EN\" \
107                                         \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\
108                                         <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
109
110                                 data << "<head>";
111                                 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
112                                 data << "<title>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</title>";
113                                 data << "</head><body>";
114                                 data << "<h1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</h1>";
115
116                                 data << "<div class='totals'>";
117                                 data << "<h2>Totals</h2>";
118                                 data << "<table>";
119                                 data << "<tr><td>Users</td><td>" << clientlist.size() << "</td></tr>";
120                                 data << "<tr><td>Channels</td><td>" << chanlist.size() << "</td></tr>";
121                                 data << "<tr><td>Opers</td><td>" << all_opers.size() << "</td></tr>";
122                                 data << "<tr><td>Sockets</td><td>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</td></tr>";
123                                 data << "</table>";
124                                 data << "</div>";
125
126                                 data << "<div class='modules'>";
127                                 data << "<h2>Modules</h2>";
128                                 data << "<table>";
129                                 for (int i = 0; i <= MODCOUNT; i++)
130                                 {
131                                         if (Config->module_names[i] != "")
132                                                 data << "<tr><td>" << Config->module_names[i] << "</td></tr>";
133                                 }
134                                 data << "</table>";
135                                 data << "</div>";
136
137                                 data << "<div class='channels'>";
138                                 data << "<h2>Channels</h2>";
139                                 data << "<table>";
140                                 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
141
142                                 /* If the list has changed since last time it was displayed, re-sort it
143                                  * this time only (not every time, as this would be moronic)
144                                  */
145                                 if (this->changed)
146                                         this->SortList();
147
148                                 int n = 0;
149                                 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
150                                 {
151                                         chanrec* c = Srv->FindChannel(a->second.c_str());
152                                         if (c)
153                                         {
154                                                 data << "<tr><td>" << a->first << "</td><td>" << a->second << "</td>";
155                                                 data << "<td>" << c->GetOppedUsers()->size() << "</td>";
156                                                 data << "<td>" << c->GetHalfoppedUsers()->size() << "</td>";
157                                                 data << "<td>" << c->GetVoicedUsers()->size() << "</td>";
158                                                 data << "<td>" << c->topic << "</td>";
159                                                 data << "</tr>";
160                                         }
161                                 }
162
163                                 data << "</table>";
164                                 data << "</div>";
165
166
167
168
169
170                                 data << "<div class='validion'>";
171                                 data << "<p><a href='http://validator.w3.org/check?uri=referer'><img src='http://www.w3.org/Icons/valid-xhtml11' alt='Valid XHTML 1.1' height='31' width='88' /></a></p>";
172                                 data << "</div>";
173                                 
174                                 data << "</body>";
175                                 data << "</html>";
176
177                                 /* Send the document back to m_httpd */
178                                 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html; charset=iso-8859-1\r\n");
179                                 Request req((char*)&response, (Module*)this, event->GetSource());
180                                 req.Send();
181
182                                 log(DEBUG,"Sent");
183                         }
184                 }
185         }
186
187         void OnChannelDelete(chanrec* chan)
188         {
189                 StatsIter a = sh->find(chan->name);
190                 if (a != sh->end())
191                 {
192                         sh->erase(a);
193                 }
194                 this->changed = true;
195         }
196
197         void OnUserJoin(userrec* user, chanrec* channel)
198         {
199                 StatsIter a = sh->find(channel->name);
200                 if (a != sh->end())
201                 {
202                         a->second++;
203                 }
204                 else
205                 {
206                         irc::string name = channel->name;
207                         sh->insert(std::pair<irc::string,int>(name,1));
208                 }
209                 this->changed = true;
210         }
211
212         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
213         {
214                 StatsIter a = sh->find(channel->name);
215                 if (a != sh->end())
216                 {
217                         a->second--;
218                 }
219                 this->changed = true;
220         }
221
222         void OnUserQuit(userrec* user, const std::string &message)
223         {
224                 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
225                 {
226                         if (((ucrec*)(*v))->channel)
227                         {
228                                 chanrec* c = ((ucrec*)(*v))->channel;
229                                 StatsIter a = sh->find(c->name);
230                                 if (a != sh->end())
231                                 {
232                                         a->second--;
233                                 }
234                         }
235                 }
236                 this->changed = true;
237         }
238
239         char* OnRequest(Request* request)
240         {
241                 return NULL;
242         }
243
244         void Implements(char* List)
245         {
246                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
247         }
248
249         virtual ~ModuleHttpStats()
250         {
251                 delete sh;
252         }
253
254         virtual Version GetVersion()
255         {
256                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
257         }
258 };
259
260
261 class ModuleHttpStatsFactory : public ModuleFactory
262 {
263  public:
264         ModuleHttpStatsFactory()
265         {
266         }
267         
268         ~ModuleHttpStatsFactory()
269         {
270         }
271         
272         virtual Module * CreateModule(Server* Me)
273         {
274                 return new ModuleHttpStats(Me);
275         }
276 };
277
278
279 extern "C" void * init_module( void )
280 {
281         return new ModuleHttpStatsFactory;
282 }