]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/commands/cmd_whowas.h
core_whowas Split database logic into a WhoWas::Manager class
[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 /* Forward ref for typedefs */
27 class WhoWasGroup;
28
29 namespace WhoWas
30 {
31         /** Everything known about one nick
32          */
33         struct Nick : public intrusive_list_node<Nick>
34         {
35                 /** A group of users related by nickname
36                  */
37                 typedef std::deque<WhoWasGroup*> List;
38
39                 /** Container where each element has information about one occurrence of this nick
40                  */
41                 List entries;
42
43                 /** Time this nick was added to the database
44                  */
45                 const time_t addtime;
46
47                 /** Nickname whose information is stored in this class
48                  */
49                 const std::string nick;
50
51                 /** Constructor to initialize fields
52                  */
53                 Nick(const std::string& nickname);
54
55                 /** Destructor, deallocates all elements in the entries container
56                  */
57                 ~Nick();
58         };
59
60         class Manager
61         {
62          public:
63                 /** Add a user to the whowas database. Called when a user quits.
64                  * @param user The user to add to the database
65                  */
66                 void Add(User* user);
67
68                 /** Retrieves statistics about the whowas database
69                  * @return Whowas statistics
70                  */
71                 std::string GetStats() const;
72
73                 /** Expires old entries
74                  */
75                 void Maintain();
76
77                 /** Updates the current configuration which may result in the database being pruned if the
78                  * new values are lower than the current ones.
79                  * @param NewGroupSize Maximum number of nicks allowed in the database. In case there are this many nicks
80                  * in the database and one more is added, the oldest one is removed (FIFO).
81                  * @param NewMaxGroups Maximum number of entries per nick
82                  * @param NewMaxKeep Seconds how long each nick should be kept
83                  */
84                 void UpdateConfig(unsigned int NewGroupSize, unsigned int NewMaxGroups, unsigned int NewMaxKeep);
85
86                 /** Retrieves all data known about a given nick
87                  * @param nick Nickname to find, case insensitive (IRC casemapping)
88                  * @return A pointer to a WhoWas::Nick if the nick was found, NULL otherwise
89                  */
90                 const Nick* FindNick(const std::string& nick) const;
91
92                 /** Returns true if WHOWAS is enabled according to the current configuration
93                  * @return True if WHOWAS is enabled according to the configuration, false if WHOWAS is disabled
94                  */
95                 bool IsEnabled() const;
96
97                 /** Constructor
98                  */
99                 Manager();
100
101                 /** Destructor
102                  */
103                 ~Manager();
104
105          private:
106                 /** Order in which the users were added into the map, used to remove oldest nick
107                  */
108                 typedef intrusive_list_tail<Nick> FIFO;
109
110                 /** Sets of users in the whowas system
111                  */
112                 typedef TR1NS::unordered_map<std::string, WhoWas::Nick*, irc::insensitive, irc::StrHashComp> whowas_users;
113
114                 /** Primary container, links nicknames tracked by WHOWAS to a list of records
115                  */
116                 whowas_users whowas;
117
118                 /** List of nicknames in the order they were inserted into the map
119                  */
120                 FIFO whowas_fifo;
121
122                 /** Max number of WhoWas entries per user.
123                  */
124                 unsigned int GroupSize;
125
126                 /** Max number of cumulative user-entries in WhoWas.
127                  * When max reached and added to, push out oldest entry FIFO style.
128                  */
129                 unsigned int MaxGroups;
130
131                 /** Max seconds a user is kept in WhoWas before being pruned.
132                  */
133                 unsigned int MaxKeep;
134
135                 /** Shrink all data structures to honor the current settings
136                  */
137                 void Prune();
138         };
139 }
140
141 /** Handle /WHOWAS. These command handlers can be reloaded by the core,
142  * and handle basic RFC1459 commands. Commands within modules work
143  * the same way, however, they can be fully unloaded, where these
144  * may not.
145  */
146 class CommandWhowas : public Command
147 {
148   public:
149         /** Manager handling all whowas database related tasks
150          */
151         WhoWas::Manager manager;
152
153         CommandWhowas(Module* parent);
154         /** Handle command.
155          * @param parameters The parameters to the comamnd
156          * @param pcnt The number of parameters passed to teh command
157          * @param user The user issuing the command
158          * @return A value from CmdResult to indicate command success or failure.
159          */
160         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
161 };
162
163 /** Used to hold WHOWAS information
164  */
165 class WhoWasGroup
166 {
167  public:
168         /** Real host
169          */
170         std::string host;
171         /** Displayed host
172          */
173         std::string dhost;
174         /** Ident
175          */
176         std::string ident;
177         /** Server name
178          */
179         std::string server;
180         /** Fullname (GECOS)
181          */
182         std::string gecos;
183         /** Signon time
184          */
185         time_t signon;
186
187         /** Initialize this WhoWasFroup with a user
188          */
189         WhoWasGroup(User* user);
190 };