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