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 stats.assign("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)");
133 ext->Extend("stats", stats.c_str());
136 void cmd_whowas::AddToWhoWas(userrec* user)
138 /* if whowas disabled */
139 if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
144 whowas_users::iterator iter = whowas.find(user->nick);
146 if (iter == whowas.end())
148 whowas_set* n = new whowas_set;
149 WhoWasGroup *a = new WhoWasGroup(user);
151 whowas[user->nick] = n;
152 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick));
154 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
156 whowas_users::iterator iter = whowas.find(whowas_fifo[0].second);
157 if (iter != whowas.end())
159 whowas_set* n = (whowas_set*)iter->second;
162 while (n->begin() != n->end())
164 WhoWasGroup *a = *(n->begin());
172 whowas_fifo.pop_front();
177 whowas_set* group = (whowas_set*)iter->second;
178 WhoWasGroup *a = new WhoWasGroup(user);
181 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
183 WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
190 /* on rehash, refactor maps according to new conf values */
191 void cmd_whowas::PruneWhoWas(time_t t)
194 int groupsize = ServerInstance->Config->WhoWasGroupSize;
195 int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
196 int maxkeep = ServerInstance->Config->WhoWasMaxKeep;
198 /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
199 whowas_users::iterator iter;
201 while ((fifosize = (int)whowas_fifo.size()) > 0)
203 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
205 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->Log(DEFAULT, "BUG: Whowas maps got corrupted! (1)");
213 whowas_set* n = (whowas_set*)iter->second;
216 while (n->begin() != n->end())
218 WhoWasGroup *a = *(n->begin());
225 whowas_fifo.pop_front();
231 /* Then cut the whowas sets to new size (groupsize) */
232 fifosize = (int)whowas_fifo.size();
233 for (int i = 0; i < fifosize; i++)
235 iter = whowas.find(whowas_fifo[0].second);
236 /* hopefully redundant integrity check, but added while debugging r6216 */
237 if (iter == whowas.end())
239 /* this should never happen, if it does maps are corrupt */
240 ServerInstance->Log(DEFAULT, "BUG: Whowas maps got corrupted! (2)");
243 whowas_set* n = (whowas_set*)iter->second;
246 int nickcount = n->size();
247 while (n->begin() != n->end() && nickcount > groupsize)
249 WhoWasGroup *a = *(n->begin());
258 /* call maintain once an hour to remove expired nicks */
259 void cmd_whowas::MaintainWhoWas(time_t t)
261 for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
263 whowas_set* n = (whowas_set*)iter->second;
266 while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
268 WhoWasGroup *a = *(n->begin());
270 n->erase(n->begin());
276 cmd_whowas::~cmd_whowas()
280 ServerInstance->Timers->DelTimer(timer);
283 whowas_users::iterator iter;
285 while ((fifosize = (int)whowas_fifo.size()) > 0)
287 iter = whowas.find(whowas_fifo[0].second);
288 /* hopefully redundant integrity check, but added while debugging r6216 */
289 if (iter == whowas.end())
291 /* this should never happen, if it does maps are corrupt */
292 ServerInstance->Log(DEFAULT, "BUG: Whowas maps got corrupted! (3)");
295 whowas_set* n = (whowas_set*)iter->second;
298 while (n->begin() != n->end())
300 WhoWasGroup *a = *(n->begin());
307 whowas_fifo.pop_front();
311 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
313 this->host = strdup(user->host);
314 this->dhost = strdup(user->dhost);
315 this->ident = strdup(user->ident);
316 this->server = user->server;
317 this->gecos = strdup(user->fullname);
320 WhoWasGroup::~WhoWasGroup()
332 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
333 void WhoWasMaintainTimer::Tick(time_t t)
335 command_t* whowas_command = ServerInstance->Parser->GetHandler("WHOWAS");
338 std::deque<classbase*> params;
339 whowas_command->HandleInternal(WHOWAS_MAINTAIN, params);