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 user_hash clientlist;
32 extern chan_hash chanlist;
33 extern std::vector<userrec*> all_opers;
34 extern InspIRCd* ServerInstance;
35 extern ServerConfig* Config;
39 typedef std::map<irc::string,int> StatsHash;
40 typedef StatsHash::iterator StatsIter;
42 typedef std::vector<std::pair<int,irc::string> > SortedList;
43 typedef SortedList::iterator SortedIter;
45 static StatsHash* sh = new StatsHash();
46 static SortedList* so = new SortedList();
48 class ModuleHttpStats : public Module
51 std::string stylesheet;
59 this->stylesheet = c.ReadValue("httpstats", "stylesheet", 0);
62 ModuleHttpStats(Server* Me) : Module::Module(Me)
66 this->changed = false;
69 void InsertOrder(irc::string channel, int count)
71 /* This function figures out where in the sorted list to put an item from the hash */
73 for (a = so->begin(); a != so->end(); a++)
75 /* Found an item equal to or less than, we insert our item before it */
76 if (a->first <= count)
78 so->insert(a,std::pair<int,irc::string>(count,channel));
82 /* There are no items in the list yet, insert something at the beginning */
83 so->insert(so->begin(), std::pair<int,irc::string>(count,channel));
88 /* Sorts the hash into the sorted list using an insertion sort */
90 for (StatsIter a = sh->begin(); a != sh->end(); a++)
91 InsertOrder(a->first, a->second);
92 this->changed = false;
95 void OnEvent(Event* event)
97 std::stringstream data("");
99 if (event->GetEventID() == "httpd_url")
101 HTTPRequest* http = (HTTPRequest*)event->GetData();
103 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
105 data << "<!DOCTYPE html PUBLIC \
106 \"-//W3C//DTD XHTML 1.1//EN\" \
107 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n\
108 <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">";
111 data << "<link rel='stylesheet' href='" << this->stylesheet << "' type='text/css' />";
112 data << "<title>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</title>";
113 data << "</head><body>";
114 data << "<h1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</h1>";
116 data << "<div class='totals'>";
117 data << "<h2>Totals</h2>";
119 data << "<tr><td>Users</td><td>" << clientlist.size() << "</td></tr>";
120 data << "<tr><td>Channels</td><td>" << chanlist.size() << "</td></tr>";
121 data << "<tr><td>Opers</td><td>" << all_opers.size() << "</td></tr>";
122 data << "<tr><td>Sockets</td><td>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</td></tr>";
126 data << "<div class='modules'>";
127 data << "<h2>Modules</h2>";
129 for (int i = 0; i <= MODCOUNT; i++)
131 if (Config->module_names[i] != "")
132 data << "<tr><td>" << Config->module_names[i] << "</td></tr>";
137 data << "<div class='channels'>";
138 data << "<h2>Channels</h2>";
140 data << "<tr><th>Users</th><th>Name</th><th>@</th><th>%</th><th>+</th><th>Topic</th></tr>";
142 /* If the list has changed since last time it was displayed, re-sort it
143 * this time only (not every time, as this would be moronic)
149 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
151 chanrec* c = Srv->FindChannel(a->second.c_str());
154 data << "<tr><td>" << a->first << "</td><td>" << a->second << "</td>";
155 data << "<td>" << c->GetOppedUsers()->size() << "</td>";
156 data << "<td>" << c->GetHalfoppedUsers()->size() << "</td>";
157 data << "<td>" << c->GetVoicedUsers()->size() << "</td>";
158 data << "<td>" << c->topic << "</td>";
170 data << "<div class='validion'>";
171 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>";
177 /* Send the document back to m_httpd */
178 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/html; charset=iso-8859-1\r\n");
179 Request req((char*)&response, (Module*)this, event->GetSource());
187 void OnChannelDelete(chanrec* chan)
189 StatsIter a = sh->find(chan->name);
194 this->changed = true;
197 void OnUserJoin(userrec* user, chanrec* channel)
199 StatsIter a = sh->find(channel->name);
206 irc::string name = channel->name;
207 sh->insert(std::pair<irc::string,int>(name,1));
209 this->changed = true;
212 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage)
214 StatsIter a = sh->find(channel->name);
219 this->changed = true;
222 void OnUserQuit(userrec* user, const std::string &message)
224 for (std::vector<ucrec*>::const_iterator v = user->chans.begin(); v != user->chans.end(); v++)
226 if (((ucrec*)(*v))->channel)
228 chanrec* c = ((ucrec*)(*v))->channel;
229 StatsIter a = sh->find(c->name);
236 this->changed = true;
239 char* OnRequest(Request* request)
244 void Implements(char* List)
246 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
249 virtual ~ModuleHttpStats()
254 virtual Version GetVersion()
256 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
261 class ModuleHttpStatsFactory : public ModuleFactory
264 ModuleHttpStatsFactory()
268 ~ModuleHttpStatsFactory()
272 virtual Module * CreateModule(Server* Me)
274 return new ModuleHttpStats(Me);
279 extern "C" void * init_module( void )
281 return new ModuleHttpStatsFactory;