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"
29 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
33 typedef std::map<irc::string,int> StatsHash;
34 typedef StatsHash::iterator StatsIter;
36 typedef std::vector<std::pair<int,irc::string> > SortedList;
37 typedef SortedList::iterator SortedIter;
39 static StatsHash* sh = new StatsHash();
40 static SortedList* so = new SortedList();
42 class ModuleHttpStats : public Module
45 std::string stylesheet;
52 ConfigReader c(ServerInstance);
53 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
56 ModuleHttpStats(InspIRCd* Me) : Module::Module(Me)
60 this->changed = false;
63 void InsertOrder(irc::string channel, int count)
65 /* This function figures out where in the sorted list to put an item from the hash */
67 for (a = so->begin(); a != so->end(); a++)
69 /* Found an item equal to or less than, we insert our item before it */
70 if (a->first <= count)
72 so->insert(a,std::pair<int,irc::string>(count,channel));
76 /* There are no items in the list yet, insert something at the beginning */
77 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
82 /* Sorts the hash into the sorted list using an insertion sort */
84 for (StatsIter a = sh->begin(); a != sh->end(); a++)
85 InsertOrder(a->first, a->second);
86 this->changed = false;
89 void OnEvent(Event* event)
91 std::stringstream data("");
93 if (event->GetEventID() == "httpd_url")
95 HTTPRequest* http = (HTTPRequest*)event->GetData();
97 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
99 data << "<!DOCTYPE html PUBLIC \
100 \"-//W3C//DTD XHTML 1.1//EN\" \
101 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\
102 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
105 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
106 data << "<title>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</title>";
107 data << "</head><body>";
108 data << "<h1>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</h1>";
110 data << "<div class='totals'>";
111 data << "<h2>Totals</h2>";
113 data << "<tr><td>Users</td><td>" << ServerInstance->clientlist.size() << "</td></tr>";
114 data << "<tr><td>Channels</td><td>" << ServerInstance->chanlist.size() << "</td></tr>";
115 data << "<tr><td>Opers</td><td>" << ServerInstance->all_opers.size() << "</td></tr>";
116 data << "<tr><td>Sockets</td><td>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</td></tr>";
120 data << "<div class='modules'>";
121 data << "<h2>Modules</h2>";
123 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
125 if (ServerInstance->Config->module_names[i] != "")
126 data << "<tr><td>" << ServerInstance->Config->module_names[i] << "</td></tr>";
131 data << "<div class='channels'>";
132 data << "<h2>Channels</h2>";
134 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
136 /* If the list has changed since last time it was displayed, re-sort it
137 * this time only (not every time, as this would be moronic)
143 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
145 chanrec* c = ServerInstance->FindChan(a->second.c_str());
148 data << "<tr><td>" << a->first << "</td><td>" << a->second << "</td>";
149 data << "<td>" << c->GetOppedUsers()->size() << "</td>";
150 data << "<td>" << c->GetHalfoppedUsers()->size() << "</td>";
151 data << "<td>" << c->GetVoicedUsers()->size() << "</td>";
152 data << "<td>" << c->topic << "</td>";
164 data << "<div class='validion'>";
165 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>";
171 /* Send the document back to m_httpd */
172 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html; charset=iso-8859-1\r\n");
173 Request req((char*)&response, (Module*)this, event->GetSource());
176 ServerInstance->Log(DEBUG,"Sent");
181 void OnChannelDelete(chanrec* chan)
183 StatsIter a = sh->find(chan->name);
188 this->changed = true;
191 void OnUserJoin(userrec* user, chanrec* channel)
193 StatsIter a = sh->find(channel->name);
200 irc::string name = channel->name;
201 sh->insert(std::pair<irc::string,int>(name,1));
203 this->changed = true;
206 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
208 StatsIter a = sh->find(channel->name);
213 this->changed = true;
216 void OnUserQuit(userrec* user, const std::string &message)
218 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
220 if (((ucrec*)(*v))->channel)
222 chanrec* c = ((ucrec*)(*v))->channel;
223 StatsIter a = sh->find(c->name);
230 this->changed = true;
233 char* OnRequest(Request* request)
238 void Implements(char* List)
240 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
243 virtual ~ModuleHttpStats()
248 virtual Version GetVersion()
250 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
255 class ModuleHttpStatsFactory : public ModuleFactory
258 ModuleHttpStatsFactory()
262 ~ModuleHttpStatsFactory()
266 virtual Module * CreateModule(InspIRCd* Me)
268 return new ModuleHttpStats(Me);
273 extern "C" void * init_module( void )
275 return new ModuleHttpStatsFactory;