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