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 CommandWhowas::CommandWhowas( Module* parent)
27 : Command(parent, "WHOWAS", 1), WhoWasGroupSize(0), WhoWasMaxGroups(0), WhoWasMaxKeep(0)
29 syntax = "<nick>{,<nick>}";
33 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
35 /* if whowas disabled in config */
36 if (this->WhoWasGroupSize == 0 || this->WhoWasMaxGroups == 0)
38 user->WriteNumeric(421, "%s %s :This command has been disabled.",user->nick.c_str(),name.c_str());
42 whowas_users::iterator i = whowas.find(assign(parameters[0]));
44 if (i == whowas.end())
46 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
47 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
52 whowas_set* grp = i->second;
55 for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
58 time_t rawtime = u->signon;
62 timeinfo = localtime(&rawtime);
64 strncpy(b,asctime(timeinfo),24);
67 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),parameters[0].c_str(),
68 u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
70 if (user->HasPrivPermission("users/auspex"))
71 user->WriteNumeric(379, "%s %s :was connecting from *@%s",
72 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
74 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
75 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), ServerInstance->Config->HideWhoisServer.c_str(), b);
77 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), u->server.c_str(), b);
82 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
83 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
88 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
92 std::string CommandWhowas::GetStats()
96 whowas_users_fifo::iterator iter;
97 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
99 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
102 whowas_size += n->size();
103 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
106 return "Whowas entries: " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)";
109 void CommandWhowas::AddToWhoWas(User* user)
111 /* if whowas disabled */
112 if (this->WhoWasGroupSize == 0 || this->WhoWasMaxGroups == 0)
117 whowas_users::iterator iter = whowas.find(irc::string(user->nick.c_str()));
119 if (iter == whowas.end())
121 whowas_set* n = new whowas_set;
122 WhoWasGroup *a = new WhoWasGroup(user);
124 whowas[user->nick.c_str()] = n;
125 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick.c_str()));
127 if ((int)(whowas.size()) > this->WhoWasMaxGroups)
129 whowas_users::iterator iter2 = whowas.find(whowas_fifo[0].second);
130 if (iter2 != whowas.end())
132 whowas_set* n2 = (whowas_set*)iter2->second;
136 while (n2->begin() != n2->end())
138 WhoWasGroup *a2 = *(n2->begin());
147 whowas_fifo.pop_front();
152 whowas_set* group = (whowas_set*)iter->second;
153 WhoWasGroup *a = new WhoWasGroup(user);
156 if ((int)(group->size()) > this->WhoWasGroupSize)
158 WhoWasGroup *a2 = (WhoWasGroup*)*(group->begin());
165 /* on rehash, refactor maps according to new conf values */
166 void CommandWhowas::PruneWhoWas(time_t t)
169 int groupsize = this->WhoWasGroupSize;
170 int maxgroups = this->WhoWasMaxGroups;
171 int maxkeep = this->WhoWasMaxKeep;
173 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
174 whowas_users::iterator iter;
176 while ((fifosize = (int)whowas_fifo.size()) > 0)
178 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
180 iter = whowas.find(whowas_fifo[0].second);
182 /* hopefully redundant integrity check, but added while debugging r6216 */
183 if (iter == whowas.end())
185 /* this should never happen, if it does maps are corrupt */
186 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
190 whowas_set* n = (whowas_set*)iter->second;
194 while (n->begin() != n->end())
196 WhoWasGroup *a = *(n->begin());
204 whowas_fifo.pop_front();
210 /* Then cut the whowas sets to new size (groupsize) */
211 fifosize = (int)whowas_fifo.size();
212 for (int i = 0; i < fifosize; i++)
214 iter = whowas.find(whowas_fifo[0].second);
215 /* hopefully redundant integrity check, but added while debugging r6216 */
216 if (iter == whowas.end())
218 /* this should never happen, if it does maps are corrupt */
219 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (2)");
222 whowas_set* n = (whowas_set*)iter->second;
225 int nickcount = n->size();
226 while (n->begin() != n->end() && nickcount > groupsize)
228 WhoWasGroup *a = *(n->begin());
237 /* call maintain once an hour to remove expired nicks */
238 void CommandWhowas::MaintainWhoWas(time_t t)
240 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
242 whowas_set* n = (whowas_set*)iter->second;
245 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - this->WhoWasMaxKeep))
247 WhoWasGroup *a = *(n->begin());
249 n->erase(n->begin());
255 CommandWhowas::~CommandWhowas()
257 whowas_users::iterator iter;
259 while ((fifosize = (int)whowas_fifo.size()) > 0)
261 iter = whowas.find(whowas_fifo[0].second);
263 /* hopefully redundant integrity check, but added while debugging r6216 */
264 if (iter == whowas.end())
266 /* this should never happen, if it does maps are corrupt */
267 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (3)");
271 whowas_set* n = (whowas_set*)iter->second;
275 while (n->begin() != n->end())
277 WhoWasGroup *a = *(n->begin());
285 whowas_fifo.pop_front();
289 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
290 server(user->server), gecos(user->fullname), signon(user->signon)
294 WhoWasGroup::~WhoWasGroup()
298 class ModuleWhoWas : public Module
303 ModuleWhoWas() : cmd(this)
309 ServerInstance->Modules->AddService(cmd);
313 void OnGarbageCollect()
315 /* Removes all entries older than WhoWasMaxKeep */
316 cmd.MaintainWhoWas(ServerInstance->Time());
319 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
321 cmd.AddToWhoWas(user);
324 ModResult OnStats(char symbol, User* user, string_list &results)
327 results.push_back(ServerInstance->Config->ServerName+" 249 "+user->nick+" :"+cmd.GetStats());
329 return MOD_RES_PASSTHRU;
332 void OnRehash(User* user)
334 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
335 int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
336 int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
337 int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
339 if ((NewGroupSize == cmd.WhoWasGroupSize) && (NewMaxGroups == cmd.WhoWasMaxGroups) && (NewMaxKeep == cmd.WhoWasMaxKeep))
342 cmd.WhoWasGroupSize = NewGroupSize;
343 cmd.WhoWasMaxGroups = NewMaxGroups;
344 cmd.WhoWasMaxKeep = NewMaxKeep;
345 cmd.PruneWhoWas(ServerInstance->Time());
350 return Version("WHOWAS", VF_VENDOR);
354 MODULE_INIT(ModuleWhoWas)