1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
22 #include "configreader.h"
24 #include "inspsocket.h"
25 #include "helperfuncs.h"
29 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
31 extern std::vector<userrec*> all_opers;
32 extern InspIRCd* ServerInstance;
36 typedef std::map<irc::string,int> StatsHash;
37 typedef StatsHash::iterator StatsIter;
39 typedef std::vector<std::pair<int,irc::string> > SortedList;
40 typedef SortedList::iterator SortedIter;
42 static StatsHash* sh = new StatsHash();
43 static SortedList* so = new SortedList();
45 class ModuleHttpStats : public Module
48 std::string stylesheet;
56 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
59 ModuleHttpStats(Server* Me) : Module::Module(Me)
63 this->changed = false;
66 void InsertOrder(irc::string channel, int count)
68 /* This function figures out where in the sorted list to put an item from the hash */
70 for (a = so->begin(); a != so->end(); a++)
72 /* Found an item equal to or less than, we insert our item before it */
73 if (a->first <= count)
75 so->insert(a,std::pair<int,irc::string>(count,channel));
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));
85 /* Sorts the hash into the sorted list using an insertion sort */
87 for (StatsIter a = sh->begin(); a != sh->end(); a++)
88 InsertOrder(a->first, a->second);
89 this->changed = false;
92 void OnEvent(Event* event)
94 std::stringstream data("");
96 if (event->GetEventID() == "httpd_url")
98 HTTPRequest* http = (HTTPRequest*)event->GetData();
100 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
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\">";
108 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
109 data << "<title>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</title>";
110 data << "</head><body>";
111 data << "<h1>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</h1>";
113 data << "<div class='totals'>";
114 data << "<h2>Totals</h2>";
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>";
123 data << "<div class='modules'>";
124 data << "<h2>Modules</h2>";
126 for (int i = 0; i <= MODCOUNT; i++)
128 if (ServerInstance->Config->module_names[i] != "")
129 data << "<tr><td>" << ServerInstance->Config->module_names[i] << "</td></tr>";
134 data << "<div class='channels'>";
135 data << "<h2>Channels</h2>";
137 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
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)
146 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
148 chanrec* c = ServerInstance->FindChan(a->second.c_str());
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>";
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>";
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());
184 void OnChannelDelete(chanrec* chan)
186 StatsIter a = sh->find(chan->name);
191 this->changed = true;
194 void OnUserJoin(userrec* user, chanrec* channel)
196 StatsIter a = sh->find(channel->name);
203 irc::string name = channel->name;
204 sh->insert(std::pair<irc::string,int>(name,1));
206 this->changed = true;
209 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
211 StatsIter a = sh->find(channel->name);
216 this->changed = true;
219 void OnUserQuit(userrec* user, const std::string &message)
221 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
223 if (((ucrec*)(*v))->channel)
225 chanrec* c = ((ucrec*)(*v))->channel;
226 StatsIter a = sh->find(c->name);
233 this->changed = true;
236 char* OnRequest(Request* request)
241 void Implements(char* List)
243 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
246 virtual ~ModuleHttpStats()
251 virtual Version GetVersion()
253 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
258 class ModuleHttpStatsFactory : public ModuleFactory
261 ModuleHttpStatsFactory()
265 ~ModuleHttpStatsFactory()
269 virtual Module * CreateModule(Server* Me)
271 return new ModuleHttpStats(Me);
276 extern "C" void * init_module( void )
278 return new ModuleHttpStatsFactory;