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