]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
Fix recursion of QuitUser in SendQ quits
[user/henk/code/inspircd.git] / include / users.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __USERS_H__
15 #define __USERS_H__
16
17 #include "socket.h"
18 #include "inspsocket.h"
19 #include "dns.h"
20 #include "mode.h"
21
22 /** connect class types
23  */
24 enum ClassTypes {
25         /** connect:allow */
26         CC_ALLOW = 0,
27         /** connect:deny */
28         CC_DENY  = 1,
29         /** named connect block (for opers, etc) */
30         CC_NAMED = 2
31 };
32
33 /** RFC1459 channel modes
34  */
35 enum UserModes {
36         /** +s: Server notice mask */
37         UM_SNOMASK = 's' - 65,
38         /** +w: WALLOPS */
39         UM_WALLOPS = 'w' - 65,
40         /** +i: Invisible */
41         UM_INVISIBLE = 'i' - 65,
42         /** +o: Operator */
43         UM_OPERATOR = 'o' - 65
44 };
45
46 /** Registration state of a user, e.g.
47  * have they sent USER, NICK, PASS yet?
48  */
49 enum RegistrationState {
50
51 #ifndef WIN32   // Burlex: This is already defined in win32, luckily it is still 0.
52         REG_NONE = 0,           /* Has sent nothing */
53 #endif
54
55         REG_USER = 1,           /* Has sent USER */
56         REG_NICK = 2,           /* Has sent NICK */
57         REG_NICKUSER = 3,       /* Bitwise combination of REG_NICK and REG_USER */
58         REG_ALL = 7             /* REG_NICKUSER plus next bit along */
59 };
60
61 enum UserType {
62         USERTYPE_LOCAL = 1,
63         USERTYPE_REMOTE = 2,
64         USERTYPE_SERVER = 3
65 };
66
67 /** Holds information relevent to <connect allow> and <connect deny> tags in the config file.
68  */
69 struct CoreExport ConnectClass : public refcountbase
70 {
71         reference<ConfigTag> config;
72         /** Type of line, either CC_ALLOW or CC_DENY
73          */
74         char type;
75
76         /** True if this class uses fake lag to manage flood, false if it kills */
77         bool fakelag;
78
79         /** Connect class name
80          */
81         std::string name;
82
83         /** Max time to register the connection in seconds
84          */
85         unsigned int registration_timeout;
86
87         /** Host mask for this line
88          */
89         std::string host;
90
91         /** Number of seconds between pings for this line
92          */
93         unsigned int pingtime;
94
95         /** Maximum size of sendq for users in this class (bytes)
96          * Users cannot send commands if they go over this limit
97          */
98         unsigned long softsendqmax;
99
100         /** Maximum size of sendq for users in this class (bytes)
101          * Users are killed if they go over this limit
102          */
103         unsigned long hardsendqmax;
104
105         /** Maximum size of recvq for users in this class (bytes)
106          */
107         unsigned long recvqmax;
108
109         /** Seconds worth of penalty before penalty system activates
110          */
111         unsigned int penaltythreshold;
112
113         /** Maximum rate of commands (units: millicommands per second) */
114         unsigned int commandrate;
115
116         /** Local max when connecting by this connection class
117          */
118         unsigned long maxlocal;
119
120         /** Global max when connecting by this connection class
121          */
122         unsigned long maxglobal;
123
124         /** Max channels for this class
125          */
126         unsigned int maxchans;
127
128         /** How many users may be in this connect class before they are refused?
129          * (0 = no limit = default)
130          */
131         unsigned long limit;
132
133         /** Create a new connect class with no settings.
134          */
135         ConnectClass(ConfigTag* tag, char type, const std::string& mask);
136         /** Create a new connect class with inherited settings.
137          */
138         ConnectClass(ConfigTag* tag, char type, const std::string& mask, const ConnectClass& parent);
139
140         /** Update the settings in this block to match the given block */
141         void Update(const ConnectClass* newSettings);
142
143         const std::string& GetName() { return name; }
144         const std::string& GetHost() { return host; }
145
146         /** Returns the registration timeout
147          */
148         time_t GetRegTimeout()
149         {
150                 return (registration_timeout ? registration_timeout : 90);
151         }
152
153         /** Returns the ping frequency
154          */
155         unsigned int GetPingTime()
156         {
157                 return (pingtime ? pingtime : 120);
158         }
159
160         /** Returns the maximum sendq value (soft limit)
161          * Note that this is in addition to internal OS buffers
162          */
163         unsigned long GetSendqSoftMax()
164         {
165                 return (softsendqmax ? softsendqmax : 4096);
166         }
167
168         /** Returns the maximum sendq value (hard limit)
169          */
170         unsigned long GetSendqHardMax()
171         {
172                 return (hardsendqmax ? hardsendqmax : 0x100000);
173         }
174
175         /** Returns the maximum recvq value
176          */
177         unsigned long GetRecvqMax()
178         {
179                 return (recvqmax ? recvqmax : 4096);
180         }
181
182         /** Returns the penalty threshold value
183          */
184         unsigned int GetPenaltyThreshold()
185         {
186                 return penaltythreshold ? penaltythreshold : (fakelag ? 10 : 20);
187         }
188
189         unsigned int GetCommandRate()
190         {
191                 return commandrate ? commandrate : 1000;
192         }
193
194         /** Return the maximum number of local sessions
195          */
196         unsigned long GetMaxLocal()
197         {
198                 return maxlocal;
199         }
200
201         /** Returns the maximum number of global sessions
202          */
203         unsigned long GetMaxGlobal()
204         {
205                 return maxglobal;
206         }
207 };
208
209 /** Holds all information about a user
210  * This class stores all information about a user connected to the irc server. Everything about a
211  * connection is stored here primarily, from the user's socket ID (file descriptor) through to the
212  * user's nickname and hostname.
213  */
214 class CoreExport User : public Extensible
215 {
216  private:
217         /** Cached nick!ident@dhost value using the displayed hostname
218          */
219         std::string cached_fullhost;
220
221         /** Cached ident@ip value using the real IP address
222          */
223         std::string cached_hostip;
224
225         /** Cached ident@realhost value using the real hostname
226          */
227         std::string cached_makehost;
228
229         /** Cached nick!ident@realhost value using the real hostname
230          */
231         std::string cached_fullrealhost;
232
233         /** Set by GetIPString() to avoid constantly re-grabbing IP via sockets voodoo.
234          */
235         std::string cachedip;
236
237  public:
238
239         /** Hostname of connection.
240          * This should be valid as per RFC1035.
241          */
242         std::string host;
243
244         /** Time that the object was instantiated (used for TS calculation etc)
245         */
246         time_t age;
247
248         /** Time the connection was created, set in the constructor. This
249          * may be different from the time the user's classbase object was
250          * created.
251          */
252         time_t signon;
253
254         /** Time that the connection last sent a message, used to calculate idle time
255          */
256         time_t idle_lastmsg;
257
258         /** Client address that the user is connected from.
259          * Do not modify this value directly, use SetClientIP() to change it.
260          * Port is not valid for remote users.
261          */
262         irc::sockets::sockaddrs client_sa;
263
264         /** The users nickname.
265          * An invalid nickname indicates an unregistered connection prior to the NICK command.
266          * Use InspIRCd::IsNick() to validate nicknames.
267          */
268         std::string nick;
269
270         /** The user's unique identifier.
271          * This is the unique identifier which the user has across the network.
272          */
273         const std::string uuid;
274
275         /** The users ident reply.
276          * Two characters are added to the user-defined limit to compensate for the tilde etc.
277          */
278         std::string ident;
279
280         /** The host displayed to non-opers (used for cloaking etc).
281          * This usually matches the value of User::host.
282          */
283         std::string dhost;
284
285         /** The users full name (GECOS).
286          */
287         std::string fullname;
288
289         /** The user's mode list.
290          * NOT a null terminated string.
291          * Also NOT an array.
292          * Much love to the STL for giving us an easy to use bitset, saving us RAM.
293          * if (modes[modeletter-65]) is set, then the mode is
294          * set, for example, to work out if mode +s is set, we  check the field
295          * User::modes['s'-65] != 0.
296          * The following RFC characters o, w, s, i have constants defined via an
297          * enum, such as UM_SERVERNOTICE and UM_OPETATOR.
298          */
299         std::bitset<64> modes;
300
301         /** What snomasks are set on this user.
302          * This functions the same as the above modes.
303          */
304         std::bitset<64> snomasks;
305
306         /** Channels this user is on
307          */
308         UserChanList chans;
309
310         /** The server the user is connected to.
311          */
312         const std::string server;
313
314         /** The user's away message.
315          * If this string is empty, the user is not marked as away.
316          */
317         std::string awaymsg;
318
319         /** Time the user last went away.
320          * This is ONLY RELIABLE if user IS_AWAY()!
321          */
322         time_t awaytime;
323
324         /** The oper type they logged in as, if they are an oper.
325          */
326         reference<OperInfo> oper;
327
328         /** Used by User to indicate the registration status of the connection
329          * It is a bitfield of the REG_NICK, REG_USER and REG_ALL bits to indicate
330          * the connection state.
331          */
332         unsigned int registered:3;
333
334         /** True when DNS lookups are completed.
335          * The UserResolver classes res_forward and res_reverse will
336          * set this value once they complete.
337          */
338         unsigned int dns_done:1;
339
340         /** Whether or not to send an snotice about this user's quitting
341          */
342         unsigned int quietquit:1;
343
344         /** If this is set to true, then all socket operations for the user
345          * are dropped into the bit-bucket.
346          * This value is set by QuitUser, and is not needed seperately from that call.
347          * Please note that setting this value alone will NOT cause the user to quit.
348          */
349         unsigned int quitting:1;
350
351         /** Recursion fix: user is out of SendQ and will be quit as soon as possible.
352          * This can't be handled normally because QuitUser itself calls Write on other
353          * users, which could trigger their SendQ to overrun.
354          */
355         unsigned int quitting_sendq:1;
356
357         /** This is true if the user matched an exception (E:Line). It is used to save time on ban checks.
358          */
359         unsigned int exempt:1;
360
361         /** has the user responded to their previous ping?
362          */
363         unsigned int lastping:1;
364
365         /** What type of user is this? */
366         const unsigned int usertype:2;
367
368         /** Get client IP string from sockaddr, using static internal buffer
369          * @return The IP string
370          */
371         const char* GetIPString();
372
373         /** Get CIDR mask, using default range, for this user
374          */
375         irc::sockets::cidr_mask GetCIDRMask();
376
377         /** Sets the client IP for this user
378          * @return true if the conversion was successful
379          */
380         bool SetClientIP(const char* sip);
381
382         /** Constructor
383          * @throw CoreException if the UID allocated to the user already exists
384          */
385         User(const std::string &uid, const std::string& srv, int objtype);
386
387         /** Check if the user matches a G or K line, and disconnect them if they do.
388          * @param doZline True if ZLines should be checked (if IP has changed since initial connect)
389          * Returns true if the user matched a ban, false else.
390          */
391         bool CheckLines(bool doZline = false);
392
393         /** Returns the full displayed host of the user
394          * This member function returns the hostname of the user as seen by other users
395          * on the server, in nick!ident&at;host form.
396          * @return The full masked host of the user
397          */
398         virtual const std::string& GetFullHost();
399
400         /** Returns the full real host of the user
401          * This member function returns the hostname of the user as seen by other users
402          * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
403          * e.g. through a module, then this method will ignore it and return the true hostname.
404          * @return The full real host of the user
405          */
406         virtual const std::string& GetFullRealHost();
407
408         /** This clears any cached results that are used for GetFullRealHost() etc.
409          * The results of these calls are cached as generating them can be generally expensive.
410          */
411         void InvalidateCache();
412
413         /** Create a displayable mode string for this users snomasks
414          * @return The notice mask character sequence
415          */
416         const char* FormatNoticeMasks();
417
418         /** Process a snomask modifier string, e.g. +abc-de
419          * @param sm A sequence of notice mask characters
420          * @return The cleaned mode sequence which can be output,
421          * e.g. in the above example if masks c and e are not
422          * valid, this function will return +ab-d
423          */
424         std::string ProcessNoticeMasks(const char *sm);
425
426         /** Returns true if a notice mask is set
427          * @param sm A notice mask character to check
428          * @return True if the notice mask is set
429          */
430         bool IsNoticeMaskSet(unsigned char sm);
431
432         /** Changed a specific notice mask value
433          * @param sm The server notice mask to change
434          * @param value An on/off value for this mask
435          */
436         void SetNoticeMask(unsigned char sm, bool value);
437
438         /** Create a displayable mode string for this users umodes
439          * @param The mode string
440          */
441         const char* FormatModes(bool showparameters = false);
442
443         /** Returns true if a specific mode is set
444          * @param m The user mode
445          * @return True if the mode is set
446          */
447         bool IsModeSet(unsigned char m);
448
449         /** Set a specific usermode to on or off
450          * @param m The user mode
451          * @param value On or off setting of the mode
452          */
453         void SetMode(unsigned char m, bool value);
454
455         /** Returns true or false for if a user can execute a privilaged oper command.
456          * This is done by looking up their oper type from User::oper, then referencing
457          * this to their oper classes and checking the commands they can execute.
458          * @param command A command (should be all CAPS)
459          * @return True if this user can execute the command
460          */
461         virtual bool HasPermission(const std::string &command);
462
463         /** Returns true if a user has a given permission.
464          * This is used to check whether or not users may perform certain actions which admins may not wish to give to
465          * all operators, yet are not commands. An example might be oper override, mass messaging (/notice $*), etc.
466          *
467          * @param privstr The priv to chec, e.g. "users/override/topic". These are loaded free-form from the config file.
468          * @param noisy If set to true, the user is notified that they do not have the specified permission where applicable. If false, no notification is sent.
469          * @return True if this user has the permission in question.
470          */
471         virtual bool HasPrivPermission(const std::string &privstr, bool noisy = false);
472
473         /** Returns true or false if a user can set a privileged user or channel mode.
474          * This is done by looking up their oper type from User::oper, then referencing
475          * this to their oper classes, and checking the modes they can set.
476          * @param mode The mode the check
477          * @param type ModeType (MODETYPE_CHANNEL or MODETYPE_USER).
478          * @return True if the user can set or unset this mode.
479          */
480         virtual bool HasModePermission(unsigned char mode, ModeType type);
481
482         /** Creates a wildcard host.
483          * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
484          * @return The wildcarded hostname in *!*@host form
485          */
486         char* MakeWildHost();
487
488         /** Creates a usermask with real host.
489          * Takes a buffer to use and fills the given buffer with the hostmask in the format user@host
490          * @return the usermask in the format user@host
491          */
492         const std::string& MakeHost();
493
494         /** Creates a usermask with real ip.
495          * Takes a buffer to use and fills the given buffer with the ipmask in the format user@ip
496          * @return the usermask in the format user@ip
497          */
498         const std::string& MakeHostIP();
499
500         /** Add the user to WHOWAS system
501          */
502         void AddToWhoWas();
503
504         /** Oper up the user using the given opertype.
505          * This will also give the +o usermode.
506          */
507         void Oper(OperInfo* info);
508
509         /** Force a nickname change.
510          * If the nickname change fails (for example, because the nick in question
511          * already exists) this function will return false, and you must then either
512          * output an error message, or quit the user for nickname collision.
513          * @param newnick The nickname to change to
514          * @return True if the nickchange was successful.
515          */
516         inline bool ForceNickChange(const char* newnick) { return ChangeNick(newnick, true); }
517
518         /** Oper down.
519          * This will clear the +o usermode and unset the user's oper type
520          */
521         void UnOper();
522
523         /** Write text to this user, appending CR/LF. Works on local users only.
524          * @param text A std::string to send to the user
525          */
526         virtual void Write(const std::string &text);
527
528         /** Write text to this user, appending CR/LF.
529          * Works on local users only.
530          * @param text The format string for text to send to the user
531          * @param ... POD-type format arguments
532          */
533         virtual void Write(const char *text, ...) CUSTOM_PRINTF(2, 3);
534
535         /** Write text to this user, appending CR/LF and prepending :server.name
536          * Works on local users only.
537          * @param text A std::string to send to the user
538          */
539         void WriteServ(const std::string& text);
540
541         /** Write text to this user, appending CR/LF and prepending :server.name
542          * Works on local users only.
543          * @param text The format string for text to send to the user
544          * @param ... POD-type format arguments
545          */
546         void WriteServ(const char* text, ...) CUSTOM_PRINTF(2, 3);
547
548         void WriteNumeric(unsigned int numeric, const char* text, ...) CUSTOM_PRINTF(3, 4);
549
550         void WriteNumeric(unsigned int numeric, const std::string &text);
551
552         /** Write text to this user, appending CR/LF and prepending :nick!user@host of the user provided in the first parameter.
553          * @param user The user to prepend the :nick!user@host of
554          * @param text A std::string to send to the user
555          */
556         void WriteFrom(User *user, const std::string &text);
557
558         /** Write text to this user, appending CR/LF and prepending :nick!user@host of the user provided in the first parameter.
559          * @param user The user to prepend the :nick!user@host of
560          * @param text The format string for text to send to the user
561          * @param ... POD-type format arguments
562          */
563         void WriteFrom(User *user, const char* text, ...) CUSTOM_PRINTF(3, 4);
564
565         /** Write text to the user provided in the first parameter, appending CR/LF, and prepending THIS user's :nick!user@host.
566          * @param dest The user to route the message to
567          * @param text A std::string to send to the user
568          */
569         void WriteTo(User *dest, const std::string &data);
570
571         /** Write text to the user provided in the first parameter, appending CR/LF, and prepending THIS user's :nick!user@host.
572          * @param dest The user to route the message to
573          * @param text The format string for text to send to the user
574          * @param ... POD-type format arguments
575          */
576         void WriteTo(User *dest, const char *data, ...) CUSTOM_PRINTF(3, 4);
577
578         /** Write to all users that can see this user (including this user in the list), appending CR/LF
579          * @param text A std::string to send to the users
580          */
581         void WriteCommonRaw(const std::string &line, bool include_self = true);
582
583         /** Write to all users that can see this user (including this user in the list), appending CR/LF
584          * @param text The format string for text to send to the users
585          * @param ... POD-type format arguments
586          */
587         void WriteCommon(const char* text, ...) CUSTOM_PRINTF(2, 3);
588
589         /** Write to all users that can see this user (not including this user in the list), appending CR/LF
590          * @param text The format string for text to send to the users
591          * @param ... POD-type format arguments
592          */
593         void WriteCommonExcept(const char* text, ...) CUSTOM_PRINTF(2, 3);
594
595         /** Write a quit message to all common users, as in User::WriteCommonExcept but with a specific
596          * quit message for opers only.
597          * @param normal_text Normal user quit message
598          * @param oper_text Oper only quit message
599          */
600         void WriteCommonQuit(const std::string &normal_text, const std::string &oper_text);
601
602         /** Dump text to a user target, splitting it appropriately to fit
603          * @param LinePrefix text to prefix each complete line with
604          * @param TextStream the text to send to the user
605          */
606         void SendText(const std::string &LinePrefix, std::stringstream &TextStream);
607
608         /** Write to the user, routing the line if the user is remote.
609          */
610         virtual void SendText(const std::string& line) = 0;
611
612         /** Write to the user, routing the line if the user is remote.
613          */
614         void SendText(const char* text, ...) CUSTOM_PRINTF(2, 3);
615
616         /** Return true if the user shares at least one channel with another user
617          * @param other The other user to compare the channel list against
618          * @return True if the given user shares at least one channel with this user
619          */
620         bool SharesChannelWith(User *other);
621
622         /** Send fake quit/join messages for host or ident cycle.
623          * Run this after the item in question has changed.
624          * You should not need to use this function, call ChangeDisplayedHost instead
625          *
626          * @param The entire QUIT line, including the source using the old value
627          */
628         void DoHostCycle(const std::string &quitline);
629
630         /** Change the displayed host of a user.
631          * ALWAYS use this function, rather than writing User::dhost directly,
632          * as this triggers module events allowing the change to be syncronized to
633          * remote servers. This will also emulate a QUIT and rejoin (where configured)
634          * before setting their host field.
635          * @param host The new hostname to set
636          * @return True if the change succeeded, false if it didn't
637          */
638         bool ChangeDisplayedHost(const char* host);
639
640         /** Change the ident (username) of a user.
641          * ALWAYS use this function, rather than writing User::ident directly,
642          * as this correctly causes the user to seem to quit (where configured)
643          * before setting their ident field.
644          * @param host The new ident to set
645          * @return True if the change succeeded, false if it didn't
646          */
647         bool ChangeIdent(const char* newident);
648
649         /** Change a users realname field.
650          * ALWAYS use this function, rather than writing User::fullname directly,
651          * as this triggers module events allowing the change to be syncronized to
652          * remote servers.
653          * @param gecos The user's new realname
654          * @return True if the change succeeded, false if otherwise
655          */
656         bool ChangeName(const char* gecos);
657
658         /** Change a user's nick
659          * @param newnick The new nick
660          * @param force True if the change is being forced (should not be blocked by modes like +N)
661          * @return True if the change succeeded
662          */
663         bool ChangeNick(const std::string& newnick, bool force = false);
664
665         /** Send a command to all local users from this user
666          * The command given must be able to send text with the
667          * first parameter as a servermask (e.g. $*), so basically
668          * you should use PRIVMSG or NOTICE.
669          * @param command the command to send
670          * @param text The text format string to send
671          * @param ... Format arguments
672          */
673         void SendAll(const char* command, const char* text, ...) CUSTOM_PRINTF(3, 4);
674
675         /** Compile a channel list for this user.  Used internally by WHOIS
676          * @param source The user to prepare the channel list for
677          * @param spy Whether to return the spy channel list rather than the normal one
678          * @return This user's channel list
679          */
680         std::string ChannelList(User* source, bool spy);
681
682         /** Split the channel list in cl which came from dest, and spool it to this user
683          * Used internally by WHOIS
684          * @param dest The user the original channel list came from
685          * @param cl The  channel list as a string obtained from User::ChannelList()
686          */
687         void SplitChanList(User* dest, const std::string &cl);
688
689         /** Remove this user from all channels they are on, and delete any that are now empty.
690          * This is used by QUIT, and will not send part messages!
691          */
692         void PurgeEmptyChannels();
693
694         /** Get the connect class which this user belongs to. NULL for remote users.
695          * @return A pointer to this user's connect class.
696          */
697         virtual ConnectClass* GetClass();
698
699         /** Default destructor
700          */
701         virtual ~User();
702         virtual CullResult cull();
703 };
704
705 class CoreExport UserIOHandler : public StreamSocket
706 {
707  public:
708         LocalUser* const user;
709         UserIOHandler(LocalUser* me) : user(me) {}
710         void OnDataReady();
711         void OnError(BufferedSocketError error);
712
713         /** Adds to the user's write buffer.
714          * You may add any amount of text up to this users sendq value, if you exceed the
715          * sendq value, the user will be removed, and further buffer adds will be dropped.
716          * @param data The data to add to the write buffer
717          */
718         void AddWriteBuf(const std::string &data);
719 };
720
721 typedef unsigned int already_sent_t;
722
723 class CoreExport LocalUser : public User
724 {
725         /** A list of channels the user has a pending invite to.
726          * Upon INVITE channels are added, and upon JOIN, the
727          * channels are removed from this list.
728          */
729         InvitedList invites;
730
731  public:
732         LocalUser(int fd, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
733         CullResult cull();
734
735         UserIOHandler eh;
736
737         /** Stats counter for bytes inbound
738          */
739         int bytes_in;
740
741         /** Stats counter for bytes outbound
742          */
743         int bytes_out;
744
745         /** Stats counter for commands inbound
746          */
747         int cmds_in;
748
749         /** Stats counter for commands outbound
750          */
751         int cmds_out;
752
753         /** Password specified by the user when they registered (if any).
754          * This is stored even if the <connect> block doesnt need a password, so that
755          * modules may check it.
756          */
757         std::string password;
758
759         /** Contains a pointer to the connect class a user is on from
760          */
761         reference<ConnectClass> MyClass;
762
763         ConnectClass* GetClass();
764
765         /** Call this method to find the matching <connect> for a user, and to check them against it.
766          */
767         void CheckClass();
768
769         /** Server address and port that this user is connected to.
770          */
771         irc::sockets::sockaddrs server_sa;
772
773         /**
774          * @return The port number of this user.
775          */
776         int GetServerPort();
777
778         /** Used by PING checking code
779          */
780         time_t nping;
781
782         /** This value contains how far into the penalty threshold the user is.
783          * This is used either to enable fake lag or for excess flood quits
784          */
785         unsigned int CommandFloodPenalty;
786
787         static already_sent_t already_sent_id;
788         already_sent_t already_sent;
789
790         /** Stored reverse lookup from res_forward. Should not be used after resolution.
791          */
792         std::string stored_host;
793
794         /** Starts a DNS lookup of the user's IP.
795          * This will cause two UserResolver classes to be instantiated.
796          * When complete, these objects set User::dns_done to true.
797          */
798         void StartDNSLookup();
799
800         /** Use this method to fully connect a user.
801          * This will send the message of the day, check G/K/E lines, etc.
802          */
803         void FullConnect();
804
805         /** Set the connect class to which this user belongs to.
806          * @param explicit_name Set this string to tie the user to a specific class name. Otherwise, the class is fitted by checking <connect> tags from the configuration file.
807          * @return A reference to this user's current connect class.
808          */
809         void SetClass(const std::string &explicit_name = "");
810
811         void SendText(const std::string& line);
812         void Write(const std::string& text);
813         void Write(const char*, ...) CUSTOM_PRINTF(2, 3);
814
815         /** Returns the list of channels this user has been invited to but has not yet joined.
816          * @return A list of channels the user is invited to
817          */
818         InvitedList* GetInviteList();
819
820         /** Returns true if a user is invited to a channel.
821          * @param channel A channel name to look up
822          * @return True if the user is invited to the given channel
823          */
824         bool IsInvited(const irc::string &channel);
825
826         /** Adds a channel to a users invite list (invites them to a channel)
827          * @param channel A channel name to add
828          * @param timeout When the invite should expire (0 == never)
829          */
830         void InviteTo(const irc::string &channel, time_t timeout);
831
832         /** Removes a channel from a users invite list.
833          * This member function is called on successfully joining an invite only channel
834          * to which the user has previously been invited, to clear the invitation.
835          * @param channel The channel to remove the invite to
836          */
837         void RemoveInvite(const irc::string &channel);
838
839         /** Returns true or false for if a user can execute a privilaged oper command.
840          * This is done by looking up their oper type from User::oper, then referencing
841          * this to their oper classes and checking the commands they can execute.
842          * @param command A command (should be all CAPS)
843          * @return True if this user can execute the command
844          */
845         bool HasPermission(const std::string &command);
846
847         /** Returns true if a user has a given permission.
848          * This is used to check whether or not users may perform certain actions which admins may not wish to give to
849          * all operators, yet are not commands. An example might be oper override, mass messaging (/notice $*), etc.
850          *
851          * @param privstr The priv to chec, e.g. "users/override/topic". These are loaded free-form from the config file.
852          * @param noisy If set to true, the user is notified that they do not have the specified permission where applicable. If false, no notification is sent.
853          * @return True if this user has the permission in question.
854          */
855         bool HasPrivPermission(const std::string &privstr, bool noisy = false);
856
857         /** Returns true or false if a user can set a privileged user or channel mode.
858          * This is done by looking up their oper type from User::oper, then referencing
859          * this to their oper classes, and checking the modes they can set.
860          * @param mode The mode the check
861          * @param type ModeType (MODETYPE_CHANNEL or MODETYPE_USER).
862          * @return True if the user can set or unset this mode.
863          */
864         bool HasModePermission(unsigned char mode, ModeType type);
865 };
866
867 class CoreExport RemoteUser : public User
868 {
869  public:
870         RemoteUser(const std::string& uid, const std::string& srv) : User(uid, srv, USERTYPE_REMOTE)
871         {
872         }
873         virtual void SendText(const std::string& line);
874 };
875
876 class CoreExport FakeUser : public User
877 {
878  public:
879         FakeUser(const std::string &uid, const std::string& srv) : User(uid, srv, USERTYPE_SERVER)
880         {
881                 nick = srv;
882         }
883
884         virtual CullResult cull();
885         virtual void SendText(const std::string& line);
886         virtual const std::string& GetFullHost();
887         virtual const std::string& GetFullRealHost();
888 };
889
890 /* Faster than dynamic_cast */
891 /** Is a local user */
892 inline LocalUser* IS_LOCAL(User* u)
893 {
894         return u->usertype == USERTYPE_LOCAL ? static_cast<LocalUser*>(u) : NULL;
895 }
896 /** Is a remote user */
897 inline RemoteUser* IS_REMOTE(User* u)
898 {
899         return u->usertype == USERTYPE_REMOTE ? static_cast<RemoteUser*>(u) : NULL;
900 }
901 /** Is a server fakeuser */
902 inline FakeUser* IS_SERVER(User* u)
903 {
904         return u->usertype == USERTYPE_SERVER ? static_cast<FakeUser*>(u) : NULL;
905 }
906 /** Is an oper */
907 #define IS_OPER(x) (x->oper)
908 /** Is away */
909 #define IS_AWAY(x) (!x->awaymsg.empty())
910
911 /** Derived from Resolver, and performs user forward/reverse lookups.
912  */
913 class CoreExport UserResolver : public Resolver
914 {
915  private:
916         /** UUID we are looking up */
917         std::string uuid;
918         /** True if the lookup is forward, false if is a reverse lookup
919          */
920         bool fwd;
921  public:
922         /** Create a resolver.
923          * @param Instance The creating instance
924          * @param user The user to begin lookup on
925          * @param to_resolve The IP or host to resolve
926          * @param qt The query type
927          * @param cache Modified by the constructor if the result was cached
928          */
929         UserResolver(LocalUser* user, std::string to_resolve, QueryType qt, bool &cache);
930
931         /** Called on successful lookup
932          * @param result Result string
933          * @param ttl Time to live for result
934          * @param cached True if the result was found in the cache
935          */
936         void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached);
937
938         /** Called on failed lookup
939          * @param e Error code
940          * @param errormessage Error message string
941          */
942         void OnError(ResolverError e, const std::string &errormessage);
943 };
944
945 #endif