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