]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/users.h
Merge branch 'insp20' into master.
[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         /** If set then the hostname which is displayed to users. */
248         std::string displayhost;
249
250         /** The real hostname of this user. */
251         std::string realhost;
252
253         /** The real name of this user. */
254         std::string realname;
255
256         /** The user's mode list.
257          * Much love to the STL for giving us an easy to use bitset, saving us RAM.
258          * if (modes[modeid]) is set, then the mode is set.
259          * For example, to work out if mode +i is set, we check the field
260          * User::modes[invisiblemode->modeid] == true.
261          */
262         std::bitset<ModeParser::MODEID_MAX> modes;
263
264  public:
265         /** To execute a function for each local neighbor of a user, inherit from this class and
266          * pass an instance of it to User::ForEachNeighbor().
267          */
268         class ForEachNeighborHandler
269         {
270          public:
271                 /** Method to execute for each local neighbor of a user.
272                  * Derived classes must implement this.
273                  * @param user Current neighbor
274                  */
275                 virtual void Execute(LocalUser* user) = 0;
276         };
277
278         /** List of Memberships for this user
279          */
280         typedef insp::intrusive_list<Membership> ChanList;
281
282         /** Time that the object was instantiated (used for TS calculation etc)
283         */
284         time_t age;
285
286         /** Time the connection was created, set in the constructor. This
287          * may be different from the time the user's classbase object was
288          * created.
289          */
290         time_t signon;
291
292         /** Client address that the user is connected from.
293          * Do not modify this value directly, use SetClientIP() to change it.
294          * Port is not valid for remote users.
295          */
296         irc::sockets::sockaddrs client_sa;
297
298         /** The users nickname.
299          * An invalid nickname indicates an unregistered connection prior to the NICK command.
300          * Use InspIRCd::IsNick() to validate nicknames.
301          */
302         std::string nick;
303
304         /** The user's unique identifier.
305          * This is the unique identifier which the user has across the network.
306          */
307         const std::string uuid;
308
309         /** The users ident reply.
310          * Two characters are added to the user-defined limit to compensate for the tilde etc.
311          */
312         std::string ident;
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 UserType 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         /** Retrieves this user's hostname.
363          * @param uncloak If true then return the real host; otherwise, the display host.
364          */
365         const std::string& GetHost(bool uncloak) const;
366
367         /** Retrieves this user's displayed hostname. */
368         const std::string& GetDisplayedHost() const;
369
370         /** Retrieves this user's real hostname. */
371         const std::string& GetRealHost() const;
372
373         /** Retrieves this user's real name. */
374         const std::string& GetRealName() const;
375
376         /** Get CIDR mask, using default range, for this user
377          */
378         irc::sockets::cidr_mask GetCIDRMask();
379
380         /** Sets the client IP for this user
381          * @return true if the conversion was successful
382          */
383         virtual bool SetClientIP(const std::string& address, bool recheck_eline = true);
384
385         virtual void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true);
386
387         /** Constructor
388          * @throw CoreException if the UID allocated to the user already exists
389          */
390         User(const std::string& uid, Server* srv, UserType objtype);
391
392         /** Returns the full displayed host of the user
393          * This member function returns the hostname of the user as seen by other users
394          * on the server, in nick!ident\@host form.
395          * @return The full masked host of the user
396          */
397         virtual const std::string& GetFullHost();
398
399         /** Returns the full real host of the user
400          * This member function returns the hostname of the user as seen by other users
401          * on the server, in nick!ident\@host form. If any form of hostname cloaking is in operation,
402          * e.g. through a module, then this method will ignore it and return the true hostname.
403          * @return The full real host of the user
404          */
405         virtual const std::string& GetFullRealHost();
406
407         /** This clears any cached results that are used for GetFullRealHost() etc.
408          * The results of these calls are cached as generating them can be generally expensive.
409          */
410         void InvalidateCache();
411
412         /** Returns whether this user is currently away or not. If true,
413          * further information can be found in User::awaymsg and User::awaytime
414          * @return True if the user is away, false otherwise
415          */
416         bool IsAway() const { return (!awaymsg.empty()); }
417
418         /** Returns whether this user is an oper or not. If true,
419          * oper information can be obtained from User::oper
420          * @return True if the user is an oper, false otherwise
421          */
422         bool IsOper() const { return oper; }
423
424         /** Returns true if a notice mask is set
425          * @param sm A notice mask character to check
426          * @return True if the notice mask is set
427          */
428         bool IsNoticeMaskSet(unsigned char sm);
429
430         /** Get the mode letters of modes set on the user as a string.
431          * @param includeparams True to get the parameters of the modes as well. Defaults to false.
432          * @return Mode letters of modes set on the user and optionally the parameters of those modes, if any.
433          * The returned string always begins with a '+' character. If the user has no modes set, "+" is returned.
434          */
435         std::string GetModeLetters(bool includeparams = false) const;
436
437         /** Returns true if a specific mode is set
438          * @param m The user mode
439          * @return True if the mode is set
440          */
441         bool IsModeSet(unsigned char m) const;
442         bool IsModeSet(const ModeHandler* mh) const;
443         bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
444         bool IsModeSet(UserModeReference& moderef) const;
445
446         /** Set a specific usermode to on or off
447          * @param mh The user mode
448          * @param value On or off setting of the mode
449          */
450         void SetMode(ModeHandler* mh, bool value);
451         void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
452
453         /** Returns true or false for if a user can execute a privilaged oper command.
454          * This is done by looking up their oper type from User::oper, then referencing
455          * this to their oper classes and checking the commands they can execute.
456          * @param command A command (should be all CAPS)
457          * @return True if this user can execute the command
458          */
459         virtual bool HasPermission(const std::string &command);
460
461         /** Returns true if a user has a given permission.
462          * This is used to check whether or not users may perform certain actions which admins may not wish to give to
463          * all operators, yet are not commands. An example might be oper override, mass messaging (/notice $*), etc.
464          *
465          * @param privstr The priv to chec, e.g. "users/override/topic". These are loaded free-form from the config file.
466          * @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.
467          * @return True if this user has the permission in question.
468          */
469         virtual bool HasPrivPermission(const std::string &privstr, bool noisy = false);
470
471         /** Returns true or false if a user can set a privileged user or channel mode.
472          * This is done by looking up their oper type from User::oper, then referencing
473          * this to their oper classes, and checking the modes they can set.
474          * @param mh Mode to check
475          * @return True if the user can set or unset this mode.
476          */
477         virtual bool HasModePermission(const ModeHandler* mh) const;
478
479         /** Creates a usermask with real host.
480          * Takes a buffer to use and fills the given buffer with the hostmask in the format user\@host
481          * @return the usermask in the format user\@host
482          */
483         const std::string& MakeHost();
484
485         /** Creates a usermask with real ip.
486          * Takes a buffer to use and fills the given buffer with the ipmask in the format user\@ip
487          * @return the usermask in the format user\@ip
488          */
489         const std::string& MakeHostIP();
490
491         /** Oper up the user using the given opertype.
492          * This will also give the +o usermode.
493          */
494         void Oper(OperInfo* info);
495
496         /** Oper down.
497          * This will clear the +o usermode and unset the user's oper type
498          */
499         void UnOper();
500
501         /** Sends a server notice to this user.
502          * @param text The contents of the message to send.
503          */
504         void WriteNotice(const std::string& text);
505
506         /** Send a NOTICE message from the local server to the user.
507          * @param text Text to send
508          */
509         virtual void WriteRemoteNotice(const std::string& text);
510
511         virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
512
513         template <typename T1>
514         void WriteRemoteNumeric(unsigned int numeric, T1 p1)
515         {
516                 Numeric::Numeric n(numeric);
517                 n.push(p1);
518                 WriteRemoteNumeric(n);
519         }
520
521         template <typename T1, typename T2>
522         void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
523         {
524                 Numeric::Numeric n(numeric);
525                 n.push(p1);
526                 n.push(p2);
527                 WriteRemoteNumeric(n);
528         }
529
530         template <typename T1, typename T2, typename T3>
531         void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
532         {
533                 Numeric::Numeric n(numeric);
534                 n.push(p1);
535                 n.push(p2);
536                 n.push(p3);
537                 WriteRemoteNumeric(n);
538         }
539
540         template <typename T1, typename T2, typename T3, typename T4>
541         void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
542         {
543                 Numeric::Numeric n(numeric);
544                 n.push(p1);
545                 n.push(p2);
546                 n.push(p3);
547                 n.push(p4);
548                 WriteRemoteNumeric(n);
549         }
550
551         template <typename T1, typename T2, typename T3, typename T4, typename T5>
552         void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
553         {
554                 Numeric::Numeric n(numeric);
555                 n.push(p1);
556                 n.push(p2);
557                 n.push(p3);
558                 n.push(p4);
559                 n.push(p5);
560                 WriteRemoteNumeric(n);
561         }
562
563         void WriteNumeric(const Numeric::Numeric& numeric);
564
565         template <typename T1>
566         void WriteNumeric(unsigned int numeric, T1 p1)
567         {
568                 Numeric::Numeric n(numeric);
569                 n.push(p1);
570                 WriteNumeric(n);
571         }
572
573         template <typename T1, typename T2>
574         void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
575         {
576                 Numeric::Numeric n(numeric);
577                 n.push(p1);
578                 n.push(p2);
579                 WriteNumeric(n);
580         }
581
582         template <typename T1, typename T2, typename T3>
583         void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
584         {
585                 Numeric::Numeric n(numeric);
586                 n.push(p1);
587                 n.push(p2);
588                 n.push(p3);
589                 WriteNumeric(n);
590         }
591
592         template <typename T1, typename T2, typename T3, typename T4>
593         void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
594         {
595                 Numeric::Numeric n(numeric);
596                 n.push(p1);
597                 n.push(p2);
598                 n.push(p3);
599                 n.push(p4);
600                 WriteNumeric(n);
601         }
602
603         template <typename T1, typename T2, typename T3, typename T4, typename T5>
604         void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
605         {
606                 Numeric::Numeric n(numeric);
607                 n.push(p1);
608                 n.push(p2);
609                 n.push(p3);
610                 n.push(p4);
611                 n.push(p5);
612                 WriteNumeric(n);
613         }
614
615         /** Write to all users that can see this user (including this user in the list if include_self is true), appending CR/LF
616          * @param protoev Protocol event to send, may contain any number of messages.
617          * @param include_self Should the message be sent back to the author?
618          */
619         void WriteCommonRaw(ClientProtocol::Event& protoev, bool include_self = true);
620
621         /** Execute a function once for each local neighbor of this user. By default, the neighbors of a user are the users
622          * who have at least one common channel with the user. Modules are allowed to alter the set of neighbors freely.
623          * This function is used for example to send something conditionally to neighbors, or to send different messages
624          * to different users depending on their oper status.
625          * @param handler Function object to call, inherited from ForEachNeighborHandler.
626          * @param include_self True to include this user in the set of neighbors, false otherwise.
627          * Modules may override this. Has no effect if this user is not local.
628          */
629         void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
630
631         /** Return true if the user shares at least one channel with another user
632          * @param other The other user to compare the channel list against
633          * @return True if the given user shares at least one channel with this user
634          */
635         bool SharesChannelWith(User *other);
636
637         /** Change the displayed hostname of this user.
638          * @param host The new displayed hostname of this user.
639          * @return True if the hostname was changed successfully; otherwise, false.
640          */
641         bool ChangeDisplayedHost(const std::string& host);
642
643         /** Change the real hostname of this user.
644          * @param host The new real hostname of this user.
645          * @param resetdisplay Whether to reset the display host to this value.
646          */
647         void ChangeRealHost(const std::string& host, bool resetdisplay);
648
649         /** Change the ident (username) of a user.
650          * ALWAYS use this function, rather than writing User::ident directly,
651          * as this triggers module events allowing the change to be syncronized to
652          * remote servers.
653          * @param newident The new ident to set
654          * @return True if the change succeeded, false if it didn't
655          */
656         bool ChangeIdent(const std::string& newident);
657
658         /** Change a users realname field.
659          * @param real The user's new real name
660          * @return True if the change succeeded, false if otherwise
661          */
662         bool ChangeRealName(const std::string& real);
663
664         /** Change a user's nick
665          * @param newnick The new nick. If equal to the users uuid, the nick change always succeeds.
666          * @param newts The time at which this nick change happened.
667          * @return True if the change succeeded
668          */
669         bool ChangeNick(const std::string& newnick, time_t newts = 0);
670
671         /** Remove this user from all channels they are on, and delete any that are now empty.
672          * This is used by QUIT, and will not send part messages!
673          */
674         void PurgeEmptyChannels();
675
676         /** Default destructor
677          */
678         virtual ~User();
679         CullResult cull() CXX11_OVERRIDE;
680 };
681
682 class CoreExport UserIOHandler : public StreamSocket
683 {
684  public:
685         LocalUser* const user;
686         UserIOHandler(LocalUser* me) : user(me) {}
687         void OnDataReady() CXX11_OVERRIDE;
688         void OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote) CXX11_OVERRIDE;
689         void OnError(BufferedSocketError error) CXX11_OVERRIDE;
690
691         /** Adds to the user's write buffer.
692          * You may add any amount of text up to this users sendq value, if you exceed the
693          * sendq value, the user will be removed, and further buffer adds will be dropped.
694          * @param data The data to add to the write buffer
695          */
696         void AddWriteBuf(const std::string &data);
697 };
698
699 typedef unsigned int already_sent_t;
700
701 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
702 {
703         /** Add a serialized message to the send queue of the user.
704          * @param serialized Bytes to add.
705          */
706         void Write(const ClientProtocol::SerializedMessage& serialized);
707
708         /** Send a protocol event to the user, consisting of one or more messages.
709          * @param protoev Event to send, may contain any number of messages.
710          * @param msglist Message list used temporarily internally to pass to hooks and store messages
711          * before Write().
712          */
713         void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
714
715         /** Message list, can be passed to the two parameter Send().
716          */
717         static ClientProtocol::MessageList sendmsglist;
718
719  public:
720         LocalUser(int fd, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
721         CullResult cull() CXX11_OVERRIDE;
722
723         UserIOHandler eh;
724
725         /** Serializer to use when communicating with the user
726          */
727         ClientProtocol::Serializer* serializer;
728
729         /** Stats counter for bytes inbound
730          */
731         unsigned int bytes_in;
732
733         /** Stats counter for bytes outbound
734          */
735         unsigned int bytes_out;
736
737         /** Stats counter for commands inbound
738          */
739         unsigned int cmds_in;
740
741         /** Stats counter for commands outbound
742          */
743         unsigned int cmds_out;
744
745         /** Password specified by the user when they registered (if any).
746          * This is stored even if the \<connect> block doesnt need a password, so that
747          * modules may check it.
748          */
749         std::string password;
750
751         /** Contains a pointer to the connect class a user is on from
752          */
753         reference<ConnectClass> MyClass;
754
755         /** Get the connect class which this user belongs to.
756          * @return A pointer to this user's connect class.
757          */
758         ConnectClass* GetClass() const { return MyClass; }
759
760         /** Call this method to find the matching \<connect> for a user, and to check them against it.
761          */
762         void CheckClass(bool clone_count = true);
763
764         /** Server address and port that this user is connected to.
765          */
766         irc::sockets::sockaddrs server_sa;
767
768         /**
769          * @return The port number of this user.
770          */
771         int GetServerPort();
772
773         /** Recursion fix: user is out of SendQ and will be quit as soon as possible.
774          * This can't be handled normally because QuitUser itself calls Write on other
775          * users, which could trigger their SendQ to overrun.
776          */
777         unsigned int quitting_sendq:1;
778
779         /** has the user responded to their previous ping?
780          */
781         unsigned int lastping:1;
782
783         /** This is true if the user matched an exception (E:Line). It is used to save time on ban checks.
784          */
785         unsigned int exempt:1;
786
787         /** Used by PING checking code
788          */
789         time_t nping;
790
791         /** Time that the connection last sent a message, used to calculate idle time
792          */
793         time_t idle_lastmsg;
794
795         /** This value contains how far into the penalty threshold the user is.
796          * This is used either to enable fake lag or for excess flood quits
797          */
798         unsigned int CommandFloodPenalty;
799
800         already_sent_t already_sent;
801
802         /** Check if the user matches a G or K line, and disconnect them if they do.
803          * @param doZline True if ZLines should be checked (if IP has changed since initial connect)
804          * Returns true if the user matched a ban, false else.
805          */
806         bool CheckLines(bool doZline = false);
807
808         /** Use this method to fully connect a user.
809          * This will send the message of the day, check G/K/E lines, etc.
810          */
811         void FullConnect();
812
813         /** Set the connect class to which this user belongs to.
814          * @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.
815          * @return A reference to this user's current connect class.
816          */
817         void SetClass(const std::string &explicit_name = "");
818
819         bool SetClientIP(const std::string& address, bool recheck_eline = true) CXX11_OVERRIDE;
820
821         void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true) CXX11_OVERRIDE;
822
823         /** Send a NOTICE message from the local server to the user.
824          * The message will be sent even if the user is connected to a remote server.
825          * @param text Text to send
826          */
827         void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
828
829         /** Returns true or false for if a user can execute a privilaged oper command.
830          * This is done by looking up their oper type from User::oper, then referencing
831          * this to their oper classes and checking the commands they can execute.
832          * @param command A command (should be all CAPS)
833          * @return True if this user can execute the command
834          */
835         bool HasPermission(const std::string &command) CXX11_OVERRIDE;
836
837         /** Returns true if a user has a given permission.
838          * This is used to check whether or not users may perform certain actions which admins may not wish to give to
839          * all operators, yet are not commands. An example might be oper override, mass messaging (/notice $*), etc.
840          *
841          * @param privstr The priv to chec, e.g. "users/override/topic". These are loaded free-form from the config file.
842          * @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.
843          * @return True if this user has the permission in question.
844          */
845         bool HasPrivPermission(const std::string &privstr, bool noisy = false) CXX11_OVERRIDE;
846
847         /** Returns true or false if a user can set a privileged user or channel mode.
848          * This is done by looking up their oper type from User::oper, then referencing
849          * this to their oper classes, and checking the modes they can set.
850          * @param mh Mode to check
851          * @return True if the user can set or unset this mode.
852          */
853         bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
854
855         /** Change nick to uuid, unset REG_NICK and send a nickname overruled numeric.
856          * This is called when another user (either local or remote) needs the nick of this user and this user
857          * isn't registered.
858          */
859         void OverruleNick();
860
861         /** Send a protocol event to the user, consisting of one or more messages.
862          * @param protoev Event to send, may contain any number of messages.
863          */
864         void Send(ClientProtocol::Event& protoev);
865
866         /** Send a single message to the user.
867          * @param protoevprov Protocol event provider.
868          * @param msg Message to send.
869          */
870         void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
871 };
872
873 class RemoteUser : public User
874 {
875  public:
876         RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
877         {
878         }
879 };
880
881 class CoreExport FakeUser : public User
882 {
883  public:
884         FakeUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_SERVER)
885         {
886                 nick = srv->GetName();
887         }
888
889         FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
890                 : User(uid, new Server(sname, sdesc), USERTYPE_SERVER)
891         {
892                 nick = sname;
893         }
894
895         CullResult cull() CXX11_OVERRIDE;
896         const std::string& GetFullHost() CXX11_OVERRIDE;
897         const std::string& GetFullRealHost() CXX11_OVERRIDE;
898 };
899
900 /* Faster than dynamic_cast */
901 /** Is a local user */
902 inline LocalUser* IS_LOCAL(User* u)
903 {
904         return u->usertype == USERTYPE_LOCAL ? static_cast<LocalUser*>(u) : NULL;
905 }
906 /** Is a remote user */
907 inline RemoteUser* IS_REMOTE(User* u)
908 {
909         return u->usertype == USERTYPE_REMOTE ? static_cast<RemoteUser*>(u) : NULL;
910 }
911 /** Is a server fakeuser */
912 inline FakeUser* IS_SERVER(User* u)
913 {
914         return u->usertype == USERTYPE_SERVER ? static_cast<FakeUser*>(u) : NULL;
915 }
916
917 inline bool User::IsModeSet(const ModeHandler* mh) const
918 {
919         return (modes[mh->GetId()]);
920 }
921
922 inline bool User::IsModeSet(UserModeReference& moderef) const
923 {
924         if (!moderef)
925                 return false;
926         return IsModeSet(*moderef);
927 }
928
929 inline void User::SetMode(ModeHandler* mh, bool value)
930 {
931         modes[mh->GetId()] = value;
932 }