1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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 char* const* parameters, int, 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,command.c_str());
40 whowas_users::iterator i = whowas.find(parameters[0]);
42 if (i == whowas.end())
44 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick,parameters[0]);
45 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick,parameters[0]);
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 /* XXX - 'b' could be only 25 chars long and then strlcpy() would terminate it for us too? */
63 strlcpy(b,asctime(timeinfo),MAXBUF);
66 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick,parameters[0],u->ident,u->dhost,u->gecos);
69 user->WriteNumeric(379, "%s %s :was connecting from *@%s", user->nick, parameters[0], u->host);
71 if (*ServerInstance->Config->HideWhoisServer && !IS_OPER(user))
72 user->WriteNumeric(312, "%s %s %s :%s",user->nick,parameters[0], ServerInstance->Config->HideWhoisServer, b);
74 user->WriteNumeric(312, "%s %s %s :%s",user->nick,parameters[0], u->server, b);
79 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick,parameters[0]);
80 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick,parameters[0]);
85 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick,parameters[0]);
89 CmdResult CommandWhowas::HandleInternal(const unsigned int id, const std::deque<classbase*> ¶meters)
94 AddToWhoWas((User*)parameters[0]);
98 GetStats((Extensible*)parameters[0]);
102 PruneWhoWas(ServerInstance->Time());
105 case WHOWAS_MAINTAIN:
106 MaintainWhoWas(ServerInstance->Time());
115 void CommandWhowas::GetStats(Extensible* ext)
118 int whowas_bytes = 0;
119 whowas_users_fifo::iterator iter;
120 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
122 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
125 whowas_size += n->size();
126 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
129 stats.assign("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)");
130 ext->Extend("stats", stats.c_str());
133 void CommandWhowas::AddToWhoWas(User* user)
135 /* if whowas disabled */
136 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
141 whowas_users::iterator iter = whowas.find(user->nick);
143 if (iter == whowas.end())
145 whowas_set* n = new whowas_set;
146 WhoWasGroup *a = new WhoWasGroup(user);
148 whowas[user->nick] = n;
149 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick));
151 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
153 whowas_users::iterator iter2 = whowas.find(whowas_fifo[0].second);
154 if (iter2 != whowas.end())
156 whowas_set* n2 = (whowas_set*)iter2->second;
160 while (n2->begin() != n2->end())
162 WhoWasGroup *a2 = *(n2->begin());
171 whowas_fifo.pop_front();
176 whowas_set* group = (whowas_set*)iter->second;
177 WhoWasGroup *a = new WhoWasGroup(user);
180 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
182 WhoWasGroup *a2 = (WhoWasGroup*)*(group->begin());
189 /* on rehash, refactor maps according to new conf values */
190 void CommandWhowas::PruneWhoWas(time_t t)
193 int groupsize = ServerInstance->Config->WhoWasGroupSize;
194 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
195 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
197 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
198 whowas_users::iterator iter;
200 while ((fifosize = (int)whowas_fifo.size()) > 0)
202 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
204 iter = whowas.find(whowas_fifo[0].second);
206 /* hopefully redundant integrity check, but added while debugging r6216 */
207 if (iter == whowas.end())
209 /* this should never happen, if it does maps are corrupt */
210 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (1)");
214 whowas_set* n = (whowas_set*)iter->second;
218 while (n->begin() != n->end())
220 WhoWasGroup *a = *(n->begin());
228 whowas_fifo.pop_front();
234 /* Then cut the whowas sets to new size (groupsize) */
235 fifosize = (int)whowas_fifo.size();
236 for (int i = 0; i < fifosize; i++)
238 iter = whowas.find(whowas_fifo[0].second);
239 /* hopefully redundant integrity check, but added while debugging r6216 */
240 if (iter == whowas.end())
242 /* this should never happen, if it does maps are corrupt */
243 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (2)");
246 whowas_set* n = (whowas_set*)iter->second;
249 int nickcount = n->size();
250 while (n->begin() != n->end() && nickcount > groupsize)
252 WhoWasGroup *a = *(n->begin());
261 /* call maintain once an hour to remove expired nicks */
262 void CommandWhowas::MaintainWhoWas(time_t t)
264 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
266 whowas_set* n = (whowas_set*)iter->second;
269 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
271 WhoWasGroup *a = *(n->begin());
273 n->erase(n->begin());
279 CommandWhowas::~CommandWhowas()
283 ServerInstance->Timers->DelTimer(timer);
286 whowas_users::iterator iter;
288 while ((fifosize = (int)whowas_fifo.size()) > 0)
290 iter = whowas.find(whowas_fifo[0].second);
292 /* hopefully redundant integrity check, but added while debugging r6216 */
293 if (iter == whowas.end())
295 /* this should never happen, if it does maps are corrupt */
296 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (3)");
300 whowas_set* n = (whowas_set*)iter->second;
304 while (n->begin() != n->end())
306 WhoWasGroup *a = *(n->begin());
314 whowas_fifo.pop_front();
318 WhoWasGroup::WhoWasGroup(User* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
320 this->host = strdup(user->host);
321 this->dhost = strdup(user->dhost);
322 this->ident = strdup(user->ident);
323 this->server = user->server;
324 this->gecos = strdup(user->fullname);
327 WhoWasGroup::~WhoWasGroup()
339 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
340 void WhoWasMaintainTimer::Tick(time_t)
342 Command* whowas_command = ServerInstance->Parser->GetHandler("WHOWAS");
345 std::deque<classbase*> params;
346 whowas_command->HandleInternal(WHOWAS_MAINTAIN, params);