1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "configreader.h"
19 #include "inspsocket.h"
23 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
25 typedef std::map<irc::string,int> StatsHash;
26 typedef StatsHash::iterator StatsIter;
28 typedef std::vector<std::pair<int,irc::string> > SortedList;
29 typedef SortedList::iterator SortedIter;
31 static StatsHash* sh = new StatsHash();
32 static SortedList* so = new SortedList();
34 class ModuleHttpStats : public Module
37 std::string stylesheet;
44 ConfigReader c(ServerInstance);
45 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
48 ModuleHttpStats(InspIRCd* Me) : Module::Module(Me)
52 this->changed = false;
55 void InsertOrder(irc::string channel, int count)
57 /* This function figures out where in the sorted list to put an item from the hash */
59 for (a = so->begin(); a != so->end(); a++)
61 /* Found an item equal to or less than, we insert our item before it */
62 if (a->first <= count)
64 so->insert(a,std::pair<int,irc::string>(count,channel));
68 /* There are no items in the list yet, insert something at the beginning */
69 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
74 /* Sorts the hash into the sorted list using an insertion sort */
76 for (StatsIter a = sh->begin(); a != sh->end(); a++)
77 InsertOrder(a->first, a->second);
78 this->changed = false;
81 void OnEvent(Event* event)
83 std::stringstream data("");
85 if (event->GetEventID() == "httpd_url")
87 HTTPRequest* http = (HTTPRequest*)event->GetData();
89 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
91 data << "<!DOCTYPE html PUBLIC \
92 \"-//W3C//DTD XHTML 1.1//EN\" \
93 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\
94 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
97 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
98 data << "<title>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</title>";
99 data << "</head><body>";
100 data << "<h1>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</h1>";
102 data << "<div class='totals'>";
103 data << "<h2>Totals</h2>";
105 data << "<tr><td>Users</td><td>" << ServerInstance->clientlist->size() << "</td></tr>";
106 data << "<tr><td>Channels</td><td>" << ServerInstance->chanlist->size() << "</td></tr>";
107 data << "<tr><td>Opers</td><td>" << ServerInstance->all_opers.size() << "</td></tr>";
108 data << "<tr><td>Sockets</td><td>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</td></tr>";
112 data << "<div class='modules'>";
113 data << "<h2>Modules</h2>";
115 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
117 if (ServerInstance->Config->module_names[i] != "")
118 data << "<tr><td>" << ServerInstance->Config->module_names[i] << "</td></tr>";
123 data << "<div class='channels'>";
124 data << "<h2>Channels</h2>";
126 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
128 /* If the list has changed since last time it was displayed, re-sort it
129 * this time only (not every time, as this would be moronic)
135 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
137 chanrec* c = ServerInstance->FindChan(a->second.c_str());
140 data << "<tr><td>" << a->first << "</td><td>" << a->second << "</td>";
141 data << "<td>" << c->GetOppedUsers()->size() << "</td>";
142 data << "<td>" << c->GetHalfoppedUsers()->size() << "</td>";
143 data << "<td>" << c->GetVoicedUsers()->size() << "</td>";
144 data << "<td>" << c->topic << "</td>";
156 data << "<div class='validion'>";
157 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>";
163 /* Send the document back to m_httpd */
164 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html; charset=iso-8859-1\r\n");
165 Request req((char*)&response, (Module*)this, event->GetSource());
171 void OnChannelDelete(chanrec* chan)
173 StatsIter a = sh->find(chan->name);
178 this->changed = true;
181 void OnUserJoin(userrec* user, chanrec* channel)
183 StatsIter a = sh->find(channel->name);
190 irc::string name = channel->name;
191 sh->insert(std::pair<irc::string,int>(name,1));
193 this->changed = true;
196 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
198 StatsIter a = sh->find(channel->name);
203 this->changed = true;
206 void OnUserQuit(userrec* user, const std::string &message)
208 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
210 chanrec* c = v->first;
211 StatsIter a = sh->find(c->name);
217 this->changed = true;
220 char* OnRequest(Request* request)
225 void Implements(char* List)
227 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
230 virtual ~ModuleHttpStats()
236 virtual Version GetVersion()
238 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
243 class ModuleHttpStatsFactory : public ModuleFactory
246 ModuleHttpStatsFactory()
250 ~ModuleHttpStatsFactory()
254 virtual Module * CreateModule(InspIRCd* Me)
256 return new ModuleHttpStats(Me);
261 extern "C" void * init_module( void )
263 return new ModuleHttpStatsFactory;