]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
30e524e8559d13f0e115aece301f1cdcb815bca3
[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         /** Returns true if a user is invited to a channel.
268          */
269         virtual bool IsInvited(irc::string &channel);
270         
271         /** Adds a channel to a users invite list (invites them to a channel)
272          */
273         virtual void InviteTo(irc::string &channel);
274         
275         /** Removes a channel from a users invite list.
276          * This member function is called on successfully joining an invite only channel
277          * to which the user has previously been invited, to clear the invitation.
278          */
279         virtual void RemoveInvite(irc::string &channel);
280         
281         /** Returns true or false for if a user can execute a privilaged oper command.
282          * This is done by looking up their oper type from userrec::oper, then referencing
283          * this to their oper classes and checking the commands they can execute.
284          */
285         bool HasPermission(const std::string &command);
286
287         /** Calls read() to read some data for this user using their fd.
288          */
289         int ReadData(void* buffer, size_t size);
290
291         /** This method adds data to the buffer of the user.
292          * The buffer can grow to any size within limits of the available memory,
293          * managed by the size of a std::string, however if any individual line in
294          * the buffer grows over 600 bytes in length (which is 88 chars over the
295          * RFC-specified limit per line) then the method will return false and the
296          * text will not be inserted.
297          */
298         bool AddBuffer(const std::string &a);
299
300         /** This method returns true if the buffer contains at least one carriage return
301          * character (e.g. one complete line may be read)
302          */
303         bool BufferIsReady();
304
305         /** This function clears the entire buffer by setting it to an empty string.
306          */
307         void ClearBuffer();
308
309         /** This method returns the first available string at the tail end of the buffer
310          * and advances the tail end of the buffer past the string. This means it is
311          * a one way operation in a similar way to strtok(), and multiple calls return
312          * multiple lines if they are available. The results of this function if there
313          * are no lines to be read are unknown, always use BufferIsReady() to check if
314          * it is ok to read the buffer before calling GetBuffer().
315          */
316         std::string GetBuffer();
317
318         /** Sets the write error for a connection. This is done because the actual disconnect
319          * of a client may occur at an inopportune time such as half way through /LIST output.
320          * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
321          * errored clients purged.
322          */
323         void SetWriteError(const std::string &error);
324
325         /** Returns the write error which last occured on this connection or an empty string
326          * if none occured.
327          */
328         const char* GetWriteError();
329
330         /** Adds to the user's write buffer.
331          * You may add any amount of text up to this users sendq value, if you exceed the
332          * sendq value, SetWriteError() will be called to set the users error string to
333          * "SendQ exceeded", and further buffer adds will be dropped.
334          */
335         void AddWriteBuf(const std::string &data);
336
337         /** Flushes as much of the user's buffer to the file descriptor as possible.
338          * This function may not always flush the entire buffer, rather instead as much of it
339          * as it possibly can. If the send() call fails to send the entire buffer, the buffer
340          * position is advanced forwards and the rest of the data sent at the next call to
341          * this method.
342          */
343         void FlushWriteBuf();
344
345         /** Returns the list of channels this user has been invited to but has not yet joined.
346          */
347         InvitedList* GetInviteList();
348
349         /** Creates a wildcard host.
350          * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
351          */
352         char* MakeWildHost();
353
354         /** Creates a host.
355          * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
356          */
357         void MakeHost(char* nhost);
358
359         /** Shuts down and closes the user's socket
360          */
361         void CloseSocket();
362
363         /** Default destructor
364          */
365         virtual ~userrec();
366
367 #ifdef THREADED_DNS
368         /** Thread used for threaded lookups
369          */
370         pthread_t dnsthread;
371 #endif
372 };
373
374 /** Used to hold WHOWAS information
375  */
376 class WhoWasGroup
377 {
378  public:
379         char* host;
380         char* dhost;
381         char* ident;
382         const char* server;
383         char* gecos;
384         time_t signon;
385
386         WhoWasGroup(userrec* user);
387         ~WhoWasGroup();
388 };
389
390 typedef std::deque<WhoWasGroup*> whowas_set;
391 typedef std::map<irc::string,whowas_set*> whowas_users;
392
393 void AddOper(userrec* user);
394 void DeleteOper(userrec* user);
395 void kill_link(userrec *user,const char* r);
396 void kill_link_silent(userrec *user,const char* r);
397 void AddWhoWas(userrec* u);
398 void MaintainWhoWas(time_t TIME);
399 void AddClient(int socket, int port, bool iscached, in_addr ip4);
400 void FullConnectUser(userrec* user, CullList* Goners);
401 userrec* ReHashNick(char* Old, char* New);
402 void force_nickchange(userrec* user,const char* newnick);
403
404 /* Configuration callbacks */
405 bool InitTypes(const char* tag);
406 bool InitClasses(const char* tag);
407 bool DoType(const char* tag, char** entries, void** values, int* types);
408 bool DoClass(const char* tag, char** entries, void** values, int* types);
409 bool DoneClassesAndTypes(const char* tag);
410
411 long FindMatchingGlobal(userrec* user);
412 long FindMatchingLocal(userrec* user);
413
414 #endif