]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_whowas.cpp
Now even less leakage on add.
[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                                 DELETE(n);
174                                 whowas.erase(iter);
175                         }
176                         whowas_fifo.pop_front();
177                 }
178         }
179         else
180         {
181                 whowas_set* group = (whowas_set*)iter->second;
182
183                 ServerInstance->Log(DEBUG,"Using existing whowas group for %s",user->nick);
184
185                 WhoWasGroup *a = new WhoWasGroup(user);
186                 group->push_back(a);
187
188                 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
189                 {
190                         ServerInstance->Log(DEBUG,"Trimming existing group '%s' to %d entries",user->nick, ServerInstance->Config->WhoWasGroupSize);
191                         WhoWasGroup *a = (WhoWasGroup*)*(group->begin());
192                         DELETE(a);
193                         group->pop_front();
194                 }
195         }
196 }
197
198 /* on rehash, refactor maps according to new conf values */
199 void cmd_whowas::PruneWhoWas(time_t t)
200 {
201         /* config values */
202         int groupsize = ServerInstance->Config->WhoWasGroupSize;
203         int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
204         int maxkeep =   ServerInstance->Config->WhoWasMaxKeep;
205
206         /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
207         whowas_users::iterator iter;
208         int fifosize;
209         while ((fifosize = (int)whowas_fifo.size()) > 0)
210         {
211                 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
212                 {
213                         iter = whowas.find(whowas_fifo[0].second);
214                         /* hopefully redundant integrity check, but added while debugging r6216 */
215                         if (iter == whowas.end())
216                         {
217                                 /* this should never happen, if it does maps are corrupt */
218                                 ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
219                                 return;
220                         }
221                         whowas_set* n = (whowas_set*)iter->second;
222                         if (n->size())
223                         {
224                                 while (n->begin() != n->end())
225                                 {
226                                         WhoWasGroup *a = *(n->begin());
227                                         DELETE(a);
228                                         n->pop_front();
229                                 }
230                         }
231                         DELETE(n);
232                         whowas.erase(iter);
233                         whowas_fifo.pop_front();
234                 }
235                 else
236                         break;
237         }
238
239         /* Then cut the whowas sets to new size (groupsize) */
240         fifosize = (int)whowas_fifo.size();
241         for (int i = 0; i < fifosize; i++)
242         {
243                 iter = whowas.find(whowas_fifo[0].second);
244                 /* hopefully redundant integrity check, but added while debugging r6216 */
245                 if (iter == whowas.end())
246                 {
247                         /* this should never happen, if it does maps are corrupt */
248                         ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (2)");
249                         return;
250                 }
251                 whowas_set* n = (whowas_set*)iter->second;
252                 if (n->size())
253                 {
254                         int nickcount = n->size();
255                         while (n->begin() != n->end() && nickcount > groupsize)
256                         {
257                                 WhoWasGroup *a = *(n->begin());
258                                 DELETE(a);
259                                 n->pop_front();
260                                 nickcount--;
261                         }
262                 }
263         }
264 }
265
266 cmd_whowas::~cmd_whowas()
267 {
268         if (timer)
269         {
270                 ServerInstance->Timers->DelTimer(timer);
271         }
272
273         whowas_users::iterator iter;
274         int fifosize;
275         while ((fifosize = (int)whowas_fifo.size()) > 0)
276         {
277                 iter = whowas.find(whowas_fifo[0].second);
278                 /* hopefully redundant integrity check, but added while debugging r6216 */
279                 if (iter == whowas.end())
280                 {
281                         /* this should never happen, if it does maps are corrupt */
282                         ServerInstance->Log(DEBUG, "Whowas maps got corrupted! (1)");
283                         return;
284                 }
285                 whowas_set* n = (whowas_set*)iter->second;
286                 if (n->size())
287                 {
288                         while (n->begin() != n->end())
289                         {
290                                 WhoWasGroup *a = *(n->begin());
291                                 DELETE(a);
292                                 n->pop_front();
293                         }
294                 }
295                 DELETE(n);
296                 whowas.erase(iter);
297                 whowas_fifo.pop_front();
298         }
299 }
300
301 WhoWasGroup::WhoWasGroup(userrec* user) : host(NULL), dhost(NULL), ident(NULL), server(NULL), gecos(NULL), signon(user->signon)
302 {
303         this->host = strdup(user->host);
304         this->dhost = strdup(user->dhost);
305         this->ident = strdup(user->ident);
306         this->server = user->server;
307         this->gecos = strdup(user->fullname);
308 }
309
310 WhoWasGroup::~WhoWasGroup()
311 {
312         if (host)
313                 free(host);
314         if (dhost)
315                 free(dhost);
316         if (ident)
317                 free(ident);
318         if (gecos)
319                 free(gecos);
320 }
321
322 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
323 void MaintainTimer::Tick(time_t t)
324 {
325         for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
326         {
327                 whowas_set* n = (whowas_set*)iter->second;
328                 if (n->size())
329                 {
330                         while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
331                         {
332                                 WhoWasGroup *a = *(n->begin());
333                                 DELETE(a);
334                                 n->erase(n->begin());
335                         }
336                 }
337         }
338         timer = new MaintainTimer(ServerInstance, 3600);
339         ServerInstance->Timers->AddTimer(timer);
340 }