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)
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 << "<inspircdstats>";
92 data << "<server><name>" << ServerInstance->Config->ServerName << "</name><gecos>" << ServerInstance->Config->ServerDesc << "</gecos></server>";
95 data << "<usercount>" << ServerInstance->clientlist->size() << "</usercount>";
96 data << "<channelcount>" << ServerInstance->chanlist->size() << "</channelcount>";
97 data << "<opercount>" << ServerInstance->all_opers.size() << "</opercount>";
98 data << "<socketcount>" << (ServerInstance->SE->GetMaxFds() - ServerInstance->SE->GetRemainingFds()) << "</socketcount><socketmax>" << ServerInstance->SE->GetMaxFds() <<
99 "</socketmax><socketengine>" << ServerInstance->SE->GetName() << "')</socketengine>";
100 data << "</general>";
101 data << "<modulelist>";
102 for (int i = 0; i <= ServerInstance->GetModuleCount(); i++)
104 if (!ServerInstance->Config->module_names[i].empty())
105 data << "<module>" << ServerInstance->Config->module_names[i] << "</module>";
107 data << "</modulelist>";
109 data << "<channellist>";
110 /* If the list has changed since last time it was displayed, re-sort it
111 * this time only (not every time, as this would be moronic)
117 for (SortedIter a = so->begin(); ((a != so->end()) && (n < 25)); a++, n++)
119 chanrec* c = ServerInstance->FindChan(a->second.c_str());
120 if (c && !c->IsModeSet('s') && !c->IsModeSet('p'))
123 data << "<usercount>" << c->GetUsers()->size() << "</usercount><channelname>" << c->name << "</channelname>";
124 data << "<channelops>" << c->GetOppedUsers()->size() << "</channelops>";
125 data << "<channelhalfops>" << c->GetHalfoppedUsers()->size() << "</channelhalfops>";
126 data << "<channelvoices>" << c->GetVoicedUsers()->size() << "</channelvoices>";
127 data << "<channeltopic>" << c->topic << "</channeltopic>";
128 data << "</channel>";
132 data << "</channellist>";
133 data << "</inspircdstats>";
135 /* Send the document back to m_httpd */
136 HTTPDocument response(http->sock, &data, 200, "X-Powered-By: m_http_stats.so\r\nContent-Type: text/xml\r\n");
137 Request req((char*)&response, (Module*)this, event->GetSource());
143 void OnChannelDelete(chanrec* chan)
145 StatsIter a = sh->find(chan->name);
150 this->changed = true;
153 void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
155 StatsIter a = sh->find(channel->name);
162 irc::string name = channel->name;
163 sh->insert(std::pair<irc::string,int>(name,1));
165 this->changed = true;
168 void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
170 StatsIter a = sh->find(channel->name);
175 this->changed = true;
178 void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
180 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
182 chanrec* c = v->first;
183 StatsIter a = sh->find(c->name);
189 this->changed = true;
192 char* OnRequest(Request* request)
197 void Implements(char* List)
199 List[I_OnEvent] = List[I_OnRequest] = List[I_OnChannelDelete] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = 1;
202 virtual ~ModuleHttpStats()
208 virtual Version GetVersion()
210 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
214 MODULE_INIT(ModuleHttpStats)