]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_whowas.cpp
f456a57db68dd8a877173166c900f7e63b57aea8
[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 enum
27 {
28         // From RFC 1459.
29         RPL_WHOWASUSER = 314,
30         RPL_ENDOFWHOWAS = 369,
31
32         // InspIRCd-specific.
33         RPL_WHOWASIP = 652
34 };
35
36 CommandWhowas::CommandWhowas( Module* parent)
37         : Command(parent, "WHOWAS", 1)
38 {
39         syntax = "<nick>{,<nick>}";
40         Penalty = 2;
41 }
42
43 CmdResult CommandWhowas::Handle (const std::vector<std::string>& parameters, User* user)
44 {
45         /* if whowas disabled in config */
46         if (!manager.IsEnabled())
47         {
48                 user->WriteNumeric(ERR_UNKNOWNCOMMAND, name, "This command has been disabled.");
49                 return CMD_FAILURE;
50         }
51
52         const WhoWas::Nick* const nick = manager.FindNick(parameters[0]);
53         if (!nick)
54         {
55                 user->WriteNumeric(ERR_WASNOSUCHNICK, parameters[0], "There was no such nickname");
56         }
57         else
58         {
59                 const WhoWas::Nick::List& list = nick->entries;
60                 for (WhoWas::Nick::List::const_iterator i = list.begin(); i != list.end(); ++i)
61                 {
62                         WhoWas::Entry* u = *i;
63
64                         user->WriteNumeric(RPL_WHOWASUSER, parameters[0], u->ident, u->dhost, '*', u->gecos);
65
66                         if (user->HasPrivPermission("users/auspex"))
67                                 user->WriteNumeric(RPL_WHOWASIP, parameters[0], InspIRCd::Format("was connecting from *@%s", u->host.c_str()));
68
69                         std::string signon = InspIRCd::TimeString(u->signon);
70                         bool hide_server = (!ServerInstance->Config->HideServer.empty() && !user->HasPrivPermission("servers/auspex"));
71                         user->WriteNumeric(RPL_WHOISSERVER, parameters[0], (hide_server ? ServerInstance->Config->HideServer : u->server), signon);
72                 }
73         }
74
75         user->WriteNumeric(RPL_ENDOFWHOWAS, parameters[0], "End of WHOWAS");
76         return CMD_SUCCESS;
77 }
78
79 WhoWas::Manager::Manager()
80         : GroupSize(0), MaxGroups(0), MaxKeep(0)
81 {
82 }
83
84 const WhoWas::Nick* WhoWas::Manager::FindNick(const std::string& nickname) const
85 {
86         whowas_users::const_iterator it = whowas.find(nickname);
87         if (it == whowas.end())
88                 return NULL;
89         return it->second;
90 }
91
92 WhoWas::Manager::Stats WhoWas::Manager::GetStats() const
93 {
94         size_t entrycount = 0;
95         for (whowas_users::const_iterator i = whowas.begin(); i != whowas.end(); ++i)
96         {
97                 WhoWas::Nick::List& list = i->second->entries;
98                 entrycount += list.size();
99         }
100
101         Stats stats;
102         stats.entrycount = entrycount;
103         return stats;
104 }
105
106 void WhoWas::Manager::Add(User* user)
107 {
108         if (!IsEnabled())
109                 return;
110
111         // Insert nick if it doesn't exist
112         // 'first' will point to the newly inserted element or to the existing element with an equivalent key
113         std::pair<whowas_users::iterator, bool> ret = whowas.insert(std::make_pair(user->nick, static_cast<WhoWas::Nick*>(NULL)));
114
115         if (ret.second) // If inserted
116         {
117                 // This nick is new, create a list for it and add the first record to it
118                 WhoWas::Nick* nick = new WhoWas::Nick(ret.first->first);
119                 nick->entries.push_back(new Entry(user));
120                 ret.first->second = nick;
121
122                 // Add this nick to the fifo too
123                 whowas_fifo.push_back(nick);
124
125                 if (whowas.size() > this->MaxGroups)
126                 {
127                         // Too many nicks, remove the nick which was inserted the longest time ago from both the map and the fifo
128                         PurgeNick(whowas_fifo.front());
129                 }
130         }
131         else
132         {
133                 // We've met this nick before, add a new record to the list
134                 WhoWas::Nick::List& list = ret.first->second->entries;
135                 list.push_back(new Entry(user));
136
137                 // If there are too many records for this nick, remove the oldest (front)
138                 if (list.size() > this->GroupSize)
139                 {
140                         delete list.front();
141                         list.pop_front();
142                 }
143         }
144 }
145
146 /* on rehash, refactor maps according to new conf values */
147 void WhoWas::Manager::Prune()
148 {
149         time_t min = ServerInstance->Time() - this->MaxKeep;
150
151         /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
152         while (!whowas_fifo.empty())
153         {
154                 WhoWas::Nick* nick = whowas_fifo.front();
155                 if ((whowas_fifo.size() > this->MaxGroups) || (nick->addtime < min))
156                         PurgeNick(nick);
157                 else
158                         break;
159         }
160
161         /* Then cut the whowas sets to new size (groupsize) */
162         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); )
163         {
164                 WhoWas::Nick::List& list = i->second->entries;
165                 while (list.size() > this->GroupSize)
166                 {
167                         delete list.front();
168                         list.pop_front();
169                 }
170
171                 if (list.empty())
172                         PurgeNick(i++);
173                 else
174                         ++i;
175         }
176 }
177
178 /* call maintain once an hour to remove expired nicks */
179 void WhoWas::Manager::Maintain()
180 {
181         time_t min = ServerInstance->Time() - this->MaxKeep;
182         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); )
183         {
184                 WhoWas::Nick::List& list = i->second->entries;
185                 while (!list.empty() && list.front()->signon < min)
186                 {
187                         delete list.front();
188                         list.pop_front();
189                 }
190
191                 if (list.empty())
192                         PurgeNick(i++);
193                 else
194                         ++i;
195         }
196 }
197
198 WhoWas::Manager::~Manager()
199 {
200         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
201         {
202                 WhoWas::Nick* nick = i->second;
203                 delete nick;
204         }
205 }
206
207 bool WhoWas::Manager::IsEnabled() const
208 {
209         return ((GroupSize != 0) && (MaxGroups != 0));
210 }
211
212 void WhoWas::Manager::UpdateConfig(unsigned int NewGroupSize, unsigned int NewMaxGroups, unsigned int NewMaxKeep)
213 {
214         if ((NewGroupSize == GroupSize) && (NewMaxGroups == MaxGroups) && (NewMaxKeep == MaxKeep))
215                 return;
216
217         GroupSize = NewGroupSize;
218         MaxGroups = NewMaxGroups;
219         MaxKeep = NewMaxKeep;
220         Prune();
221 }
222
223 void WhoWas::Manager::PurgeNick(whowas_users::iterator it)
224 {
225         WhoWas::Nick* nick = it->second;
226         whowas_fifo.erase(nick);
227         whowas.erase(it);
228         delete nick;
229 }
230
231 void WhoWas::Manager::PurgeNick(WhoWas::Nick* nick)
232 {
233         whowas_users::iterator it = whowas.find(nick->nick);
234         if (it == whowas.end())
235         {
236                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in whowas database, please report");
237                 return;
238         }
239         PurgeNick(it);
240 }
241
242 WhoWas::Entry::Entry(User* user)
243         : host(user->GetRealHost())
244         , dhost(user->GetDisplayedHost())
245         , ident(user->ident)
246         , server(user->server->GetName())
247         , gecos(user->fullname)
248         , signon(user->signon)
249 {
250 }
251
252 WhoWas::Nick::Nick(const std::string& nickname)
253         : addtime(ServerInstance->Time())
254         , nick(nickname)
255 {
256 }
257
258 WhoWas::Nick::~Nick()
259 {
260         stdalgo::delete_all(entries);
261 }
262
263 class ModuleWhoWas : public Module
264 {
265         CommandWhowas cmd;
266
267  public:
268         ModuleWhoWas() : cmd(this)
269         {
270         }
271
272         void OnGarbageCollect() CXX11_OVERRIDE
273         {
274                 // Remove all entries older than MaxKeep
275                 cmd.manager.Maintain();
276         }
277
278         void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
279         {
280                 cmd.manager.Add(user);
281         }
282
283         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
284         {
285                 if (stats.GetSymbol() == 'z')
286                         stats.AddRow(249, "Whowas entries: "+ConvToStr(cmd.manager.GetStats().entrycount));
287
288                 return MOD_RES_PASSTHRU;
289         }
290
291         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
292         {
293                 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
294                 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
295                 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
296                 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
297
298                 cmd.manager.UpdateConfig(NewGroupSize, NewMaxGroups, NewMaxKeep);
299         }
300
301         Version GetVersion() CXX11_OVERRIDE
302         {
303                 return Version("WHOWAS", VF_VENDOR);
304         }
305 };
306
307 MODULE_INIT(ModuleWhoWas)