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