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 if (i == whowas.end())
44 user->WriteServ("406 %s %s :There was no such nickname",user->nick,parameters[0]);
45 user->WriteServ("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->WriteServ("314 %s %s %s %s * :%s",user->nick,parameters[0],u->ident,u->dhost,u->gecos);
69 user->WriteServ("379 %s %s :was connecting from *@%s", user->nick, parameters[0], u->host);
71 if(*ServerInstance->Config->HideWhoisServer && !(*user->oper))
72 user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], ServerInstance->Config->HideWhoisServer, b);
74 user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], u->server, b);
79 user->WriteServ("406 %s %s :There was no such nickname",user->nick,parameters[0]);
80 user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
85 user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
89 CmdResult cmd_whowas::HandleInternal(const unsigned int id, const std::deque<classbase*> ¶meters)
94 AddToWhoWas((userrec*)parameters[0]);
98 GetStats((Extensible*)parameters[0]);
102 PruneWhoWas(ServerInstance->Time());
105 case WHOWAS_MAINTAIN:
106 MaintainWhoWas(ServerInstance->Time());
115 void cmd_whowas::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 ext->Extend("stats", std::string("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)").c_str());
132 void cmd_whowas::AddToWhoWas(userrec* user)
134 /* if whowas disabled */
135 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
140 whowas_users::iterator iter = whowas.find(user->nick);
142 if (iter == whowas.end())
144 whowas_set* n = new whowas_set;
145 WhoWasGroup *a = new WhoWasGroup(user);
147 whowas[user->nick] = n;
148 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick));
150 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
152 whowas_users::iterator iter = whowas.find(whowas_fifo[0].second);
153 if (iter != whowas.end())
155 whowas_set* n = (whowas_set*)iter->second;
158 while (n->begin() != n->end())
160 WhoWasGroup *a = *(n->begin());
168 whowas_fifo.pop_front();
173 whowas_set* group = (whowas_set*)iter->second;
174 WhoWasGroup *a = new WhoWasGroup(user);
177 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
179 WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
186 /* on rehash, refactor maps according to new conf values */
187 void cmd_whowas::PruneWhoWas(time_t t)
190 int groupsize = ServerInstance->Config->WhoWasGroupSize;
191 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
192 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
194 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
195 whowas_users::iterator iter;
197 while ((fifosize = (int)whowas_fifo.size()) > 0)
199 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
201 iter = whowas.find(whowas_fifo[0].second);
202 /* hopefully redundant integrity check, but added while debugging r6216 */
203 if (iter == whowas.end())
205 /* this should never happen, if it does maps are corrupt */
206 ServerInstance->Log(DEBUG, "BUG: Whowas maps got corrupted! (1)");
209 whowas_set* n = (whowas_set*)iter->second;
212 while (n->begin() != n->end())
214 WhoWasGroup *a = *(n->begin());
221 whowas_fifo.pop_front();
227 /* Then cut the whowas sets to new size (groupsize) */
228 fifosize = (int)whowas_fifo.size();
229 for (int i = 0; i < fifosize; i++)
231 iter = whowas.find(whowas_fifo[0].second);
232 /* hopefully redundant integrity check, but added while debugging r6216 */
233 if (iter == whowas.end())
235 /* this should never happen, if it does maps are corrupt */
236 ServerInstance->Log(DEBUG, "BUG: Whowas maps got corrupted! (2)");
239 whowas_set* n = (whowas_set*)iter->second;
242 int nickcount = n->size();
243 while (n->begin() != n->end() && nickcount > groupsize)
245 WhoWasGroup *a = *(n->begin());
254 /* call maintain once an hour to remove expired nicks */
255 void cmd_whowas::MaintainWhoWas(time_t t)
257 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
259 whowas_set* n = (whowas_set*)iter->second;
262 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
264 WhoWasGroup *a = *(n->begin());
266 n->erase(n->begin());
272 cmd_whowas::~cmd_whowas()
276 ServerInstance->Timers->DelTimer(timer);
279 whowas_users::iterator iter;
281 while ((fifosize = (int)whowas_fifo.size()) > 0)
283 iter = whowas.find(whowas_fifo[0].second);
284 /* hopefully redundant integrity check, but added while debugging r6216 */
285 if (iter == whowas.end())
287 /* this should never happen, if it does maps are corrupt */
288 ServerInstance->Log(DEBUG, "BUG: Whowas maps got corrupted! (3)");
291 whowas_set* n = (whowas_set*)iter->second;
294 while (n->begin() != n->end())
296 WhoWasGroup *a = *(n->begin());
303 whowas_fifo.pop_front();
307 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
309 this->host = strdup(user->host);
310 this->dhost = strdup(user->dhost);
311 this->ident = strdup(user->ident);
312 this->server = user->server;
313 this->gecos = strdup(user->fullname);
316 WhoWasGroup::~WhoWasGroup()
328 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
329 void MaintainTimer::Tick(time_t t)
331 command_t* whowas_command = ServerInstance->Parser->GetHandler("WHOWAS");
334 std::deque<classbase*> params;
335 whowas_command->HandleInternal(WHOWAS_MAINTAIN, params);