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 HasPermission(const std::string &command);
460 
469  virtual bool HasPrivPermission(const std::string &privstr, bool noisy = false);
470 
477  virtual bool HasModePermission(const ModeHandler* mh) const;
478 
483  const std::string& MakeHost();
484 
489  const std::string& MakeHostIP();
490 
494  void Oper(OperInfo* info);
495 
499  void UnOper();
500 
504  void WriteNotice(const std::string& text);
505 
509  virtual void WriteRemoteNotice(const std::string& text);
510 
511  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
512 
513  template <typename T1>
514  void WriteRemoteNumeric(unsigned int numeric, T1 p1)
515  {
516  Numeric::Numeric n(numeric);
517  n.push(p1);
518  WriteRemoteNumeric(n);
519  }
520 
521  template <typename T1, typename T2>
522  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
523  {
524  Numeric::Numeric n(numeric);
525  n.push(p1);
526  n.push(p2);
527  WriteRemoteNumeric(n);
528  }
529 
530  template <typename T1, typename T2, typename T3>
531  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
532  {
533  Numeric::Numeric n(numeric);
534  n.push(p1);
535  n.push(p2);
536  n.push(p3);
537  WriteRemoteNumeric(n);
538  }
539 
540  template <typename T1, typename T2, typename T3, typename T4>
541  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
542  {
543  Numeric::Numeric n(numeric);
544  n.push(p1);
545  n.push(p2);
546  n.push(p3);
547  n.push(p4);
548  WriteRemoteNumeric(n);
549  }
550 
551  template <typename T1, typename T2, typename T3, typename T4, typename T5>
552  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
553  {
554  Numeric::Numeric n(numeric);
555  n.push(p1);
556  n.push(p2);
557  n.push(p3);
558  n.push(p4);
559  n.push(p5);
560  WriteRemoteNumeric(n);
561  }
562 
563  void WriteNumeric(const Numeric::Numeric& numeric);
564 
565  template <typename T1>
566  void WriteNumeric(unsigned int numeric, T1 p1)
567  {
568  Numeric::Numeric n(numeric);
569  n.push(p1);
570  WriteNumeric(n);
571  }
572 
573  template <typename T1, typename T2>
574  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
575  {
576  Numeric::Numeric n(numeric);
577  n.push(p1);
578  n.push(p2);
579  WriteNumeric(n);
580  }
581 
582  template <typename T1, typename T2, typename T3>
583  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
584  {
585  Numeric::Numeric n(numeric);
586  n.push(p1);
587  n.push(p2);
588  n.push(p3);
589  WriteNumeric(n);
590  }
591 
592  template <typename T1, typename T2, typename T3, typename T4>
593  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
594  {
595  Numeric::Numeric n(numeric);
596  n.push(p1);
597  n.push(p2);
598  n.push(p3);
599  n.push(p4);
600  WriteNumeric(n);
601  }
602 
603  template <typename T1, typename T2, typename T3, typename T4, typename T5>
604  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
605  {
606  Numeric::Numeric n(numeric);
607  n.push(p1);
608  n.push(p2);
609  n.push(p3);
610  n.push(p4);
611  n.push(p5);
612  WriteNumeric(n);
613  }
614 
619  void WriteCommonRaw(ClientProtocol::Event& protoev, bool include_self = true);
620 
629  void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
630 
635  bool SharesChannelWith(User *other);
636 
641  bool ChangeDisplayedHost(const std::string& host);
642 
647  void ChangeRealHost(const std::string& host, bool resetdisplay);
648 
656  bool ChangeIdent(const std::string& newident);
657 
662  bool ChangeRealName(const std::string& real);
663 
669  bool ChangeNick(const std::string& newnick, time_t newts = 0);
670 
674  void PurgeEmptyChannels();
675 
678  virtual ~User();
679  CullResult cull() CXX11_OVERRIDE;
680 };
681 
682 class CoreExport UserIOHandler : public StreamSocket
683 {
684  private:
685  size_t checked_until;
686  public:
687  LocalUser* const user;
689  : StreamSocket(StreamSocket::SS_USER)
690  , checked_until(0)
691  , user(me)
692  {
693  }
694  void OnDataReady() CXX11_OVERRIDE;
695  bool OnSetEndPoint(const irc::sockets::sockaddrs& local, const irc::sockets::sockaddrs& remote) CXX11_OVERRIDE;
696  void OnError(BufferedSocketError error) CXX11_OVERRIDE;
697 
703  void AddWriteBuf(const std::string &data);
704 };
705 
706 typedef unsigned int already_sent_t;
707 
708 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
709 {
713  void Write(const ClientProtocol::SerializedMessage& serialized);
714 
720  void Send(ClientProtocol::Event& protoev, ClientProtocol::MessageList& msglist);
721 
724  static ClientProtocol::MessageList sendmsglist;
725 
726  public:
728  CullResult cull() CXX11_OVERRIDE;
729 
730  UserIOHandler eh;
731 
735 
738  unsigned int bytes_in;
739 
742  unsigned int bytes_out;
743 
746  unsigned int cmds_in;
747 
750  unsigned int cmds_out;
751 
756  std::string password;
757 
761 
765  ConnectClass* GetClass() const { return MyClass; }
766 
769  void CheckClass(bool clone_count = true);
770 
774 
778  int GetServerPort();
779 
784  unsigned int quitting_sendq:1;
785 
788  unsigned int lastping:1;
789 
792  unsigned int exempt:1;
793 
796  time_t nping;
797 
800  time_t idle_lastmsg;
801 
805  unsigned int CommandFloodPenalty;
806 
807  already_sent_t already_sent;
808 
813  bool CheckLines(bool doZline = false);
814 
818  void FullConnect();
819 
824  void SetClass(const std::string &explicit_name = "");
825 
826  bool SetClientIP(const std::string& address) CXX11_OVERRIDE;
827 
828  void SetClientIP(const irc::sockets::sockaddrs& sa) CXX11_OVERRIDE;
829 
834  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
835 
842  bool HasPermission(const std::string &command) CXX11_OVERRIDE;
843 
852  bool HasPrivPermission(const std::string &privstr, bool noisy = false) CXX11_OVERRIDE;
853 
860  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
861 
866  void OverruleNick();
867 
871  void Send(ClientProtocol::Event& protoev);
872 
877  void Send(ClientProtocol::EventProvider& protoevprov, ClientProtocol::Message& msg);
878 };
879 
880 class RemoteUser : public User
881 {
882  public:
883  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
884  {
885  }
886 };
887 
888 class CoreExport FakeUser : public User
889 {
890  public:
891  FakeUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_SERVER)
892  {
893  nick = srv->GetName();
894  }
895 
896  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
897  : User(uid, new Server(sname, sdesc), USERTYPE_SERVER)
898  {
899  nick = sname;
900  }
901 
902  CullResult cull() CXX11_OVERRIDE;
903  const std::string& GetFullHost() CXX11_OVERRIDE;
904  const std::string& GetFullRealHost() CXX11_OVERRIDE;
905 };
906 
907 /* Faster than dynamic_cast */
909 inline LocalUser* IS_LOCAL(User* u)
910 {
911  return u->usertype == USERTYPE_LOCAL ? static_cast<LocalUser*>(u) : NULL;
912 }
914 inline RemoteUser* IS_REMOTE(User* u)
915 {
916  return u->usertype == USERTYPE_REMOTE ? static_cast<RemoteUser*>(u) : NULL;
917 }
919 inline FakeUser* IS_SERVER(User* u)
920 {
921  return u->usertype == USERTYPE_SERVER ? static_cast<FakeUser*>(u) : NULL;
922 }
923 
924 inline bool User::IsModeSet(const ModeHandler* mh) const
925 {
926  return ((mh->GetId() != ModeParser::MODEID_MAX) && (modes[mh->GetId()]));
927 }
928 
929 inline bool User::IsModeSet(UserModeReference& moderef) const
930 {
931  if (!moderef)
932  return false;
933  return IsModeSet(*moderef);
934 }
935 
936 inline void User::SetMode(ModeHandler* mh, bool value)
937 {
938  if (mh && mh->GetId() != ModeParser::MODEID_MAX)
939  modes[mh->GetId()] = value;
940 }
bool IsAway() const
Definition: users.h:416
time_t GetRegTimeout()
Definition: users.h:158
virtual const std::string & GetFullRealHost()
Definition: users.cpp:149
unsigned int bytes_in
Definition: users.h:738
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:742
unsigned long GetSendqHardMax()
Definition: users.h:180
ConnectClass * GetClass() const
Definition: users.h:765
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:329
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
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:800
insp::flat_set< int > ports
Definition: users.h:141
Definition: users.h:66
Definition: users.h:708
Definition: socket.h:58
bool IsOper() const
Definition: users.h:422
unsigned int cmds_in
Definition: users.h:746
unsigned int GetPingTime()
Definition: users.h:165
time_t nping
Definition: users.h:796
virtual bool HasPermission(const std::string &command)
Definition: users.cpp:182
char type
Definition: users.h:71
ClientProtocol::Serializer * serializer
Definition: users.h:734
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:792
Definition: users.h:880
Definition: base.h:85
Definition: socket.h:78
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:936
reference< OperInfo > oper
Definition: users.h:339
unsigned int cmds_out
Definition: users.h:750
Definition: clientprotocol.h:24
std::string password
Definition: users.h:756
const std::string & GetName() const
Definition: server.h:54
virtual bool HasPrivPermission(const std::string &privstr, bool noisy=false)
Definition: users.cpp:198
std::string nick
Definition: users.h:302
virtual const std::string & GetFullHost()
Definition: users.cpp:139
bool IsModeSet(unsigned char m) const
Definition: users.cpp:38
Definition: users.h:888
const UserType usertype
Definition: users.h:355
irc::sockets::sockaddrs client_sa
Definition: users.h:296
reference< ConnectClass > MyClass
Definition: users.h:760
virtual void WriteRemoteNotice(const std::string &text)
Definition: users.cpp:845
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:712
unsigned int lastping
Definition: users.h:788
unsigned long GetMaxLocal()
Definition: users.h:206
irc::sockets::sockaddrs server_sa
Definition: users.h:773
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:784
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:682
unsigned int CommandFloodPenalty
Definition: users.h:805
CullResult cull() CXX11_OVERRIDE
Definition: base.cpp:200
bool fakelag
Definition: users.h:74
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
ChanList chans
Definition: users.h:321
bool maxconnwarn
Definition: users.h:123