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