1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 "configreader.h"
17 #include "commands/cmd_whowas.h"
19 WhoWasMaintainTimer * timer;
21 extern "C" DllExport command_t* init_command(InspIRCd* Instance)
23 return new cmd_whowas(Instance);
26 cmd_whowas::cmd_whowas(InspIRCd* Instance)
27 : command_t(Instance, "WHOWAS", 0, 1)
29 syntax = "<nick>{,<nick>}";
30 timer = new WhoWasMaintainTimer(Instance, 3600);
31 Instance->Timers->AddTimer(timer);
34 CmdResult cmd_whowas::Handle (const char** parameters, int pcnt, userrec* user)
36 /* if whowas disabled in config */
37 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
39 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
43 whowas_users::iterator i = whowas.find(parameters[0]);
45 if (i == whowas.end())
47 user->WriteServ("406 %s %s :There was no such nickname",user->nick,parameters[0]);
48 user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
53 whowas_set* grp = i->second;
56 for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
59 time_t rawtime = u->signon;
63 timeinfo = localtime(&rawtime);
65 /* XXX - 'b' could be only 25 chars long and then strlcpy() would terminate it for us too? */
66 strlcpy(b,asctime(timeinfo),MAXBUF);
69 user->WriteServ("314 %s %s %s %s * :%s",user->nick,parameters[0],u->ident,u->dhost,u->gecos);
72 user->WriteServ("379 %s %s :was connecting from *@%s", user->nick, parameters[0], u->host);
74 if (*ServerInstance->Config->HideWhoisServer && !IS_OPER(user))
75 user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], ServerInstance->Config->HideWhoisServer, b);
77 user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], u->server, b);
82 user->WriteServ("406 %s %s :There was no such nickname",user->nick,parameters[0]);
83 user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
88 user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
92 CmdResult cmd_whowas::HandleInternal(const unsigned int id, const std::deque<classbase*> ¶meters)
97 AddToWhoWas((userrec*)parameters[0]);
101 GetStats((Extensible*)parameters[0]);
105 PruneWhoWas(ServerInstance->Time());
108 case WHOWAS_MAINTAIN:
109 MaintainWhoWas(ServerInstance->Time());
118 void cmd_whowas::GetStats(Extensible* ext)
121 int whowas_bytes = 0;
122 whowas_users_fifo::iterator iter;
123 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
125 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
128 whowas_size += n->size();
129 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
132 ext->Extend("stats", std::string("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)").c_str());
135 void cmd_whowas::AddToWhoWas(userrec* user)
137 /* if whowas disabled */
138 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
143 whowas_users::iterator iter = whowas.find(user->nick);
145 if (iter == whowas.end())
147 whowas_set* n = new whowas_set;
148 WhoWasGroup *a = new WhoWasGroup(user);
150 whowas[user->nick] = n;
151 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick));
153 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
155 whowas_users::iterator iter = whowas.find(whowas_fifo[0].second);
156 if (iter != whowas.end())
158 whowas_set* n = (whowas_set*)iter->second;
161 while (n->begin() != n->end())
163 WhoWasGroup *a = *(n->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 *a = (WhoWasGroup*)*(group->begin());
189 /* on rehash, refactor maps according to new conf values */
190 void cmd_whowas::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);
205 /* hopefully redundant integrity check, but added while debugging r6216 */
206 if (iter == whowas.end())
208 /* this should never happen, if it does maps are corrupt */
209 ServerInstance->Log(DEBUG, "BUG: Whowas maps got corrupted! (1)");
212 whowas_set* n = (whowas_set*)iter->second;
215 while (n->begin() != n->end())
217 WhoWasGroup *a = *(n->begin());
224 whowas_fifo.pop_front();
230 /* Then cut the whowas sets to new size (groupsize) */
231 fifosize = (int)whowas_fifo.size();
232 for (int i = 0; i < fifosize; i++)
234 iter = whowas.find(whowas_fifo[0].second);
235 /* hopefully redundant integrity check, but added while debugging r6216 */
236 if (iter == whowas.end())
238 /* this should never happen, if it does maps are corrupt */
239 ServerInstance->Log(DEBUG, "BUG: Whowas maps got corrupted! (2)");
242 whowas_set* n = (whowas_set*)iter->second;
245 int nickcount = n->size();
246 while (n->begin() != n->end() && nickcount > groupsize)
248 WhoWasGroup *a = *(n->begin());
257 /* call maintain once an hour to remove expired nicks */
258 void cmd_whowas::MaintainWhoWas(time_t t)
260 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
262 whowas_set* n = (whowas_set*)iter->second;
265 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
267 WhoWasGroup *a = *(n->begin());
269 n->erase(n->begin());
275 cmd_whowas::~cmd_whowas()
279 ServerInstance->Timers->DelTimer(timer);
282 whowas_users::iterator iter;
284 while ((fifosize = (int)whowas_fifo.size()) > 0)
286 iter = whowas.find(whowas_fifo[0].second);
287 /* hopefully redundant integrity check, but added while debugging r6216 */
288 if (iter == whowas.end())
290 /* this should never happen, if it does maps are corrupt */
291 ServerInstance->Log(DEBUG, "BUG: Whowas maps got corrupted! (3)");
294 whowas_set* n = (whowas_set*)iter->second;
297 while (n->begin() != n->end())
299 WhoWasGroup *a = *(n->begin());
306 whowas_fifo.pop_front();
310 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
312 this->host = strdup(user->host);
313 this->dhost = strdup(user->dhost);
314 this->ident = strdup(user->ident);
315 this->server = user->server;
316 this->gecos = strdup(user->fullname);
319 WhoWasGroup::~WhoWasGroup()
331 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
332 void WhoWasMaintainTimer::Tick(time_t t)
334 command_t* whowas_command = ServerInstance->Parser->GetHandler("WHOWAS");
337 std::deque<classbase*> params;
338 whowas_command->HandleInternal(WHOWAS_MAINTAIN, params);