]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_whowas.cpp
4308195ecf6d2ac59c33dde2092d1486dff4e3e2
[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(assign(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(irc::string(user->nick.c_str()), 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;
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(std::make_pair(ServerInstance->Time(), ret.first->first));
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                         whowas_users::iterator it = whowas.find(whowas_fifo.front().second);
118                         if (it != whowas.end())
119                         {
120                                 WhoWas::Nick* set = it->second;
121                                 delete set;
122                                 whowas.erase(it);
123                         }
124                         whowas_fifo.pop_front();
125                 }
126         }
127         else
128         {
129                 // We've met this nick before, add a new record to the list
130                 whowas_set* set = &ret.first->second->entries;
131                 set->push_back(new WhoWasGroup(user));
132
133                 // If there are too many records for this nick, remove the oldest (front)
134                 if (set->size() > this->GroupSize)
135                 {
136                         delete set->front();
137                         set->pop_front();
138                 }
139         }
140 }
141
142 /* on rehash, refactor maps according to new conf values */
143 void CommandWhowas::Prune()
144 {
145         time_t min = ServerInstance->Time() - this->MaxKeep;
146
147         /* first cut the list to new size (maxgroups) and also prune entries that are timed out. */
148         while (!whowas_fifo.empty())
149         {
150                 if ((whowas_fifo.size() > this->MaxGroups) || (whowas_fifo.front().first < min))
151                 {
152                         whowas_users::iterator iter = whowas.find(whowas_fifo.front().second);
153
154                         /* hopefully redundant integrity check, but added while debugging r6216 */
155                         if (iter == whowas.end())
156                         {
157                                 /* this should never happen, if it does maps are corrupt */
158                                 ServerInstance->Logs->Log("WHOWAS", LOG_DEFAULT, "BUG: Whowas maps got corrupted! (1)");
159                                 return;
160                         }
161
162                         WhoWas::Nick* nick = iter->second;
163                         delete nick;
164                         whowas.erase(iter);
165                         whowas_fifo.pop_front();
166                 }
167                 else
168                         break;
169         }
170
171         /* Then cut the whowas sets to new size (groupsize) */
172         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
173         {
174                 whowas_set* n = &i->second->entries;
175                 while (n->size() > this->GroupSize)
176                 {
177                         delete n->front();
178                         n->pop_front();
179                 }
180         }
181 }
182
183 /* call maintain once an hour to remove expired nicks */
184 void CommandWhowas::Maintain()
185 {
186         time_t min = ServerInstance->Time() - this->MaxKeep;
187         for (whowas_users::iterator i = whowas.begin(); i != whowas.end(); ++i)
188         {
189                 whowas_set* set = &i->second->entries;
190                 while (!set->empty() && set->front()->signon < min)
191                 {
192                         delete set->front();
193                         set->pop_front();
194                 }
195         }
196 }
197
198 CommandWhowas::~CommandWhowas()
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 WhoWasGroup::WhoWasGroup(User* user) : host(user->host), dhost(user->dhost), ident(user->ident),
208         server(user->server->GetName()), gecos(user->fullname), signon(user->signon)
209 {
210 }
211
212 WhoWas::Nick::Nick()
213         : addtime(ServerInstance->Time())
214 {
215 }
216
217 WhoWas::Nick::~Nick()
218 {
219         stdalgo::delete_all(entries);
220 }
221
222 class ModuleWhoWas : public Module
223 {
224         CommandWhowas cmd;
225
226  public:
227         ModuleWhoWas() : cmd(this)
228         {
229         }
230
231         void OnGarbageCollect()
232         {
233                 // Remove all entries older than MaxKeep
234                 cmd.Maintain();
235         }
236
237         void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
238         {
239                 cmd.AddToWhoWas(user);
240         }
241
242         ModResult OnStats(char symbol, User* user, string_list &results)
243         {
244                 if (symbol == 'z')
245                         results.push_back("249 "+user->nick+" :"+cmd.GetStats());
246
247                 return MOD_RES_PASSTHRU;
248         }
249
250         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
251         {
252                 ConfigTag* tag = ServerInstance->Config->ConfValue("whowas");
253                 unsigned int NewGroupSize = tag->getInt("groupsize", 10, 0, 10000);
254                 unsigned int NewMaxGroups = tag->getInt("maxgroups", 10240, 0, 1000000);
255                 unsigned int NewMaxKeep = tag->getDuration("maxkeep", 3600, 3600);
256
257                 if ((NewGroupSize == cmd.GroupSize) && (NewMaxGroups == cmd.MaxGroups) && (NewMaxKeep == cmd.MaxKeep))
258                         return;
259
260                 cmd.GroupSize = NewGroupSize;
261                 cmd.MaxGroups = NewMaxGroups;
262                 cmd.MaxKeep = NewMaxKeep;
263                 cmd.Prune();
264         }
265
266         Version GetVersion()
267         {
268                 return Version("WHOWAS", VF_VENDOR);
269         }
270 };
271
272 MODULE_INIT(ModuleWhoWas)