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