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"
25 #include "modules/stats.h"
31 RPL_ENDOFWHOWAS = 369,
37 CommandWhowas::CommandWhowas( Module* parent)
38 : Command(parent, "WHOWAS", 1)
44 CmdResult CommandWhowas::Handle(User* user, const Params& parameters)
46 /* if whowas disabled in config */
47 if (!manager.IsEnabled())
49 user->WriteNumeric(ERR_UNKNOWNCOMMAND, name, "This command has been disabled.");
53 const WhoWas::Nick* const nick = manager.FindNick(parameters[0]);
56 user->WriteNumeric(ERR_WASNOSUCHNICK, parameters[0], "There was no such nickname");
60 const WhoWas::Nick::List& list = nick->entries;
61 for (WhoWas::Nick::List::const_iterator i = list.begin(); i != list.end(); ++i)
63 WhoWas::Entry* u = *i;
65 user->WriteNumeric(RPL_WHOWASUSER, parameters[0], u->ident, u->dhost, '*', u->real);
67 if (user->HasPrivPermission("users/auspex"))
68 user->WriteNumeric(RPL_WHOWASIP, parameters[0], InspIRCd::Format("was connecting from *@%s", u->host.c_str()));
70 std::string signon = InspIRCd::TimeString(u->signon);
71 bool hide_server = (!ServerInstance->Config->HideServer.empty() && !user->HasPrivPermission("servers/auspex"));
72 user->WriteNumeric(RPL_WHOISSERVER, parameters[0], (hide_server ? ServerInstance->Config->HideServer : u->server), signon);
76 user->WriteNumeric(RPL_ENDOFWHOWAS, parameters[0], "End of WHOWAS");
80 WhoWas::Manager::Manager()
81 : GroupSize(0), MaxGroups(0), MaxKeep(0)
85 const WhoWas::Nick* WhoWas::Manager::FindNick(const std::string& nickname) const
87 whowas_users::const_iterator it = whowas.find(nickname);
88 if (it == whowas.end())
93 WhoWas::Manager::Stats WhoWas::Manager::GetStats() const
95 size_t entrycount = 0;
96 for (whowas_users::const_iterator i = whowas.begin(); i != whowas.end(); ++i)
98 WhoWas::Nick::List& list = i->second->entries;
99 entrycount += list.size();
103 stats.entrycount = entrycount;
107 void WhoWas::Manager::Add(User* user)
112 // Insert nick if it doesn't exist
113 // 'first' will point to the newly inserted element or to the existing element with an equivalent key
114 std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(user->nick, static_cast<WhoWas::Nick*>(NULL)));
116 if (ret.second) // If inserted
118 // This nick is new, create a list for it and add the first record to it
119 WhoWas::Nick* nick = new WhoWas::Nick(ret.first->first);
120 nick->entries.push_back(new Entry(user));
121 ret.first->second = nick;
123 // Add this nick to the fifo too
124 whowas_fifo.push_back(nick);
126 if (whowas.size() > this->MaxGroups)
128 // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
129 PurgeNick(whowas_fifo.front());
134 // We've met this nick before, add a new record to the list
135 WhoWas::Nick::List& list = ret.first->second->entries;
136 list.push_back(new Entry(user));
138 // If there are too many records for this nick, remove the oldest (front)
139 if (list.size() > this->GroupSize)
147 /* on rehash, refactor maps according to new conf values */
148 void WhoWas::Manager::Prune()
150 time_t min = ServerInstance->Time() - this->MaxKeep;
152 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
153 while (!whowas_fifo.empty())
155 WhoWas::Nick* nick = whowas_fifo.front();
156 if ((whowas_fifo.size() > this->MaxGroups) || (nick->addtime < min))
162 /* Then cut the whowas sets to new size (groupsize) */
163 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); )
165 WhoWas::Nick::List& list = i->second->entries;
166 while (list.size() > this->GroupSize)
179 /* call maintain once an hour to remove expired nicks */
180 void WhoWas::Manager::Maintain()
182 time_t min = ServerInstance->Time() - this->MaxKeep;
183 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); )
185 WhoWas::Nick::List& list = i->second->entries;
186 while (!list.empty() && list.front()->signon < min)
199 WhoWas::Manager::~Manager()
201 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
203 WhoWas::Nick* nick = i->second;
208 bool WhoWas::Manager::IsEnabled() const
210 return ((GroupSize != 0) && (MaxGroups != 0));
213 void WhoWas::Manager::UpdateConfig(unsigned int NewGroupSize, unsigned int NewMaxGroups, unsigned int NewMaxKeep)
215 if ((NewGroupSize == GroupSize) && (NewMaxGroups == MaxGroups) && (NewMaxKeep == MaxKeep))
218 GroupSize = NewGroupSize;
219 MaxGroups = NewMaxGroups;
220 MaxKeep = NewMaxKeep;
224 void WhoWas::Manager::PurgeNick(whowas_users::iterator it)
226 WhoWas::Nick* nick = it->second;
227 whowas_fifo.erase(nick);
232 void WhoWas::Manager::PurgeNick(WhoWas::Nick* nick)
234 whowas_users::iterator it = whowas.find(nick->nick);
235 if (it == whowas.end())
237 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in whowas database, please report");
243 WhoWas::Entry::Entry(User* user)
244 : host(user->GetRealHost())
245 , dhost(user->GetDisplayedHost())
247 , server(user->server->GetName())
248 , real(user->GetRealName())
249 , signon(user->signon)
253 WhoWas::Nick::Nick(const std::string& nickname)
254 : addtime(ServerInstance->Time())
259 WhoWas::Nick::~Nick()
261 stdalgo::delete_all(entries);
264 class ModuleWhoWas : public Module, public Stats::EventListener
270 : Stats::EventListener(this)
275 void OnGarbageCollect() CXX11_OVERRIDE
277 // Remove all entries older than MaxKeep
278 cmd.manager.Maintain();
281 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
283 cmd.manager.Add(user);
286 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
288 if (stats.GetSymbol() == 'z')
289 stats.AddRow(249, "Whowas entries: "+ConvToStr(cmd.manager.GetStats().entrycount));
291 return MOD_RES_PASSTHRU;
294 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
296 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
297 unsigned int NewGroupSize = tag->getUInt("groupsize", 10, 0, 10000);
298 unsigned int NewMaxGroups = tag->getUInt("maxgroups", 10240, 0, 1000000);
299 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
301 cmd.manager.UpdateConfig(NewGroupSize, NewMaxGroups, NewMaxKeep);
304 Version GetVersion() CXX11_OVERRIDE
306 return Version("Provides the WHOWAS command", VF_VENDOR);
310 MODULE_INIT(ModuleWhoWas)