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 };
680 
681 class CoreExport UserIOHandler : public StreamSocket
682 {
683  private:
684  size_t checked_until;
685  public:
686  LocalUser* const user;
688  : StreamSocket(StreamSocket::SS_USER)
689  , checked_until(0)
690  , user(me)
691  {
692  }
693  void OnDataReady() CXX11_OVERRIDE;
694  bool OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote) CXX11_OVERRIDE;
695  void OnError(BufferedSocketError error) CXX11_OVERRIDE;
696 
702  void AddWriteBuf(const std::string &data);
703 };
704 
705 typedef unsigned int already_sent_t;
706 
707 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
708 {
712  void Write(const ClientProtocol::SerializedMessage& serialized);
713 
719  void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
720 
723  static ClientProtocol::MessageList sendmsglist;
724 
725  public:
727  CullResult cull() CXX11_OVERRIDE;
728 
729  UserIOHandler eh;
730 
734 
737  unsigned int bytes_in;
738 
741  unsigned int bytes_out;
742 
745  unsigned int cmds_in;
746 
749  unsigned int cmds_out;
750 
755  std::string password;
756 
760 
764  ConnectClass* GetClass() const { return MyClass; }
765 
768  void CheckClass(bool clone_count = true);
769 
773 
778  unsigned int quitting_sendq:1;
779 
782  unsigned int lastping:1;
783 
786  unsigned int exempt:1;
787 
789  time_t nextping;
790 
793  time_t idle_lastmsg;
794 
798  unsigned int CommandFloodPenalty;
799 
800  already_sent_t already_sent;
801 
806  bool CheckLines(bool doZline = false);
807 
811  void FullConnect();
812 
817  void SetClass(const std::string &explicit_name = "");
818 
819  bool SetClientIP(const std::string& address) CXX11_OVERRIDE;
820 
821  void SetClientIP(const irc::sockets::sockaddrs& sa) CXX11_OVERRIDE;
822 
827  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
828 
835  bool HasCommandPermission(const std::string& command) CXX11_OVERRIDE;
836 
844  bool HasPrivPermission(const std::string& privstr) CXX11_OVERRIDE;
845 
852  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
853 
858  void OverruleNick();
859 
863  void Send(ClientProtocol::Event& protoev);
864 
869  void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
870 };
871 
872 class RemoteUser : public User
873 {
874  public:
875  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
876  {
877  }
878 };
879 
880 class CoreExport FakeUser : public User
881 {
882  public:
883  FakeUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_SERVER)
884  {
885  nick = srv->GetName();
886  }
887 
888  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
889  : User(uid, new Server(sname, sdesc), USERTYPE_SERVER)
890  {
891  nick = sname;
892  }
893 
894  CullResult cull() CXX11_OVERRIDE;
895  const std::string& GetFullHost() CXX11_OVERRIDE;
896  const std::string& GetFullRealHost() CXX11_OVERRIDE;
897 };
898 
899 /* Faster than dynamic_cast */
901 inline LocalUser* IS_LOCAL(User* u)
902 {
903  return u->usertype == USERTYPE_LOCAL ? static_cast<LocalUser*>(u) : NULL;
904 }
906 inline RemoteUser* IS_REMOTE(User* u)
907 {
908  return u->usertype == USERTYPE_REMOTE ? static_cast<RemoteUser*>(u) : NULL;
909 }
911 inline FakeUser* IS_SERVER(User* u)
912 {
913  return u->usertype == USERTYPE_SERVER ? static_cast<FakeUser*>(u) : NULL;
914 }
915 
916 inline bool User::IsModeSet(const ModeHandler* mh) const
917 {
918  return ((mh->GetId() != ModeParser::MODEID_MAX) && (modes[mh->GetId()]));
919 }
920 
921 inline bool User::IsModeSet(UserModeReference& moderef) const
922 {
923  if (!moderef)
924  return false;
925  return IsModeSet(*moderef);
926 }
927 
928 inline void User::SetMode(ModeHandler* mh, bool value)
929 {
930  if (mh && mh->GetId() != ModeParser::MODEID_MAX)
931  modes[mh->GetId()] = value;
932 }
bool IsAway() const
Definition: users.h:416
time_t GetRegTimeout()
Definition: users.h:158
virtual const std::string & GetFullRealHost()
Definition: users.cpp:146
unsigned int bytes_in
Definition: users.h:737
Definition: server.h:22
Definition: mode.h:95
unsigned int registered
Definition: users.h:345
unsigned int penaltythreshold
Definition: users.h:108
Definition: dynref.h:130
unsigned int quitting
Definition: users.h:352
std::string awaymsg
Definition: users.h:330
std::string ident
Definition: users.h:312
std::string host
Definition: users.h:86
unsigned int bytes_out
Definition: users.h:741
unsigned long GetSendqHardMax()
Definition: users.h:180
ConnectClass * GetClass() const
Definition: users.h:764
unsigned long GetSendqSoftMax()
Definition: users.h:173
unsigned long maxglobal
Definition: users.h:119
Definition: configreader.h:37
time_t signon
Definition: users.h:290
unsigned long recvqmax
Definition: users.h:104
Definition: intrusive_list.h:33
Definition: numeric.h:29
Definition: extensible.h:90
CullResult cull() CXX11_OVERRIDE
Definition: users.cpp:316
unsigned int commandrate
Definition: users.h:111
Definition: configreader.h:172
time_t age
Definition: users.h:284
unsigned int registration_timeout
Definition: users.h:82
unsigned long maxlocal
Definition: users.h:115
virtual bool HasModePermission(const ModeHandler *mh) const
Definition: users.cpp:155
unsigned long hardsendqmax
Definition: users.h:100
Definition: users.h:224
unsigned long softsendqmax
Definition: users.h:95
time_t idle_lastmsg
Definition: users.h:793
insp::flat_set< int > ports
Definition: users.h:141
Definition: users.h:66
Definition: users.h:707
Definition: socket.h:58
bool IsOper() const
Definition: users.h:422
unsigned int cmds_in
Definition: users.h:745
unsigned int GetPingTime()
Definition: users.h:165
char type
Definition: users.h:71
ClientProtocol::Serializer * serializer
Definition: users.h:733
Definition: inspsocket.h:108
std::string name
Definition: users.h:78
Definition: clientprotocol.h:461
unsigned long limit
Definition: users.h:132
Definition: clientprotocol.h:668
Definition: users.h:268
unsigned int exempt
Definition: users.h:786
Definition: users.h:872
Definition: base.h:85
Definition: socket.h:78
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:928
Definition: clientprotocol.h:180
reference< OperInfo > oper
Definition: users.h:339
unsigned int cmds_out
Definition: users.h:749
std::string password
Definition: users.h:755
const std::string & GetName() const
Definition: server.h:54
virtual bool HasPrivPermission(const std::string &privstr)
Definition: users.cpp:195
std::string nick
Definition: users.h:302
virtual const std::string & GetFullHost()
Definition: users.cpp:137
bool IsModeSet(unsigned char m) const
Definition: users.cpp:38
Definition: users.h:880
const UserType usertype
Definition: users.h:355
time_t nextping
Definition: users.h:789
irc::sockets::sockaddrs client_sa
Definition: users.h:296
reference< ConnectClass > MyClass
Definition: users.h:759
virtual void WriteRemoteNotice(const std::string &text)
Definition: users.cpp:827
Server * server
Definition: users.h:325
insp::intrusive_list< Membership > ChanList
Definition: users.h:280
virtual bool SetClientIP(const std::string &address)
Definition: users.cpp:694
unsigned int lastping
Definition: users.h:782
unsigned long GetMaxLocal()
Definition: users.h:206
irc::sockets::sockaddrs server_sa
Definition: users.h:772
unsigned int GetPenaltyThreshold()
Definition: users.h:194
unsigned long GetRecvqMax()
Definition: users.h:187
bool resolvehostnames
Definition: users.h:136
unsigned int quitting_sendq
Definition: users.h:778
std::bitset< 64 > snomasks
Definition: users.h:317
unsigned int maxchans
Definition: users.h:127
unsigned long GetMaxGlobal()
Definition: users.h:213
const std::string uuid
Definition: users.h:307
User(const std::string &uid, Server *srv, UserType objtype)
Definition: users.cpp:68
Definition: users.h:681
unsigned int CommandFloodPenalty
Definition: users.h:798
CullResult cull() CXX11_OVERRIDE
Definition: base.cpp:200
bool fakelag
Definition: users.h:74
Definition: clientprotocol.h:614
Definition: base.h:31
unsigned int pingtime
Definition: users.h:90
time_t awaytime
Definition: users.h:335
Id GetId() const
Definition: mode.h:241
virtual bool HasCommandPermission(const std::string &command)
Definition: users.cpp:179
ChanList chans
Definition: users.h:321
bool maxconnwarn
Definition: users.h:123