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