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