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::iterator i = whowas.find(assign(parameters[0]));
45 if (i == whowas.end())
47 user->WriteNumeric(ERR_WASNOSUCHNICK, "%s :There was no such nickname", parameters[0].c_str());
51 WhoWas::Nick* nick = i->second;
52 whowas_set* grp = &nick->entries;
55 for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
59 user->WriteNumeric(RPL_WHOWASUSER, "%s %s %s * :%s", parameters[0].c_str(),
60 u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
62 if (user->HasPrivPermission("users/auspex"))
63 user->WriteNumeric(RPL_WHOWASIP, "%s :was connecting from *@%s",
64 parameters[0].c_str(), u->host.c_str());
66 std::string signon = InspIRCd::TimeString(u->signon);
67 bool hide_server = (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"));
68 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());
73 user->WriteNumeric(ERR_WASNOSUCHNICK, "%s :There was no such nickname", parameters[0].c_str());
77 user->WriteNumeric(RPL_ENDOFWHOWAS, "%s :End of WHOWAS", parameters[0].c_str());
81 std::string CommandWhowas::GetStats()
84 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
86 whowas_set* n = &i->second->entries;
87 whowas_size += n->size();
89 return "Whowas entries: " + ConvToStr(whowas_size);
92 void CommandWhowas::AddToWhoWas(User* user)
94 /* if whowas disabled */
95 if (this->GroupSize == 0 || this->MaxGroups == 0)
100 // Insert nick if it doesn't exist
101 // 'first' will point to the newly inserted element or to the existing element with an equivalent key
102 std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(irc::string(user->nick.c_str()), static_cast<WhoWas::Nick*>(NULL)));
104 if (ret.second) // If inserted
106 // This nick is new, create a list for it and add the first record to it
107 WhoWas::Nick* nick = new WhoWas::Nick(ret.first->first);
108 nick->entries.push_back(new WhoWasGroup(user));
109 ret.first->second = nick;
111 // Add this nick to the fifo too
112 whowas_fifo.push_back(nick);
114 if (whowas.size() > this->MaxGroups)
116 // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
117 nick = whowas_fifo.front();
118 whowas_fifo.pop_front();
119 whowas.erase(nick->nick);
125 // We've met this nick before, add a new record to the list
126 whowas_set* set = &ret.first->second->entries;
127 set->push_back(new WhoWasGroup(user));
129 // If there are too many records for this nick, remove the oldest (front)
130 if (set->size() > this->GroupSize)
138 /* on rehash, refactor maps according to new conf values */
139 void CommandWhowas::Prune()
141 time_t min = ServerInstance->Time() - this->MaxKeep;
143 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
144 while (!whowas_fifo.empty())
146 WhoWas::Nick* nick = whowas_fifo.front();
147 if ((whowas_fifo.size() > this->MaxGroups) || (nick->addtime < min))
149 /* hopefully redundant integrity check, but added while debugging r6216 */
150 if (!whowas.erase(nick->nick))
152 /* this should never happen, if it does maps are corrupt */
153 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
157 whowas_fifo.pop_front();
164 /* Then cut the whowas sets to new size (groupsize) */
165 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
167 whowas_set* n = &i->second->entries;
168 while (n->size() > this->GroupSize)
176 /* call maintain once an hour to remove expired nicks */
177 void CommandWhowas::Maintain()
179 time_t min = ServerInstance->Time() - this->MaxKeep;
180 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
182 whowas_set* set = &i->second->entries;
183 while (!set->empty() && set->front()->signon < min)
191 CommandWhowas::~CommandWhowas()
193 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
195 WhoWas::Nick* nick = i->second;
200 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
201 server(user->server->GetName()), gecos(user->fullname), signon(user->signon)
205 WhoWas::Nick::Nick(const irc::string& nickname)
206 : addtime(ServerInstance->Time())
211 WhoWas::Nick::~Nick()
213 stdalgo::delete_all(entries);
216 class ModuleWhoWas : public Module
221 ModuleWhoWas() : cmd(this)
225 void OnGarbageCollect()
227 // Remove all entries older than MaxKeep
231 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
233 cmd.AddToWhoWas(user);
236 ModResult OnStats(char symbol, User* user, string_list &results)
239 results.push_back("249 "+user->nick+" :"+cmd.GetStats());
241 return MOD_RES_PASSTHRU;
244 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
246 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
247 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
248 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
249 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
251 if ((NewGroupSize == cmd.GroupSize) && (NewMaxGroups == cmd.MaxGroups) && (NewMaxKeep == cmd.MaxKeep))
254 cmd.GroupSize = NewGroupSize;
255 cmd.MaxGroups = NewMaxGroups;
256 cmd.MaxKeep = NewMaxKeep;
262 return Version("WHOWAS", VF_VENDOR);
266 MODULE_INIT(ModuleWhoWas)