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