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