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;
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(std::make_pair(ServerInstance->Time(), ret.first->first));
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 whowas_users::iterator it = whowas.find(whowas_fifo.front().second);
118 if (it != whowas.end())
120 WhoWas::Nick* set = it->second;
124 whowas_fifo.pop_front();
129 // We've met this nick before, add a new record to the list
130 whowas_set* set = &ret.first->second->entries;
131 set->push_back(new WhoWasGroup(user));
133 // If there are too many records for this nick, remove the oldest (front)
134 if (set->size() > this->GroupSize)
142 /* on rehash, refactor maps according to new conf values */
143 void CommandWhowas::Prune()
145 time_t min = ServerInstance->Time() - this->MaxKeep;
147 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
148 while (!whowas_fifo.empty())
150 if ((whowas_fifo.size() > this->MaxGroups) || (whowas_fifo.front().first < min))
152 whowas_users::iterator iter = whowas.find(whowas_fifo.front().second);
154 /* hopefully redundant integrity check, but added while debugging r6216 */
155 if (iter == whowas.end())
157 /* this should never happen, if it does maps are corrupt */
158 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
162 WhoWas::Nick* nick = iter->second;
165 whowas_fifo.pop_front();
171 /* Then cut the whowas sets to new size (groupsize) */
172 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
174 whowas_set* n = &i->second->entries;
175 while (n->size() > this->GroupSize)
183 /* call maintain once an hour to remove expired nicks */
184 void CommandWhowas::Maintain()
186 time_t min = ServerInstance->Time() - this->MaxKeep;
187 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
189 whowas_set* set = &i->second->entries;
190 while (!set->empty() && set->front()->signon < min)
198 CommandWhowas::~CommandWhowas()
200 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
202 WhoWas::Nick* nick = i->second;
207 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
208 server(user->server->GetName()), gecos(user->fullname), signon(user->signon)
213 : addtime(ServerInstance->Time())
217 WhoWas::Nick::~Nick()
219 stdalgo::delete_all(entries);
222 class ModuleWhoWas : public Module
227 ModuleWhoWas() : cmd(this)
231 void OnGarbageCollect()
233 // Remove all entries older than MaxKeep
237 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
239 cmd.AddToWhoWas(user);
242 ModResult OnStats(char symbol, User* user, string_list &results)
245 results.push_back("249 "+user->nick+" :"+cmd.GetStats());
247 return MOD_RES_PASSTHRU;
250 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
252 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
253 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
254 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
255 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
257 if ((NewGroupSize == cmd.GroupSize) && (NewMaxGroups == cmd.MaxGroups) && (NewMaxKeep == cmd.MaxKeep))
260 cmd.GroupSize = NewGroupSize;
261 cmd.MaxGroups = NewMaxGroups;
262 cmd.MaxKeep = NewMaxKeep;
268 return Version("WHOWAS", VF_VENDOR);
272 MODULE_INIT(ModuleWhoWas)