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(421, "%s %s :This command has been disabled.",user->nick.c_str(),name.c_str());
43 whowas_users::iterator i = whowas.find(assign(parameters[0]));
45 if (i == whowas.end())
47 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
51 whowas_set* grp = i->second;
54 for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
58 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),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(379, "%s %s :was connecting from *@%s",
63 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
65 std::string signon = ServerInstance->TimeString(u->signon);
66 bool hide_server = (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"));
67 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(), parameters[0].c_str(), (hide_server ? ServerInstance->Config->HideWhoisServer.c_str() : u->server.c_str()), signon.c_str());
72 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
76 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
80 std::string CommandWhowas::GetStats()
84 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
86 whowas_set* n = i->second;
87 whowas_size += n->size();
88 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
90 return "Whowas entries: " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)";
93 void CommandWhowas::AddToWhoWas(User* user)
95 /* if whowas disabled */
96 if (this->GroupSize == 0 || this->MaxGroups == 0)
101 // Insert nick if it doesn't exist
102 // 'first' will point to the newly inserted element or to the existing element with an equivalent key
103 std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(irc::string(user->nick.c_str()), static_cast<whowas_set*>(NULL)));
105 if (ret.second) // If inserted
107 // This nick is new, create a list for it and add the first record to it
108 whowas_set* n = new whowas_set;
109 n->push_back(new WhoWasGroup(user));
110 ret.first->second = n;
112 // Add this nick to the fifo too
113 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(), ret.first->first));
115 if (whowas.size() > this->MaxGroups)
117 // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
118 whowas_users::iterator it = whowas.find(whowas_fifo.front().second);
119 if (it != whowas.end())
121 whowas_set* set = it->second;
122 for (whowas_set::iterator i = set->begin(); i != set->end(); ++i)
128 whowas_fifo.pop_front();
133 // We've met this nick before, add a new record to the list
134 whowas_set* set = ret.first->second;
135 set->push_back(new WhoWasGroup(user));
137 // If there are too many records for this nick, remove the oldest (front)
138 if (set->size() > this->GroupSize)
146 /* on rehash, refactor maps according to new conf values */
147 void CommandWhowas::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 if ((whowas_fifo.size() > this->MaxGroups) || (whowas_fifo.front().first < min))
156 whowas_users::iterator iter = whowas.find(whowas_fifo.front().second);
158 /* hopefully redundant integrity check, but added while debugging r6216 */
159 if (iter == whowas.end())
161 /* this should never happen, if it does maps are corrupt */
162 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
166 whowas_set* set = iter->second;
167 for (whowas_set::iterator i = set->begin(); i != set->end(); ++i)
172 whowas_fifo.pop_front();
178 /* Then cut the whowas sets to new size (groupsize) */
179 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
181 whowas_set* n = i->second;
182 while (n->size() > this->GroupSize)
190 /* call maintain once an hour to remove expired nicks */
191 void CommandWhowas::Maintain()
193 time_t min = ServerInstance->Time() - this->MaxKeep;
194 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
196 whowas_set* set = i->second;
197 while (!set->empty() && set->front()->signon < min)
205 CommandWhowas::~CommandWhowas()
207 for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
209 whowas_set* set = i->second;
210 for (whowas_set::iterator j = set->begin(); j != set->end(); ++j)
217 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
218 server(user->server), gecos(user->fullname), signon(user->signon)
222 class ModuleWhoWas : public Module
227 ModuleWhoWas() : cmd(this)
233 ServerInstance->Modules->AddService(cmd);
236 void OnGarbageCollect()
238 // Remove all entries older than MaxKeep
242 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
244 cmd.AddToWhoWas(user);
247 ModResult OnStats(char symbol, User* user, string_list &results)
250 results.push_back(ServerInstance->Config->ServerName+" 249 "+user->nick+" :"+cmd.GetStats());
252 return MOD_RES_PASSTHRU;
255 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
257 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
258 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
259 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
260 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
262 if ((NewGroupSize == cmd.GroupSize) && (NewMaxGroups == cmd.MaxGroups) && (NewMaxKeep == cmd.MaxKeep))
265 cmd.GroupSize = NewGroupSize;
266 cmd.MaxGroups = NewMaxGroups;
267 cmd.MaxKeep = NewMaxKeep;
273 return Version("WHOWAS", VF_VENDOR);
277 MODULE_INIT(ModuleWhoWas)