2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "commands/cmd_whowas.h"
26 WhoWasMaintainTimer * timer;
28 CommandWhowas::CommandWhowas( Module* parent) : Command(parent, "WHOWAS", 1)
30 syntax = "<nick>{,<nick>}";
32 timer = new WhoWasMaintainTimer(3600);
33 ServerInstance->Timers->AddTimer(timer);
36 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
38 /* if whowas disabled in config */
39 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
41 user->WriteNumeric(421, "%s %s :This command has been disabled.",user->nick.c_str(),name.c_str());
45 whowas_users::iterator i = whowas.find(assign(parameters[0]));
47 if (i == whowas.end())
49 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
50 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
55 whowas_set* grp = i->second;
58 for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
62 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),parameters[0].c_str(),
63 u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
65 if (user->HasPrivPermission("users/auspex"))
66 user->WriteNumeric(379, "%s %s :was connecting from *@%s",
67 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
69 std::string signon = ServerInstance->TimeString(u->signon);
70 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
71 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), ServerInstance->Config->HideWhoisServer.c_str(), signon.c_str());
73 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), u->server.c_str(), signon.c_str());
78 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
79 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
84 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
88 std::string CommandWhowas::GetStats()
92 whowas_users_fifo::iterator iter;
93 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
95 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
98 whowas_size += n->size();
99 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
102 return "Whowas entries: " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)";
105 void CommandWhowas::AddToWhoWas(User* user)
107 /* if whowas disabled */
108 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
113 whowas_users::iterator iter = whowas.find(irc::string(user->nick.c_str()));
115 if (iter == whowas.end())
117 whowas_set* n = new whowas_set;
118 WhoWasGroup *a = new WhoWasGroup(user);
120 whowas[user->nick.c_str()] = n;
121 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick.c_str()));
123 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
125 whowas_users::iterator iter2 = whowas.find(whowas_fifo[0].second);
126 if (iter2 != whowas.end())
128 whowas_set* n2 = (whowas_set*)iter2->second;
132 while (n2->begin() != n2->end())
134 WhoWasGroup *a2 = *(n2->begin());
143 whowas_fifo.pop_front();
148 whowas_set* group = (whowas_set*)iter->second;
149 WhoWasGroup *a = new WhoWasGroup(user);
152 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
154 WhoWasGroup *a2 = (WhoWasGroup*)*(group->begin());
161 /* on rehash, refactor maps according to new conf values */
162 void CommandWhowas::PruneWhoWas(time_t t)
165 int groupsize = ServerInstance->Config->WhoWasGroupSize;
166 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
167 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
169 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
170 whowas_users::iterator iter;
172 while ((fifosize = (int)whowas_fifo.size()) > 0)
174 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
176 iter = whowas.find(whowas_fifo[0].second);
178 /* hopefully redundant integrity check, but added while debugging r6216 */
179 if (iter == whowas.end())
181 /* this should never happen, if it does maps are corrupt */
182 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (1)");
186 whowas_set* n = (whowas_set*)iter->second;
190 while (n->begin() != n->end())
192 WhoWasGroup *a = *(n->begin());
200 whowas_fifo.pop_front();
206 /* Then cut the whowas sets to new size (groupsize) */
207 fifosize = (int)whowas_fifo.size();
208 for (int i = 0; i < fifosize; i++)
210 iter = whowas.find(whowas_fifo[0].second);
211 /* hopefully redundant integrity check, but added while debugging r6216 */
212 if (iter == whowas.end())
214 /* this should never happen, if it does maps are corrupt */
215 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (2)");
218 whowas_set* n = (whowas_set*)iter->second;
221 int nickcount = n->size();
222 while (n->begin() != n->end() && nickcount > groupsize)
224 WhoWasGroup *a = *(n->begin());
233 /* call maintain once an hour to remove expired nicks */
234 void CommandWhowas::MaintainWhoWas(time_t t)
236 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
238 whowas_set* n = (whowas_set*)iter->second;
241 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
243 WhoWasGroup *a = *(n->begin());
245 n->erase(n->begin());
251 CommandWhowas::~CommandWhowas()
255 ServerInstance->Timers->DelTimer(timer);
258 whowas_users::iterator iter;
260 while ((fifosize = (int)whowas_fifo.size()) > 0)
262 iter = whowas.find(whowas_fifo[0].second);
264 /* hopefully redundant integrity check, but added while debugging r6216 */
265 if (iter == whowas.end())
267 /* this should never happen, if it does maps are corrupt */
268 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (3)");
272 whowas_set* n = (whowas_set*)iter->second;
276 while (n->begin() != n->end())
278 WhoWasGroup *a = *(n->begin());
286 whowas_fifo.pop_front();
290 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
291 server(user->server), gecos(user->fullname), signon(user->signon)
295 WhoWasGroup::~WhoWasGroup()
299 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
300 void WhoWasMaintainTimer::Tick(time_t)
302 Module* whowas = ServerInstance->Modules->Find("cmd_whowas.so");
305 WhowasRequest(whowas, whowas, WhowasRequest::WHOWAS_MAINTAIN).Send();
309 class ModuleWhoWas : public Module
313 ModuleWhoWas() : cmd(this)
319 ServerInstance->Modules->AddService(cmd);
322 void OnRequest(Request& request)
324 WhowasRequest& req = static_cast<WhowasRequest&>(request);
327 case WhowasRequest::WHOWAS_ADD:
328 cmd.AddToWhoWas(req.user);
330 case WhowasRequest::WHOWAS_STATS:
331 req.value = cmd.GetStats();
333 case WhowasRequest::WHOWAS_PRUNE:
334 cmd.PruneWhoWas(ServerInstance->Time());
336 case WhowasRequest::WHOWAS_MAINTAIN:
337 cmd.MaintainWhoWas(ServerInstance->Time());
344 return Version("WHOWAS Command", VF_VENDOR);
348 MODULE_INIT(ModuleWhoWas)