]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/commands/cmd_whowas.h
070858cc504140282a26d2c8457b041d4954ecc7
[user/henk/code/inspircd.git] / include / commands / cmd_whowas.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #pragma once
23
24 #include "modules.h"
25
26 namespace WhoWas
27 {
28         /** One entry for a nick. There may be multiple entries for a nick.
29          */
30         struct Entry
31         {
32                 /** Real host
33                  */
34                 const std::string host;
35
36                 /** Displayed host
37                  */
38                 const std::string dhost;
39
40                 /** Ident
41                  */
42                 const std::string ident;
43
44                 /** Server name
45                  */
46                 const std::string server;
47
48                 /** Full name (GECOS)
49                  */
50                 const std::string gecos;
51
52                 /** Signon time
53                  */
54                 const time_t signon;
55
56                 /** Initialize this Entry with a user
57                  */
58                 Entry(User* user);
59         };
60
61         /** Everything known about one nick
62          */
63         struct Nick : public insp::intrusive_list_node<Nick>
64         {
65                 /** A group of users related by nickname
66                  */
67                 typedef std::deque<Entry*> List;
68
69                 /** Container where each element has information about one occurrence of this nick
70                  */
71                 List entries;
72
73                 /** Time this nick was added to the database
74                  */
75                 const time_t addtime;
76
77                 /** Nickname whose information is stored in this class
78                  */
79                 const std::string nick;
80
81                 /** Constructor to initialize fields
82                  */
83                 Nick(const std::string& nickname);
84
85                 /** Destructor, deallocates all elements in the entries container
86                  */
87                 ~Nick();
88         };
89
90         class Manager
91         {
92          public:
93                 struct Stats
94                 {
95                         /** Number of currently existing WhoWas::Entry objects
96                          */
97                         size_t entrycount;
98                 };
99
100                 /** Add a user to the whowas database. Called when a user quits.
101                  * @param user The user to add to the database
102                  */
103                 void Add(User* user);
104
105                 /** Retrieves statistics about the whowas database
106                  * @return Whowas statistics as a WhoWas::Manager::Stats struct
107                  */
108                 Stats GetStats() const;
109
110                 /** Expires old entries
111                  */
112                 void Maintain();
113
114                 /** Updates the current configuration which may result in the database being pruned if the
115                  * new values are lower than the current ones.
116                  * @param NewGroupSize Maximum number of nicks allowed in the database. In case there are this many nicks
117                  * in the database and one more is added, the oldest one is removed (FIFO).
118                  * @param NewMaxGroups Maximum number of entries per nick
119                  * @param NewMaxKeep Seconds how long each nick should be kept
120                  */
121                 void UpdateConfig(unsigned int NewGroupSize, unsigned int NewMaxGroups, unsigned int NewMaxKeep);
122
123                 /** Retrieves all data known about a given nick
124                  * @param nick Nickname to find, case insensitive (IRC casemapping)
125                  * @return A pointer to a WhoWas::Nick if the nick was found, NULL otherwise
126                  */
127                 const Nick* FindNick(const std::string& nick) const;
128
129                 /** Returns true if WHOWAS is enabled according to the current configuration
130                  * @return True if WHOWAS is enabled according to the configuration, false if WHOWAS is disabled
131                  */
132                 bool IsEnabled() const;
133
134                 /** Constructor
135                  */
136                 Manager();
137
138                 /** Destructor
139                  */
140                 ~Manager();
141
142          private:
143                 /** Order in which the users were added into the map, used to remove oldest nick
144                  */
145                 typedef insp::intrusive_list_tail<Nick> FIFO;
146
147                 /** Sets of users in the whowas system
148                  */
149                 typedef TR1NS::unordered_map<std::string, WhoWas::Nick*, irc::insensitive, irc::StrHashComp> whowas_users;
150
151                 /** Primary container, links nicknames tracked by WHOWAS to a list of records
152                  */
153                 whowas_users whowas;
154
155                 /** List of nicknames in the order they were inserted into the map
156                  */
157                 FIFO whowas_fifo;
158
159                 /** Max number of WhoWas entries per user.
160                  */
161                 unsigned int GroupSize;
162
163                 /** Max number of cumulative user-entries in WhoWas.
164                  * When max reached and added to, push out oldest entry FIFO style.
165                  */
166                 unsigned int MaxGroups;
167
168                 /** Max seconds a user is kept in WhoWas before being pruned.
169                  */
170                 unsigned int MaxKeep;
171
172                 /** Shrink all data structures to honor the current settings
173                  */
174                 void Prune();
175
176                 /** Remove a nick (and all entries belonging to it) from the database
177                  * @param it Iterator to the nick to purge
178                  */
179                 void PurgeNick(whowas_users::iterator it);
180
181                 /** Remove a nick (and all entries belonging to it) from the database
182                  * @param nick Nick to purge
183                  */
184                 void PurgeNick(WhoWas::Nick* nick);
185         };
186 }
187
188 /** Handle /WHOWAS. These command handlers can be reloaded by the core,
189  * and handle basic RFC1459 commands. Commands within modules work
190  * the same way, however, they can be fully unloaded, where these
191  * may not.
192  */
193 class CommandWhowas : public Command
194 {
195   public:
196         /** Manager handling all whowas database related tasks
197          */
198         WhoWas::Manager manager;
199
200         CommandWhowas(Module* parent);
201         /** Handle command.
202          * @param parameters The parameters to the comamnd
203          * @param user The user issuing the command
204          * @return A value from CmdResult to indicate command success or failure.
205          */
206         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
207 };