]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
383439c0bab9ccf8dc48d7fafc63f06006d07a7b
[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 ChannelModes {
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         UserChanList chans;
174         
175         /** The server the user is connected to.
176          */
177         const char* server;
178         
179         /** The user's away message.
180          * If this string is empty, the user is not marked as away.
181          */
182         char awaymsg[MAXAWAY+1];
183         
184         /** Number of lines the user can place into the buffer
185          * (up to the global NetBufferSize bytes) before they
186          * are disconnected for excess flood
187          */
188         int flood;
189         
190         /** Number of seconds this user is given to send USER/NICK
191          * If they do not send their details in this time limit they
192          * will be disconnected
193          */
194         unsigned int timeout;
195         
196         /** The oper type they logged in as, if they are an oper.
197          * This is used to check permissions in operclasses, so that
198          * we can say 'yay' or 'nay' to any commands they issue.
199          * The value of this is the value of a valid 'type name=' tag.
200          */
201         char oper[NICKMAX];
202
203         /** True when DNS lookups are completed.
204          */
205         bool dns_done;
206
207         /** Number of seconds between PINGs for this user (set from &lt;connect:allow&gt; tag
208          */
209         unsigned int pingmax;
210
211         /** Password specified by the user when they registered.
212          * This is stored even if the <connect> block doesnt need a password, so that
213          * modules may check it.
214          */
215         char password[64];
216
217         /** User's receive queue.
218          * Lines from the IRCd awaiting processing are stored here.
219          * Upgraded april 2005, old system a bit hairy.
220          */
221         std::string recvq;
222
223         /** User's send queue.
224          * Lines waiting to be sent are stored here until their buffer is flushed.
225          */
226         std::string sendq;
227
228         /** Flood counters
229          */
230         int lines_in;
231         time_t reset_due;
232         long threshold;
233
234         /** IPV4 ip address
235          */
236         in_addr ip4;
237
238         /* Write error string
239          */
240         std::string WriteError;
241
242         /** Maximum size this user's sendq can become
243          */
244         long sendqmax;
245
246         /** Maximum size this user's recvq can become
247          */
248         long recvqmax;
249
250         /** Default constructor
251          */
252         userrec();
253         
254         /** Returns the full displayed host of the user
255          * This member function returns the hostname of the user as seen by other users
256          * on the server, in nick!ident&at;host form.
257          */
258         virtual char* GetFullHost();
259         
260         /** Returns the full real host of the user
261          * This member function returns the hostname of the user as seen by other users
262          * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
263          * e.g. through a module, then this method will ignore it and return the true hostname.
264          */
265         virtual char* GetFullRealHost();
266
267         /*
268          * Create a displayable mode string for this users umodes
269          */
270         const char* FormatModes();
271         
272         /** Returns true if a user is invited to a channel.
273          */
274         virtual bool IsInvited(irc::string &channel);
275         
276         /** Adds a channel to a users invite list (invites them to a channel)
277          */
278         virtual void InviteTo(irc::string &channel);
279         
280         /** Removes a channel from a users invite list.
281          * This member function is called on successfully joining an invite only channel
282          * to which the user has previously been invited, to clear the invitation.
283          */
284         virtual void RemoveInvite(irc::string &channel);
285         
286         /** Returns true or false for if a user can execute a privilaged oper command.
287          * This is done by looking up their oper type from userrec::oper, then referencing
288          * this to their oper classes and checking the commands they can execute.
289          */
290         bool HasPermission(const std::string &command);
291
292         /** Calls read() to read some data for this user using their fd.
293          */
294         int ReadData(void* buffer, size_t size);
295
296         /** This method adds data to the buffer of the user.
297          * The buffer can grow to any size within limits of the available memory,
298          * managed by the size of a std::string, however if any individual line in
299          * the buffer grows over 600 bytes in length (which is 88 chars over the
300          * RFC-specified limit per line) then the method will return false and the
301          * text will not be inserted.
302          */
303         bool AddBuffer(const std::string &a);
304
305         /** This method returns true if the buffer contains at least one carriage return
306          * character (e.g. one complete line may be read)
307          */
308         bool BufferIsReady();
309
310         /** This function clears the entire buffer by setting it to an empty string.
311          */
312         void ClearBuffer();
313
314         /** This method returns the first available string at the tail end of the buffer
315          * and advances the tail end of the buffer past the string. This means it is
316          * a one way operation in a similar way to strtok(), and multiple calls return
317          * multiple lines if they are available. The results of this function if there
318          * are no lines to be read are unknown, always use BufferIsReady() to check if
319          * it is ok to read the buffer before calling GetBuffer().
320          */
321         std::string GetBuffer();
322
323         /** Sets the write error for a connection. This is done because the actual disconnect
324          * of a client may occur at an inopportune time such as half way through /LIST output.
325          * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
326          * errored clients purged.
327          */
328         void SetWriteError(const std::string &error);
329
330         /** Returns the write error which last occured on this connection or an empty string
331          * if none occured.
332          */
333         const char* GetWriteError();
334
335         /** Adds to the user's write buffer.
336          * You may add any amount of text up to this users sendq value, if you exceed the
337          * sendq value, SetWriteError() will be called to set the users error string to
338          * "SendQ exceeded", and further buffer adds will be dropped.
339          */
340         void AddWriteBuf(const std::string &data);
341
342         /** Flushes as much of the user's buffer to the file descriptor as possible.
343          * This function may not always flush the entire buffer, rather instead as much of it
344          * as it possibly can. If the send() call fails to send the entire buffer, the buffer
345          * position is advanced forwards and the rest of the data sent at the next call to
346          * this method.
347          */
348         void FlushWriteBuf();
349
350         /** Returns the list of channels this user has been invited to but has not yet joined.
351          */
352         InvitedList* GetInviteList();
353
354         /** Creates a wildcard host.
355          * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
356          */
357         char* MakeWildHost();
358
359         /** Creates a host.
360          * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
361          */
362         void MakeHost(char* nhost);
363
364         /** Shuts down and closes the user's socket
365          */
366         void CloseSocket();
367
368         /** Default destructor
369          */
370         virtual ~userrec();
371
372 #ifdef THREADED_DNS
373         /** Thread used for threaded lookups
374          */
375         pthread_t dnsthread;
376 #endif
377 };
378
379 /** Used to hold WHOWAS information
380  */
381 class WhoWasGroup
382 {
383  public:
384         char* host;
385         char* dhost;
386         char* ident;
387         const char* server;
388         char* gecos;
389         time_t signon;
390
391         WhoWasGroup(userrec* user);
392         ~WhoWasGroup();
393 };
394
395 typedef std::deque<WhoWasGroup*> whowas_set;
396 typedef std::map<irc::string,whowas_set*> whowas_users;
397
398 void AddOper(userrec* user);
399 void DeleteOper(userrec* user);
400 void kill_link(userrec *user,const char* r);
401 void kill_link_silent(userrec *user,const char* r);
402 void AddWhoWas(userrec* u);
403 void MaintainWhoWas(time_t TIME);
404 void AddClient(int socket, int port, bool iscached, in_addr ip4);
405 void FullConnectUser(userrec* user, CullList* Goners);
406 userrec* ReHashNick(char* Old, char* New);
407 void force_nickchange(userrec* user,const char* newnick);
408
409 /* Configuration callbacks */
410 bool InitTypes(const char* tag);
411 bool InitClasses(const char* tag);
412 bool DoType(const char* tag, char** entries, void** values, int* types);
413 bool DoClass(const char* tag, char** entries, void** values, int* types);
414 bool DoneClassesAndTypes(const char* tag);
415
416 long FindMatchingGlobal(userrec* user);
417 long FindMatchingLocal(userrec* user);
418
419 #endif