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