]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_whowas.cpp
Move all /WHOWAS related out of core and into cmd_whowas.
[user/henk/code/inspircd.git] / src / cmd_whowas.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "configreader.h"
15 #include "users.h"
16 #include "commands/cmd_whowas.h"
17
18 extern "C" command_t* init_command(InspIRCd* Instance)
19 {
20         return new cmd_whowas(Instance);
21 }
22
23 cmd_whowas::cmd_whowas(InspIRCd* Instance)
24 : command_t(Instance, "WHOWAS", 0, 1)
25 {
26         syntax = "<nick>{,<nick>}";
27         timer = new MaintainTimer(Instance, 3600);
28         Instance->Timers->AddTimer(timer);
29 }
30
31 CmdResult cmd_whowas::Handle (const char** parameters, int pcnt, userrec* user)
32 {
33         /* if whowas disabled in config */
34         if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
35         {
36                 user->WriteServ("421 %s %s :This command has been disabled.",user->nick,command.c_str());
37                 return CMD_FAILURE;
38         }
39                         
40         whowas_users::iterator i = whowas.find(parameters[0]);
41
42         ServerInstance->Log(DEBUG,"Entered cmd_whowas");
43
44         if (i == whowas.end())
45         {
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]);
49                 return CMD_FAILURE;
50         }
51         else
52         {
53                 ServerInstance->Log(DEBUG,"Whowas set found");
54                 whowas_set* grp = i->second;
55                 if (grp->size())
56                 {
57                         for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
58                         {
59                                 ServerInstance->Log(DEBUG,"Spool whowas entry");
60                                 WhoWasGroup* u = *ux;
61                                 time_t rawtime = u->signon;
62                                 tm *timeinfo;
63                                 char b[MAXBUF];
64         
65                                 timeinfo = localtime(&rawtime);
66                                 
67                                 /* XXX - 'b' could be only 25 chars long and then strlcpy() would terminate it for us too? */
68                                 strlcpy(b,asctime(timeinfo),MAXBUF);
69                                 b[24] = 0;
70
71                                 user->WriteServ("314 %s %s %s %s * :%s",user->nick,parameters[0],u->ident,u->dhost,u->gecos);
72                                 
73                                 if(*user->oper)
74                                         user->WriteServ("379 %s %s :was connecting from *@%s", user->nick, parameters[0], u->host);
75                                 
76                                 if(*ServerInstance->Config->HideWhoisServer && !(*user->oper))
77                                         user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], ServerInstance->Config->HideWhoisServer, b);
78                                 else
79                                         user->WriteServ("312 %s %s %s :%s",user->nick,parameters[0], u->server, b);
80                         }
81                 }
82                 else
83                 {
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]);
87                         return CMD_FAILURE;
88                 }
89         }
90
91         user->WriteServ("369 %s %s :End of WHOWAS",user->nick,parameters[0]);
92         return CMD_SUCCESS;
93 };
94
95 CmdResult cmd_whowas::HandleInternal(const unsigned int id, const std::deque<classbase*> &parameters)
96 {
97         switch (id)
98         {
99                 case WHOWAS_ADD:
100                         AddToWhoWas((userrec*)parameters[0]);
101                 break;
102
103                 case WHOWAS_STATS:
104                         GetStats((Extensible*)parameters[0]);
105                 break;
106
107                 case WHOWAS_PRUNE:
108                         PruneWhoWas(ServerInstance->Time());
109                 break;
110
111                 default:
112                 break;
113         }
114         return CMD_SUCCESS;
115 };
116
117 void cmd_whowas::GetStats(Extensible* ext)
118 {
119         int whowas_size = 0;
120         int whowas_bytes = 0;
121         whowas_users_fifo::iterator iter;
122         for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
123         {
124                 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
125                 if (n->size())
126                 {
127                         whowas_size += n->size();
128                         whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
129                 }
130         }
131         ext->Extend("stats", std::string("Whowas(MAPSETS) " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)").c_str());
132 }
133
134 void cmd_whowas::AddToWhoWas(userrec* user)
135 {
136         /* if whowas disabled */
137         if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
138         {
139                 return;
140         }
141
142         whowas_users::iterator iter = whowas.find(user->nick);
143
144         ServerInstance->Log(DEBUG,"Add to whowas lists");
145
146         if (iter == whowas.end())
147         {
148                 ServerInstance->Log(DEBUG,"Adding new whowas set for %s",user->nick);
149
150                 whowas_set* n = new whowas_set;
151                 WhoWasGroup *a = new WhoWasGroup(user);
152                 n->push_back(a);
153                 whowas[user->nick] = n;
154                 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick));
155
156                 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
157                 {
158                         ServerInstance->Log(DEBUG,"Maxgroups of %d reached deleting oldest group '%s'",ServerInstance->Config->WhoWasMaxGroups, whowas_fifo[0].second.c_str());
159
160                         whowas_users::iterator iter = whowas.find(whowas_fifo[0].second);
161                         if (iter != whowas.end())
162                         {
163                                 whowas_set* n = (whowas_set*)iter->second;
164                                 if (n->size())
165                                 {
166                                         while (n->begin() != n->end())
167                                         {
168                                                 WhoWasGroup *a = *(n->begin());
169                                                 DELETE(a);
170                                                 n->pop_front();
171                                         }
172                                 }
173                                 whowas.erase(iter);
174                         }
175                         whowas_fifo.pop_front();
176                 }
177         }
178         else
179         {
180                 whowas_set* group = (whowas_set*)iter->second;
181
182                 ServerInstance->Log(DEBUG,"Using existing whowas group for %s",user->nick);
183
184                 WhoWasGroup *a = new WhoWasGroup(user);
185                 group->push_back(a);
186
187                 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
188                 {
189                         ServerInstance->Log(DEBUG,"Trimming existing group '%s' to %d entries",user->nick, ServerInstance->Config->WhoWasGroupSize);
190                         WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
191                         DELETE(a);
192                         group->pop_front();
193                 }
194         }
195 }
196
197 /* on rehash, refactor maps according to new conf values */
198 void cmd_whowas::PruneWhoWas(time_t t)
199 {
200         /* config values */
201         int groupsize = ServerInstance->Config->WhoWasGroupSize;
202         int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
203         int maxkeep =   ServerInstance->Config->WhoWasMaxKeep;
204
205         /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
206         whowas_users::iterator iter;
207         int fifosize;
208         while ((fifosize = (int)whowas_fifo.size()) > 0)
209         {
210                 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
211                 {
212                         iter = whowas.find(whowas_fifo[0].second);
213                         /* hopefully redundant integrity check, but added while debugging r6216 */
214                         if (iter == whowas.end())
215                         {
216                                 /* this should never happen, if it does maps are corrupt */
217                                 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
218                                 return;
219                         }
220                         whowas_set* n = (whowas_set*)iter->second;
221                         if (n->size())
222                         {
223                                 while (n->begin() != n->end())
224                                 {
225                                         WhoWasGroup *a = *(n->begin());
226                                         DELETE(a);
227                                         n->pop_front();
228                                 }
229                         }
230                         DELETE(n);
231                         whowas.erase(iter);
232                         whowas_fifo.pop_front();
233                 }
234                 else
235                         break;
236         }
237
238         /* Then cut the whowas sets to new size (groupsize) */
239         fifosize = (int)whowas_fifo.size();
240         for (int i = 0; i < fifosize; i++)
241         {
242                 iter = whowas.find(whowas_fifo[0].second);
243                 /* hopefully redundant integrity check, but added while debugging r6216 */
244                 if (iter == whowas.end())
245                 {
246                         /* this should never happen, if it does maps are corrupt */
247                         ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (2)");
248                         return;
249                 }
250                 whowas_set* n = (whowas_set*)iter->second;
251                 if (n->size())
252                 {
253                         int nickcount = n->size();
254                         while (n->begin() != n->end() && nickcount > groupsize)
255                         {
256                                 WhoWasGroup *a = *(n->begin());
257                                 DELETE(a);
258                                 n->pop_front();
259                                 nickcount--;
260                         }
261                 }
262         }
263 }
264
265 cmd_whowas::~cmd_whowas()
266 {
267         if (timer)
268         {
269                 ServerInstance->Timers->DelTimer(timer);
270         }
271
272         whowas_users::iterator iter;
273         int fifosize;
274         while ((fifosize = (int)whowas_fifo.size()) > 0)
275         {
276                 iter = whowas.find(whowas_fifo[0].second);
277                 /* hopefully redundant integrity check, but added while debugging r6216 */
278                 if (iter == whowas.end())
279                 {
280                         /* this should never happen, if it does maps are corrupt */
281                         ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
282                         return;
283                 }
284                 whowas_set* n = (whowas_set*)iter->second;
285                 if (n->size())
286                 {
287                         while (n->begin() != n->end())
288                         {
289                                 WhoWasGroup *a = *(n->begin());
290                                 DELETE(a);
291                                 n->pop_front();
292                         }
293                 }
294                 DELETE(n);
295                 whowas.erase(iter);
296                 whowas_fifo.pop_front();
297         }
298 }
299
300 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
301 {
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);
307 }
308
309 WhoWasGroup::~WhoWasGroup()
310 {
311         if (host)
312                 free(host);
313         if (dhost)
314                 free(dhost);
315         if (ident)
316                 free(ident);
317         if (gecos)
318                 free(gecos);
319 }
320
321 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
322 void MaintainTimer::Tick(time_t t)
323 {
324         for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
325         {
326                 whowas_set* n = (whowas_set*)iter->second;
327                 if (n->size())
328                 {
329                         while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
330                         {
331                                 WhoWasGroup *a = *(n->begin());
332                                 DELETE(a);
333                                 n->erase(n->begin());
334                         }
335                 }
336         }
337         timer = new MaintainTimer(ServerInstance, 3600);
338         ServerInstance->Timers->AddTimer(timer);
339 }