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++)
61 time_t rawtime = u->signon;
65 timeinfo = localtime(&rawtime);
67 strncpy(b,asctime(timeinfo),24);
70 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),parameters[0].c_str(),
71 u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
73 if (user->HasPrivPermission("users/auspex"))
74 user->WriteNumeric(379, "%s %s :was connecting from *@%s",
75 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
77 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
78 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), ServerInstance->Config->HideWhoisServer.c_str(), b);
80 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), u->server.c_str(), b);
85 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
86 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
91 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
95 std::string CommandWhowas::GetStats()
99 whowas_users_fifo::iterator iter;
100 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
102 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
105 whowas_size += n->size();
106 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
109 return "Whowas entries: " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)";
112 void CommandWhowas::AddToWhoWas(User* user)
114 /* if whowas disabled */
115 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
120 whowas_users::iterator iter = whowas.find(irc::string(user->nick.c_str()));
122 if (iter == whowas.end())
124 whowas_set* n = new whowas_set;
125 WhoWasGroup *a = new WhoWasGroup(user);
127 whowas[user->nick.c_str()] = n;
128 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick.c_str()));
130 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
132 whowas_users::iterator iter2 = whowas.find(whowas_fifo[0].second);
133 if (iter2 != whowas.end())
135 whowas_set* n2 = (whowas_set*)iter2->second;
139 while (n2->begin() != n2->end())
141 WhoWasGroup *a2 = *(n2->begin());
150 whowas_fifo.pop_front();
155 whowas_set* group = (whowas_set*)iter->second;
156 WhoWasGroup *a = new WhoWasGroup(user);
159 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
161 WhoWasGroup *a2 = (WhoWasGroup*)*(group->begin());
168 /* on rehash, refactor maps according to new conf values */
169 void CommandWhowas::PruneWhoWas(time_t t)
172 int groupsize = ServerInstance->Config->WhoWasGroupSize;
173 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
174 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
176 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
177 whowas_users::iterator iter;
179 while ((fifosize = (int)whowas_fifo.size()) > 0)
181 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
183 iter = whowas.find(whowas_fifo[0].second);
185 /* hopefully redundant integrity check, but added while debugging r6216 */
186 if (iter == whowas.end())
188 /* this should never happen, if it does maps are corrupt */
189 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (1)");
193 whowas_set* n = (whowas_set*)iter->second;
197 while (n->begin() != n->end())
199 WhoWasGroup *a = *(n->begin());
207 whowas_fifo.pop_front();
213 /* Then cut the whowas sets to new size (groupsize) */
214 fifosize = (int)whowas_fifo.size();
215 for (int i = 0; i < fifosize; i++)
217 iter = whowas.find(whowas_fifo[0].second);
218 /* hopefully redundant integrity check, but added while debugging r6216 */
219 if (iter == whowas.end())
221 /* this should never happen, if it does maps are corrupt */
222 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (2)");
225 whowas_set* n = (whowas_set*)iter->second;
228 int nickcount = n->size();
229 while (n->begin() != n->end() && nickcount > groupsize)
231 WhoWasGroup *a = *(n->begin());
240 /* call maintain once an hour to remove expired nicks */
241 void CommandWhowas::MaintainWhoWas(time_t t)
243 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
245 whowas_set* n = (whowas_set*)iter->second;
248 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
250 WhoWasGroup *a = *(n->begin());
252 n->erase(n->begin());
258 CommandWhowas::~CommandWhowas()
262 ServerInstance->Timers->DelTimer(timer);
265 whowas_users::iterator iter;
267 while ((fifosize = (int)whowas_fifo.size()) > 0)
269 iter = whowas.find(whowas_fifo[0].second);
271 /* hopefully redundant integrity check, but added while debugging r6216 */
272 if (iter == whowas.end())
274 /* this should never happen, if it does maps are corrupt */
275 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (3)");
279 whowas_set* n = (whowas_set*)iter->second;
283 while (n->begin() != n->end())
285 WhoWasGroup *a = *(n->begin());
293 whowas_fifo.pop_front();
297 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
298 server(user->server), gecos(user->fullname), signon(user->signon)
302 WhoWasGroup::~WhoWasGroup()
306 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
307 void WhoWasMaintainTimer::Tick(time_t)
309 Module* whowas = ServerInstance->Modules->Find("cmd_whowas.so");
312 WhowasRequest(whowas, whowas, WhowasRequest::WHOWAS_MAINTAIN).Send();
316 class ModuleWhoWas : public Module
320 ModuleWhoWas() : cmd(this)
326 ServerInstance->Modules->AddService(cmd);
329 void OnRequest(Request& request)
331 WhowasRequest& req = static_cast<WhowasRequest&>(request);
334 case WhowasRequest::WHOWAS_ADD:
335 cmd.AddToWhoWas(req.user);
337 case WhowasRequest::WHOWAS_STATS:
338 req.value = cmd.GetStats();
340 case WhowasRequest::WHOWAS_PRUNE:
341 cmd.PruneWhoWas(ServerInstance->Time());
343 case WhowasRequest::WHOWAS_MAINTAIN:
344 cmd.MaintainWhoWas(ServerInstance->Time());
351 return Version("WHOWAS Command", VF_VENDOR);
355 MODULE_INIT(ModuleWhoWas)