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"
22 /* $ModDesc: Provides statistics over HTTP via m_httpd.so */
24 typedef std::map<irc::string,int> StatsHash;
25 typedef StatsHash::iterator StatsIter;
27 typedef std::vector<std::pair<int,irc::string> > SortedList;
28 typedef SortedList::iterator SortedIter;
30 static StatsHash* sh = new StatsHash();
31 static SortedList* so = new SortedList();
33 class ModuleHttpStats : public Module
36 std::string stylesheet;
43 ConfigReader c(ServerInstance);
44 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
47 ModuleHttpStats(InspIRCd* Me) : Module(Me)
51 this->changed = false;
54 void InsertOrder(irc::string channel, int count)
56 /* This function figures out where in the sorted list to put an item from the hash */
58 for (a = so->begin(); a != so->end(); a++)
60 /* Found an item equal to or less than, we insert our item before it */
61 if (a->first <= count)
63 so->insert(a,std::pair<int,irc::string>(count,channel));
67 /* There are no items in the list yet, insert something at the beginning */
68 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
73 /* Sorts the hash into the sorted list using an insertion sort */
75 for (StatsIter a = sh->begin(); a != sh->end(); a++)
76 InsertOrder(a->first, a->second);
77 this->changed = false;
80 void OnEvent(Event* event)
82 std::stringstream data("");
84 if (event->GetEventID() == "httpd_url")
86 HTTPRequest* http = (HTTPRequest*)event->GetData();
88 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
90 data << "<!DOCTYPE html PUBLIC \
91 \"-//W3C//DTD XHTML 1.1//EN\" \
92 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\
93 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
96 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
97 data << "<title>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</title>";
98 data << "</head><body>";
99 data << "<h1>InspIRCd server statisitics for " << ServerInstance->Config->ServerName << " (" << ServerInstance->Config->ServerDesc << ")</h1>";
101 data << "<div class='totals'>";
102 data << "<h2>Totals</h2>";
104 data << "<tr><td>Users</td><td>" << ServerInstance->clientlist->size() << "</td></tr>";
105 data << "<tr><td>Channels</td><td>" << ServerInstance->chanlist->size() << "</td></tr>";
106 data << "<tr><td>Opers</td><td>" << ServerInstance->all_opers.size() << "</td></tr>";
107 data << "<tr><td>Sockets</td><td>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</td></tr>";
111 data << "<div class='modules'>";
112 data << "<h2>Modules</h2>";
114 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
116 if (!ServerInstance->Config->module_names[i].empty())
117 data << "<tr><td>" << ServerInstance->Config->module_names[i] << "</td></tr>";
122 data << "<div class='channels'>";
123 data << "<h2>Channels</h2>";
125 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
127 /* If the list has changed since last time it was displayed, re-sort it
128 * this time only (not every time, as this would be moronic)
134 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
136 chanrec* c = ServerInstance->FindChan(a->second.c_str());
139 data << "<tr><td>" << a->first << "</td><td>" << a->second << "</td>";
140 data << "<td>" << c->GetOppedUsers()->size() << "</td>";
141 data << "<td>" << c->GetHalfoppedUsers()->size() << "</td>";
142 data << "<td>" << c->GetVoicedUsers()->size() << "</td>";
143 data << "<td>" << c->topic << "</td>";
155 data << "<div class='validion'>";
156 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>";
162 /* Send the document back to m_httpd */
163 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html; charset=iso-8859-1\r\n");
164 Request req((char*)&response, (Module*)this, event->GetSource());
170 void OnChannelDelete(chanrec* chan)
172 StatsIter a = sh->find(chan->name);
177 this->changed = true;
180 void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
182 StatsIter a = sh->find(channel->name);
189 irc::string name = channel->name;
190 sh->insert(std::pair<irc::string,int>(name,1));
192 this->changed = true;
195 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
197 StatsIter a = sh->find(channel->name);
202 this->changed = true;
205 void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
207 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
209 chanrec* c = v->first;
210 StatsIter a = sh->find(c->name);
216 this->changed = true;
219 char* OnRequest(Request* request)
224 void Implements(char* List)
226 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
229 virtual ~ModuleHttpStats()
235 virtual Version GetVersion()
237 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
242 class ModuleHttpStatsFactory : public ModuleFactory
245 ModuleHttpStatsFactory()
249 ~ModuleHttpStatsFactory()
253 virtual Module * CreateModule(InspIRCd* Me)
255 return new ModuleHttpStats(Me);
260 extern "C" DllExport void * init_module( void )
262 return new ModuleHttpStatsFactory;