InspIRCd  3.0
users.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
6  * Copyright (C) 2008 Thomas Stagner <[email protected]>
7  * Copyright (C) 2003-2007 Craig Edwards <[email protected]>
8  * Copyright (C) 2007 Burlex <[email protected]???>
9  * Copyright (C) 2007 Dennis Friis <[email protected]>
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 
34 enum ClassTypes {
36  CC_ALLOW = 0,
38  CC_DENY = 1,
40  CC_NAMED = 2
41 };
42 
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 
66 struct CoreExport ConnectClass : public refcountbase
67 {
68  reference<ConfigTag> config;
71  char type;
72 
74  bool fakelag;
75 
78  std::string name;
79 
82  unsigned int registration_timeout;
83 
86  std::string host;
87 
90  unsigned int pingtime;
91 
95  unsigned long softsendqmax;
96 
100  unsigned long hardsendqmax;
101 
104  unsigned long recvqmax;
105 
108  unsigned int penaltythreshold;
109 
111  unsigned int commandrate;
112 
115  unsigned long maxlocal;
116 
119  unsigned long maxglobal;
120 
124 
127  unsigned int maxchans;
128 
132  unsigned long limit;
133 
137 
142 
145  ConnectClass(ConfigTag* tag, char type, const std::string& mask);
148  ConnectClass(ConfigTag* tag, char type, const std::string& mask, const ConnectClass& parent);
149 
151  void Update(const ConnectClass* newSettings);
152 
153  const std::string& GetName() { return name; }
154  const std::string& GetHost() { return host; }
155 
158  time_t GetRegTimeout()
159  {
160  return (registration_timeout ? registration_timeout : 90);
161  }
162 
165  unsigned int GetPingTime()
166  {
167  return (pingtime ? pingtime : 120);
168  }
169 
173  unsigned long GetSendqSoftMax()
174  {
175  return (softsendqmax ? softsendqmax : 4096);
176  }
177 
180  unsigned long GetSendqHardMax()
181  {
182  return (hardsendqmax ? hardsendqmax : 0x100000);
183  }
184 
187  unsigned long GetRecvqMax()
188  {
189  return (recvqmax ? recvqmax : 4096);
190  }
191 
194  unsigned int GetPenaltyThreshold()
195  {
196  return penaltythreshold ? penaltythreshold : (fakelag ? 10 : 20);
197  }
198 
199  unsigned int GetCommandRate()
200  {
201  return commandrate ? commandrate : 1000;
202  }
203 
206  unsigned long GetMaxLocal()
207  {
208  return maxlocal;
209  }
210 
213  unsigned long GetMaxGlobal()
214  {
215  return maxglobal;
216  }
217 };
218 
224 class CoreExport User : public Extensible
225 {
226  private:
229  std::string cached_fullhost;
230 
233  std::string cached_hostip;
234 
237  std::string cached_makehost;
238 
241  std::string cached_fullrealhost;
242 
245  std::string cachedip;
246 
248  std::string displayhost;
249 
251  std::string realhost;
252 
254  std::string realname;
255 
262  std::bitset<ModeParser::MODEID_MAX> modes;
263 
264  public:
269  {
270  public:
275  virtual void Execute(LocalUser* user) = 0;
276  };
277 
281 
284  time_t age;
285 
290  time_t signon;
291 
297 
302  std::string nick;
303 
307  const std::string uuid;
308 
312  std::string ident;
313 
317  std::bitset<64> snomasks;
318 
322 
326 
330  std::string awaymsg;
331 
335  time_t awaytime;
336 
340 
345  unsigned int registered:3;
346 
352  unsigned int quitting:1;
353 
355  const UserType usertype:2;
356 
360  const std::string& GetIPString();
361 
365  const std::string& GetHost(bool uncloak) const;
366 
368  const std::string& GetDisplayedHost() const;
369 
371  const std::string& GetRealHost() const;
372 
374  const std::string& GetRealName() const;
375 
378  irc::sockets::cidr_mask GetCIDRMask();
379 
383  virtual bool SetClientIP(const std::string& address);
384 
385  virtual void SetClientIP(const irc::sockets::sockaddrs& sa);
386 
390  User(const std::string& uid, Server* srv, UserType objtype);
391 
397  virtual const std::string& GetFullHost();
398 
405  virtual const std::string& GetFullRealHost();
406 
410  void InvalidateCache();
411 
416  bool IsAway() const { return (!awaymsg.empty()); }
417 
422  bool IsOper() const { return oper; }
423 
428  bool IsNoticeMaskSet(unsigned char sm);
429 
435  std::string GetModeLetters(bool includeparams = false) const;
436 
441  bool IsModeSet(unsigned char m) const;
442  bool IsModeSet(const ModeHandler* mh) const;
443  bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
444  bool IsModeSet(UserModeReference& moderef) const;
445 
450  void SetMode(ModeHandler* mh, bool value);
451  void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
452 
459  virtual bool HasCommandPermission(const std::string& command);
460 
468  virtual bool HasPrivPermission(const std::string& privstr);
469 
476  virtual bool HasModePermission(const ModeHandler* mh) const;
477 
482  const std::string& MakeHost();
483 
488  const std::string& MakeHostIP();
489 
493  void Oper(OperInfo* info);
494 
498  void UnOper();
499 
503  void WriteNotice(const std::string& text);
504 
508  virtual void WriteRemoteNotice(const std::string& text);
509 
510  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
511 
512  template <typename T1>
513  void WriteRemoteNumeric(unsigned int numeric, T1 p1)
514  {
515  Numeric::Numeric n(numeric);
516  n.push(p1);
517  WriteRemoteNumeric(n);
518  }
519 
520  template <typename T1, typename T2>
521  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
522  {
523  Numeric::Numeric n(numeric);
524  n.push(p1);
525  n.push(p2);
526  WriteRemoteNumeric(n);
527  }
528 
529  template <typename T1, typename T2, typename T3>
530  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
531  {
532  Numeric::Numeric n(numeric);
533  n.push(p1);
534  n.push(p2);
535  n.push(p3);
536  WriteRemoteNumeric(n);
537  }
538 
539  template <typename T1, typename T2, typename T3, typename T4>
540  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
541  {
542  Numeric::Numeric n(numeric);
543  n.push(p1);
544  n.push(p2);
545  n.push(p3);
546  n.push(p4);
547  WriteRemoteNumeric(n);
548  }
549 
550  template <typename T1, typename T2, typename T3, typename T4, typename T5>
551  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
552  {
553  Numeric::Numeric n(numeric);
554  n.push(p1);
555  n.push(p2);
556  n.push(p3);
557  n.push(p4);
558  n.push(p5);
559  WriteRemoteNumeric(n);
560  }
561 
562  void WriteNumeric(const Numeric::Numeric& numeric);
563 
564  template <typename T1>
565  void WriteNumeric(unsigned int numeric, T1 p1)
566  {
567  Numeric::Numeric n(numeric);
568  n.push(p1);
569  WriteNumeric(n);
570  }
571 
572  template <typename T1, typename T2>
573  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
574  {
575  Numeric::Numeric n(numeric);
576  n.push(p1);
577  n.push(p2);
578  WriteNumeric(n);
579  }
580 
581  template <typename T1, typename T2, typename T3>
582  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
583  {
584  Numeric::Numeric n(numeric);
585  n.push(p1);
586  n.push(p2);
587  n.push(p3);
588  WriteNumeric(n);
589  }
590 
591  template <typename T1, typename T2, typename T3, typename T4>
592  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
593  {
594  Numeric::Numeric n(numeric);
595  n.push(p1);
596  n.push(p2);
597  n.push(p3);
598  n.push(p4);
599  WriteNumeric(n);
600  }
601 
602  template <typename T1, typename T2, typename T3, typename T4, typename T5>
603  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
604  {
605  Numeric::Numeric n(numeric);
606  n.push(p1);
607  n.push(p2);
608  n.push(p3);
609  n.push(p4);
610  n.push(p5);
611  WriteNumeric(n);
612  }
613 
618  void WriteCommonRaw(ClientProtocol::Event& protoev, bool include_self = true);
619 
628  void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
629 
634  bool SharesChannelWith(User *other);
635 
640  bool ChangeDisplayedHost(const std::string& host);
641 
646  void ChangeRealHost(const std::string& host, bool resetdisplay);
647 
655  bool ChangeIdent(const std::string& newident);
656 
661  bool ChangeRealName(const std::string& real);
662 
668  bool ChangeNick(const std::string& newnick, time_t newts = 0);
669 
673  void PurgeEmptyChannels();
674 
677  virtual ~User();
678  CullResult cull() CXX11_OVERRIDE;
679 
681  bool Deserialize(Data& data) CXX11_OVERRIDE;
682 
684  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
685 };
686 
687 class CoreExport UserIOHandler : public StreamSocket
688 {
689  private:
690  size_t checked_until;
691  public:
692  LocalUser* const user;
694  : StreamSocket(StreamSocket::SS_USER)
695  , checked_until(0)
696  , user(me)
697  {
698  }
699  void OnDataReady() CXX11_OVERRIDE;
700  bool OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote) CXX11_OVERRIDE;
701  void OnError(BufferedSocketError error) CXX11_OVERRIDE;
702 
708  void AddWriteBuf(const std::string &data);
709 
713  void SwapInternals(UserIOHandler& other);
714 };
715 
716 typedef unsigned int already_sent_t;
717 
718 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
719 {
723  void Write(const ClientProtocol::SerializedMessage& serialized);
724 
730  void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
731 
734  static ClientProtocol::MessageList sendmsglist;
735 
736  public:
738  LocalUser(int fd, const std::string& uuid, Serializable::Data& data);
739 
740  CullResult cull() CXX11_OVERRIDE;
741 
742  UserIOHandler eh;
743 
747 
750  unsigned int bytes_in;
751 
754  unsigned int bytes_out;
755 
758  unsigned int cmds_in;
759 
762  unsigned int cmds_out;
763 
768  std::string password;
769 
773 
777  ConnectClass* GetClass() const { return MyClass; }
778 
781  void CheckClass(bool clone_count = true);
782 
786 
791  unsigned int quitting_sendq:1;
792 
795  unsigned int lastping:1;
796 
799  unsigned int exempt:1;
800 
802  time_t nextping;
803 
806  time_t idle_lastmsg;
807 
811  unsigned int CommandFloodPenalty;
812 
813  already_sent_t already_sent;
814 
819  bool CheckLines(bool doZline = false);
820 
824  void FullConnect();
825 
830  void SetClass(const std::string &explicit_name = "");
831 
832  bool SetClientIP(const std::string& address) CXX11_OVERRIDE;
833 
834  void SetClientIP(const irc::sockets::sockaddrs& sa) CXX11_OVERRIDE;
835 
840  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
841 
848  bool HasCommandPermission(const std::string& command) CXX11_OVERRIDE;
849 
857  bool HasPrivPermission(const std::string& privstr) CXX11_OVERRIDE;
858 
865  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
866 
871  void OverruleNick();
872 
876  void Send(ClientProtocol::Event& protoev);
877 
882  void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
883 
885  bool Deserialize(Data& data) CXX11_OVERRIDE;
886 
888  bool Serialize(Serializable::Data& data) CXX11_OVERRIDE;
889 };
890 
891 class RemoteUser : public User
892 {
893  public:
894  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
895  {
896  }
897 };
898 
899 class CoreExport FakeUser : public User
900 {
901  public:
902  FakeUser(const std::string& uid, Server* srv)
903  : User(uid, srv, USERTYPE_SERVER)
904  {
905  nick = srv->GetName();
906  }
907 
908  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
909  : User(uid, new Server(uid, sname, sdesc), USERTYPE_SERVER)
910  {
911  nick = sname;
912  }
913 
914  CullResult cull() CXX11_OVERRIDE;
915  const std::string& GetFullHost() CXX11_OVERRIDE;
916  const std::string& GetFullRealHost() CXX11_OVERRIDE;
917 };
918 
919 /* Faster than dynamic_cast */
921 inline LocalUser* IS_LOCAL(User* u)
922 {
923  return (u != NULL && u->usertype == USERTYPE_LOCAL) ? static_cast<LocalUser*>(u) : NULL;
924 }
926 inline RemoteUser* IS_REMOTE(User* u)
927 {
928  return (u != NULL && u->usertype == USERTYPE_REMOTE) ? static_cast<RemoteUser*>(u) : NULL;
929 }
931 inline FakeUser* IS_SERVER(User* u)
932 {
933  return (u != NULL && u->usertype == USERTYPE_SERVER) ? static_cast<FakeUser*>(u) : NULL;
934 }
935 
936 inline bool User::IsModeSet(const ModeHandler* mh) const
937 {
938  return ((mh->GetId() != ModeParser::MODEID_MAX) && (modes[mh->GetId()]));
939 }
940 
941 inline bool User::IsModeSet(UserModeReference& moderef) const
942 {
943  if (!moderef)
944  return false;
945  return IsModeSet(*moderef);
946 }
947 
948 inline void User::SetMode(ModeHandler* mh, bool value)
949 {
950  if (mh && mh->GetId() != ModeParser::MODEID_MAX)
951  modes[mh->GetId()] = value;
952 }
User::chans
ChanList chans
Definition: users.h:321
ConnectClass::limit
unsigned long limit
Definition: users.h:132
User::Serialize
bool Serialize(Serializable::Data &data) CXX11_OVERRIDE
Definition: serializable.cpp:174
ClientProtocol::EventProvider
Definition: clientprotocol.h:614
OperInfo
Definition: configreader.h:172
ConnectClass::name
std::string name
Definition: users.h:78
Extensible
Definition: extensible.h:136
FakeUser
Definition: users.h:899
LocalUser::bytes_out
unsigned int bytes_out
Definition: users.h:754
irc::sockets::cidr_mask
Definition: socket.h:78
ConnectClass::fakelag
bool fakelag
Definition: users.h:74
User::signon
time_t signon
Definition: users.h:290
ConnectClass
Definition: users.h:66
User::ident
std::string ident
Definition: users.h:312
Serializable
Definition: serialize.h:23
insp::flat_set< int >
RemoteUser
Definition: users.h:891
Server
Definition: server.h:22
ClientProtocol::Event
Definition: clientprotocol.h:461
User::IsAway
bool IsAway() const
Definition: users.h:416
UserIOHandler
Definition: users.h:687
ConnectClass::GetPenaltyThreshold
unsigned int GetPenaltyThreshold()
Definition: users.h:194
Serializable::Data
Definition: serialize.h:30
Server::GetName
const std::string & GetName() const
Definition: server.h:74
User::awaymsg
std::string awaymsg
Definition: users.h:330
ConnectClass::commandrate
unsigned int commandrate
Definition: users.h:111
ConnectClass::penaltythreshold
unsigned int penaltythreshold
Definition: users.h:108
LocalUser::idle_lastmsg
time_t idle_lastmsg
Definition: users.h:806
reference< ConfigTag >
ConnectClass::maxchans
unsigned int maxchans
Definition: users.h:127
User::HasModePermission
virtual bool HasModePermission(const ModeHandler *mh) const
Definition: users.cpp:164
LocalUser::cmds_in
unsigned int cmds_in
Definition: users.h:758
ConnectClass::GetRecvqMax
unsigned long GetRecvqMax()
Definition: users.h:187
UserModeReference
Definition: dynref.h:130
refcountbase
Definition: base.h:85
ConnectClass::type
char type
Definition: users.h:71
User::cull
CullResult cull() CXX11_OVERRIDE
Definition: users.cpp:334
LocalUser::serializer
ClientProtocol::Serializer * serializer
Definition: users.h:746
User::age
time_t age
Definition: users.h:284
StreamSocket
Definition: inspsocket.h:108
User::oper
reference< OperInfo > oper
Definition: users.h:339
ConnectClass::ports
insp::flat_set< int > ports
Definition: users.h:141
irc::sockets::sockaddrs
Definition: socket.h:58
User::IsOper
bool IsOper() const
Definition: users.h:422
ConnectClass::resolvehostnames
bool resolvehostnames
Definition: users.h:136
User::HasPrivPermission
virtual bool HasPrivPermission(const std::string &privstr)
Definition: users.cpp:204
User::ForEachNeighborHandler
Definition: users.h:268
ConnectClass::GetSendqSoftMax
unsigned long GetSendqSoftMax()
Definition: users.h:173
LocalUser::exempt
unsigned int exempt
Definition: users.h:799
ConnectClass::softsendqmax
unsigned long softsendqmax
Definition: users.h:95
User::GetFullHost
virtual const std::string & GetFullHost()
Definition: users.cpp:146
ConnectClass::maxlocal
unsigned long maxlocal
Definition: users.h:115
ConnectClass::maxconnwarn
bool maxconnwarn
Definition: users.h:123
ConnectClass::hardsendqmax
unsigned long hardsendqmax
Definition: users.h:100
User::client_sa
irc::sockets::sockaddrs client_sa
Definition: users.h:296
User::nick
std::string nick
Definition: users.h:302
LocalUser::MyClass
reference< ConnectClass > MyClass
Definition: users.h:772
ConnectClass::GetMaxGlobal
unsigned long GetMaxGlobal()
Definition: users.h:213
User::snomasks
std::bitset< 64 > snomasks
Definition: users.h:317
insp::intrusive_list_node
Definition: intrusive_list.h:33
ConfigTag
Definition: configreader.h:37
User::SetMode
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:948
insp::intrusive_list< Membership >
User::IsModeSet
bool IsModeSet(unsigned char m) const
Definition: users.cpp:38
LocalUser::cmds_out
unsigned int cmds_out
Definition: users.h:762
ConnectClass::host
std::string host
Definition: users.h:86
User::usertype
const UserType usertype
Definition: users.h:355
ModeHandler::GetId
Id GetId() const
Definition: mode.h:244
User::User
User(const std::string &uid, Server *srv, UserType objtype)
Definition: users.cpp:68
LocalUser::password
std::string password
Definition: users.h:768
User::WriteRemoteNotice
virtual void WriteRemoteNotice(const std::string &text)
Definition: users.cpp:854
User::SetClientIP
virtual bool SetClientIP(const std::string &address)
Definition: users.cpp:722
ConnectClass::recvqmax
unsigned long recvqmax
Definition: users.h:104
ConnectClass::GetRegTimeout
time_t GetRegTimeout()
Definition: users.h:158
User
Definition: users.h:224
User::uuid
const std::string uuid
Definition: users.h:307
CullResult
Definition: base.h:31
ConnectClass::GetPingTime
unsigned int GetPingTime()
Definition: users.h:165
ConnectClass::GetMaxLocal
unsigned long GetMaxLocal()
Definition: users.h:206
LocalUser::server_sa
irc::sockets::sockaddrs server_sa
Definition: users.h:785
User::awaytime
time_t awaytime
Definition: users.h:335
ClientProtocol::Message
Definition: clientprotocol.h:180
LocalUser::nextping
time_t nextping
Definition: users.h:802
Extensible::cull
CullResult cull() CXX11_OVERRIDE
Definition: base.cpp:200
User::server
Server * server
Definition: users.h:325
User::ChanList
insp::intrusive_list< Membership > ChanList
Definition: users.h:280
ConnectClass::maxglobal
unsigned long maxglobal
Definition: users.h:119
LocalUser::lastping
unsigned int lastping
Definition: users.h:795
ConnectClass::pingtime
unsigned int pingtime
Definition: users.h:90
LocalUser::bytes_in
unsigned int bytes_in
Definition: users.h:750
Numeric::Numeric
Definition: numeric.h:29
User::quitting
unsigned int quitting
Definition: users.h:352
LocalUser::CommandFloodPenalty
unsigned int CommandFloodPenalty
Definition: users.h:811
ConnectClass::registration_timeout
unsigned int registration_timeout
Definition: users.h:82
LocalUser::quitting_sendq
unsigned int quitting_sendq
Definition: users.h:791
ClientProtocol::Serializer
Definition: clientprotocol.h:668
User::Deserialize
bool Deserialize(Data &data) CXX11_OVERRIDE
Definition: serializable.cpp:108
LocalUser::GetClass
ConnectClass * GetClass() const
Definition: users.h:777
ConnectClass::GetSendqHardMax
unsigned long GetSendqHardMax()
Definition: users.h:180
ModeParser::MODEID_MAX
static const ModeHandler::Id MODEID_MAX
Definition: mode.h:548
ModeHandler
Definition: mode.h:95
User::GetFullRealHost
virtual const std::string & GetFullRealHost()
Definition: users.cpp:155
LocalUser
Definition: users.h:718
User::registered
unsigned int registered
Definition: users.h:345
User::HasCommandPermission
virtual bool HasCommandPermission(const std::string &command)
Definition: users.cpp:188