]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
Fix dns socket leak found in stable
[user/henk/code/inspircd.git] / include / users.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef __USERS_H__ 
18 #define __USERS_H__ 
19
20 #include <string>
21
22 #ifdef THREADED_DNS
23 #include <pthread.h>
24 #endif
25
26 #include "inspircd_config.h" 
27 #include "socket.h"
28 #include "channels.h"
29 #include "inspstring.h"
30 #include "connection.h"
31 #include "hashcomp.h"
32 #include "cull_list.h"
33
34 enum ChanStatus {
35         STATUS_OP     = 4,
36         STATUS_HOP    = 2,
37         STATUS_VOICE  = 1,
38         STATUS_NORMAL = 0
39 };
40
41 enum ClassTypes {
42         CC_ALLOW = 0,
43         CC_DENY  = 1
44 };
45
46 /** RFC1459 channel modes
47  *  */
48 enum UserModes {
49         UM_SERVERNOTICE = 's'-65,
50         UM_WALLOPS = 'w'-65,
51         UM_INVISIBLE = 'i'-65,
52         UM_OPERATOR = 'o'-65,
53 };
54
55 /** Holds a channel name to which a user has been invited.
56  */
57 class Invited : public classbase
58 {
59  public:
60          irc::string channel;
61 };
62
63
64 /** Holds information relevent to &lt;connect allow&gt; and &lt;connect deny&gt; tags in the config file.
65  */
66 class ConnectClass : public classbase
67 {
68  public:
69         /** Type of line, either CC_ALLOW or CC_DENY
70          */
71         char type;
72         /** Max time to register the connection in seconds
73          */
74         int registration_timeout;
75         /** Number of lines in buffer before excess flood is triggered
76          */
77         int flood;
78         /** Host mask for this line
79          */
80         std::string host;
81         /** Number of seconds between pings for this line
82          */
83         int pingtime;
84         /** (Optional) Password for this line
85          */
86         std::string pass;
87
88         /** Threshold value for flood disconnect
89          */
90         int threshold;
91
92         /** Maximum size of sendq for users in this class (bytes)
93          */
94         long sendqmax;
95
96         /** Maximum size of recvq for users in this class (bytes)
97          */
98         long recvqmax;
99
100         /** Local max when connecting by this connection class
101          */
102         long maxlocal;
103
104         /** Global max when connecting by this connection class
105          */
106         long maxglobal;
107         
108         ConnectClass() : registration_timeout(0), flood(0), host(""), pingtime(0), pass(""), threshold(0), sendqmax(0), recvqmax(0)
109         {
110         }
111 };
112
113 /** Holds a complete list of all channels to which a user has been invited and has not yet joined.
114  */
115 typedef std::vector<Invited> InvitedList;
116
117
118
119 /** Holds a complete list of all allow and deny tags from the configuration file (connection classes)
120  */
121 typedef std::vector<ConnectClass> ClassVector;
122
123 /** Typedef for the list of user-channel records for a user
124  */
125 typedef std::vector<ucrec*> UserChanList;
126
127 /** Holds all information about a user
128  * This class stores all information about a user connected to the irc server. Everything about a
129  * connection is stored here primarily, from the user's socket ID (file descriptor) through to the
130  * user's nickname and hostname. Use the Find method of the server class to locate a specific user
131  * by nickname.
132  */
133 class userrec : public connection
134 {
135  private:
136
137         /** A list of channels the user has a pending invite to.
138          */
139         InvitedList invites;
140  public:
141         
142         /** The users nickname.
143          * An invalid nickname indicates an unregistered connection prior to the NICK command.
144          */
145         
146         char nick[NICKMAX];
147         
148         /** The users ident reply.
149          * Two characters are added to the user-defined limit to compensate for the tilde etc.
150          */
151         char ident[IDENTMAX+2];
152
153         /** The host displayed to non-opers (used for cloaking etc).
154          * This usually matches the value of userrec::host.
155          */
156         char dhost[65];
157         
158         /** The users full name.
159          */
160         char fullname[MAXGECOS+1];
161         
162         /** The user's mode list.
163          * This is NOT a null terminated string! In the 1.1 version of InspIRCd
164          * this is an array of values in a similar way to channel modes.
165          * A value of 1 in field (modeletter-65) indicates that the mode is
166          * set, for example, to work out if mode +s is set, we  check the field
167          * userrec::modes['s'-65] != 0.
168          * The following RFC characters o, w, s, i have constants defined via an
169          * enum, such as UM_SERVERNOTICE and UM_OPETATOR.
170          */
171         char modes[64];
172
173         /** What snomasks are set on this user.
174          * This functions the same as the above modes.
175          */
176         char snomasks[64];
177
178         UserChanList chans;
179         
180         /** The server the user is connected to.
181          */
182         const char* server;
183         
184         /** The user's away message.
185          * If this string is empty, the user is not marked as away.
186          */
187         char awaymsg[MAXAWAY+1];
188         
189         /** Number of lines the user can place into the buffer
190          * (up to the global NetBufferSize bytes) before they
191          * are disconnected for excess flood
192          */
193         int flood;
194         
195         /** Number of seconds this user is given to send USER/NICK
196          * If they do not send their details in this time limit they
197          * will be disconnected
198          */
199         unsigned int timeout;
200         
201         /** The oper type they logged in as, if they are an oper.
202          * This is used to check permissions in operclasses, so that
203          * we can say 'yay' or 'nay' to any commands they issue.
204          * The value of this is the value of a valid 'type name=' tag.
205          */
206         char oper[NICKMAX];
207
208         /** True when DNS lookups are completed.
209          */
210         bool dns_done;
211
212         /** Number of seconds between PINGs for this user (set from &lt;connect:allow&gt; tag
213          */
214         unsigned int pingmax;
215
216         /** Password specified by the user when they registered.
217          * This is stored even if the <connect> block doesnt need a password, so that
218          * modules may check it.
219          */
220         char password[64];
221
222         /** User's receive queue.
223          * Lines from the IRCd awaiting processing are stored here.
224          * Upgraded april 2005, old system a bit hairy.
225          */
226         std::string recvq;
227
228         /** User's send queue.
229          * Lines waiting to be sent are stored here until their buffer is flushed.
230          */
231         std::string sendq;
232
233         /** Flood counters
234          */
235         int lines_in;
236         time_t reset_due;
237         long threshold;
238
239         /** IPV4 ip address
240          */
241         in_addr ip4;
242
243         /* Write error string
244          */
245         std::string WriteError;
246
247         /** Maximum size this user's sendq can become
248          */
249         long sendqmax;
250
251         /** Maximum size this user's recvq can become
252          */
253         long recvqmax;
254
255         /** Default constructor
256          */
257         userrec();
258         
259         /** Returns the full displayed host of the user
260          * This member function returns the hostname of the user as seen by other users
261          * on the server, in nick!ident&at;host form.
262          */
263         virtual char* GetFullHost();
264         
265         /** Returns the full real host of the user
266          * This member function returns the hostname of the user as seen by other users
267          * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
268          * e.g. through a module, then this method will ignore it and return the true hostname.
269          */
270         virtual char* GetFullRealHost();
271
272         /*
273          * Create a displayable mode string for this users umodes
274          */
275         const char* FormatNoticeMasks();
276
277         bool userrec::ProcessNoticeMasks(const char *sm);
278
279         bool IsNoticeMaskSet(unsigned char sm);
280
281         void SetNoticeMask(unsigned char sm, bool value);
282
283         /*
284          * Create a displayable mode string for this users umodes
285          */
286         const char* FormatModes();
287
288         bool IsModeSet(unsigned char m);
289
290         void SetMode(unsigned char m, bool value);
291         
292         /** Returns true if a user is invited to a channel.
293          */
294         virtual bool IsInvited(irc::string &channel);
295         
296         /** Adds a channel to a users invite list (invites them to a channel)
297          */
298         virtual void InviteTo(irc::string &channel);
299         
300         /** Removes a channel from a users invite list.
301          * This member function is called on successfully joining an invite only channel
302          * to which the user has previously been invited, to clear the invitation.
303          */
304         virtual void RemoveInvite(irc::string &channel);
305         
306         /** Returns true or false for if a user can execute a privilaged oper command.
307          * This is done by looking up their oper type from userrec::oper, then referencing
308          * this to their oper classes and checking the commands they can execute.
309          */
310         bool HasPermission(const std::string &command);
311
312         /** Calls read() to read some data for this user using their fd.
313          */
314         int ReadData(void* buffer, size_t size);
315
316         /** This method adds data to the buffer of the user.
317          * The buffer can grow to any size within limits of the available memory,
318          * managed by the size of a std::string, however if any individual line in
319          * the buffer grows over 600 bytes in length (which is 88 chars over the
320          * RFC-specified limit per line) then the method will return false and the
321          * text will not be inserted.
322          */
323         bool AddBuffer(const std::string &a);
324
325         /** This method returns true if the buffer contains at least one carriage return
326          * character (e.g. one complete line may be read)
327          */
328         bool BufferIsReady();
329
330         /** This function clears the entire buffer by setting it to an empty string.
331          */
332         void ClearBuffer();
333
334         /** This method returns the first available string at the tail end of the buffer
335          * and advances the tail end of the buffer past the string. This means it is
336          * a one way operation in a similar way to strtok(), and multiple calls return
337          * multiple lines if they are available. The results of this function if there
338          * are no lines to be read are unknown, always use BufferIsReady() to check if
339          * it is ok to read the buffer before calling GetBuffer().
340          */
341         std::string GetBuffer();
342
343         /** Sets the write error for a connection. This is done because the actual disconnect
344          * of a client may occur at an inopportune time such as half way through /LIST output.
345          * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
346          * errored clients purged.
347          */
348         void SetWriteError(const std::string &error);
349
350         /** Returns the write error which last occured on this connection or an empty string
351          * if none occured.
352          */
353         const char* GetWriteError();
354
355         /** Adds to the user's write buffer.
356          * You may add any amount of text up to this users sendq value, if you exceed the
357          * sendq value, SetWriteError() will be called to set the users error string to
358          * "SendQ exceeded", and further buffer adds will be dropped.
359          */
360         void AddWriteBuf(const std::string &data);
361
362         /** Flushes as much of the user's buffer to the file descriptor as possible.
363          * This function may not always flush the entire buffer, rather instead as much of it
364          * as it possibly can. If the send() call fails to send the entire buffer, the buffer
365          * position is advanced forwards and the rest of the data sent at the next call to
366          * this method.
367          */
368         void FlushWriteBuf();
369
370         /** Returns the list of channels this user has been invited to but has not yet joined.
371          */
372         InvitedList* GetInviteList();
373
374         /** Creates a wildcard host.
375          * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
376          */
377         char* MakeWildHost();
378
379         /** Creates a host.
380          * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
381          */
382         void MakeHost(char* nhost);
383
384         /** Shuts down and closes the user's socket
385          */
386         void CloseSocket();
387
388         /** Default destructor
389          */
390         virtual ~userrec();
391
392 #ifdef THREADED_DNS
393         /** Thread used for threaded lookups
394          */
395         pthread_t dnsthread;
396 #else
397         int dns_fd;
398 #endif
399 };
400
401 /** Used to hold WHOWAS information
402  */
403 class WhoWasGroup : public classbase
404 {
405  public:
406         char* host;
407         char* dhost;
408         char* ident;
409         const char* server;
410         char* gecos;
411         time_t signon;
412
413         WhoWasGroup(userrec* user);
414         ~WhoWasGroup();
415 };
416
417 typedef std::deque<WhoWasGroup*> whowas_set;
418 typedef std::map<irc::string,whowas_set*> whowas_users;
419
420 void AddOper(userrec* user);
421 void DeleteOper(userrec* user);
422 void kill_link(userrec *user,const char* r);
423 void kill_link_silent(userrec *user,const char* r);
424 void AddWhoWas(userrec* u);
425 void MaintainWhoWas(time_t TIME);
426 void AddClient(int socket, int port, bool iscached, in_addr ip4);
427 void FullConnectUser(userrec* user, CullList* Goners);
428 userrec* ReHashNick(char* Old, char* New);
429 void force_nickchange(userrec* user,const char* newnick);
430
431 /* Configuration callbacks */
432 bool InitTypes(const char* tag);
433 bool InitClasses(const char* tag);
434 bool DoType(const char* tag, char** entries, void** values, int* types);
435 bool DoClass(const char* tag, char** entries, void** values, int* types);
436 bool DoneClassesAndTypes(const char* tag);
437
438 long FindMatchingGlobal(userrec* user);
439 long FindMatchingLocal(userrec* user);
440
441 #endif