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)
28 , GroupSize(0), MaxGroups(0), MaxKeep(0)
30 syntax = "<nick>{,<nick>}";
34 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
36 /* if whowas disabled in config */
37 if (this->GroupSize == 0 || this->MaxGroups == 0)
39 user->WriteNumeric(ERR_UNKNOWNCOMMAND, "%s :This command has been disabled.", name.c_str());
43 whowas_users::const_iterator it = whowas.find(parameters[0]);
44 if (it == whowas.end())
46 user->WriteNumeric(ERR_WASNOSUCHNICK, "%s :There was no such nickname", parameters[0].c_str());
50 const WhoWas::Nick& nick = *it->second;
51 const WhoWas::Nick::List& list = nick.entries;
54 for (WhoWas::Nick::List::const_iterator i = list.begin(); i != list.end(); ++i)
58 user->WriteNumeric(RPL_WHOWASUSER, "%s %s %s * :%s", parameters[0].c_str(),
59 u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
61 if (user->HasPrivPermission("users/auspex"))
62 user->WriteNumeric(RPL_WHOWASIP, "%s :was connecting from *@%s",
63 parameters[0].c_str(), u->host.c_str());
65 std::string signon = InspIRCd::TimeString(u->signon);
66 bool hide_server = (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"));
67 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());
72 user->WriteNumeric(ERR_WASNOSUCHNICK, "%s :There was no such nickname", parameters[0].c_str());
76 user->WriteNumeric(RPL_ENDOFWHOWAS, "%s :End of WHOWAS", parameters[0].c_str());
80 std::string CommandWhowas::GetStats()
83 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
85 WhoWas::Nick::List& list = i->second->entries;
86 whowas_size += list.size();
88 return "Whowas entries: " + ConvToStr(whowas_size);
91 void CommandWhowas::AddToWhoWas(User* user)
93 /* if whowas disabled */
94 if (this->GroupSize == 0 || this->MaxGroups == 0)
99 // Insert nick if it doesn't exist
100 // 'first' will point to the newly inserted element or to the existing element with an equivalent key
101 std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(user->nick, static_cast<WhoWas::Nick*>(NULL)));
103 if (ret.second) // If inserted
105 // This nick is new, create a list for it and add the first record to it
106 WhoWas::Nick* nick = new WhoWas::Nick(ret.first->first);
107 nick->entries.push_back(new WhoWasGroup(user));
108 ret.first->second = nick;
110 // Add this nick to the fifo too
111 whowas_fifo.push_back(nick);
113 if (whowas.size() > this->MaxGroups)
115 // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
116 nick = whowas_fifo.front();
117 whowas_fifo.pop_front();
118 whowas.erase(nick->nick);
124 // We've met this nick before, add a new record to the list
125 WhoWas::Nick::List& list = ret.first->second->entries;
126 list.push_back(new WhoWasGroup(user));
128 // If there are too many records for this nick, remove the oldest (front)
129 if (list.size() > this->GroupSize)
137 /* on rehash, refactor maps according to new conf values */
138 void CommandWhowas::Prune()
140 time_t min = ServerInstance->Time() - this->MaxKeep;
142 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
143 while (!whowas_fifo.empty())
145 WhoWas::Nick* nick = whowas_fifo.front();
146 if ((whowas_fifo.size() > this->MaxGroups) || (nick->addtime < min))
148 /* hopefully redundant integrity check, but added while debugging r6216 */
149 if (!whowas.erase(nick->nick))
151 /* this should never happen, if it does maps are corrupt */
152 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
156 whowas_fifo.pop_front();
163 /* Then cut the whowas sets to new size (groupsize) */
164 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
166 WhoWas::Nick::List& list = i->second->entries;
167 while (list.size() > this->GroupSize)
175 /* call maintain once an hour to remove expired nicks */
176 void CommandWhowas::Maintain()
178 time_t min = ServerInstance->Time() - this->MaxKeep;
179 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
181 WhoWas::Nick::List& list = i->second->entries;
182 while (!list.empty() && list.front()->signon < min)
190 CommandWhowas::~CommandWhowas()
192 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
194 WhoWas::Nick* nick = i->second;
199 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
200 server(user->server->GetName()), gecos(user->fullname), signon(user->signon)
204 WhoWas::Nick::Nick(const std::string& nickname)
205 : addtime(ServerInstance->Time())
210 WhoWas::Nick::~Nick()
212 stdalgo::delete_all(entries);
215 class ModuleWhoWas : public Module
220 ModuleWhoWas() : cmd(this)
224 void OnGarbageCollect()
226 // Remove all entries older than MaxKeep
230 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
232 cmd.AddToWhoWas(user);
235 ModResult OnStats(char symbol, User* user, string_list &results)
238 results.push_back("249 "+user->nick+" :"+cmd.GetStats());
240 return MOD_RES_PASSTHRU;
243 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
245 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
246 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
247 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
248 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
250 if ((NewGroupSize == cmd.GroupSize) && (NewMaxGroups == cmd.MaxGroups) && (NewMaxKeep == cmd.MaxKeep))
253 cmd.GroupSize = NewGroupSize;
254 cmd.MaxGroups = NewMaxGroups;
255 cmd.MaxKeep = NewMaxKeep;
261 return Version("WHOWAS", VF_VENDOR);
265 MODULE_INIT(ModuleWhoWas)