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"
30 RPL_ENDOFWHOWAS = 369,
36 CommandWhowas::CommandWhowas( Module* parent)
37 : Command(parent, "WHOWAS", 1)
39 syntax = "<nick>{,<nick>}";
43 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
45 /* if whowas disabled in config */
46 if (!manager.IsEnabled())
48 user->WriteNumeric(ERR_UNKNOWNCOMMAND, name, "This command has been disabled.");
52 const WhoWas::Nick* const nick = manager.FindNick(parameters[0]);
55 user->WriteNumeric(ERR_WASNOSUCHNICK, parameters[0], "There was no such nickname");
59 const WhoWas::Nick::List& list = nick->entries;
60 for (WhoWas::Nick::List::const_iterator i = list.begin(); i != list.end(); ++i)
62 WhoWas::Entry* u = *i;
64 user->WriteNumeric(RPL_WHOWASUSER, parameters[0], u->ident, u->dhost, '*', u->gecos);
66 if (user->HasPrivPermission("users/auspex"))
67 user->WriteNumeric(RPL_WHOWASIP, parameters[0], InspIRCd::Format("was connecting from *@%s", u->host.c_str()));
69 std::string signon = InspIRCd::TimeString(u->signon);
70 bool hide_server = (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"));
71 user->WriteNumeric(RPL_WHOISSERVER, parameters[0], (hide_server ? ServerInstance->Config->HideWhoisServer : u->server), signon);
75 user->WriteNumeric(RPL_ENDOFWHOWAS, parameters[0], "End of WHOWAS");
79 WhoWas::Manager::Manager()
80 : GroupSize(0), MaxGroups(0), MaxKeep(0)
84 const WhoWas::Nick* WhoWas::Manager::FindNick(const std::string& nickname) const
86 whowas_users::const_iterator it = whowas.find(nickname);
87 if (it == whowas.end())
92 WhoWas::Manager::Stats WhoWas::Manager::GetStats() const
94 size_t entrycount = 0;
95 for (whowas_users::const_iterator i = whowas.begin(); i != whowas.end(); ++i)
97 WhoWas::Nick::List& list = i->second->entries;
98 entrycount += list.size();
102 stats.entrycount = entrycount;
106 void WhoWas::Manager::Add(User* user)
111 // Insert nick if it doesn't exist
112 // 'first' will point to the newly inserted element or to the existing element with an equivalent key
113 std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(user->nick, static_cast<WhoWas::Nick*>(NULL)));
115 if (ret.second) // If inserted
117 // This nick is new, create a list for it and add the first record to it
118 WhoWas::Nick* nick = new WhoWas::Nick(ret.first->first);
119 nick->entries.push_back(new Entry(user));
120 ret.first->second = nick;
122 // Add this nick to the fifo too
123 whowas_fifo.push_back(nick);
125 if (whowas.size() > this->MaxGroups)
127 // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
128 PurgeNick(whowas_fifo.front());
133 // We've met this nick before, add a new record to the list
134 WhoWas::Nick::List& list = ret.first->second->entries;
135 list.push_back(new Entry(user));
137 // If there are too many records for this nick, remove the oldest (front)
138 if (list.size() > this->GroupSize)
146 /* on rehash, refactor maps according to new conf values */
147 void WhoWas::Manager::Prune()
149 time_t min = ServerInstance->Time() - this->MaxKeep;
151 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
152 while (!whowas_fifo.empty())
154 WhoWas::Nick* nick = whowas_fifo.front();
155 if ((whowas_fifo.size() > this->MaxGroups) || (nick->addtime < min))
161 /* Then cut the whowas sets to new size (groupsize) */
162 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); )
164 WhoWas::Nick::List& list = i->second->entries;
165 while (list.size() > this->GroupSize)
178 /* call maintain once an hour to remove expired nicks */
179 void WhoWas::Manager::Maintain()
181 time_t min = ServerInstance->Time() - this->MaxKeep;
182 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); )
184 WhoWas::Nick::List& list = i->second->entries;
185 while (!list.empty() && list.front()->signon < min)
198 WhoWas::Manager::~Manager()
200 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
202 WhoWas::Nick* nick = i->second;
207 bool WhoWas::Manager::IsEnabled() const
209 return ((GroupSize != 0) && (MaxGroups != 0));
212 void WhoWas::Manager::UpdateConfig(unsigned int NewGroupSize, unsigned int NewMaxGroups, unsigned int NewMaxKeep)
214 if ((NewGroupSize == GroupSize) && (NewMaxGroups == MaxGroups) && (NewMaxKeep == MaxKeep))
217 GroupSize = NewGroupSize;
218 MaxGroups = NewMaxGroups;
219 MaxKeep = NewMaxKeep;
223 void WhoWas::Manager::PurgeNick(whowas_users::iterator it)
225 WhoWas::Nick* nick = it->second;
226 whowas_fifo.erase(nick);
231 void WhoWas::Manager::PurgeNick(WhoWas::Nick* nick)
233 whowas_users::iterator it = whowas.find(nick->nick);
234 if (it == whowas.end())
236 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in whowas database, please report");
242 WhoWas::Entry::Entry(User* user)
243 : host(user->GetRealHost())
244 , dhost(user->GetDisplayedHost())
246 , server(user->server->GetName())
247 , gecos(user->fullname)
248 , signon(user->signon)
252 WhoWas::Nick::Nick(const std::string& nickname)
253 : addtime(ServerInstance->Time())
258 WhoWas::Nick::~Nick()
260 stdalgo::delete_all(entries);
263 class ModuleWhoWas : public Module
268 ModuleWhoWas() : cmd(this)
272 void OnGarbageCollect() CXX11_OVERRIDE
274 // Remove all entries older than MaxKeep
275 cmd.manager.Maintain();
278 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
280 cmd.manager.Add(user);
283 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
285 if (stats.GetSymbol() == 'z')
286 stats.AddRow(249, "Whowas entries: "+ConvToStr(cmd.manager.GetStats().entrycount));
288 return MOD_RES_PASSTHRU;
291 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
293 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
294 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
295 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
296 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
298 cmd.manager.UpdateConfig(NewGroupSize, NewMaxGroups, NewMaxKeep);
301 Version GetVersion() CXX11_OVERRIDE
303 return Version("WHOWAS", VF_VENDOR);
307 MODULE_INIT(ModuleWhoWas)