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