]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/usermanager.h
Extract port binding code to a function and improve output.
[user/henk/code/inspircd.git] / include / usermanager.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 #include <list>
23
24 class CoreExport UserManager : public fakederef<UserManager>
25 {
26  public:
27         struct CloneCounts
28         {
29                 unsigned int global;
30                 unsigned int local;
31                 CloneCounts() : global(0), local(0) { }
32         };
33
34         /** Container that maps IP addresses to clone counts
35          */
36         typedef std::map<irc::sockets::cidr_mask, CloneCounts> CloneMap;
37
38         /** Sequence container in which each element is a User*
39          */
40         typedef std::vector<User*> OperList;
41
42         /** A list holding local users
43         */
44         typedef insp::intrusive_list<LocalUser> LocalList;
45
46  private:
47         /** Map of IP addresses for clone counting
48          */
49         CloneMap clonemap;
50
51         /** A CloneCounts that contains zero for both local and global
52          */
53         const CloneCounts zeroclonecounts;
54
55         /** Local client list, a list containing only local clients
56          */
57         LocalList local_users;
58
59         /** Last used already sent id, used when sending messages to neighbors to help determine whether the message has
60          * been sent to a particular user or not. See User::ForEachNeighbor() for more info.
61          */
62         already_sent_t already_sent_id;
63
64  public:
65         /** Constructor, initializes variables
66          */
67         UserManager();
68
69         /** Destructor, destroys all users in clientlist
70          */
71         ~UserManager();
72
73         /** Nickname string -> User* map. Contains all users, including unregistered ones.
74          */
75         user_hash clientlist;
76
77         /** UUID -> User* map. Contains all users, including unregistered ones.
78          */
79         user_hash uuidlist;
80
81         /** Oper list, a vector containing all local and remote opered users
82          */
83         OperList all_opers;
84
85         /** Number of unregistered users online right now.
86          * (Unregistered means before USER/NICK/dns)
87          */
88         unsigned int unregistered_count;
89
90         /** Perform background user events for all local users such as PING checks, registration timeouts,
91          * penalty management and recvq processing for users who have data in their recvq due to throttling.
92          */
93         void DoBackgroundUserStuff();
94
95         /** Handle a client connection.
96          * Creates a new LocalUser object, inserts it into the appropriate containers,
97          * initializes it as not yet registered, and adds it to the socket engine.
98          *
99          * The new user may immediately be quit after being created, for example if the user limit
100          * is reached or if the user is banned.
101          * @param socket File descriptor of the connection
102          * @param via Listener socket that this user connected to
103          * @param client The IP address and client port of the user
104          * @param server The server IP address and port used by the user
105          */
106         void AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
107
108         /** Disconnect a user gracefully.
109          * When this method returns the user provided will be quit, but the User object will continue to be valid and will be deleted at the end of the current main loop iteration.
110          * @param user The user to remove
111          * @param quitreason The quit reason to show to normal users
112          * @param operreason The quit reason to show to opers, can be NULL if same as quitreason
113          */
114         void QuitUser(User* user, const std::string& quitreason, const std::string* operreason = NULL);
115
116         /** Add a user to the clone map
117          * @param user The user to add
118          */
119         void AddClone(User* user);
120
121         /** Remove all clone counts from the user, you should
122          * use this if you change the user's IP address
123          * after they have registered.
124          * @param user The user to remove
125          */
126         void RemoveCloneCounts(User *user);
127
128         /** Rebuild clone counts. Required when \<cidr> settings change.
129          */
130         void RehashCloneCounts();
131
132         /** Return the number of local and global clones of this user
133          * @param user The user to get the clone counts for
134          * @return The clone counts of this user. The returned reference is volatile - you
135          * must assume that it becomes invalid as soon as you call any function other than
136          * your own.
137          */
138         const CloneCounts& GetCloneCounts(User* user) const;
139
140         /** Return a map containg IP addresses and their clone counts
141          * @return The clone count map
142          */
143         const CloneMap& GetCloneMap() const { return clonemap; }
144
145         /** Return a count of all global users, unknown and known connections
146          * @return The number of users on the network, including local unregistered users
147          */
148         unsigned int UserCount() const { return this->clientlist.size(); }
149
150         /** Return a count of fully registered connections on the network
151          * @return The number of registered users on the network
152          */
153         unsigned int RegisteredUserCount() { return this->clientlist.size() - this->UnregisteredUserCount(); }
154
155         /** Return a count of opered (umode +o) users on the network
156          * @return The number of opers on the network
157          */
158         unsigned int OperCount() const { return this->all_opers.size(); }
159
160         /** Return a count of local unregistered (before NICK/USER) users
161          * @return The number of local unregistered (unknown) connections
162          */
163         unsigned int UnregisteredUserCount() const { return this->unregistered_count; }
164
165         /** Return a count of local registered users
166          * @return The number of registered local users
167          */
168         unsigned int LocalUserCount() const { return (this->local_users.size() - this->UnregisteredUserCount()); }
169
170         /** Get a hash map containing all users, keyed by their nickname
171          * @return A hash map mapping nicknames to User pointers
172          */
173         user_hash& GetUsers() { return clientlist; }
174
175         /** Get a list containing all local users
176          * @return A const list of local users
177          */
178         const LocalList& GetLocalUsers() const { return local_users; }
179
180         /** Send a server notice to all local users
181          * @param text The text format string to send
182          * @param ... The format arguments
183          */
184         void ServerNoticeAll(const char* text, ...) CUSTOM_PRINTF(2, 3);
185
186         /** Retrieves the next already sent id, guaranteed to be not equal to any user's already_sent field
187          * @return Next already_sent id
188          */
189         already_sent_t NextAlreadySentId();
190 };