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"
28 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
30 typedef std::map<irc::string,int> StatsHash;
31 typedef StatsHash::iterator StatsIter;
33 typedef std::vector<std::pair<int,irc::string> > SortedList;
34 typedef SortedList::iterator SortedIter;
36 static StatsHash* sh = new StatsHash();
37 static SortedList* so = new SortedList();
39 class ModuleHttpStats : public Module
42 std::string stylesheet;
49 ConfigReader c(ServerInstance);
50 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
53 ModuleHttpStats(InspIRCd* Me) : Module::Module(Me)
57 this->changed = false;
60 void InsertOrder(irc::string channel, int count)
62 /* This function figures out where in the sorted list to put an item from the hash */
64 for (a = so->begin(); a != so->end(); a++)
66 /* Found an item equal to or less than, we insert our item before it */
67 if (a->first <= count)
69 so->insert(a,std::pair<int,irc::string>(count,channel));
73 /* There are no items in the list yet, insert something at the beginning */
74 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
79 /* Sorts the hash into the sorted list using an insertion sort */
81 for (StatsIter a = sh->begin(); a != sh->end(); a++)
82 InsertOrder(a->first, a->second);
83 this->changed = false;
86 void OnEvent(Event* event)
88 std::stringstream data("");
90 if (event->GetEventID() == "httpd_url")
92 HTTPRequest* http = (HTTPRequest*)event->GetData();
94 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
96 data << "<!DOCTYPE html PUBLIC \
97 \"-//W3C//DTD XHTML 1.1//EN\" \
98 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\
99 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
102 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
103 data << "<title>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</title>";
104 data << "</head><body>";
105 data << "<h1>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</h1>";
107 data << "<div class='totals'>";
108 data << "<h2>Totals</h2>";
110 data << "<tr><td>Users</td><td>" << ServerInstance->clientlist.size() << "</td></tr>";
111 data << "<tr><td>Channels</td><td>" << ServerInstance->chanlist.size() << "</td></tr>";
112 data << "<tr><td>Opers</td><td>" << ServerInstance->all_opers.size() << "</td></tr>";
113 data << "<tr><td>Sockets</td><td>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</td></tr>";
117 data << "<div class='modules'>";
118 data << "<h2>Modules</h2>";
120 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
122 if (ServerInstance->Config->module_names[i] != "")
123 data << "<tr><td>" << ServerInstance->Config->module_names[i] << "</td></tr>";
128 data << "<div class='channels'>";
129 data << "<h2>Channels</h2>";
131 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
133 /* If the list has changed since last time it was displayed, re-sort it
134 * this time only (not every time, as this would be moronic)
140 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
142 chanrec* c = ServerInstance->FindChan(a->second.c_str());
145 data << "<tr><td>" << a->first << "</td><td>" << a->second << "</td>";
146 data << "<td>" << c->GetOppedUsers()->size() << "</td>";
147 data << "<td>" << c->GetHalfoppedUsers()->size() << "</td>";
148 data << "<td>" << c->GetVoicedUsers()->size() << "</td>";
149 data << "<td>" << c->topic << "</td>";
161 data << "<div class='validion'>";
162 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>";
168 /* Send the document back to m_httpd */
169 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html; charset=iso-8859-1\r\n");
170 Request req((char*)&response, (Module*)this, event->GetSource());
173 ServerInstance->Log(DEBUG,"Sent");
178 void OnChannelDelete(chanrec* chan)
180 StatsIter a = sh->find(chan->name);
185 this->changed = true;
188 void OnUserJoin(userrec* user, chanrec* channel)
190 StatsIter a = sh->find(channel->name);
197 irc::string name = channel->name;
198 sh->insert(std::pair<irc::string,int>(name,1));
200 this->changed = true;
203 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
205 StatsIter a = sh->find(channel->name);
210 this->changed = true;
213 void OnUserQuit(userrec* user, const std::string &message)
215 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
217 if (((ucrec*)(*v))->channel)
219 chanrec* c = ((ucrec*)(*v))->channel;
220 StatsIter a = sh->find(c->name);
227 this->changed = true;
230 char* OnRequest(Request* request)
235 void Implements(char* List)
237 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
240 virtual ~ModuleHttpStats()
246 virtual Version GetVersion()
248 return Version(1, 0, 0, 0, VF_VENDOR, API_VERSION);
253 class ModuleHttpStatsFactory : public ModuleFactory
256 ModuleHttpStatsFactory()
260 ~ModuleHttpStatsFactory()
264 virtual Module * CreateModule(InspIRCd* Me)
266 return new ModuleHttpStats(Me);
271 extern "C" void * init_module( void )
273 return new ModuleHttpStatsFactory;