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 * ---------------------------------------------------
14 #include "configreader.h"
16 #include "commands/cmd_whowas.h"
18 extern "C" command_t* init_command(InspIRCd* Instance)
20 return new cmd_whowas(Instance);
23 cmd_whowas::cmd_whowas(InspIRCd* Instance)
24 : command_t(Instance, "WHOWAS", 0, 1)
26 syntax = "<nick>{,<nick>}";
27 timer = new MaintainTimer(Instance, 3600);
28 Instance->Timers->AddTimer(timer);
31 CmdResult cmd_whowas::Handle (const char** parameters, int pcnt, userrec* user)
33 /* if whowas disabled in config */
34 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
36 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
40 whowas_users::iterator i = whowas.find(parameters[0]);
42 ServerInstance->Log(DEBUG,"Entered cmd_whowas");
44 if (i == whowas.end())
46 ServerInstance->Log(DEBUG,"No such nick in whowas");
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 ServerInstance->Log(DEBUG,"Whowas set found");
54 whowas_set* grp = i->second;
57 for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
59 ServerInstance->Log(DEBUG,"Spool whowas entry");
61 time_t rawtime = u->signon;
65 timeinfo = localtime(&rawtime);
67 /* XXX - 'b' could be only 25 chars long and then strlcpy() would terminate it for us too? */
68 strlcpy(b,asctime(timeinfo),MAXBUF);
71 user->WriteServ("314 %s %s %s %s * :%s",user->nick,parameters[0],u->ident,u->dhost,u->gecos);
74 user->WriteServ("379 %s %s :was connecting from *@%s", user->nick, parameters[0], u->host);
76 if(*ServerInstance->Config->HideWhoisServer && !(*user->oper))
77 user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], ServerInstance->Config->HideWhoisServer, b);
79 user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], u->server, b);
84 ServerInstance->Log(DEBUG,"Oops, empty whowas set found");
85 user->WriteServ("406 %s %s :There was no such nickname",user->nick,parameters[0]);
86 user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
91 user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
95 CmdResult cmd_whowas::HandleInternal(const unsigned int id, const std::deque<classbase*> ¶meters)
100 AddToWhoWas((userrec*)parameters[0]);
104 GetStats((Extensible*)parameters[0]);
108 PruneWhoWas(ServerInstance->Time());
117 void cmd_whowas::GetStats(Extensible* ext)
120 int whowas_bytes = 0;
121 whowas_users_fifo::iterator iter;
122 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
124 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
127 whowas_size += n->size();
128 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
131 ext->Extend("stats", std::string("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)").c_str());
134 void cmd_whowas::AddToWhoWas(userrec* user)
136 /* if whowas disabled */
137 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
142 whowas_users::iterator iter = whowas.find(user->nick);
144 ServerInstance->Log(DEBUG,"Add to whowas lists");
146 if (iter == whowas.end())
148 ServerInstance->Log(DEBUG,"Adding new whowas set for %s",user->nick);
150 whowas_set* n = new whowas_set;
151 WhoWasGroup *a = new WhoWasGroup(user);
153 whowas[user->nick] = n;
154 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick));
156 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
158 ServerInstance->Log(DEBUG,"Maxgroups of %d reached deleting oldest group '%s'",ServerInstance->Config->WhoWasMaxGroups, whowas_fifo[0].second.c_str());
160 whowas_users::iterator iter = whowas.find(whowas_fifo[0].second);
161 if (iter != whowas.end())
163 whowas_set* n = (whowas_set*)iter->second;
166 while (n->begin() != n->end())
168 WhoWasGroup *a = *(n->begin());
175 whowas_fifo.pop_front();
180 whowas_set* group = (whowas_set*)iter->second;
182 ServerInstance->Log(DEBUG,"Using existing whowas group for %s",user->nick);
184 WhoWasGroup *a = new WhoWasGroup(user);
187 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
189 ServerInstance->Log(DEBUG,"Trimming existing group '%s' to %d entries",user->nick, ServerInstance->Config->WhoWasGroupSize);
190 WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
197 /* on rehash, refactor maps according to new conf values */
198 void cmd_whowas::PruneWhoWas(time_t t)
201 int groupsize = ServerInstance->Config->WhoWasGroupSize;
202 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
203 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
205 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
206 whowas_users::iterator iter;
208 while ((fifosize = (int)whowas_fifo.size()) > 0)
210 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
212 iter = whowas.find(whowas_fifo[0].second);
213 /* hopefully redundant integrity check, but added while debugging r6216 */
214 if (iter == whowas.end())
216 /* this should never happen, if it does maps are corrupt */
217 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
220 whowas_set* n = (whowas_set*)iter->second;
223 while (n->begin() != n->end())
225 WhoWasGroup *a = *(n->begin());
232 whowas_fifo.pop_front();
238 /* Then cut the whowas sets to new size (groupsize) */
239 fifosize = (int)whowas_fifo.size();
240 for (int i = 0; i < fifosize; i++)
242 iter = whowas.find(whowas_fifo[0].second);
243 /* hopefully redundant integrity check, but added while debugging r6216 */
244 if (iter == whowas.end())
246 /* this should never happen, if it does maps are corrupt */
247 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (2)");
250 whowas_set* n = (whowas_set*)iter->second;
253 int nickcount = n->size();
254 while (n->begin() != n->end() && nickcount > groupsize)
256 WhoWasGroup *a = *(n->begin());
265 cmd_whowas::~cmd_whowas()
269 ServerInstance->Timers->DelTimer(timer);
272 whowas_users::iterator iter;
274 while ((fifosize = (int)whowas_fifo.size()) > 0)
276 iter = whowas.find(whowas_fifo[0].second);
277 /* hopefully redundant integrity check, but added while debugging r6216 */
278 if (iter == whowas.end())
280 /* this should never happen, if it does maps are corrupt */
281 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
284 whowas_set* n = (whowas_set*)iter->second;
287 while (n->begin() != n->end())
289 WhoWasGroup *a = *(n->begin());
296 whowas_fifo.pop_front();
300 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
302 this->host = strdup(user->host);
303 this->dhost = strdup(user->dhost);
304 this->ident = strdup(user->ident);
305 this->server = user->server;
306 this->gecos = strdup(user->fullname);
309 WhoWasGroup::~WhoWasGroup()
321 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
322 void MaintainTimer::Tick(time_t t)
324 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
326 whowas_set* n = (whowas_set*)iter->second;
329 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
331 WhoWasGroup *a = *(n->begin());
333 n->erase(n->begin());
337 timer = new MaintainTimer(ServerInstance, 3600);
338 ServerInstance->Timers->AddTimer(timer);