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)
29 syntax = "<nick>{,<nick>}";
33 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
35 /* if whowas disabled in config */
36 if (!manager.IsEnabled())
38 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s :This command has been disabled.", name.c_str());
42 const WhoWas::Nick* const nick = manager.FindNick(parameters[0]);
45 user->WriteNumeric(ERR_WASNOSUCHNICK, "%s :There was no such nickname", parameters[0].c_str());
49 const WhoWas::Nick::List& list = nick->entries;
50 for (WhoWas::Nick::List::const_iterator i = list.begin(); i != list.end(); ++i)
52 WhoWas::Entry* u = *i;
54 user->WriteNumeric(RPL_WHOWASUSER, "%s %s %s * :%s", parameters[0].c_str(), u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
56 if (user->HasPrivPermission("users/auspex"))
57 user->WriteNumeric(RPL_WHOWASIP, "%s :was connecting from *@%s", parameters[0].c_str(), u->host.c_str());
59 std::string signon = InspIRCd::TimeString(u->signon);
60 bool hide_server = (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"));
61 user->WriteNumeric(RPL_WHOISSERVER, "%s %s :%s", parameters[0].c_str(), (hide_server ? ServerInstance->Config->HideWhoisServer.c_str() : u->server.c_str()), signon.c_str());
65 user->WriteNumeric(RPL_ENDOFWHOWAS, "%s :End of WHOWAS", parameters[0].c_str());
69 WhoWas::Manager::Manager()
70 : GroupSize(0), MaxGroups(0), MaxKeep(0)
74 const WhoWas::Nick* WhoWas::Manager::FindNick(const std::string& nickname) const
76 whowas_users::const_iterator it = whowas.find(nickname);
77 if (it == whowas.end())
80 const Nick* nick = it->second;
81 if (nick->entries.empty())
86 WhoWas::Manager::Stats WhoWas::Manager::GetStats() const
88 size_t entrycount = 0;
89 for (whowas_users::const_iterator i = whowas.begin(); i != whowas.end(); ++i)
91 WhoWas::Nick::List& list = i->second->entries;
92 entrycount += list.size();
96 stats.entrycount = entrycount;
100 void WhoWas::Manager::Add(User* user)
105 // Insert nick if it doesn't exist
106 // 'first' will point to the newly inserted element or to the existing element with an equivalent key
107 std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(user->nick, static_cast<WhoWas::Nick*>(NULL)));
109 if (ret.second) // If inserted
111 // This nick is new, create a list for it and add the first record to it
112 WhoWas::Nick* nick = new WhoWas::Nick(ret.first->first);
113 nick->entries.push_back(new Entry(user));
114 ret.first->second = nick;
116 // Add this nick to the fifo too
117 whowas_fifo.push_back(nick);
119 if (whowas.size() > this->MaxGroups)
121 // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
122 PurgeNick(whowas_fifo.front());
127 // We've met this nick before, add a new record to the list
128 WhoWas::Nick::List& list = ret.first->second->entries;
129 list.push_back(new Entry(user));
131 // If there are too many records for this nick, remove the oldest (front)
132 if (list.size() > this->GroupSize)
140 /* on rehash, refactor maps according to new conf values */
141 void WhoWas::Manager::Prune()
143 time_t min = ServerInstance->Time() - this->MaxKeep;
145 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
146 while (!whowas_fifo.empty())
148 WhoWas::Nick* nick = whowas_fifo.front();
149 if ((whowas_fifo.size() > this->MaxGroups) || (nick->addtime < min))
155 /* Then cut the whowas sets to new size (groupsize) */
156 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
158 WhoWas::Nick::List& list = i->second->entries;
159 while (list.size() > this->GroupSize)
167 /* call maintain once an hour to remove expired nicks */
168 void WhoWas::Manager::Maintain()
170 time_t min = ServerInstance->Time() - this->MaxKeep;
171 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
173 WhoWas::Nick::List& list = i->second->entries;
174 while (!list.empty() && list.front()->signon < min)
182 WhoWas::Manager::~Manager()
184 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
186 WhoWas::Nick* nick = i->second;
191 bool WhoWas::Manager::IsEnabled() const
193 return ((GroupSize != 0) && (MaxGroups != 0));
196 void WhoWas::Manager::UpdateConfig(unsigned int NewGroupSize, unsigned int NewMaxGroups, unsigned int NewMaxKeep)
198 if ((NewGroupSize == GroupSize) && (NewMaxGroups == MaxGroups) && (NewMaxKeep == MaxKeep))
201 GroupSize = NewGroupSize;
202 MaxGroups = NewMaxGroups;
203 MaxKeep = NewMaxKeep;
207 void WhoWas::Manager::PurgeNick(whowas_users::iterator it)
209 WhoWas::Nick* nick = it->second;
210 whowas_fifo.erase(nick);
215 void WhoWas::Manager::PurgeNick(WhoWas::Nick* nick)
217 whowas_users::iterator it = whowas.find(nick->nick);
218 if (it == whowas.end())
220 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in whowas database, please report");
226 WhoWas::Entry::Entry(User* user)
230 , server(user->server->GetName())
231 , gecos(user->fullname)
232 , signon(user->signon)
236 WhoWas::Nick::Nick(const std::string& nickname)
237 : addtime(ServerInstance->Time())
242 WhoWas::Nick::~Nick()
244 stdalgo::delete_all(entries);
247 class ModuleWhoWas : public Module
252 ModuleWhoWas() : cmd(this)
256 void OnGarbageCollect()
258 // Remove all entries older than MaxKeep
259 cmd.manager.Maintain();
262 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
264 cmd.manager.Add(user);
267 ModResult OnStats(char symbol, User* user, string_list &results)
270 results.push_back("249 "+user->nick+" :Whowas entries: "+ConvToStr(cmd.manager.GetStats().entrycount));
272 return MOD_RES_PASSTHRU;
275 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
277 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
278 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
279 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
280 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
282 cmd.manager.UpdateConfig(NewGroupSize, NewMaxGroups, NewMaxKeep);
287 return Version("WHOWAS", VF_VENDOR);
291 MODULE_INIT(ModuleWhoWas)