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