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;
34 typedef std::map<irc::string,int> StatsHash;
35 typedef StatsHash::iterator StatsIter;
37 typedef std::vector<std::pair<int,irc::string> > SortedList;
38 typedef SortedList::iterator SortedIter;
40 static StatsHash* sh = new StatsHash();
41 static SortedList* so = new SortedList();
43 class ModuleHttpStats : public Module
46 std::string stylesheet;
54 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
57 ModuleHttpStats(Server* Me) : Module::Module(Me)
61 this->changed = false;
64 void InsertOrder(irc::string channel, int count)
66 /* This function figures out where in the sorted list to put an item from the hash */
68 for (a = so->begin(); a != so->end(); a++)
70 /* Found an item equal to or less than, we insert our item before it */
71 if (a->first <= count)
73 so->insert(a,std::pair<int,irc::string>(count,channel));
77 /* There are no items in the list yet, insert something at the beginning */
78 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
83 /* Sorts the hash into the sorted list using an insertion sort */
85 for (StatsIter a = sh->begin(); a != sh->end(); a++)
86 InsertOrder(a->first, a->second);
87 this->changed = false;
90 void OnEvent(Event* event)
92 std::stringstream data("");
94 if (event->GetEventID() == "httpd_url")
96 HTTPRequest* http = (HTTPRequest*)event->GetData();
98 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
100 data << "<!DOCTYPE html PUBLIC \
101 \"-//W3C//DTD XHTML 1.1//EN\" \
102 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\
103 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
106 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
107 data << "<title>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</title>";
108 data << "</head><body>";
109 data << "<h1>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</h1>";
111 data << "<div class='totals'>";
112 data << "<h2>Totals</h2>";
114 data << "<tr><td>Users</td><td>" << ServerInstance->clientlist.size() << "</td></tr>";
115 data << "<tr><td>Channels</td><td>" << ServerInstance->chanlist.size() << "</td></tr>";
116 data << "<tr><td>Opers</td><td>" << all_opers.size() << "</td></tr>";
117 data << "<tr><td>Sockets</td><td>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</td></tr>";
121 data << "<div class='modules'>";
122 data << "<h2>Modules</h2>";
124 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
126 if (ServerInstance->Config->module_names[i] != "")
127 data << "<tr><td>" << ServerInstance->Config->module_names[i] << "</td></tr>";
132 data << "<div class='channels'>";
133 data << "<h2>Channels</h2>";
135 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
137 /* If the list has changed since last time it was displayed, re-sort it
138 * this time only (not every time, as this would be moronic)
144 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
146 chanrec* c = ServerInstance->FindChan(a->second.c_str());
149 data << "<tr><td>" << a->first << "</td><td>" << a->second << "</td>";
150 data << "<td>" << c->GetOppedUsers()->size() << "</td>";
151 data << "<td>" << c->GetHalfoppedUsers()->size() << "</td>";
152 data << "<td>" << c->GetVoicedUsers()->size() << "</td>";
153 data << "<td>" << c->topic << "</td>";
165 data << "<div class='validion'>";
166 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>";
172 /* Send the document back to m_httpd */
173 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html; charset=iso-8859-1\r\n");
174 Request req((char*)&response, (Module*)this, event->GetSource());
182 void OnChannelDelete(chanrec* chan)
184 StatsIter a = sh->find(chan->name);
189 this->changed = true;
192 void OnUserJoin(userrec* user, chanrec* channel)
194 StatsIter a = sh->find(channel->name);
201 irc::string name = channel->name;
202 sh->insert(std::pair<irc::string,int>(name,1));
204 this->changed = true;
207 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
209 StatsIter a = sh->find(channel->name);
214 this->changed = true;
217 void OnUserQuit(userrec* user, const std::string &message)
219 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
221 if (((ucrec*)(*v))->channel)
223 chanrec* c = ((ucrec*)(*v))->channel;
224 StatsIter a = sh->find(c->name);
231 this->changed = true;
234 char* OnRequest(Request* request)
239 void Implements(char* List)
241 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
244 virtual ~ModuleHttpStats()
249 virtual Version GetVersion()
251 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
256 class ModuleHttpStatsFactory : public ModuleFactory
259 ModuleHttpStatsFactory()
263 ~ModuleHttpStatsFactory()
267 virtual Module * CreateModule(Server* Me)
269 return new ModuleHttpStats(Me);
274 extern "C" void * init_module( void )
276 return new ModuleHttpStatsFactory;