]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Add config <options:disablehmac> to support disabling of HMAC, and tidy up to detect...
[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                 }
169         }
170
171         void OnChannelDelete(chanrec* chan)
172         {
173                 StatsIter a = sh->find(chan->name);
174                 if (a != sh->end())
175                 {
176                         sh->erase(a);
177                 }
178                 this->changed = true;
179         }
180
181         void OnUserJoin(userrec* user, chanrec* channel)
182         {
183                 StatsIter a = sh->find(channel->name);
184                 if (a != sh->end())
185                 {
186                         a->second++;
187                 }
188                 else
189                 {
190                         irc::string name = channel->name;
191                         sh->insert(std::pair<irc::string,int>(name,1));
192                 }
193                 this->changed = true;
194         }
195
196         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
197         {
198                 StatsIter a = sh->find(channel->name);
199                 if (a != sh->end())
200                 {
201                         a->second--;
202                 }
203                 this->changed = true;
204         }
205
206         void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
207         {
208                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
209                 {
210                         chanrec* c = v->first;
211                         StatsIter a = sh->find(c->name);
212                         if (a != sh->end())
213                         {
214                                 a->second--;
215                         }
216                 }
217                 this->changed = true;
218         }
219
220         char* OnRequest(Request* request)
221         {
222                 return NULL;
223         }
224
225         void Implements(char* List)
226         {
227                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
228         }
229
230         virtual ~ModuleHttpStats()
231         {
232                 delete sh;
233                 delete so;
234         }
235
236         virtual Version GetVersion()
237         {
238                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
239         }
240 };
241
242
243 class ModuleHttpStatsFactory : public ModuleFactory
244 {
245  public:
246         ModuleHttpStatsFactory()
247         {
248         }
249         
250         ~ModuleHttpStatsFactory()
251         {
252         }
253         
254         virtual Module * CreateModule(InspIRCd* Me)
255         {
256                 return new ModuleHttpStats(Me);
257         }
258 };
259
260
261 extern "C" void * init_module( void )
262 {
263         return new ModuleHttpStatsFactory;
264 }