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