]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
70b73e13dc333a1dce0e6311029c24eb755e3c4d
[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 = true;
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 << "<inspircdstats>";
91
92                                 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
93
94                                 data << "<general>";
95                                 data << "<usercount>" << ServerInstance->clientlist->size() << "</usercount>";
96                                 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
97                                 data << "<opercount>" << ServerInstance->all_opers.size() << "</opercount>";
98                                 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
99                                         "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "')</socketengine>";
100                                 data << "</general>";
101                                 data << "<modulelist>";
102                                 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
103                                 {
104                                         if (!ServerInstance->Config->module_names[i].empty())
105                                                 data << "<module>" << ServerInstance->Config->module_names[i] << "</module>";
106                                 }
107                                 data << "</modulelist>";
108
109                                 data << "<channellist>";
110                                 /* If the list has changed since last time it was displayed, re-sort it
111                                  * this time only (not every time, as this would be moronic)
112                                  */
113                                 if (this->changed)
114                                         this->SortList();
115
116                                 int n = 0;
117                                 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
118                                 {
119                                         chanrec* c = ServerInstance->FindChan(a->second.c_str());
120                                         if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
121                                         {
122                                                 data << "<channel>";
123                                                 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
124                                                 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
125                                                 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
126                                                 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
127                                                 data << "<channeltopic>" << c->topic << "</channeltopic>";
128                                                 data << "</channel>";
129                                         }
130                                 }
131
132                                 data << "</channellist>";
133                                 data << "</inspircdstats>";
134
135                                 /* Send the document back to m_httpd */
136                                 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/xml\r\n");
137                                 Request req((char*)&response, (Module*)this, event->GetSource());
138                                 req.Send();
139                         }
140                 }
141         }
142
143         void OnChannelDelete(chanrec* chan)
144         {
145                 StatsIter a = sh->find(chan->name);
146                 if (a != sh->end())
147                 {
148                         sh->erase(a);
149                 }
150                 this->changed = true;
151         }
152
153         void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
154         {
155                 StatsIter a = sh->find(channel->name);
156                 if (a != sh->end())
157                 {
158                         a->second++;
159                 }
160                 else
161                 {
162                         irc::string name = channel->name;
163                         sh->insert(std::pair<irc::string,int>(name,1));
164                 }
165                 this->changed = true;
166         }
167
168         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
169         {
170                 StatsIter a = sh->find(channel->name);
171                 if (a != sh->end())
172                 {
173                         a->second--;
174                 }
175                 this->changed = true;
176         }
177
178         void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
179         {
180                 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
181                 {
182                         chanrec* c = v->first;
183                         StatsIter a = sh->find(c->name);
184                         if (a != sh->end())
185                         {
186                                 a->second--;
187                         }
188                 }
189                 this->changed = true;
190         }
191
192         char* OnRequest(Request* request)
193         {
194                 return NULL;
195         }
196
197         void Implements(char* List)
198         {
199                 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
200         }
201
202         virtual ~ModuleHttpStats()
203         {
204                 delete sh;
205                 delete so;
206         }
207
208         virtual Version GetVersion()
209         {
210                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
211         }
212 };
213
214 MODULE_INIT(ModuleHttpStats)