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