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