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