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