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