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());
111 case WHOWAS_MAINTAIN:
112 MaintainWhoWas(ServerInstance->Time());
121 void cmd_whowas::GetStats(Extensible* ext)
124 int whowas_bytes = 0;
125 whowas_users_fifo::iterator iter;
126 for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
128 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
131 whowas_size += n->size();
132 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
135 ext->Extend("stats", std::string("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)").c_str());
138 void cmd_whowas::AddToWhoWas(userrec* user)
140 /* if whowas disabled */
141 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
146 whowas_users::iterator iter = whowas.find(user->nick);
148 ServerInstance->Log(DEBUG,"Add to whowas lists");
150 if (iter == whowas.end())
152 ServerInstance->Log(DEBUG,"Adding new whowas set for %s",user->nick);
154 whowas_set* n = new whowas_set;
155 WhoWasGroup *a = new WhoWasGroup(user);
157 whowas[user->nick] = n;
158 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick));
160 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
162 ServerInstance->Log(DEBUG,"Maxgroups of %d reached deleting oldest group '%s'",ServerInstance->Config->WhoWasMaxGroups, whowas_fifo[0].second.c_str());
164 whowas_users::iterator iter = whowas.find(whowas_fifo[0].second);
165 if (iter != whowas.end())
167 whowas_set* n = (whowas_set*)iter->second;
170 while (n->begin() != n->end())
172 WhoWasGroup *a = *(n->begin());
180 whowas_fifo.pop_front();
185 whowas_set* group = (whowas_set*)iter->second;
187 ServerInstance->Log(DEBUG,"Using existing whowas group for %s",user->nick);
189 WhoWasGroup *a = new WhoWasGroup(user);
192 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
194 ServerInstance->Log(DEBUG,"Trimming existing group '%s' to %d entries",user->nick, ServerInstance->Config->WhoWasGroupSize);
195 WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
202 /* on rehash, refactor maps according to new conf values */
203 void cmd_whowas::PruneWhoWas(time_t t)
206 int groupsize = ServerInstance->Config->WhoWasGroupSize;
207 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
208 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
210 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
211 whowas_users::iterator iter;
213 while ((fifosize = (int)whowas_fifo.size()) > 0)
215 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
217 iter = whowas.find(whowas_fifo[0].second);
218 /* hopefully redundant integrity check, but added while debugging r6216 */
219 if (iter == whowas.end())
221 /* this should never happen, if it does maps are corrupt */
222 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
225 whowas_set* n = (whowas_set*)iter->second;
228 while (n->begin() != n->end())
230 WhoWasGroup *a = *(n->begin());
237 whowas_fifo.pop_front();
243 /* Then cut the whowas sets to new size (groupsize) */
244 fifosize = (int)whowas_fifo.size();
245 for (int i = 0; i < fifosize; i++)
247 iter = whowas.find(whowas_fifo[0].second);
248 /* hopefully redundant integrity check, but added while debugging r6216 */
249 if (iter == whowas.end())
251 /* this should never happen, if it does maps are corrupt */
252 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (2)");
255 whowas_set* n = (whowas_set*)iter->second;
258 int nickcount = n->size();
259 while (n->begin() != n->end() && nickcount > groupsize)
261 WhoWasGroup *a = *(n->begin());
270 /* call maintain once an hour to remove expired nicks */
271 void cmd_whowas::MaintainWhoWas(time_t t)
273 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
275 whowas_set* n = (whowas_set*)iter->second;
278 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
280 WhoWasGroup *a = *(n->begin());
282 n->erase(n->begin());
288 cmd_whowas::~cmd_whowas()
292 ServerInstance->Timers->DelTimer(timer);
295 whowas_users::iterator iter;
297 while ((fifosize = (int)whowas_fifo.size()) > 0)
299 iter = whowas.find(whowas_fifo[0].second);
300 /* hopefully redundant integrity check, but added while debugging r6216 */
301 if (iter == whowas.end())
303 /* this should never happen, if it does maps are corrupt */
304 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (3)");
307 whowas_set* n = (whowas_set*)iter->second;
310 while (n->begin() != n->end())
312 WhoWasGroup *a = *(n->begin());
319 whowas_fifo.pop_front();
323 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
325 this->host = strdup(user->host);
326 this->dhost = strdup(user->dhost);
327 this->ident = strdup(user->ident);
328 this->server = user->server;
329 this->gecos = strdup(user->fullname);
332 WhoWasGroup::~WhoWasGroup()
344 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
345 void MaintainTimer::Tick(time_t t)
347 command_t* whowas_command = ServerInstance->Parser->GetHandler("WHOWAS");
350 std::deque<classbase*> params;
351 whowas_command->HandleInternal(WHOWAS_MAINTAIN, params);