]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
Convert MySQL to SQLv3
[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         /** This is true if the user matched an exception (E:Line). It is used to save time on ban checks.
352          */
353         unsigned int exempt:1;
354
355         /** has the user responded to their previous ping?
356          */
357         unsigned int lastping:1;
358
359         /** What type of user is this? */
360         const unsigned int usertype:2;
361
362         /** Get client IP string from sockaddr, using static internal buffer
363          * @return The IP string
364          */
365         const char* GetIPString();
366
367         /** Get CIDR mask, using default range, for this user
368          */
369         irc::sockets::cidr_mask GetCIDRMask();
370
371         /** Sets the client IP for this user
372          * @return true if the conversion was successful
373          */
374         bool SetClientIP(const char* sip);
375
376         /** Constructor
377          * @throw CoreException if the UID allocated to the user already exists
378          */
379         User(const std::string &uid, const std::string& srv, int objtype);
380
381         /** Check if the user matches a G or K line, and disconnect them if they do.
382          * @param doZline True if ZLines should be checked (if IP has changed since initial connect)
383          * Returns true if the user matched a ban, false else.
384          */
385         bool CheckLines(bool doZline = false);
386
387         /** Returns the full displayed host of the user
388          * This member function returns the hostname of the user as seen by other users
389          * on the server, in nick!ident&at;host form.
390          * @return The full masked host of the user
391          */
392         virtual const std::string& GetFullHost();
393
394         /** Returns the full real host of the user
395          * This member function returns the hostname of the user as seen by other users
396          * on the server, in nick!ident&at;host form. If any form of hostname cloaking is in operation,
397          * e.g. through a module, then this method will ignore it and return the true hostname.
398          * @return The full real host of the user
399          */
400         virtual const std::string& GetFullRealHost();
401
402         /** This clears any cached results that are used for GetFullRealHost() etc.
403          * The results of these calls are cached as generating them can be generally expensive.
404          */
405         void InvalidateCache();
406
407         /** Create a displayable mode string for this users snomasks
408          * @return The notice mask character sequence
409          */
410         const char* FormatNoticeMasks();
411
412         /** Process a snomask modifier string, e.g. +abc-de
413          * @param sm A sequence of notice mask characters
414          * @return The cleaned mode sequence which can be output,
415          * e.g. in the above example if masks c and e are not
416          * valid, this function will return +ab-d
417          */
418         std::string ProcessNoticeMasks(const char *sm);
419
420         /** Returns true if a notice mask is set
421          * @param sm A notice mask character to check
422          * @return True if the notice mask is set
423          */
424         bool IsNoticeMaskSet(unsigned char sm);
425
426         /** Changed a specific notice mask value
427          * @param sm The server notice mask to change
428          * @param value An on/off value for this mask
429          */
430         void SetNoticeMask(unsigned char sm, bool value);
431
432         /** Create a displayable mode string for this users umodes
433          * @param The mode string
434          */
435         const char* FormatModes(bool showparameters = false);
436
437         /** Returns true if a specific mode is set
438          * @param m The user mode
439          * @return True if the mode is set
440          */
441         bool IsModeSet(unsigned char m);
442
443         /** Set a specific usermode to on or off
444          * @param m The user mode
445          * @param value On or off setting of the mode
446          */
447         void SetMode(unsigned char m, bool value);
448
449         /** Returns true or false for if a user can execute a privilaged oper command.
450          * This is done by looking up their oper type from User::oper, then referencing
451          * this to their oper classes and checking the commands they can execute.
452          * @param command A command (should be all CAPS)
453          * @return True if this user can execute the command
454          */
455         virtual bool HasPermission(const std::string &command);
456
457         /** Returns true if a user has a given permission.
458          * This is used to check whether or not users may perform certain actions which admins may not wish to give to
459          * all operators, yet are not commands. An example might be oper override, mass messaging (/notice $*), etc.
460          *
461          * @param privstr The priv to chec, e.g. "users/override/topic". These are loaded free-form from the config file.
462          * @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.
463          * @return True if this user has the permission in question.
464          */
465         virtual bool HasPrivPermission(const std::string &privstr, bool noisy = false);
466
467         /** Returns true or false if a user can set a privileged user or channel mode.
468          * This is done by looking up their oper type from User::oper, then referencing
469          * this to their oper classes, and checking the modes they can set.
470          * @param mode The mode the check
471          * @param type ModeType (MODETYPE_CHANNEL or MODETYPE_USER).
472          * @return True if the user can set or unset this mode.
473          */
474         virtual bool HasModePermission(unsigned char mode, ModeType type);
475
476         /** Creates a wildcard host.
477          * Takes a buffer to use and fills the given buffer with the host in the format *!*@hostname
478          * @return The wildcarded hostname in *!*@host form
479          */
480         char* MakeWildHost();
481
482         /** Creates a usermask with real host.
483          * Takes a buffer to use and fills the given buffer with the hostmask in the format user@host
484          * @return the usermask in the format user@host
485          */
486         const std::string& MakeHost();
487
488         /** Creates a usermask with real ip.
489          * Takes a buffer to use and fills the given buffer with the ipmask in the format user@ip
490          * @return the usermask in the format user@ip
491          */
492         const std::string& MakeHostIP();
493
494         /** Add the user to WHOWAS system
495          */
496         void AddToWhoWas();
497
498         /** Oper up the user using the given opertype.
499          * This will also give the +o usermode.
500          */
501         void Oper(OperInfo* info);
502
503         /** Force a nickname change.
504          * If the nickname change fails (for example, because the nick in question
505          * already exists) this function will return false, and you must then either
506          * output an error message, or quit the user for nickname collision.
507          * @param newnick The nickname to change to
508          * @return True if the nickchange was successful.
509          */
510         inline bool ForceNickChange(const char* newnick) { return ChangeNick(newnick, true); }
511
512         /** Oper down.
513          * This will clear the +o usermode and unset the user's oper type
514          */
515         void UnOper();
516
517         /** Write text to this user, appending CR/LF. Works on local users only.
518          * @param text A std::string to send to the user
519          */
520         virtual void Write(const std::string &text);
521
522         /** Write text to this user, appending CR/LF.
523          * Works on local users only.
524          * @param text The format string for text to send to the user
525          * @param ... POD-type format arguments
526          */
527         virtual void Write(const char *text, ...) CUSTOM_PRINTF(2, 3);
528
529         /** Write text to this user, appending CR/LF and prepending :server.name
530          * Works on local users only.
531          * @param text A std::string to send to the user
532          */
533         void WriteServ(const std::string& text);
534
535         /** Write text to this user, appending CR/LF and prepending :server.name
536          * Works on local users only.
537          * @param text The format string for text to send to the user
538          * @param ... POD-type format arguments
539          */
540         void WriteServ(const char* text, ...) CUSTOM_PRINTF(2, 3);
541
542         void WriteNumeric(unsigned int numeric, const char* text, ...) CUSTOM_PRINTF(3, 4);
543
544         void WriteNumeric(unsigned int numeric, const std::string &text);
545
546         /** Write text to this user, appending CR/LF and prepending :nick!user@host of the user provided in the first parameter.
547          * @param user The user to prepend the :nick!user@host of
548          * @param text A std::string to send to the user
549          */
550         void WriteFrom(User *user, 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 The format string for text to send to the user
555          * @param ... POD-type format arguments
556          */
557         void WriteFrom(User *user, const char* text, ...) CUSTOM_PRINTF(3, 4);
558
559         /** Write text to the user provided in the first parameter, appending CR/LF, and prepending THIS user's :nick!user@host.
560          * @param dest The user to route the message to
561          * @param text A std::string to send to the user
562          */
563         void WriteTo(User *dest, const std::string &data);
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 The format string for text to send to the user
568          * @param ... POD-type format arguments
569          */
570         void WriteTo(User *dest, const char *data, ...) CUSTOM_PRINTF(3, 4);
571
572         /** Write to all users that can see this user (including this user in the list), appending CR/LF
573          * @param text A std::string to send to the users
574          */
575         void WriteCommonRaw(const std::string &line, bool include_self = true);
576
577         /** Write to all users that can see this user (including this user in the list), appending CR/LF
578          * @param text The format string for text to send to the users
579          * @param ... POD-type format arguments
580          */
581         void WriteCommon(const char* text, ...) CUSTOM_PRINTF(2, 3);
582
583         /** Write to all users that can see this user (not 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 WriteCommonExcept(const char* text, ...) CUSTOM_PRINTF(2, 3);
588
589         /** Write a quit message to all common users, as in User::WriteCommonExcept but with a specific
590          * quit message for opers only.
591          * @param normal_text Normal user quit message
592          * @param oper_text Oper only quit message
593          */
594         void WriteCommonQuit(const std::string &normal_text, const std::string &oper_text);
595
596         /** Dump text to a user target, splitting it appropriately to fit
597          * @param LinePrefix text to prefix each complete line with
598          * @param TextStream the text to send to the user
599          */
600         void SendText(const std::string &LinePrefix, std::stringstream &TextStream);
601
602         /** Write to the user, routing the line if the user is remote.
603          */
604         virtual void SendText(const std::string& line) = 0;
605
606         /** Write to the user, routing the line if the user is remote.
607          */
608         void SendText(const char* text, ...) CUSTOM_PRINTF(2, 3);
609
610         /** Return true if the user shares at least one channel with another user
611          * @param other The other user to compare the channel list against
612          * @return True if the given user shares at least one channel with this user
613          */
614         bool SharesChannelWith(User *other);
615
616         /** Send fake quit/join messages for host or ident cycle.
617          * Run this after the item in question has changed.
618          * You should not need to use this function, call ChangeDisplayedHost instead
619          *
620          * @param The entire QUIT line, including the source using the old value
621          */
622         void DoHostCycle(const std::string &quitline);
623
624         /** Change the displayed host of a user.
625          * ALWAYS use this function, rather than writing User::dhost directly,
626          * as this triggers module events allowing the change to be syncronized to
627          * remote servers. This will also emulate a QUIT and rejoin (where configured)
628          * before setting their host field.
629          * @param host The new hostname to set
630          * @return True if the change succeeded, false if it didn't
631          */
632         bool ChangeDisplayedHost(const char* host);
633
634         /** Change the ident (username) of a user.
635          * ALWAYS use this function, rather than writing User::ident directly,
636          * as this correctly causes the user to seem to quit (where configured)
637          * before setting their ident field.
638          * @param host The new ident to set
639          * @return True if the change succeeded, false if it didn't
640          */
641         bool ChangeIdent(const char* newident);
642
643         /** Change a users realname field.
644          * ALWAYS use this function, rather than writing User::fullname directly,
645          * as this triggers module events allowing the change to be syncronized to
646          * remote servers.
647          * @param gecos The user's new realname
648          * @return True if the change succeeded, false if otherwise
649          */
650         bool ChangeName(const char* gecos);
651
652         /** Change a user's nick
653          * @param newnick The new nick
654          * @param force True if the change is being forced (should not be blocked by modes like +N)
655          * @return True if the change succeeded
656          */
657         bool ChangeNick(const std::string& newnick, bool force = false);
658
659         /** Send a command to all local users from this user
660          * The command given must be able to send text with the
661          * first parameter as a servermask (e.g. $*), so basically
662          * you should use PRIVMSG or NOTICE.
663          * @param command the command to send
664          * @param text The text format string to send
665          * @param ... Format arguments
666          */
667         void SendAll(const char* command, const char* text, ...) CUSTOM_PRINTF(3, 4);
668
669         /** Compile a channel list for this user.  Used internally by WHOIS
670          * @param source The user to prepare the channel list for
671          * @param spy Whether to return the spy channel list rather than the normal one
672          * @return This user's channel list
673          */
674         std::string ChannelList(User* source, bool spy);
675
676         /** Split the channel list in cl which came from dest, and spool it to this user
677          * Used internally by WHOIS
678          * @param dest The user the original channel list came from
679          * @param cl The  channel list as a string obtained from User::ChannelList()
680          */
681         void SplitChanList(User* dest, const std::string &cl);
682
683         /** Remove this user from all channels they are on, and delete any that are now empty.
684          * This is used by QUIT, and will not send part messages!
685          */
686         void PurgeEmptyChannels();
687
688         /** Get the connect class which this user belongs to. NULL for remote users.
689          * @return A pointer to this user's connect class.
690          */
691         virtual ConnectClass* GetClass();
692
693         /** Default destructor
694          */
695         virtual ~User();
696         virtual CullResult cull();
697 };
698
699 class CoreExport UserIOHandler : public StreamSocket
700 {
701  public:
702         LocalUser* const user;
703         UserIOHandler(LocalUser* me) : user(me) {}
704         void OnDataReady();
705         void OnError(BufferedSocketError error);
706
707         /** Adds to the user's write buffer.
708          * You may add any amount of text up to this users sendq value, if you exceed the
709          * sendq value, the user will be removed, and further buffer adds will be dropped.
710          * @param data The data to add to the write buffer
711          */
712         void AddWriteBuf(const std::string &data);
713 };
714
715 typedef unsigned int already_sent_t;
716
717 class CoreExport LocalUser : public User
718 {
719         /** A list of channels the user has a pending invite to.
720          * Upon INVITE channels are added, and upon JOIN, the
721          * channels are removed from this list.
722          */
723         InvitedList invites;
724
725  public:
726         LocalUser(int fd, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
727         CullResult cull();
728
729         UserIOHandler eh;
730
731         /** Stats counter for bytes inbound
732          */
733         int bytes_in;
734
735         /** Stats counter for bytes outbound
736          */
737         int bytes_out;
738
739         /** Stats counter for commands inbound
740          */
741         int cmds_in;
742
743         /** Stats counter for commands outbound
744          */
745         int cmds_out;
746
747         /** Password specified by the user when they registered (if any).
748          * This is stored even if the <connect> block doesnt need a password, so that
749          * modules may check it.
750          */
751         std::string password;
752
753         /** Contains a pointer to the connect class a user is on from
754          */
755         reference<ConnectClass> MyClass;
756
757         ConnectClass* GetClass();
758
759         /** Call this method to find the matching <connect> for a user, and to check them against it.
760          */
761         void CheckClass();
762
763         /** Server address and port that this user is connected to.
764          */
765         irc::sockets::sockaddrs server_sa;
766
767         /**
768          * @return The port number of this user.
769          */
770         int GetServerPort();
771
772         /** Used by PING checking code
773          */
774         time_t nping;
775
776         /** This value contains how far into the penalty threshold the user is.
777          * This is used either to enable fake lag or for excess flood quits
778          */
779         unsigned int CommandFloodPenalty;
780
781         static already_sent_t already_sent_id;
782         already_sent_t already_sent;
783
784         /** Stored reverse lookup from res_forward. Should not be used after resolution.
785          */
786         std::string stored_host;
787
788         /** Starts a DNS lookup of the user's IP.
789          * This will cause two UserResolver classes to be instantiated.
790          * When complete, these objects set User::dns_done to true.
791          */
792         void StartDNSLookup();
793
794         /** Use this method to fully connect a user.
795          * This will send the message of the day, check G/K/E lines, etc.
796          */
797         void FullConnect();
798
799         /** Set the connect class to which this user belongs to.
800          * @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.
801          * @return A reference to this user's current connect class.
802          */
803         void SetClass(const std::string &explicit_name = "");
804
805         void SendText(const std::string& line);
806         void Write(const std::string& text);
807         void Write(const char*, ...) CUSTOM_PRINTF(2, 3);
808
809         /** Returns the list of channels this user has been invited to but has not yet joined.
810          * @return A list of channels the user is invited to
811          */
812         InvitedList* GetInviteList();
813
814         /** Returns true if a user is invited to a channel.
815          * @param channel A channel name to look up
816          * @return True if the user is invited to the given channel
817          */
818         bool IsInvited(const irc::string &channel);
819
820         /** Adds a channel to a users invite list (invites them to a channel)
821          * @param channel A channel name to add
822          * @param timeout When the invite should expire (0 == never)
823          */
824         void InviteTo(const irc::string &channel, time_t timeout);
825
826         /** Removes a channel from a users invite list.
827          * This member function is called on successfully joining an invite only channel
828          * to which the user has previously been invited, to clear the invitation.
829          * @param channel The channel to remove the invite to
830          */
831         void RemoveInvite(const irc::string &channel);
832
833         /** Returns true or false for if a user can execute a privilaged oper command.
834          * This is done by looking up their oper type from User::oper, then referencing
835          * this to their oper classes and checking the commands they can execute.
836          * @param command A command (should be all CAPS)
837          * @return True if this user can execute the command
838          */
839         bool HasPermission(const std::string &command);
840
841         /** Returns true if a user has a given permission.
842          * This is used to check whether or not users may perform certain actions which admins may not wish to give to
843          * all operators, yet are not commands. An example might be oper override, mass messaging (/notice $*), etc.
844          *
845          * @param privstr The priv to chec, e.g. "users/override/topic". These are loaded free-form from the config file.
846          * @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.
847          * @return True if this user has the permission in question.
848          */
849         bool HasPrivPermission(const std::string &privstr, bool noisy = false);
850
851         /** Returns true or false if a user can set a privileged user or channel mode.
852          * This is done by looking up their oper type from User::oper, then referencing
853          * this to their oper classes, and checking the modes they can set.
854          * @param mode The mode the check
855          * @param type ModeType (MODETYPE_CHANNEL or MODETYPE_USER).
856          * @return True if the user can set or unset this mode.
857          */
858         bool HasModePermission(unsigned char mode, ModeType type);
859 };
860
861 class CoreExport RemoteUser : public User
862 {
863  public:
864         RemoteUser(const std::string& uid, const std::string& srv) : User(uid, srv, USERTYPE_REMOTE)
865         {
866         }
867         virtual void SendText(const std::string& line);
868 };
869
870 class CoreExport FakeUser : public User
871 {
872  public:
873         FakeUser(const std::string &uid, const std::string& srv) : User(uid, srv, USERTYPE_SERVER)
874         {
875                 nick = srv;
876         }
877
878         virtual CullResult cull();
879         virtual void SendText(const std::string& line);
880         virtual const std::string& GetFullHost();
881         virtual const std::string& GetFullRealHost();
882 };
883
884 /* Faster than dynamic_cast */
885 /** Is a local user */
886 inline LocalUser* IS_LOCAL(User* u)
887 {
888         return u->usertype == USERTYPE_LOCAL ? static_cast<LocalUser*>(u) : NULL;
889 }
890 /** Is a remote user */
891 inline RemoteUser* IS_REMOTE(User* u)
892 {
893         return u->usertype == USERTYPE_REMOTE ? static_cast<RemoteUser*>(u) : NULL;
894 }
895 /** Is a server fakeuser */
896 inline FakeUser* IS_SERVER(User* u)
897 {
898         return u->usertype == USERTYPE_SERVER ? static_cast<FakeUser*>(u) : NULL;
899 }
900 /** Is an oper */
901 #define IS_OPER(x) (x->oper)
902 /** Is away */
903 #define IS_AWAY(x) (!x->awaymsg.empty())
904
905 /** Derived from Resolver, and performs user forward/reverse lookups.
906  */
907 class CoreExport UserResolver : public Resolver
908 {
909  private:
910         /** UUID we are looking up */
911         std::string uuid;
912         /** True if the lookup is forward, false if is a reverse lookup
913          */
914         bool fwd;
915  public:
916         /** Create a resolver.
917          * @param Instance The creating instance
918          * @param user The user to begin lookup on
919          * @param to_resolve The IP or host to resolve
920          * @param qt The query type
921          * @param cache Modified by the constructor if the result was cached
922          */
923         UserResolver(LocalUser* user, std::string to_resolve, QueryType qt, bool &cache);
924
925         /** Called on successful lookup
926          * @param result Result string
927          * @param ttl Time to live for result
928          * @param cached True if the result was found in the cache
929          */
930         void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached);
931
932         /** Called on failed lookup
933          * @param e Error code
934          * @param errormessage Error message string
935          */
936         void OnError(ResolverError e, const std::string &errormessage);
937 };
938
939 #endif