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