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