]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
1d8e918aa2b88dd11915a2951b900cad0dc44574
[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 class InspIRCd;
70
71 /** Derived from Resolver, and performs user forward/reverse lookups.
72  */
73 class UserResolver : public Resolver
74 {
75  private:
76         /** User this class is 'attached' to.
77          */
78         userrec* bound_user;
79         int bound_fd;
80         bool fwd;
81  public:
82         UserResolver(InspIRCd* Instance, userrec* user, std::string to_resolve, bool forward);
83
84         void OnLookupComplete(const std::string &result);
85         void OnError(ResolverError e, const std::string &errormessage);
86 };
87
88
89 /** Holds information relevent to &lt;connect allow&gt; and &lt;connect deny&gt; tags in the config file.
90  */
91 class ConnectClass : public classbase
92 {
93  public:
94         /** Type of line, either CC_ALLOW or CC_DENY
95          */
96         char type;
97         /** Max time to register the connection in seconds
98          */
99         int registration_timeout;
100         /** Number of lines in buffer before excess flood is triggered
101          */
102         int flood;
103         /** Host mask for this line
104          */
105         std::string host;
106         /** Number of seconds between pings for this line
107          */
108         int pingtime;
109         /** (Optional) Password for this line
110          */
111         std::string pass;
112
113         /** Threshold value for flood disconnect
114          */
115         int threshold;
116
117         /** Maximum size of sendq for users in this class (bytes)
118          */
119         long sendqmax;
120
121         /** Maximum size of recvq for users in this class (bytes)
122          */
123         long recvqmax;
124
125         /** Local max when connecting by this connection class
126          */
127         long maxlocal;
128
129         /** Global max when connecting by this connection class
130          */
131         long maxglobal;
132         
133         ConnectClass() : registration_timeout(0), flood(0), host(""), pingtime(0), pass(""), threshold(0), sendqmax(0), recvqmax(0)
134         {
135         }
136 };
137
138 /** Holds a complete list of all channels to which a user has been invited and has not yet joined.
139  */
140 typedef std::vector<Invited> InvitedList;
141
142
143
144 /** Holds a complete list of all allow and deny tags from the configuration file (connection classes)
145  */
146 typedef std::vector<ConnectClass> ClassVector;
147
148 /** Typedef for the list of user-channel records for a user
149  */
150 typedef std::vector<ucrec*> UserChanList;
151
152 /** Holds all information about a user
153  * This class stores all information about a user connected to the irc server. Everything about a
154  * connection is stored here primarily, from the user's socket ID (file descriptor) through to the
155  * user's nickname and hostname. Use the Find method of the server class to locate a specific user
156  * by nickname.
157  */
158 class userrec : public connection
159 {
160  private:
161         /** Pointer to creator
162          */
163         InspIRCd* ServerInstance;
164
165         /** A list of channels the user has a pending invite to.
166          */
167         InvitedList invites;
168  public:
169         /** Resolvers for looking up this users hostname
170          */
171         UserResolver* res_forward;
172
173         /** Resolvers for looking up this users hostname
174          */
175         UserResolver* res_reverse;
176
177         /** Stored reverse lookup from res_forward
178          */
179         std::string stored_host;
180
181         /** Starts a DNS lookup of the user's IP.
182          * When complete, sets userrec::dns_done to true.
183          */
184         void StartDNSLookup();
185         
186         /** The users nickname.
187          * An invalid nickname indicates an unregistered connection prior to the NICK command.
188          */
189         
190         char nick[NICKMAX];
191         
192         /** The users ident reply.
193          * Two characters are added to the user-defined limit to compensate for the tilde etc.
194          */
195         char ident[IDENTMAX+2];
196
197         /** The host displayed to non-opers (used for cloaking etc).
198          * This usually matches the value of userrec::host.
199          */
200         char dhost[65];
201         
202         /** The users full name.
203          */
204         char fullname[MAXGECOS+1];
205         
206         /** The user's mode list.
207          * This is NOT a null terminated string! In the 1.1 version of InspIRCd
208          * this is an array of values in a similar way to channel modes.
209          * A value of 1 in field (modeletter-65) indicates that the mode is
210          * set, for example, to work out if mode +s is set, we  check the field
211          * userrec::modes['s'-65] != 0.
212          * The following RFC characters o, w, s, i have constants defined via an
213          * enum, such as UM_SERVERNOTICE and UM_OPETATOR.
214          */
215         char modes[64];
216
217         /** What snomasks are set on this user.
218          * This functions the same as the above modes.
219          */
220         char snomasks[64];
221
222         /** Channels this user is on, and the permissions they have there
223          */
224         UserChanList chans;
225         
226         /** The server the user is connected to.
227          */
228         const char* server;
229         
230         /** The user's away message.
231          * If this string is empty, the user is not marked as away.
232          */
233         char awaymsg[MAXAWAY+1];
234         
235         /** Number of lines the user can place into the buffer
236          * (up to the global NetBufferSize bytes) before they
237          * are disconnected for excess flood
238          */
239         int flood;
240         
241         /** Number of seconds this user is given to send USER/NICK
242          * If they do not send their details in this time limit they
243          * will be disconnected
244          */
245         unsigned int timeout;
246         
247         /** The oper type they logged in as, if they are an oper.
248          * This is used to check permissions in operclasses, so that
249          * we can say 'yay' or 'nay' to any commands they issue.
250          * The value of this is the value of a valid 'type name=' tag.
251          */
252         char oper[NICKMAX];
253
254         /** True when DNS lookups are completed.
255          */
256         bool dns_done;
257
258         /** Number of seconds between PINGs for this user (set from &lt;connect:allow&gt; tag
259          */
260         unsigned int pingmax;
261
262         /** Password specified by the user when they registered.
263          * This is stored even if the <connect> block doesnt need a password, so that
264          * modules may check it.
265          */
266         char password[64];
267
268         /** User's receive queue.
269          * Lines from the IRCd awaiting processing are stored here.
270          * Upgraded april 2005, old system a bit hairy.
271          */
272         std::string recvq;
273
274         /** User's send queue.
275          * Lines waiting to be sent are stored here until their buffer is flushed.
276          */
277         std::string sendq;
278
279         /** Flood counters
280          */
281         int lines_in;
282         time_t reset_due;
283         long threshold;
284
285         /** IPV4 ip address
286          */
287         sockaddr* ip;
288
289         /** Initialize the clients sockaddr
290          * @param protocol_family The protocol family of the IP address, AF_INET or AF_INET6
291          * @param ip A human-readable IP address for this user matching the protcol_family
292          * @param port The port number of this user or zero for a remote user
293          */
294         void SetSockAddr(int protocol_family, const char* ip, int port);
295
296         /** Get port number from sockaddr
297          * @return The port number of this user.
298          */
299         int GetPort();
300
301         /** Get protocol family from sockaddr
302          * @return The protocol family of this user, either AF_INET or AF_INET6
303          */
304         int GetProtocolFamily();
305
306         /** Get IP string from sockaddr, using static internal buffer
307          * @return The IP string
308          */
309         const char* GetIPString();
310
311         /** Get IP string from sockaddr, using caller-specified buffer
312          * @param buf A buffer to use
313          * @return The IP string
314          */
315         const char* GetIPString(char* buf);
316
317         /* Write error string
318          */
319         std::string WriteError;
320
321         /** Maximum size this user's sendq can become
322          */
323         long sendqmax;
324
325         /** Maximum size this user's recvq can become
326          */
327         long recvqmax;
328
329         /** Default constructor
330          * @throw Nothing at present
331          */
332         userrec(InspIRCd* Instance);
333         
334         /** Returns the full displayed host of the user
335          * This member function returns the hostname of the user as seen by other users
336          * on the server, in nick!ident&at;host form.
337          * @return The full masked host of the user
338          */
339         virtual char* GetFullHost();
340         
341         /** Returns the full real host of the user
342          * This member function returns the hostname of the user as seen by other users
343          * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
344          * e.g. through a module, then this method will ignore it and return the true hostname.
345          * @return The full real host of the user
346          */
347         virtual char* GetFullRealHost();
348
349         /** Create a displayable mode string for this users snomasks
350          * @return The notice mask character sequence
351          */
352         const char* FormatNoticeMasks();
353
354         /** Process a snomask modifier string, e.g. +abc-de
355          * @param sm A sequence of notice mask characters
356          * @return True if the notice masks were successfully applied
357          */
358         bool userrec::ProcessNoticeMasks(const char *sm);
359
360         /** Returns true if a notice mask is set
361          * @param sm A notice mask character to check
362          * @return True if the notice mask is set
363          */
364         bool IsNoticeMaskSet(unsigned char sm);
365
366         /** Changed a specific notice mask value
367          * @param sm The server notice mask to change
368          * @param value An on/off value for this mask
369          */
370         void SetNoticeMask(unsigned char sm, bool value);
371
372         /** Create a displayable mode string for this users umodes
373          * @param The mode string
374          */
375         const char* FormatModes();
376
377         /** Returns true if a specific mode is set
378          * @param m The user mode
379          * @return True if the mode is set
380          */
381         bool IsModeSet(unsigned char m);
382
383         /** Set a specific usermode to on or off
384          * @param m The user mode
385          * @param value On or off setting of the mode
386          */
387         void SetMode(unsigned char m, bool value);
388         
389         /** Returns true if a user is invited to a channel.
390          * @param channel A channel name to look up
391          * @return True if the user is invited to the given channel
392          */
393         virtual bool IsInvited(irc::string &channel);
394         
395         /** Adds a channel to a users invite list (invites them to a channel)
396          * @param channel A channel name to add
397          */
398         virtual void InviteTo(irc::string &channel);
399         
400         /** Removes a channel from a users invite list.
401          * This member function is called on successfully joining an invite only channel
402          * to which the user has previously been invited, to clear the invitation.
403          * @param channel The channel to remove the invite to
404          */
405         virtual void RemoveInvite(irc::string &channel);
406         
407         /** Returns true or false for if a user can execute a privilaged oper command.
408          * This is done by looking up their oper type from userrec::oper, then referencing
409          * this to their oper classes and checking the commands they can execute.
410          * @param command A command (should be all CAPS)
411          * @return True if this user can execute the command
412          */
413         bool HasPermission(const std::string &command);
414
415         /** Calls read() to read some data for this user using their fd.
416          * @param buffer The buffer to read into
417          * @param size The size of data to read
418          * @return The number of bytes read, or -1 if an error occured.
419          */
420         int ReadData(void* buffer, size_t size);
421
422         /** This method adds data to the read buffer of the user.
423          * The buffer can grow to any size within limits of the available memory,
424          * managed by the size of a std::string, however if any individual line in
425          * the buffer grows over 600 bytes in length (which is 88 chars over the
426          * RFC-specified limit per line) then the method will return false and the
427          * text will not be inserted.
428          * @param a The string to add to the users read buffer
429          * @return True if the string was successfully added to the read buffer
430          */
431         bool AddBuffer(const std::string &a);
432
433         /** This method returns true if the buffer contains at least one carriage return
434          * character (e.g. one complete line may be read)
435          * @return True if there is at least one complete line in the users buffer
436          */
437         bool BufferIsReady();
438
439         /** This function clears the entire buffer by setting it to an empty string.
440          */
441         void ClearBuffer();
442
443         /** This method returns the first available string at the tail end of the buffer
444          * and advances the tail end of the buffer past the string. This means it is
445          * a one way operation in a similar way to strtok(), and multiple calls return
446          * multiple lines if they are available. The results of this function if there
447          * are no lines to be read are unknown, always use BufferIsReady() to check if
448          * it is ok to read the buffer before calling GetBuffer().
449          * @return The string at the tail end of this users buffer
450          */
451         std::string GetBuffer();
452
453         /** Sets the write error for a connection. This is done because the actual disconnect
454          * of a client may occur at an inopportune time such as half way through /LIST output.
455          * The WriteErrors of clients are checked at a more ideal time (in the mainloop) and
456          * errored clients purged.
457          * @param error The error string to set.
458          */
459         void SetWriteError(const std::string &error);
460
461         /** Returns the write error which last occured on this connection or an empty string
462          * if none occured.
463          * @return The error string which has occured for this user
464          */
465         const char* GetWriteError();
466
467         /** Adds to the user's write buffer.
468          * You may add any amount of text up to this users sendq value, if you exceed the
469          * sendq value, SetWriteError() will be called to set the users error string to
470          * "SendQ exceeded", and further buffer adds will be dropped.
471          * @param data The data to add to the write buffer
472          */
473         void AddWriteBuf(const std::string &data);
474
475         /** Flushes as much of the user's buffer to the file descriptor as possible.
476          * This function may not always flush the entire buffer, rather instead as much of it
477          * as it possibly can. If the send() call fails to send the entire buffer, the buffer
478          * position is advanced forwards and the rest of the data sent at the next call to
479          * this method.
480          */
481         void FlushWriteBuf();
482
483         /** Returns the list of channels this user has been invited to but has not yet joined.
484          * @return A list of channels the user is invited to
485          */
486         InvitedList* GetInviteList();
487
488         /** Creates a wildcard host.
489          * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
490          * @return The wildcarded hostname in *!*@host form
491          */
492         char* MakeWildHost();
493
494         /** Creates a host.
495          * Takes a buffer to use and fills the given buffer with the host in the format nick!user@host
496          * @param Buffer to fill with host information
497          */
498         void MakeHost(char* nhost);
499
500         /** Shuts down and closes the user's socket
501          */
502         void CloseSocket();
503
504         /** Disconnect a user gracefully
505          * @param user The user to remove
506          * @param r The quit reason
507          */
508         static void QuitUser(InspIRCd* Instance, userrec *user, const std::string &r);
509
510         /** Add the user to WHOWAS system
511          */
512         void AddToWhoWas();
513
514         /** Oper up the user using the given opertype.
515          * This will also give the +o usermode.
516          * @param opertype The oper type to oper as
517          */
518         void Oper(const std::string &opertype);
519
520         /** Use this method to fully connect a user.
521          * This will send the message of the day, check G/K/E lines, etc.
522          * @param Goners If the user is disconnected by this method call, the
523          * value of 'this' will be pushed onto this CullList. This is used by
524          * the core to connect many users in rapid succession without invalidating
525          * iterators.
526          */
527         void FullConnect(CullList* Goners);
528
529         /** Change this users hash key to a new string.
530          * You should not call this function directly. It is used by the core
531          * to update the users hash entry on a nickchange.
532          * @param New new user_hash key
533          * @return Pointer to userrec in hash (usually 'this')
534          */
535         userrec* UpdateNickHash(const char* New);
536
537         /** Force a nickname change.
538          * If the nickname change fails (for example, because the nick in question
539          * already exists) this function will return false, and you must then either
540          * output an error message, or quit the user for nickname collision.
541          * @param newnick The nickname to change to
542          * @return True if the nickchange was successful.
543          */
544         bool ForceNickChange(const char* newnick);
545
546         /** Add a client to the system.
547          * This will create a new userrec, insert it into the user_hash,
548          * initialize it as not yet registered, and add it to the socket engine.
549          */
550         static void AddClient(InspIRCd* Instance, int socket, int port, bool iscached, insp_inaddr ip);
551
552         /** Oper down.
553          * This will clear the +o usermode and unset the user's oper type
554          */
555         void UnOper();
556
557         /** Return the number of global clones of this user
558          */
559         long GlobalCloneCount();
560
561         /** Return the number of local clones of this user
562          */
563         long LocalCloneCount();
564
565         /** Write text to this user, appending CR/LF.
566          * @param text A std::string to send to the user
567          */
568         void Write(const std::string &text);
569
570         /** Write text to this user, appending CR/LF.
571          * @param text The format string for text to send to the user
572          * @param ... POD-type format arguments
573          */
574         void Write(const char *text, ...);
575
576         /** Write text to this user, appending CR/LF and prepending :server.name
577          * @param text A std::string to send to the user
578          */
579         void WriteServ(const std::string& text);
580
581         /** Write text to this user, appending CR/LF and prepending :server.name
582          * @param text The format string for text to send to the user
583          * @param ... POD-type format arguments
584          */
585         void WriteServ(const char* text, ...);
586
587         /** Write text to this user, appending CR/LF and prepending :nick!user@host of the user provided in the first parameter.
588          * @param user The user to prepend the :nick!user@host of
589          * @param text A std::string to send to the user
590          */
591         void WriteFrom(userrec *user, const std::string &text);
592
593         /** Write text to this user, appending CR/LF and prepending :nick!user@host of the user provided in the first parameter.
594          * @param user The user to prepend the :nick!user@host of
595          * @param text The format string for text to send to the user
596          * @param ... POD-type format arguments
597          */
598         void WriteFrom(userrec *user, const char* text, ...);
599
600         /** Write text to the user provided in the first parameter, appending CR/LF, and prepending THIS user's :nick!user@host.
601          * @param dest The user to route the message to
602          * @param text A std::string to send to the user
603          */
604         void WriteTo(userrec *dest, const std::string &data);
605
606         /** Write text to the user provided in the first parameter, appending CR/LF, and prepending THIS user's :nick!user@host.
607          * @param dest The user to route the message to
608          * @param text The format string for text to send to the user
609          * @param ... POD-type format arguments
610          */
611         void WriteTo(userrec *dest, const char *data, ...);
612
613         /** Write to all users that can see this user (including this user in the list), appending CR/LF
614          * @param text A std::string to send to the users
615          */
616         void WriteCommon(const std::string &text);
617
618         /** Write to all users that can see this user (including this user in the list), appending CR/LF
619          * @param text The format string for text to send to the users
620          * @param ... POD-type format arguments
621          */
622         void WriteCommon(const char* text, ...);
623
624         /** Write to all users that can see this user (not including this user in the list), appending CR/LF
625          * @param text The format string for text to send to the users
626          * @param ... POD-type format arguments
627          */
628         void WriteCommonExcept(const char* text, ...);
629
630         /** Write to all users that can see this user (not including this user in the list), appending CR/LF
631          * @param text A std::string to send to the users
632          */
633         void WriteCommonExcept(const std::string &text);
634
635         void WriteWallOps(const char* text, ...);
636
637         void WriteWallOps(const std::string &text);
638
639         bool SharesChannelWith(userrec *other);
640
641         bool ChangeDisplayedHost(const char* host);
642
643         bool ChangeName(const char* gecos);
644
645         int CountChannels();
646
647         void NoticeAll(char* text, ...);
648
649         std::string ChannelList(userrec* source);
650
651         void SplitChanList(userrec* dest, const std::string &cl);
652
653         /** Default destructor
654          */
655         virtual ~userrec();
656 };
657
658
659 namespace irc
660 {
661         /** Holds whowas related functions and classes
662          */
663         namespace whowas
664         {
665
666                 /** Used to hold WHOWAS information
667                  */
668                 class WhoWasGroup : public classbase
669                 {
670                  public:
671                         /** Real host
672                          */
673                         char* host;
674                         /** Displayed host
675                          */
676                         char* dhost;
677                         /** Ident
678                          */
679                         char* ident;
680                         /** Server name
681                          */
682                         const char* server;
683                         /** Fullname (GECOS)
684                          */
685                         char* gecos;
686                         /** Signon time
687                          */
688                         time_t signon;
689         
690                         /** Initialize this WhoQasFroup with a user
691                          */
692                         WhoWasGroup(userrec* user);
693                         /** Destructor
694                          */
695                         ~WhoWasGroup();
696                 };
697
698                 /** A group of users related by nickname
699                  */
700                 typedef std::deque<WhoWasGroup*> whowas_set;
701
702                 /** Sets of users in the whowas system
703                  */
704                 typedef std::map<irc::string,whowas_set*> whowas_users;
705
706                 /** Called every hour by the core to remove expired entries
707                  */
708                 void MaintainWhoWas(time_t TIME);
709         };
710 };
711
712 /* Configuration callbacks */
713 class ServerConfig;
714 bool InitTypes(ServerConfig* conf, const char* tag);
715 bool InitClasses(ServerConfig* conf, const char* tag);
716 bool DoType(ServerConfig* conf, const char* tag, char** entries, void** values, int* types);
717 bool DoClass(ServerConfig* conf, const char* tag, char** entries, void** values, int* types);
718 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);
719
720 #endif