1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "commands/cmd_whowas.h"
17 WhoWasMaintainTimer * timer;
19 extern "C" DllExport Command* init_command(InspIRCd* Instance)
21 return new CommandWhowas(Instance);
24 CommandWhowas::CommandWhowas(InspIRCd* Instance) : Command(Instance, "WHOWAS", 0, 1, false, 2)
26 syntax = "<nick>{,<nick>}";
27 timer = new WhoWasMaintainTimer(Instance, 3600);
28 Instance->Timers->AddTimer(timer);
31 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
33 /* if whowas disabled in config */
34 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
36 user->WriteNumeric(421, "%s %s :This command has been disabled.",user->nick.c_str(),command.c_str());
40 whowas_users::iterator i = whowas.find(assign(parameters[0]));
42 if (i == whowas.end())
44 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
45 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
50 whowas_set* grp = i->second;
53 for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
56 time_t rawtime = u->signon;
60 timeinfo = localtime(&rawtime);
62 strncpy(b,asctime(timeinfo),24);
65 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),parameters[0].c_str(),
66 u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
68 if (user->HasPrivPermission("users/auspex"))
69 user->WriteNumeric(379, "%s %s :was connecting from *@%s",
70 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
72 if (*ServerInstance->Config->HideWhoisServer && !user->HasPrivPermission("servers/auspex"))
73 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), ServerInstance->Config->HideWhoisServer, b);
75 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), u->server, b);
80 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
81 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
86 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
90 CmdResult CommandWhowas::HandleInternal(const unsigned int id, const std::deque<classbase*> ¶meters)
95 AddToWhoWas(static_cast<User*>(parameters[0]));
99 GetStats(static_cast<Extensible*>(parameters[0]));
103 PruneWhoWas(ServerInstance->Time());
106 case WHOWAS_MAINTAIN:
107 MaintainWhoWas(ServerInstance->Time());
116 void CommandWhowas::GetStats(Extensible* ext)
119 int whowas_bytes = 0;
120 whowas_users_fifo::iterator iter;
121 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
123 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
126 whowas_size += n->size();
127 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
130 stats.assign("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)");
131 ext->Extend("stats", stats.c_str());
134 void CommandWhowas::AddToWhoWas(User* user)
136 /* if whowas disabled */
137 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
142 whowas_users::iterator iter = whowas.find(irc::string(user->nick.c_str()));
144 if (iter == whowas.end())
146 whowas_set* n = new whowas_set;
147 WhoWasGroup *a = new WhoWasGroup(user);
149 whowas[user->nick.c_str()] = n;
150 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick.c_str()));
152 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
154 whowas_users::iterator iter2 = whowas.find(whowas_fifo[0].second);
155 if (iter2 != whowas.end())
157 whowas_set* n2 = (whowas_set*)iter2->second;
161 while (n2->begin() != n2->end())
163 WhoWasGroup *a2 = *(n2->begin());
172 whowas_fifo.pop_front();
177 whowas_set* group = (whowas_set*)iter->second;
178 WhoWasGroup *a = new WhoWasGroup(user);
181 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
183 WhoWasGroup *a2 = (WhoWasGroup*)*(group->begin());
190 /* on rehash, refactor maps according to new conf values */
191 void CommandWhowas::PruneWhoWas(time_t t)
194 int groupsize = ServerInstance->Config->WhoWasGroupSize;
195 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
196 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
198 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
199 whowas_users::iterator iter;
201 while ((fifosize = (int)whowas_fifo.size()) > 0)
203 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
205 iter = whowas.find(whowas_fifo[0].second);
207 /* hopefully redundant integrity check, but added while debugging r6216 */
208 if (iter == whowas.end())
210 /* this should never happen, if it does maps are corrupt */
211 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (1)");
215 whowas_set* n = (whowas_set*)iter->second;
219 while (n->begin() != n->end())
221 WhoWasGroup *a = *(n->begin());
229 whowas_fifo.pop_front();
235 /* Then cut the whowas sets to new size (groupsize) */
236 fifosize = (int)whowas_fifo.size();
237 for (int i = 0; i < fifosize; i++)
239 iter = whowas.find(whowas_fifo[0].second);
240 /* hopefully redundant integrity check, but added while debugging r6216 */
241 if (iter == whowas.end())
243 /* this should never happen, if it does maps are corrupt */
244 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (2)");
247 whowas_set* n = (whowas_set*)iter->second;
250 int nickcount = n->size();
251 while (n->begin() != n->end() && nickcount > groupsize)
253 WhoWasGroup *a = *(n->begin());
262 /* call maintain once an hour to remove expired nicks */
263 void CommandWhowas::MaintainWhoWas(time_t t)
265 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
267 whowas_set* n = (whowas_set*)iter->second;
270 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
272 WhoWasGroup *a = *(n->begin());
274 n->erase(n->begin());
280 CommandWhowas::~CommandWhowas()
284 ServerInstance->Timers->DelTimer(timer);
287 whowas_users::iterator iter;
289 while ((fifosize = (int)whowas_fifo.size()) > 0)
291 iter = whowas.find(whowas_fifo[0].second);
293 /* hopefully redundant integrity check, but added while debugging r6216 */
294 if (iter == whowas.end())
296 /* this should never happen, if it does maps are corrupt */
297 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (3)");
301 whowas_set* n = (whowas_set*)iter->second;
305 while (n->begin() != n->end())
307 WhoWasGroup *a = *(n->begin());
315 whowas_fifo.pop_front();
319 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
320 server(user->server), gecos(user->fullname), signon(user->signon)
324 WhoWasGroup::~WhoWasGroup()
328 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
329 void WhoWasMaintainTimer::Tick(time_t)
331 Command* whowas_command = ServerInstance->Parser->GetHandler("WHOWAS");
334 std::deque<classbase*> params;
335 whowas_command->HandleInternal(WHOWAS_MAINTAIN, params);