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 
253  std::bitset<ModeParser::MODEID_MAX> modes;
254 
255  public:
260  {
261  public:
266  virtual void Execute(LocalUser* user) = 0;
267  };
268 
272 
276  std::string host;
277 
280  time_t age;
281 
286  time_t signon;
287 
293 
298  std::string nick;
299 
303  const std::string uuid;
304 
308  std::string ident;
309 
313  std::string dhost;
314 
317  std::string fullname;
318 
322  std::bitset<64> snomasks;
323 
326  ChanList chans;
327 
331 
335  std::string awaymsg;
336 
340  time_t awaytime;
341 
345 
350  unsigned int registered:3;
351 
357  unsigned int quitting:1;
358 
360  const unsigned int usertype:2;
361 
365  const std::string& GetIPString();
366 
369  irc::sockets::cidr_mask GetCIDRMask();
370 
374  virtual bool SetClientIP(const char* sip, bool recheck_eline = true);
375 
376  virtual void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true);
377 
381  User(const std::string& uid, Server* srv, int objtype);
382 
388  virtual const std::string& GetFullHost();
389 
396  virtual const std::string& GetFullRealHost();
397 
401  void InvalidateCache();
402 
407  bool IsAway() const { return (!awaymsg.empty()); }
408 
413  bool IsOper() const { return oper; }
414 
419  bool IsNoticeMaskSet(unsigned char sm);
420 
426  std::string GetModeLetters(bool includeparams = false) const;
427 
432  bool IsModeSet(unsigned char m) const;
433  bool IsModeSet(const ModeHandler* mh) const;
434  bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
435  bool IsModeSet(UserModeReference& moderef) const;
436 
441  void SetMode(ModeHandler* mh, bool value);
442  void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
443 
450  virtual bool HasPermission(const std::string &command);
451 
460  virtual bool HasPrivPermission(const std::string &privstr, bool noisy = false);
461 
468  virtual bool HasModePermission(const ModeHandler* mh) const;
469 
474  const std::string& MakeHost();
475 
480  const std::string& MakeHostIP();
481 
485  void Oper(OperInfo* info);
486 
490  void UnOper();
491 
495  virtual void Write(const std::string &text);
496 
502  virtual void Write(const char *text, ...) CUSTOM_PRINTF(2, 3);
503 
508  void WriteServ(const std::string& text);
509 
515  void WriteServ(const char* text, ...) CUSTOM_PRINTF(2, 3);
516 
521  void WriteCommand(const char* command, const std::string& text);
522 
526  void WriteNotice(const std::string& text) { this->WriteCommand("NOTICE", ":" + text); }
527 
531  virtual void WriteRemoteNotice(const std::string& text);
532 
533  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
534 
535  template <typename T1>
536  void WriteRemoteNumeric(unsigned int numeric, T1 p1)
537  {
538  Numeric::Numeric n(numeric);
539  n.push(p1);
540  WriteRemoteNumeric(n);
541  }
542 
543  template <typename T1, typename T2>
544  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
545  {
546  Numeric::Numeric n(numeric);
547  n.push(p1);
548  n.push(p2);
549  WriteRemoteNumeric(n);
550  }
551 
552  template <typename T1, typename T2, typename T3>
553  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
554  {
555  Numeric::Numeric n(numeric);
556  n.push(p1);
557  n.push(p2);
558  n.push(p3);
559  WriteRemoteNumeric(n);
560  }
561 
562  template <typename T1, typename T2, typename T3, typename T4>
563  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
564  {
565  Numeric::Numeric n(numeric);
566  n.push(p1);
567  n.push(p2);
568  n.push(p3);
569  n.push(p4);
570  WriteRemoteNumeric(n);
571  }
572 
573  template <typename T1, typename T2, typename T3, typename T4, typename T5>
574  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
575  {
576  Numeric::Numeric n(numeric);
577  n.push(p1);
578  n.push(p2);
579  n.push(p3);
580  n.push(p4);
581  n.push(p5);
582  WriteRemoteNumeric(n);
583  }
584 
585  void WriteNumeric(const Numeric::Numeric& numeric);
586 
587  template <typename T1>
588  void WriteNumeric(unsigned int numeric, T1 p1)
589  {
590  Numeric::Numeric n(numeric);
591  n.push(p1);
592  WriteNumeric(n);
593  }
594 
595  template <typename T1, typename T2>
596  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
597  {
598  Numeric::Numeric n(numeric);
599  n.push(p1);
600  n.push(p2);
601  WriteNumeric(n);
602  }
603 
604  template <typename T1, typename T2, typename T3>
605  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
606  {
607  Numeric::Numeric n(numeric);
608  n.push(p1);
609  n.push(p2);
610  n.push(p3);
611  WriteNumeric(n);
612  }
613 
614  template <typename T1, typename T2, typename T3, typename T4>
615  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
616  {
617  Numeric::Numeric n(numeric);
618  n.push(p1);
619  n.push(p2);
620  n.push(p3);
621  n.push(p4);
622  WriteNumeric(n);
623  }
624 
625  template <typename T1, typename T2, typename T3, typename T4, typename T5>
626  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
627  {
628  Numeric::Numeric n(numeric);
629  n.push(p1);
630  n.push(p2);
631  n.push(p3);
632  n.push(p4);
633  n.push(p5);
634  WriteNumeric(n);
635  }
636 
641  void WriteFrom(User *user, const std::string &text);
642 
648  void WriteFrom(User *user, const char* text, ...) CUSTOM_PRINTF(3, 4);
649 
654  void WriteCommonRaw(const std::string &line, bool include_self = true);
655 
660  void WriteCommon(const char* text, ...) CUSTOM_PRINTF(2, 3);
661 
670  void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
671 
676  bool SharesChannelWith(User *other);
677 
686  bool ChangeDisplayedHost(const std::string& host);
687 
695  bool ChangeIdent(const std::string& newident);
696 
704  bool ChangeName(const std::string& gecos);
705 
711  bool ChangeNick(const std::string& newnick, time_t newts = 0);
712 
716  void PurgeEmptyChannels();
717 
720  virtual ~User();
721  virtual CullResult cull() CXX11_OVERRIDE;
722 };
723 
724 class CoreExport UserIOHandler : public StreamSocket
725 {
726  public:
727  LocalUser* const user;
728  UserIOHandler(LocalUser* me) : user(me) {}
729  void OnDataReady();
730  void OnError(BufferedSocketError error);
731 
737  void AddWriteBuf(const std::string &data);
738 };
739 
740 typedef unsigned int already_sent_t;
741 
742 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
743 {
744  public:
746  CullResult cull() CXX11_OVERRIDE;
747 
748  UserIOHandler eh;
749 
752  unsigned int bytes_in;
753 
756  unsigned int bytes_out;
757 
760  unsigned int cmds_in;
761 
764  unsigned int cmds_out;
765 
770  std::string password;
771 
775 
779  ConnectClass* GetClass() const { return MyClass; }
780 
783  void CheckClass(bool clone_count = true);
784 
788 
792  int GetServerPort();
793 
798  unsigned int quitting_sendq:1;
799 
802  unsigned int lastping:1;
803 
806  unsigned int exempt:1;
807 
810  time_t nping;
811 
814  time_t idle_lastmsg;
815 
819  unsigned int CommandFloodPenalty;
820 
821  already_sent_t already_sent;
822 
827  bool CheckLines(bool doZline = false);
828 
832  void FullConnect();
833 
838  void SetClass(const std::string &explicit_name = "");
839 
840  bool SetClientIP(const char* sip, bool recheck_eline = true) CXX11_OVERRIDE;
841 
842  void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true) CXX11_OVERRIDE;
843 
844  void Write(const std::string& text) CXX11_OVERRIDE;
845  void Write(const char*, ...) CXX11_OVERRIDE CUSTOM_PRINTF(2, 3);
846 
851  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
852 
859  bool HasPermission(const std::string &command) CXX11_OVERRIDE;
860 
869  bool HasPrivPermission(const std::string &privstr, bool noisy = false) CXX11_OVERRIDE;
870 
877  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
878 
883  void OverruleNick();
884 };
885 
886 class RemoteUser : public User
887 {
888  public:
889  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
890  {
891  }
892 };
893 
894 class CoreExport FakeUser : public User
895 {
896  public:
897  FakeUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_SERVER)
898  {
899  nick = srv->GetName();
900  }
901 
902  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
903  : User(uid, new Server(sname, sdesc), USERTYPE_SERVER)
904  {
905  nick = sname;
906  }
907 
908  virtual CullResult cull() CXX11_OVERRIDE;
909  virtual const std::string& GetFullHost() CXX11_OVERRIDE;
910  virtual const std::string& GetFullRealHost() CXX11_OVERRIDE;
911 };
912 
913 /* Faster than dynamic_cast */
915 inline LocalUser* IS_LOCAL(User* u)
916 {
917  return u->usertype == USERTYPE_LOCAL ? static_cast<LocalUser*>(u) : NULL;
918 }
920 inline RemoteUser* IS_REMOTE(User* u)
921 {
922  return u->usertype == USERTYPE_REMOTE ? static_cast<RemoteUser*>(u) : NULL;
923 }
925 inline FakeUser* IS_SERVER(User* u)
926 {
927  return u->usertype == USERTYPE_SERVER ? static_cast<FakeUser*>(u) : NULL;
928 }
929 
930 inline bool User::IsModeSet(const ModeHandler* mh) const
931 {
932  return (modes[mh->GetId()]);
933 }
934 
935 inline bool User::IsModeSet(UserModeReference& moderef) const
936 {
937  if (!moderef)
938  return false;
939  return IsModeSet(*moderef);
940 }
941 
942 inline void User::SetMode(ModeHandler* mh, bool value)
943 {
944  modes[mh->GetId()] = value;
945 }
bool IsAway() const
Definition: users.h:407
time_t GetRegTimeout()
Definition: users.h:158
unsigned int bytes_in
Definition: users.h:752
Definition: server.h:22
Definition: mode.h:95
unsigned int registered
Definition: users.h:350
unsigned int penaltythreshold
Definition: users.h:108
Definition: dynref.h:130
unsigned int quitting
Definition: users.h:357
std::string awaymsg
Definition: users.h:335
std::string ident
Definition: users.h:308
std::string host
Definition: users.h:86
Definition: hashcomp.h:69
unsigned int bytes_out
Definition: users.h:756
unsigned long GetSendqHardMax()
Definition: users.h:180
ConnectClass * GetClass() const
Definition: users.h:779
unsigned long GetSendqSoftMax()
Definition: users.h:173
unsigned long maxglobal
Definition: users.h:119
Definition: configreader.h:36
time_t signon
Definition: users.h:286
unsigned long recvqmax
Definition: users.h:104
Definition: intrusive_list.h:33
Definition: numeric.h:29
Definition: extensible.h:90
unsigned int commandrate
Definition: users.h:111
Definition: configreader.h:165
time_t age
Definition: users.h:280
unsigned int registration_timeout
Definition: users.h:82
unsigned long maxlocal
Definition: users.h:115
unsigned long hardsendqmax
Definition: users.h:100
STL namespace.
Definition: users.h:224
unsigned long softsendqmax
Definition: users.h:95
time_t idle_lastmsg
Definition: users.h:814
insp::flat_set< int > ports
Definition: users.h:141
Definition: users.h:66
Definition: users.h:742
Definition: socket.h:57
bool IsOper() const
Definition: users.h:413
unsigned int cmds_in
Definition: users.h:760
unsigned int GetPingTime()
Definition: users.h:165
time_t nping
Definition: users.h:810
void WriteNotice(const std::string &text)
Definition: users.h:526
char type
Definition: users.h:71
Definition: inspsocket.h:103
std::string name
Definition: users.h:78
unsigned long limit
Definition: users.h:132
std::string fullname
Definition: users.h:317
Definition: users.h:259
std::string dhost
Definition: users.h:313
unsigned int exempt
Definition: users.h:806
Definition: users.h:886
Definition: base.h:85
Definition: socket.h:74
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:942
Numeric & push(const T &x)
Definition: numeric.h:57
reference< OperInfo > oper
Definition: users.h:344
unsigned int cmds_out
Definition: users.h:764
std::string password
Definition: users.h:770
const std::string & GetName() const
Definition: server.h:54
std::string nick
Definition: users.h:298
bool IsModeSet(unsigned char m) const
Definition: users.cpp:36
Definition: users.h:894
irc::sockets::sockaddrs client_sa
Definition: users.h:292
std::string host
Definition: users.h:276
reference< ConnectClass > MyClass
Definition: users.h:774
Server * server
Definition: users.h:330
insp::intrusive_list< Membership > ChanList
Definition: users.h:271
unsigned int lastping
Definition: users.h:802
unsigned long GetMaxLocal()
Definition: users.h:206
irc::sockets::sockaddrs server_sa
Definition: users.h:787
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:798
std::bitset< 64 > snomasks
Definition: users.h:322
unsigned int maxchans
Definition: users.h:127
unsigned long GetMaxGlobal()
Definition: users.h:213
const std::string uuid
Definition: users.h:303
const unsigned int usertype
Definition: users.h:360
Definition: users.h:724
unsigned int CommandFloodPenalty
Definition: users.h:819
bool fakelag
Definition: users.h:74
Definition: base.h:31
unsigned int pingtime
Definition: users.h:90
time_t awaytime
Definition: users.h:340
Id GetId() const
Definition: mode.h:240
ChanList chans
Definition: users.h:326
bool maxconnwarn
Definition: users.h:123