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