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