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