]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_whowas.cpp
b406781b1932f17997814e859f5daf9a55bc823e
[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)
28         , GroupSize(0), MaxGroups(0), MaxKeep(0)
29 {
30         syntax = "<nick>{,<nick>}";
31         Penalty = 2;
32 }
33
34 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
35 {
36         /* if whowas disabled in config */
37         if (this->GroupSize == 0 || this->MaxGroups == 0)
38         {
39                 user->WriteNumeric(421, "%s %s :This command has been disabled.",user->nick.c_str(),name.c_str());
40                 return CMD_FAILURE;
41         }
42
43         whowas_users::iterator i = whowas.find(assign(parameters[0]));
44
45         if (i == whowas.end())
46         {
47                 user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
48         }
49         else
50         {
51                 whowas_set* grp = i->second;
52                 if (!grp->empty())
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[25];
60
61                                 timeinfo = localtime(&rawtime);
62
63                                 strncpy(b,asctime(timeinfo),24);
64                                 b[24] = 0;
65
66                                 user->WriteNumeric(314, "%s %s %s %s * :%s",user->nick.c_str(),parameters[0].c_str(),
67                                         u->ident.c_str(),u->dhost.c_str(),u->gecos.c_str());
68
69                                 if (user->HasPrivPermission("users/auspex"))
70                                         user->WriteNumeric(379, "%s %s :was connecting from *@%s",
71                                                 user->nick.c_str(), parameters[0].c_str(), u->host.c_str());
72
73                                 bool hide_server = (!ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"));
74                                 user->WriteNumeric(312, "%s %s %s :%s",user->nick.c_str(), parameters[0].c_str(), (hide_server ? ServerInstance->Config->HideWhoisServer.c_str() : u->server.c_str()), b);
75                         }
76                 }
77                 else
78                 {
79                         user->WriteNumeric(406, "%s %s :There was no such nickname",user->nick.c_str(),parameters[0].c_str());
80                 }
81         }
82
83         user->WriteNumeric(369, "%s %s :End of WHOWAS",user->nick.c_str(),parameters[0].c_str());
84         return CMD_SUCCESS;
85 }
86
87 std::string CommandWhowas::GetStats()
88 {
89         int whowas_size = 0;
90         int whowas_bytes = 0;
91         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
92         {
93                 whowas_set* n = i->second;
94                 whowas_size += n->size();
95                 whowas_bytes += (sizeof(whowas_set) + ( sizeof(WhoWasGroup) * n->size() ) );
96         }
97         return "Whowas entries: " +ConvToStr(whowas_size)+" ("+ConvToStr(whowas_bytes)+" bytes)";
98 }
99
100 void CommandWhowas::AddToWhoWas(User* user)
101 {
102         /* if whowas disabled */
103         if (this->GroupSize == 0 || this->MaxGroups == 0)
104         {
105                 return;
106         }
107
108         // Insert nick if it doesn't exist
109         // 'first' will point to the newly inserted element or to the existing element with an equivalent key
110         std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(irc::string(user->nick.c_str()), static_cast<whowas_set*>(NULL)));
111
112         if (ret.second) // If inserted
113         {
114                 // This nick is new, create a list for it and add the first record to it
115                 whowas_set* n = new whowas_set;
116                 n->push_back(new WhoWasGroup(user));
117                 ret.first->second = n;
118
119                 // Add this nick to the fifo too
120                 whowas_fifo.push_back(std::make_pair(ServerInstance->Time(), ret.first->first));
121
122                 if (whowas.size() > this->MaxGroups)
123                 {
124                         // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
125                         whowas_users::iterator it = whowas.find(whowas_fifo.front().second);
126                         if (it != whowas.end())
127                         {
128                                 whowas_set* set = it->second;
129                                 for (whowas_set::iterator i = set->begin(); i != set->end(); ++i)
130                                         delete *i;
131
132                                 delete set;
133                                 whowas.erase(it);
134                         }
135                         whowas_fifo.pop_front();
136                 }
137         }
138         else
139         {
140                 // We've met this nick before, add a new record to the list
141                 whowas_set* set = ret.first->second;
142                 set->push_back(new WhoWasGroup(user));
143
144                 // If there are too many records for this nick, remove the oldest (front)
145                 if (set->size() > this->GroupSize)
146                 {
147                         delete set->front();
148                         set->pop_front();
149                 }
150         }
151 }
152
153 /* on rehash, refactor maps according to new conf values */
154 void CommandWhowas::Prune()
155 {
156         time_t min = ServerInstance->Time() - this->MaxKeep;
157
158         /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
159         while (!whowas_fifo.empty())
160         {
161                 if ((whowas_fifo.size() > this->MaxGroups) || (whowas_fifo.front().first < min))
162                 {
163                         whowas_users::iterator iter = whowas.find(whowas_fifo.front().second);
164
165                         /* hopefully redundant integrity check, but added while debugging r6216 */
166                         if (iter == whowas.end())
167                         {
168                                 /* this should never happen, if it does maps are corrupt */
169                                 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
170                                 return;
171                         }
172
173                         whowas_set* set = iter->second;
174                         for (whowas_set::iterator i = set->begin(); i != set->end(); ++i)
175                                 delete *i;
176
177                         delete set;
178                         whowas.erase(iter);
179                         whowas_fifo.pop_front();
180                 }
181                 else
182                         break;
183         }
184
185         /* Then cut the whowas sets to new size (groupsize) */
186         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
187         {
188                 whowas_set* n = i->second;
189                 while (n->size() > this->GroupSize)
190                 {
191                         delete n->front();
192                         n->pop_front();
193                 }
194         }
195 }
196
197 /* call maintain once an hour to remove expired nicks */
198 void CommandWhowas::Maintain()
199 {
200         time_t min = ServerInstance->Time() - this->MaxKeep;
201         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
202         {
203                 whowas_set* set = i->second;
204                 while (!set->empty() && set->front()->signon < min)
205                 {
206                         delete set->front();
207                         set->pop_front();
208                 }
209         }
210 }
211
212 CommandWhowas::~CommandWhowas()
213 {
214         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
215         {
216                 whowas_set* set = i->second;
217                 for (whowas_set::iterator j = set->begin(); j != set->end(); ++j)
218                         delete *j;
219
220                 delete set;
221         }
222 }
223
224 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
225         server(user->server), gecos(user->fullname), signon(user->signon)
226 {
227 }
228
229 class ModuleWhoWas : public Module
230 {
231         CommandWhowas cmd;
232
233  public:
234         ModuleWhoWas() : cmd(this)
235         {
236         }
237
238         void init()
239         {
240                 ServerInstance->Modules->AddService(cmd);
241                 OnRehash(NULL);
242         }
243
244         void OnGarbageCollect()
245         {
246                 // Remove all entries older than MaxKeep
247                 cmd.Maintain();
248         }
249
250         void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
251         {
252                 cmd.AddToWhoWas(user);
253         }
254
255         ModResult OnStats(char symbol, User* user, string_list &results)
256         {
257                 if (symbol == 'z')
258                         results.push_back(ServerInstance->Config->ServerName+" 249 "+user->nick+" :"+cmd.GetStats());
259
260                 return MOD_RES_PASSTHRU;
261         }
262
263         void OnRehash(User* user)
264         {
265                 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
266                 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
267                 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
268                 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
269
270                 if ((NewGroupSize == cmd.GroupSize) && (NewMaxGroups == cmd.MaxGroups) && (NewMaxKeep == cmd.MaxKeep))
271                         return;
272
273                 cmd.GroupSize = NewGroupSize;
274                 cmd.MaxGroups = NewMaxGroups;
275                 cmd.MaxKeep = NewMaxKeep;
276                 cmd.Prune();
277         }
278
279         Version GetVersion()
280         {
281                 return Version("WHOWAS", VF_VENDOR);
282         }
283 };
284
285 MODULE_INIT(ModuleWhoWas)