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++)
92 log(DEBUG, "InsertOrder on %d %s",a->second,a->first.c_str());
93 InsertOrder(a->first, a->second);
95 this->changed = false;
98 void OnEvent(Event* event)
100 std::stringstream data("");
102 if (event->GetEventID() == "httpd_url")
104 HTTPRequest* http = (HTTPRequest*)event->GetData();
106 if ((http->GetURI() == "/stats") || (http->GetURI() == "/stats/"))
108 log(DEBUG,"HTTP URL!");
110 data << "<HTML><HEAD>";
111 data << "<TITLE>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</TITLE>";
112 data << "</HEAD><BODY>";
113 data << "<H1>InspIRCd server statisitics for " << Srv->GetServerName() << " (" << Srv->GetServerDescription() << ")</H1>";
115 data << "<DIV ID='TOTALS'>";
116 data << "<H2>Totals</H2>";
118 data << "<TR><TD>Users</TD><TD>" << clientlist.size() << "</TD></TR>";
119 data << "<TR><TD>Channels</TD><TD>" << chanlist.size() << "</TD></TR>";
120 data << "<TR><TD>Opers</TD><TD>" << all_opers.size() << "</TD></TR>";
121 data << "<TR><TD>Sockets</TD><TD>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << " (Max: " << ServerInstance->SE->GetMaxFds() << " via socket engine '" << ServerInstance->SE->GetName() << "')</TD></TR>";
125 data << "<DIV ID='MODULES'>";
126 data << "<H2>Modules</H2>";
128 for (int i = 0; i <= MODCOUNT; i++)
130 if (Config->module_names[i] != "")
131 data << "<TR><TD>" << Config->module_names[i] << "</TD></TR>";
136 data << "<DIV ID='CHANNELS'>";
137 data << "<H2>Channels</H2>";
139 data << "<TR><TH>Users</TH><TH>Name</TH><TH>@</TH><TH>%</TH><TH>+</TH></TR>";
141 /* If the list has changed since last time it was displayed, re-sort it
142 * this time only (not every time, as this would be moronic)
148 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
150 chanrec* c = Srv->FindChannel(a->second.c_str());
153 data << "<TR><TD>" << a->first << "</TD><TD>" << a->second << "</TD>";
154 data << "<TD>" << c->GetOppedUsers()->size() << "</TD>";
155 data << "<TD>" << c->GetHalfoppedUsers()->size() << "</TD>";
156 data << "<TD>" << c->GetVoicedUsers()->size() << "</TD>";
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\r\n");
170 Request req((char*)&response, (Module*)this, event->GetSource());
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()
245 virtual Version GetVersion()
247 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
252 class ModuleHttpStatsFactory : public ModuleFactory
255 ModuleHttpStatsFactory()
259 ~ModuleHttpStatsFactory()
263 virtual Module * CreateModule(Server* Me)
265 return new ModuleHttpStats(Me);
270 extern "C" void * init_module( void )
272 return new ModuleHttpStatsFactory;