]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_whowas.cpp
m_cloaking: fix host/ip comparisons #1249
[user/henk/code/inspircd.git] / src / commands / cmd_whowas.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "commands/cmd_whowas.h"
25
26 WhoWasMaintainTimer * timer;
27
28 CommandWhowas::CommandWhowas( Module* parent) : Command(parent, "WHOWAS", 1)
29 {
30         syntax = "<nick>{,<nick>}";
31         Penalty = 2;
32         timer = new WhoWasMaintainTimer(3600);
33         ServerInstance->Timers->AddTimer(timer);
34 }
35
36 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
37 {
38         /* if whowas disabled in config */
39         if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
40         {
41                 user->WriteNumeric(421, "%s %s :This command has been disabled.",user->nick.c_str(),name.c_str());
42                 return CMD_FAILURE;
43         }
44
45         whowas_users::iterator i = whowas.find(assign(parameters[0]));
46
47         if (i == whowas.end())
48         {
49                 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
50                 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
51                 return CMD_FAILURE;
52         }
53         else
54         {
55                 whowas_set* grp = i->second;
56                 if (grp->size())
57                 {
58                         for (whowas_set::iterator ux = grp->begin(); ux != grp->end(); ux++)
59                         {
60                                 WhoWasGroup* u = *ux;
61
62                                 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),parameters[0].c_str(),
63                                         u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
64
65                                 if (user->HasPrivPermission("users/auspex"))
66                                         user->WriteNumeric(379, "%s %s :was connecting from *@%s",
67                                                 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
68
69                                 std::string signon = ServerInstance->TimeString(u->signon);
70                                 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
71                                         user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), ServerInstance->Config->HideWhoisServer.c_str(), signon.c_str());
72                                 else
73                                         user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), u->server.c_str(), signon.c_str());
74                         }
75                 }
76                 else
77                 {
78                         user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
79                         user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
80                         return CMD_FAILURE;
81                 }
82         }
83
84         user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
85         return CMD_SUCCESS;
86 }
87
88 std::string CommandWhowas::GetStats()
89 {
90         int whowas_size = 0;
91         int whowas_bytes = 0;
92         whowas_users_fifo::iterator iter;
93         for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
94         {
95                 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
96                 if (n->size())
97                 {
98                         whowas_size += n->size();
99                         whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
100                 }
101         }
102         return "Whowas entries: " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)";
103 }
104
105 void CommandWhowas::AddToWhoWas(User* user)
106 {
107         /* if whowas disabled */
108         if (ServerInstance->Config->WhoWasGroupSize == 0 || ServerInstance->Config->WhoWasMaxGroups == 0)
109         {
110                 return;
111         }
112
113         whowas_users::iterator iter = whowas.find(irc::string(user->nick.c_str()));
114
115         if (iter == whowas.end())
116         {
117                 whowas_set* n = new whowas_set;
118                 WhoWasGroup *a = new WhoWasGroup(user);
119                 n->push_back(a);
120                 whowas[user->nick.c_str()] = n;
121                 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick.c_str()));
122
123                 if ((int)(whowas.size()) > ServerInstance->Config->WhoWasMaxGroups)
124                 {
125                         whowas_users::iterator iter2 = whowas.find(whowas_fifo[0].second);
126                         if (iter2 != whowas.end())
127                         {
128                                 whowas_set* n2 = (whowas_set*)iter2->second;
129
130                                 if (n2->size())
131                                 {
132                                         while (n2->begin() != n2->end())
133                                         {
134                                                 WhoWasGroup *a2 = *(n2->begin());
135                                                 delete a2;
136                                                 n2->pop_front();
137                                         }
138                                 }
139
140                                 delete n2;
141                                 whowas.erase(iter2);
142                         }
143                         whowas_fifo.pop_front();
144                 }
145         }
146         else
147         {
148                 whowas_set* group = (whowas_set*)iter->second;
149                 WhoWasGroup *a = new WhoWasGroup(user);
150                 group->push_back(a);
151
152                 if ((int)(group->size()) > ServerInstance->Config->WhoWasGroupSize)
153                 {
154                         WhoWasGroup *a2 = (WhoWasGroup*)*(group->begin());
155                         delete a2;
156                         group->pop_front();
157                 }
158         }
159 }
160
161 /* on rehash, refactor maps according to new conf values */
162 void CommandWhowas::PruneWhoWas(time_t t)
163 {
164         /* config values */
165         int groupsize = ServerInstance->Config->WhoWasGroupSize;
166         int maxgroups = ServerInstance->Config->WhoWasMaxGroups;
167         int maxkeep =   ServerInstance->Config->WhoWasMaxKeep;
168
169         /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
170         whowas_users::iterator iter;
171         int fifosize;
172         while ((fifosize = (int)whowas_fifo.size()) > 0)
173         {
174                 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
175                 {
176                         iter = whowas.find(whowas_fifo[0].second);
177
178                         /* hopefully redundant integrity check, but added while debugging r6216 */
179                         if (iter == whowas.end())
180                         {
181                                 /* this should never happen, if it does maps are corrupt */
182                                 ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (1)");
183                                 return;
184                         }
185
186                         whowas_set* n = (whowas_set*)iter->second;
187
188                         if (n->size())
189                         {
190                                 while (n->begin() != n->end())
191                                 {
192                                         WhoWasGroup *a = *(n->begin());
193                                         delete a;
194                                         n->pop_front();
195                                 }
196                         }
197
198                         delete n;
199                         whowas.erase(iter);
200                         whowas_fifo.pop_front();
201                 }
202                 else
203                         break;
204         }
205
206         /* Then cut the whowas sets to new size (groupsize) */
207         fifosize = (int)whowas_fifo.size();
208         for (int i = 0; i < fifosize; i++)
209         {
210                 iter = whowas.find(whowas_fifo[0].second);
211                 /* hopefully redundant integrity check, but added while debugging r6216 */
212                 if (iter == whowas.end())
213                 {
214                         /* this should never happen, if it does maps are corrupt */
215                         ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (2)");
216                         return;
217                 }
218                 whowas_set* n = (whowas_set*)iter->second;
219                 if (n->size())
220                 {
221                         int nickcount = n->size();
222                         while (n->begin() != n->end() && nickcount > groupsize)
223                         {
224                                 WhoWasGroup *a = *(n->begin());
225                                 delete a;
226                                 n->pop_front();
227                                 nickcount--;
228                         }
229                 }
230         }
231 }
232
233 /* call maintain once an hour to remove expired nicks */
234 void CommandWhowas::MaintainWhoWas(time_t t)
235 {
236         for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
237         {
238                 whowas_set* n = (whowas_set*)iter->second;
239                 if (n->size())
240                 {
241                         while ((n->begin() != n->end()) && ((*n->begin())->signon < t - ServerInstance->Config->WhoWasMaxKeep))
242                         {
243                                 WhoWasGroup *a = *(n->begin());
244                                 delete a;
245                                 n->erase(n->begin());
246                         }
247                 }
248         }
249 }
250
251 CommandWhowas::~CommandWhowas()
252 {
253         if (timer)
254         {
255                 ServerInstance->Timers->DelTimer(timer);
256         }
257
258         whowas_users::iterator iter;
259         int fifosize;
260         while ((fifosize = (int)whowas_fifo.size()) > 0)
261         {
262                 iter = whowas.find(whowas_fifo[0].second);
263
264                 /* hopefully redundant integrity check, but added while debugging r6216 */
265                 if (iter == whowas.end())
266                 {
267                         /* this should never happen, if it does maps are corrupt */
268                         ServerInstance->Logs->Log("WHOWAS",DEFAULT, "BUG: Whowas maps got corrupted! (3)");
269                         return;
270                 }
271
272                 whowas_set* n = (whowas_set*)iter->second;
273
274                 if (n->size())
275                 {
276                         while (n->begin() != n->end())
277                         {
278                                 WhoWasGroup *a = *(n->begin());
279                                 delete a;
280                                 n->pop_front();
281                         }
282                 }
283
284                 delete n;
285                 whowas.erase(iter);
286                 whowas_fifo.pop_front();
287         }
288 }
289
290 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
291         server(user->server), gecos(user->fullname), signon(user->signon)
292 {
293 }
294
295 WhoWasGroup::~WhoWasGroup()
296 {
297 }
298
299 /* every hour, run this function which removes all entries older than Config->WhoWasMaxKeep */
300 void WhoWasMaintainTimer::Tick(time_t)
301 {
302         Module* whowas = ServerInstance->Modules->Find("cmd_whowas.so");
303         if (whowas)
304         {
305                 WhowasRequest(whowas, whowas, WhowasRequest::WHOWAS_MAINTAIN).Send();
306         }
307 }
308
309 class ModuleWhoWas : public Module
310 {
311         CommandWhowas cmd;
312  public:
313         ModuleWhoWas() : cmd(this)
314         {
315         }
316
317         void init()
318         {
319                 ServerInstance->Modules->AddService(cmd);
320         }
321
322         void OnRequest(Request& request)
323         {
324                 WhowasRequest& req = static_cast<WhowasRequest&>(request);
325                 switch (req.type)
326                 {
327                         case WhowasRequest::WHOWAS_ADD:
328                                 cmd.AddToWhoWas(req.user);
329                                 break;
330                         case WhowasRequest::WHOWAS_STATS:
331                                 req.value = cmd.GetStats();
332                                 break;
333                         case WhowasRequest::WHOWAS_PRUNE:
334                                 cmd.PruneWhoWas(ServerInstance->Time());
335                                 break;
336                         case WhowasRequest::WHOWAS_MAINTAIN:
337                                 cmd.MaintainWhoWas(ServerInstance->Time());
338                                 break;
339                 }
340         }
341
342         Version GetVersion()
343         {
344                 return Version("WHOWAS Command", VF_VENDOR);
345         }
346 };
347
348 MODULE_INIT(ModuleWhoWas)