]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
30111870de6464f655c3f8135545efcd2a07a4a9
[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
168         /** Resolvers for looking up this users hostname
169          */
170         UserResolver* res_reverse;
171
172         /** Stored reverse lookup from res_forward
173          */
174         std::string stored_host;
175
176         /** Starts a DNS lookup of the user's IP.
177          * When complete, sets userrec::dns_done to true.
178          */
179         void StartDNSLookup();
180         
181         /** The users nickname.
182          * An invalid nickname indicates an unregistered connection prior to the NICK command.
183          */
184         
185         char nick[NICKMAX];
186         
187         /** The users ident reply.
188          * Two characters are added to the user-defined limit to compensate for the tilde etc.
189          */
190         char ident[IDENTMAX+2];
191
192         /** The host displayed to non-opers (used for cloaking etc).
193          * This usually matches the value of userrec::host.
194          */
195         char dhost[65];
196         
197         /** The users full name.
198          */
199         char fullname[MAXGECOS+1];
200         
201         /** The user's mode list.
202          * This is NOT a null terminated string! In the 1.1 version of InspIRCd
203          * this is an array of values in a similar way to channel modes.
204          * A value of 1 in field (modeletter-65) indicates that the mode is
205          * set, for example, to work out if mode +s is set, we  check the field
206          * userrec::modes['s'-65] != 0.
207          * The following RFC characters o, w, s, i have constants defined via an
208          * enum, such as UM_SERVERNOTICE and UM_OPETATOR.
209          */
210         char modes[64];
211
212         /** What snomasks are set on this user.
213          * This functions the same as the above modes.
214          */
215         char snomasks[64];
216
217         /** Channels this user is on, and the permissions they have there
218          */
219         UserChanList chans;
220         
221         /** The server the user is connected to.
222          */
223         const char* server;
224         
225         /** The user's away message.
226          * If this string is empty, the user is not marked as away.
227          */
228         char awaymsg[MAXAWAY+1];
229         
230         /** Number of lines the user can place into the buffer
231          * (up to the global NetBufferSize bytes) before they
232          * are disconnected for excess flood
233          */
234         int flood;
235         
236         /** Number of seconds this user is given to send USER/NICK
237          * If they do not send their details in this time limit they
238          * will be disconnected
239          */
240         unsigned int timeout;
241         
242         /** The oper type they logged in as, if they are an oper.
243          * This is used to check permissions in operclasses, so that
244          * we can say 'yay' or 'nay' to any commands they issue.
245          * The value of this is the value of a valid 'type name=' tag.
246          */
247         char oper[NICKMAX];
248
249         /** True when DNS lookups are completed.
250          */
251         bool dns_done;
252
253         /** Number of seconds between PINGs for this user (set from &lt;connect:allow&gt; tag
254          */
255         unsigned int pingmax;
256
257         /** Password specified by the user when they registered.
258          * This is stored even if the <connect> block doesnt need a password, so that
259          * modules may check it.
260          */
261         char password[64];
262
263         /** User's receive queue.
264          * Lines from the IRCd awaiting processing are stored here.
265          * Upgraded april 2005, old system a bit hairy.
266          */
267         std::string recvq;
268
269         /** User's send queue.
270          * Lines waiting to be sent are stored here until their buffer is flushed.
271          */
272         std::string sendq;
273
274         /** Flood counters
275          */
276         int lines_in;
277         time_t reset_due;
278         long threshold;
279
280         /** IPV4 ip address
281          */
282         sockaddr* ip;
283
284         /** Initialize the clients sockaddr
285          * @param protocol_family The protocol family of the IP address, AF_INET or AF_INET6
286          * @param ip A human-readable IP address for this user matching the protcol_family
287          * @param port The port number of this user or zero for a remote user
288          */
289         void SetSockAddr(int protocol_family, const char* ip, int port);
290
291         /** Get port number from sockaddr
292          * @return The port number of this user.
293          */
294         int GetPort();
295
296         /** Get protocol family from sockaddr
297          * @return The protocol family of this user, either AF_INET or AF_INET6
298          */
299         int GetProtocolFamily();
300
301         /** Get IP string from sockaddr, using static internal buffer
302          * @return The IP string
303          */
304         const char* GetIPString();
305
306         /** Get IP string from sockaddr, using caller-specified buffer
307          * @param buf A buffer to use
308          * @return The IP string
309          */
310         const char* GetIPString(char* buf);
311
312         /* Write error string
313          */
314         std::string WriteError;
315
316         /** Maximum size this user's sendq can become
317          */
318         long sendqmax;
319
320         /** Maximum size this user's recvq can become
321          */
322         long recvqmax;
323
324         /** Default constructor
325          * @throw Nothing at present
326          */
327         userrec();
328         
329         /** Returns the full displayed host of the user
330          * This member function returns the hostname of the user as seen by other users
331          * on the server, in nick!ident&at;host form.
332          * @return The full masked host of the user
333          */
334         virtual char* GetFullHost();
335         
336         /** Returns the full real host of the user
337          * This member function returns the hostname of the user as seen by other users
338          * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
339          * e.g. through a module, then this method will ignore it and return the true hostname.
340          * @return The full real host of the user
341          */
342         virtual char* GetFullRealHost();
343
344         /** Create a displayable mode string for this users snomasks
345          * @return The notice mask character sequence
346          */
347         const char* FormatNoticeMasks();
348
349         /** Process a snomask modifier string, e.g. +abc-de
350          * @param sm A sequence of notice mask characters
351          * @return True if the notice masks were successfully applied
352          */
353         bool userrec::ProcessNoticeMasks(const char *sm);
354
355         /** Returns true if a notice mask is set
356          * @param sm A notice mask character to check
357          * @return True if the notice mask is set
358          */
359         bool IsNoticeMaskSet(unsigned char sm);
360
361         /** Changed a specific notice mask value
362          * @param sm The server notice mask to change
363          * @param value An on/off value for this mask
364          */
365         void SetNoticeMask(unsigned char sm, bool value);
366
367         /** Create a displayable mode string for this users umodes
368          * @param The mode string
369          */
370         const char* FormatModes();
371
372         /** Returns true if a specific mode is set
373          * @param m The user mode
374          * @return True if the mode is set
375          */
376         bool IsModeSet(unsigned char m);
377
378         /** Set a specific usermode to on or off
379          * @param m The user mode
380          * @param value On or off setting of the mode
381          */
382         void SetMode(unsigned char m, bool value);
383         
384         /** Returns true if a user is invited to a channel.
385          * @param channel A channel name to look up
386          * @return True if the user is invited to the given channel
387          */
388         virtual bool IsInvited(irc::string &channel);
389         
390         /** Adds a channel to a users invite list (invites them to a channel)
391          * @param channel A channel name to add
392          */
393         virtual void InviteTo(irc::string &channel);
394         
395         /** Removes a channel from a users invite list.
396          * This member function is called on successfully joining an invite only channel
397          * to which the user has previously been invited, to clear the invitation.
398          * @param channel The channel to remove the invite to
399          */
400         virtual void RemoveInvite(irc::string &channel);
401         
402         /** Returns true or false for if a user can execute a privilaged oper command.
403          * This is done by looking up their oper type from userrec::oper, then referencing
404          * this to their oper classes and checking the commands they can execute.
405          * @param command A command (should be all CAPS)
406          * @return True if this user can execute the command
407          */
408         bool HasPermission(const std::string &command);
409
410         /** Calls read() to read some data for this user using their fd.
411          * @param buffer The buffer to read into
412          * @param size The size of data to read
413          * @return The number of bytes read, or -1 if an error occured.
414          */
415         int ReadData(void* buffer, size_t size);
416
417         /** This method adds data to the read buffer of the user.
418          * The buffer can grow to any size within limits of the available memory,
419          * managed by the size of a std::string, however if any individual line in
420          * the buffer grows over 600 bytes in length (which is 88 chars over the
421          * RFC-specified limit per line) then the method will return false and the
422          * text will not be inserted.
423          * @param a The string to add to the users read buffer
424          * @return True if the string was successfully added to the read buffer
425          */
426         bool AddBuffer(const std::string &a);
427
428         /** This method returns true if the buffer contains at least one carriage return
429          * character (e.g. one complete line may be read)
430          * @return True if there is at least one complete line in the users buffer
431          */
432         bool BufferIsReady();
433
434         /** This function clears the entire buffer by setting it to an empty string.
435          */
436         void ClearBuffer();
437
438         /** This method returns the first available string at the tail end of the buffer
439          * and advances the tail end of the buffer past the string. This means it is
440          * a one way operation in a similar way to strtok(), and multiple calls return
441          * multiple lines if they are available. The results of this function if there
442          * are no lines to be read are unknown, always use BufferIsReady() to check if
443          * it is ok to read the buffer before calling GetBuffer().
444          * @return The string at the tail end of this users buffer
445          */
446         std::string GetBuffer();
447
448         /** Sets the write error for a connection. This is done because the actual disconnect
449          * of a client may occur at an inopportune time such as half way through /LIST output.
450          * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
451          * errored clients purged.
452          * @param error The error string to set.
453          */
454         void SetWriteError(const std::string &error);
455
456         /** Returns the write error which last occured on this connection or an empty string
457          * if none occured.
458          * @return The error string which has occured for this user
459          */
460         const char* GetWriteError();
461
462         /** Adds to the user's write buffer.
463          * You may add any amount of text up to this users sendq value, if you exceed the
464          * sendq value, SetWriteError() will be called to set the users error string to
465          * "SendQ exceeded", and further buffer adds will be dropped.
466          * @param data The data to add to the write buffer
467          */
468         void AddWriteBuf(const std::string &data);
469
470         /** Flushes as much of the user's buffer to the file descriptor as possible.
471          * This function may not always flush the entire buffer, rather instead as much of it
472          * as it possibly can. If the send() call fails to send the entire buffer, the buffer
473          * position is advanced forwards and the rest of the data sent at the next call to
474          * this method.
475          */
476         void FlushWriteBuf();
477
478         /** Returns the list of channels this user has been invited to but has not yet joined.
479          * @return A list of channels the user is invited to
480          */
481         InvitedList* GetInviteList();
482
483         /** Creates a wildcard host.
484          * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
485          * @return The wildcarded hostname in *!*@host form
486          */
487         char* MakeWildHost();
488
489         /** Creates a host.
490          * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
491          * @param Buffer to fill with host information
492          */
493         void MakeHost(char* nhost);
494
495         /** Shuts down and closes the user's socket
496          */
497         void CloseSocket();
498
499         /** Disconnect a user gracefully
500          * @param user The user to remove
501          * @param r The quit reason
502          */
503         static void QuitUser(userrec *user, const std::string &r);
504
505         /** Add the user to WHOWAS system
506          */
507         void AddToWhoWas();
508
509         /** Oper up the user using the given opertype.
510          * This will also give the +o usermode.
511          * @param opertype The oper type to oper as
512          */
513         void Oper(const std::string &opertype);
514
515         /** Use this method to fully connect a user.
516          * This will send the message of the day, check G/K/E lines, etc.
517          * @param Goners If the user is disconnected by this method call, the
518          * value of 'this' will be pushed onto this CullList. This is used by
519          * the core to connect many users in rapid succession without invalidating
520          * iterators.
521          */
522         void FullConnect(CullList* Goners);
523
524         /** Change this users hash key to a new string.
525          * You should not call this function directly. It is used by the core
526          * to update the users hash entry on a nickchange.
527          * @param New new user_hash key
528          * @return Pointer to userrec in hash (usually 'this')
529          */
530         userrec* UpdateNickHash(const char* New);
531
532         /** Force a nickname change.
533          * If the nickname change fails (for example, because the nick in question
534          * already exists) this function will return false, and you must then either
535          * output an error message, or quit the user for nickname collision.
536          * @param newnick The nickname to change to
537          * @return True if the nickchange was successful.
538          */
539         bool ForceNickChange(const char* newnick);
540
541         /** Add a client to the system.
542          * This will create a new userrec, insert it into the user_hash,
543          * initialize it as not yet registered, and add it to the socket engine.
544          */
545         static void AddClient(int socket, int port, bool iscached, insp_inaddr ip);
546
547         /** Oper down.
548          * This will clear the +o usermode and unset the user's oper type
549          */
550         void UnOper();
551
552         /** Return the number of global clones of this user
553          */
554         long GlobalCloneCount();
555
556         /** Return the number of local clones of this user
557          */
558         long LocalCloneCount();
559
560         /** Default destructor
561          */
562         virtual ~userrec();
563 };
564
565
566 namespace irc
567 {
568         /** Holds whowas related functions and classes
569          */
570         namespace whowas
571         {
572
573                 /** Used to hold WHOWAS information
574                  */
575                 class WhoWasGroup : public classbase
576                 {
577                  public:
578                         /** Real host
579                          */
580                         char* host;
581                         /** Displayed host
582                          */
583                         char* dhost;
584                         /** Ident
585                          */
586                         char* ident;
587                         /** Server name
588                          */
589                         const char* server;
590                         /** Fullname (GECOS)
591                          */
592                         char* gecos;
593                         /** Signon time
594                          */
595                         time_t signon;
596         
597                         /** Initialize this WhoQasFroup with a user
598                          */
599                         WhoWasGroup(userrec* user);
600                         /** Destructor
601                          */
602                         ~WhoWasGroup();
603                 };
604
605                 /** A group of users related by nickname
606                  */
607                 typedef std::deque<WhoWasGroup*> whowas_set;
608
609                 /** Sets of users in the whowas system
610                  */
611                 typedef std::map<irc::string,whowas_set*> whowas_users;
612
613                 /** Called every hour by the core to remove expired entries
614                  */
615                 void MaintainWhoWas(time_t TIME);
616         };
617 };
618
619 /* Configuration callbacks */
620 bool InitTypes(const char* tag);
621 bool InitClasses(const char* tag);
622 bool DoType(const char* tag, char** entries, void** values, int* types);
623 bool DoClass(const char* tag, char** entries, void** values, int* types);
624 bool DoneClassesAndTypes(const char* tag);
625
626 #endif