]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_whowas.cpp
Replace hardcoded mode letters, part 3
[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 CommandWhowas::CommandWhowas( Module* parent)
27         : Command(parent, "WHOWAS", 1), WhoWasGroupSize(0), WhoWasMaxGroups(0), WhoWasMaxKeep(0)
28 {
29         syntax = "<nick>{,<nick>}";
30         Penalty = 2;
31 }
32
33 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
34 {
35         /* if whowas disabled in config */
36         if (this->WhoWasGroupSize == 0 || this->WhoWasMaxGroups == 0)
37         {
38                 user->WriteNumeric(421, "%s %s :This command has been disabled.",user->nick.c_str(),name.c_str());
39                 return CMD_FAILURE;
40         }
41
42         whowas_users::iterator i = whowas.find(assign(parameters[0]));
43
44         if (i == whowas.end())
45         {
46                 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
47                 user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
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[25];
61
62                                 timeinfo = localtime(&rawtime);
63
64                                 strncpy(b,asctime(timeinfo),24);
65                                 b[24] = 0;
66
67                                 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),parameters[0].c_str(),
68                                         u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
69
70                                 if (user->HasPrivPermission("users/auspex"))
71                                         user->WriteNumeric(379, "%s %s :was connecting from *@%s",
72                                                 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
73
74                                 if (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
75                                         user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), ServerInstance->Config->HideWhoisServer.c_str(), b);
76                                 else
77                                         user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(),parameters[0].c_str(), u->server.c_str(), b);
78                         }
79                 }
80                 else
81                 {
82                         user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
83                         user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
84                         return CMD_FAILURE;
85                 }
86         }
87
88         user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
89         return CMD_SUCCESS;
90 }
91
92 std::string CommandWhowas::GetStats()
93 {
94         int whowas_size = 0;
95         int whowas_bytes = 0;
96         whowas_users_fifo::iterator iter;
97         for (iter = whowas_fifo.begin(); iter != whowas_fifo.end(); iter++)
98         {
99                 whowas_set* n = (whowas_set*)whowas.find(iter->second)->second;
100                 if (n->size())
101                 {
102                         whowas_size += n->size();
103                         whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
104                 }
105         }
106         return "Whowas entries: " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)";
107 }
108
109 void CommandWhowas::AddToWhoWas(User* user)
110 {
111         /* if whowas disabled */
112         if (this->WhoWasGroupSize == 0 || this->WhoWasMaxGroups == 0)
113         {
114                 return;
115         }
116
117         whowas_users::iterator iter = whowas.find(irc::string(user->nick.c_str()));
118
119         if (iter == whowas.end())
120         {
121                 whowas_set* n = new whowas_set;
122                 WhoWasGroup *a = new WhoWasGroup(user);
123                 n->push_back(a);
124                 whowas[user->nick.c_str()] = n;
125                 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(),user->nick.c_str()));
126
127                 if ((int)(whowas.size()) > this->WhoWasMaxGroups)
128                 {
129                         whowas_users::iterator iter2 = whowas.find(whowas_fifo[0].second);
130                         if (iter2 != whowas.end())
131                         {
132                                 whowas_set* n2 = (whowas_set*)iter2->second;
133
134                                 if (n2->size())
135                                 {
136                                         while (n2->begin() != n2->end())
137                                         {
138                                                 WhoWasGroup *a2 = *(n2->begin());
139                                                 delete a2;
140                                                 n2->pop_front();
141                                         }
142                                 }
143
144                                 delete n2;
145                                 whowas.erase(iter2);
146                         }
147                         whowas_fifo.pop_front();
148                 }
149         }
150         else
151         {
152                 whowas_set* group = (whowas_set*)iter->second;
153                 WhoWasGroup *a = new WhoWasGroup(user);
154                 group->push_back(a);
155
156                 if ((int)(group->size()) > this->WhoWasGroupSize)
157                 {
158                         WhoWasGroup *a2 = (WhoWasGroup*)*(group->begin());
159                         delete a2;
160                         group->pop_front();
161                 }
162         }
163 }
164
165 /* on rehash, refactor maps according to new conf values */
166 void CommandWhowas::PruneWhoWas(time_t t)
167 {
168         /* config values */
169         int groupsize = this->WhoWasGroupSize;
170         int maxgroups = this->WhoWasMaxGroups;
171         int maxkeep =   this->WhoWasMaxKeep;
172
173         /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
174         whowas_users::iterator iter;
175         int fifosize;
176         while ((fifosize = (int)whowas_fifo.size()) > 0)
177         {
178                 if (fifosize > maxgroups || whowas_fifo[0].first < t - maxkeep)
179                 {
180                         iter = whowas.find(whowas_fifo[0].second);
181
182                         /* hopefully redundant integrity check, but added while debugging r6216 */
183                         if (iter == whowas.end())
184                         {
185                                 /* this should never happen, if it does maps are corrupt */
186                                 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
187                                 return;
188                         }
189
190                         whowas_set* n = (whowas_set*)iter->second;
191
192                         if (n->size())
193                         {
194                                 while (n->begin() != n->end())
195                                 {
196                                         WhoWasGroup *a = *(n->begin());
197                                         delete a;
198                                         n->pop_front();
199                                 }
200                         }
201
202                         delete n;
203                         whowas.erase(iter);
204                         whowas_fifo.pop_front();
205                 }
206                 else
207                         break;
208         }
209
210         /* Then cut the whowas sets to new size (groupsize) */
211         fifosize = (int)whowas_fifo.size();
212         for (int i = 0; i < fifosize; i++)
213         {
214                 iter = whowas.find(whowas_fifo[0].second);
215                 /* hopefully redundant integrity check, but added while debugging r6216 */
216                 if (iter == whowas.end())
217                 {
218                         /* this should never happen, if it does maps are corrupt */
219                         ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (2)");
220                         return;
221                 }
222                 whowas_set* n = (whowas_set*)iter->second;
223                 if (n->size())
224                 {
225                         int nickcount = n->size();
226                         while (n->begin() != n->end() && nickcount > groupsize)
227                         {
228                                 WhoWasGroup *a = *(n->begin());
229                                 delete a;
230                                 n->pop_front();
231                                 nickcount--;
232                         }
233                 }
234         }
235 }
236
237 /* call maintain once an hour to remove expired nicks */
238 void CommandWhowas::MaintainWhoWas(time_t t)
239 {
240         for (whowas_users::iterator iter = whowas.begin(); iter != whowas.end(); iter++)
241         {
242                 whowas_set* n = (whowas_set*)iter->second;
243                 if (n->size())
244                 {
245                         while ((n->begin() != n->end()) && ((*n->begin())->signon < t - this->WhoWasMaxKeep))
246                         {
247                                 WhoWasGroup *a = *(n->begin());
248                                 delete a;
249                                 n->erase(n->begin());
250                         }
251                 }
252         }
253 }
254
255 CommandWhowas::~CommandWhowas()
256 {
257         whowas_users::iterator iter;
258         int fifosize;
259         while ((fifosize = (int)whowas_fifo.size()) > 0)
260         {
261                 iter = whowas.find(whowas_fifo[0].second);
262
263                 /* hopefully redundant integrity check, but added while debugging r6216 */
264                 if (iter == whowas.end())
265                 {
266                         /* this should never happen, if it does maps are corrupt */
267                         ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (3)");
268                         return;
269                 }
270
271                 whowas_set* n = (whowas_set*)iter->second;
272
273                 if (n->size())
274                 {
275                         while (n->begin() != n->end())
276                         {
277                                 WhoWasGroup *a = *(n->begin());
278                                 delete a;
279                                 n->pop_front();
280                         }
281                 }
282
283                 delete n;
284                 whowas.erase(iter);
285                 whowas_fifo.pop_front();
286         }
287 }
288
289 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
290         server(user->server), gecos(user->fullname), signon(user->signon)
291 {
292 }
293
294 WhoWasGroup::~WhoWasGroup()
295 {
296 }
297
298 class ModuleWhoWas : public Module
299 {
300         CommandWhowas cmd;
301
302         void RangeCheck(int& value, int min, int max, int def, const char* msg)
303         {
304                 // From ConfigReader
305                 if (value >= min && value <= max)
306                         return;
307
308                 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: %s value of %d is not between %d and %d; set to %d.", msg, value, min, max, def);
309                 value = def;
310         }
311
312  public:
313         ModuleWhoWas() : cmd(this)
314         {
315         }
316
317         void init()
318         {
319                 ServerInstance->Modules->AddService(cmd);
320                 Implementation eventlist[] = { I_OnGarbageCollect, I_OnUserQuit, I_OnStats, I_OnRehash };
321                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
322                 OnRehash(NULL);
323         }
324
325         void OnGarbageCollect()
326         {
327                 /* Removes all entries older than WhoWasMaxKeep */
328                 cmd.MaintainWhoWas(ServerInstance->Time());
329         }
330
331         void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
332         {
333                 cmd.AddToWhoWas(user);
334         }
335
336         ModResult OnStats(char symbol, User* user, string_list &results)
337         {
338                 if (symbol == 'z')
339                         results.push_back(ServerInstance->Config->ServerName+" 249 "+user->nick+" :"+cmd.GetStats());
340
341                 return MOD_RES_PASSTHRU;
342         }
343
344         void OnRehash(User* user)
345         {
346                 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
347                 int NewGroupSize = tag->getInt("groupsize");
348                 int NewMaxGroups = tag->getInt("maxgroups");
349                 int NewMaxKeep = InspIRCd::Duration(tag->getString("maxkeep"));
350
351                 RangeCheck(NewGroupSize, 0, 10000, 10, "<whowas:groupsize>");
352                 RangeCheck(NewMaxGroups, 0, 1000000, 10240, "<whowas:maxgroups>");
353                 RangeCheck(NewMaxKeep, 3600, INT_MAX, 3600, "<whowas:maxkeep>");
354
355                 if ((NewGroupSize == cmd.WhoWasGroupSize) && (NewMaxGroups == cmd.WhoWasMaxGroups) && (NewMaxKeep == cmd.WhoWasMaxKeep))
356                         return;
357
358                 cmd.WhoWasGroupSize = NewGroupSize;
359                 cmd.WhoWasMaxGroups = NewMaxGroups;
360                 cmd.WhoWasMaxKeep = NewMaxKeep;
361                 cmd.PruneWhoWas(ServerInstance->Time());
362         }
363
364         Version GetVersion()
365         {
366                 return Version("WHOWAS", VF_VENDOR);
367         }
368 };
369
370 MODULE_INIT(ModuleWhoWas)