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 
259  std::bitset<ModeParser::MODEID_MAX> modes;
260 
261  public:
266  {
267  public:
272  virtual void Execute(LocalUser* user) = 0;
273  };
274 
278 
281  time_t age;
282 
287  time_t signon;
288 
294 
299  std::string nick;
300 
304  const std::string uuid;
305 
309  std::string ident;
310 
313  std::string fullname;
314 
318  std::bitset<64> snomasks;
319 
323 
327 
331  std::string awaymsg;
332 
336  time_t awaytime;
337 
341 
346  unsigned int registered:3;
347 
353  unsigned int quitting:1;
354 
356  const UserType usertype:2;
357 
361  const std::string& GetIPString();
362 
366  const std::string& GetHost(bool uncloak) const;
367 
369  const std::string& GetDisplayedHost() const;
370 
372  const std::string& GetRealHost() const;
373 
376  irc::sockets::cidr_mask GetCIDRMask();
377 
381  virtual bool SetClientIP(const std::string& address, bool recheck_eline = true);
382 
383  virtual void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true);
384 
388  User(const std::string& uid, Server* srv, UserType objtype);
389 
395  virtual const std::string& GetFullHost();
396 
403  virtual const std::string& GetFullRealHost();
404 
408  void InvalidateCache();
409 
414  bool IsAway() const { return (!awaymsg.empty()); }
415 
420  bool IsOper() const { return oper; }
421 
426  bool IsNoticeMaskSet(unsigned char sm);
427 
433  std::string GetModeLetters(bool includeparams = false) const;
434 
439  bool IsModeSet(unsigned char m) const;
440  bool IsModeSet(const ModeHandler* mh) const;
441  bool IsModeSet(const ModeHandler& mh) const { return IsModeSet(&mh); }
442  bool IsModeSet(UserModeReference& moderef) const;
443 
448  void SetMode(ModeHandler* mh, bool value);
449  void SetMode(ModeHandler& mh, bool value) { SetMode(&mh, value); }
450 
457  virtual bool HasPermission(const std::string &command);
458 
467  virtual bool HasPrivPermission(const std::string &privstr, bool noisy = false);
468 
475  virtual bool HasModePermission(const ModeHandler* mh) const;
476 
481  const std::string& MakeHost();
482 
487  const std::string& MakeHostIP();
488 
492  void Oper(OperInfo* info);
493 
497  void UnOper();
498 
502  virtual void Write(const std::string &text);
503 
509  virtual void Write(const char *text, ...) CUSTOM_PRINTF(2, 3);
510 
515  void WriteServ(const std::string& text);
516 
522  void WriteServ(const char* text, ...) CUSTOM_PRINTF(2, 3);
523 
528  void WriteCommand(const char* command, const std::string& text);
529 
533  void WriteNotice(const std::string& text) { this->WriteCommand("NOTICE", ":" + text); }
534 
538  virtual void WriteRemoteNotice(const std::string& text);
539 
540  virtual void WriteRemoteNumeric(const Numeric::Numeric& numeric);
541 
542  template <typename T1>
543  void WriteRemoteNumeric(unsigned int numeric, T1 p1)
544  {
545  Numeric::Numeric n(numeric);
546  n.push(p1);
547  WriteRemoteNumeric(n);
548  }
549 
550  template <typename T1, typename T2>
551  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2)
552  {
553  Numeric::Numeric n(numeric);
554  n.push(p1);
555  n.push(p2);
556  WriteRemoteNumeric(n);
557  }
558 
559  template <typename T1, typename T2, typename T3>
560  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
561  {
562  Numeric::Numeric n(numeric);
563  n.push(p1);
564  n.push(p2);
565  n.push(p3);
566  WriteRemoteNumeric(n);
567  }
568 
569  template <typename T1, typename T2, typename T3, typename T4>
570  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
571  {
572  Numeric::Numeric n(numeric);
573  n.push(p1);
574  n.push(p2);
575  n.push(p3);
576  n.push(p4);
577  WriteRemoteNumeric(n);
578  }
579 
580  template <typename T1, typename T2, typename T3, typename T4, typename T5>
581  void WriteRemoteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
582  {
583  Numeric::Numeric n(numeric);
584  n.push(p1);
585  n.push(p2);
586  n.push(p3);
587  n.push(p4);
588  n.push(p5);
589  WriteRemoteNumeric(n);
590  }
591 
592  void WriteNumeric(const Numeric::Numeric& numeric);
593 
594  template <typename T1>
595  void WriteNumeric(unsigned int numeric, T1 p1)
596  {
597  Numeric::Numeric n(numeric);
598  n.push(p1);
599  WriteNumeric(n);
600  }
601 
602  template <typename T1, typename T2>
603  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2)
604  {
605  Numeric::Numeric n(numeric);
606  n.push(p1);
607  n.push(p2);
608  WriteNumeric(n);
609  }
610 
611  template <typename T1, typename T2, typename T3>
612  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3)
613  {
614  Numeric::Numeric n(numeric);
615  n.push(p1);
616  n.push(p2);
617  n.push(p3);
618  WriteNumeric(n);
619  }
620 
621  template <typename T1, typename T2, typename T3, typename T4>
622  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4)
623  {
624  Numeric::Numeric n(numeric);
625  n.push(p1);
626  n.push(p2);
627  n.push(p3);
628  n.push(p4);
629  WriteNumeric(n);
630  }
631 
632  template <typename T1, typename T2, typename T3, typename T4, typename T5>
633  void WriteNumeric(unsigned int numeric, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
634  {
635  Numeric::Numeric n(numeric);
636  n.push(p1);
637  n.push(p2);
638  n.push(p3);
639  n.push(p4);
640  n.push(p5);
641  WriteNumeric(n);
642  }
643 
648  void WriteFrom(User *user, const std::string &text);
649 
655  void WriteFrom(User *user, const char* text, ...) CUSTOM_PRINTF(3, 4);
656 
661  void WriteCommonRaw(const std::string &line, bool include_self = true);
662 
667  void WriteCommon(const char* text, ...) CUSTOM_PRINTF(2, 3);
668 
677  void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true);
678 
683  bool SharesChannelWith(User *other);
684 
689  bool ChangeDisplayedHost(const std::string& host);
690 
695  void ChangeRealHost(const std::string& host, bool resetdisplay);
696 
704  bool ChangeIdent(const std::string& newident);
705 
713  bool ChangeName(const std::string& gecos);
714 
720  bool ChangeNick(const std::string& newnick, time_t newts = 0);
721 
725  void PurgeEmptyChannels();
726 
729  virtual ~User();
730  CullResult cull() CXX11_OVERRIDE;
731 };
732 
733 class CoreExport UserIOHandler : public StreamSocket
734 {
735  public:
736  LocalUser* const user;
737  UserIOHandler(LocalUser* me) : user(me) {}
738  void OnDataReady() CXX11_OVERRIDE;
739  void OnError(BufferedSocketError error) CXX11_OVERRIDE;
740 
746  void AddWriteBuf(const std::string &data);
747 };
748 
749 typedef unsigned int already_sent_t;
750 
751 class CoreExport LocalUser : public User, public insp::intrusive_list_node<LocalUser>
752 {
753  public:
755  CullResult cull() CXX11_OVERRIDE;
756 
757  UserIOHandler eh;
758 
761  unsigned int bytes_in;
762 
765  unsigned int bytes_out;
766 
769  unsigned int cmds_in;
770 
773  unsigned int cmds_out;
774 
779  std::string password;
780 
784 
788  ConnectClass* GetClass() const { return MyClass; }
789 
792  void CheckClass(bool clone_count = true);
793 
797 
801  int GetServerPort();
802 
807  unsigned int quitting_sendq:1;
808 
811  unsigned int lastping:1;
812 
815  unsigned int exempt:1;
816 
819  time_t nping;
820 
823  time_t idle_lastmsg;
824 
828  unsigned int CommandFloodPenalty;
829 
830  already_sent_t already_sent;
831 
836  bool CheckLines(bool doZline = false);
837 
841  void FullConnect();
842 
847  void SetClass(const std::string &explicit_name = "");
848 
849  bool SetClientIP(const std::string& address, bool recheck_eline = true) CXX11_OVERRIDE;
850 
851  void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true) CXX11_OVERRIDE;
852 
853  void Write(const std::string& text) CXX11_OVERRIDE;
854  void Write(const char*, ...) CXX11_OVERRIDE CUSTOM_PRINTF(2, 3);
855 
860  void WriteRemoteNotice(const std::string& text) CXX11_OVERRIDE;
861 
868  bool HasPermission(const std::string &command) CXX11_OVERRIDE;
869 
878  bool HasPrivPermission(const std::string &privstr, bool noisy = false) CXX11_OVERRIDE;
879 
886  bool HasModePermission(const ModeHandler* mh) const CXX11_OVERRIDE;
887 
892  void OverruleNick();
893 };
894 
895 class RemoteUser : public User
896 {
897  public:
898  RemoteUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_REMOTE)
899  {
900  }
901 };
902 
903 class CoreExport FakeUser : public User
904 {
905  public:
906  FakeUser(const std::string& uid, Server* srv) : User(uid, srv, USERTYPE_SERVER)
907  {
908  nick = srv->GetName();
909  }
910 
911  FakeUser(const std::string& uid, const std::string& sname, const std::string& sdesc)
912  : User(uid, new Server(sname, sdesc), USERTYPE_SERVER)
913  {
914  nick = sname;
915  }
916 
917  CullResult cull() CXX11_OVERRIDE;
918  const std::string& GetFullHost() CXX11_OVERRIDE;
919  const std::string& GetFullRealHost() CXX11_OVERRIDE;
920 };
921 
922 /* Faster than dynamic_cast */
924 inline LocalUser* IS_LOCAL(User* u)
925 {
926  return u->usertype == USERTYPE_LOCAL ? static_cast<LocalUser*>(u) : NULL;
927 }
929 inline RemoteUser* IS_REMOTE(User* u)
930 {
931  return u->usertype == USERTYPE_REMOTE ? static_cast<RemoteUser*>(u) : NULL;
932 }
934 inline FakeUser* IS_SERVER(User* u)
935 {
936  return u->usertype == USERTYPE_SERVER ? static_cast<FakeUser*>(u) : NULL;
937 }
938 
939 inline bool User::IsModeSet(const ModeHandler* mh) const
940 {
941  return (modes[mh->GetId()]);
942 }
943 
944 inline bool User::IsModeSet(UserModeReference& moderef) const
945 {
946  if (!moderef)
947  return false;
948  return IsModeSet(*moderef);
949 }
950 
951 inline void User::SetMode(ModeHandler* mh, bool value)
952 {
953  modes[mh->GetId()] = value;
954 }
bool IsAway() const
Definition: users.h:414
time_t GetRegTimeout()
Definition: users.h:158
virtual const std::string & GetFullRealHost()
Definition: users.cpp:146
unsigned int bytes_in
Definition: users.h:761
Definition: server.h:22
Definition: mode.h:95
unsigned int registered
Definition: users.h:346
unsigned int penaltythreshold
Definition: users.h:108
Definition: dynref.h:130
unsigned int quitting
Definition: users.h:353
std::string awaymsg
Definition: users.h:331
std::string ident
Definition: users.h:309
std::string host
Definition: users.h:86
Definition: hashcomp.h:69
unsigned int bytes_out
Definition: users.h:765
unsigned long GetSendqHardMax()
Definition: users.h:180
ConnectClass * GetClass() const
Definition: users.h:788
unsigned long GetSendqSoftMax()
Definition: users.h:173
unsigned long maxglobal
Definition: users.h:119
Definition: configreader.h:37
time_t signon
Definition: users.h:287
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:323
unsigned int commandrate
Definition: users.h:111
Definition: configreader.h:158
time_t age
Definition: users.h:281
virtual bool SetClientIP(const std::string &address, bool recheck_eline=true)
Definition: users.cpp:721
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:823
insp::flat_set< int > ports
Definition: users.h:141
Definition: users.h:66
Definition: users.h:751
Definition: socket.h:57
bool IsOper() const
Definition: users.h:420
unsigned int cmds_in
Definition: users.h:769
unsigned int GetPingTime()
Definition: users.h:165
time_t nping
Definition: users.h:819
char type
Definition: users.h:71
Definition: inspsocket.h:108
std::string name
Definition: users.h:78
unsigned long limit
Definition: users.h:132
std::string fullname
Definition: users.h:313
Definition: users.h:265
unsigned int exempt
Definition: users.h:815
Definition: users.h:895
Definition: base.h:85
Definition: socket.h:74
void SetMode(ModeHandler *mh, bool value)
Definition: users.h:951
reference< OperInfo > oper
Definition: users.h:340
unsigned int cmds_out
Definition: users.h:773
std::string password
Definition: users.h:779
const std::string & GetName() const
Definition: server.h:54
std::string nick
Definition: users.h:299
virtual const std::string & GetFullHost()
Definition: users.cpp:136
bool IsModeSet(unsigned char m) const
Definition: users.cpp:36
Definition: users.h:903
const UserType usertype
Definition: users.h:356
irc::sockets::sockaddrs client_sa
Definition: users.h:293
reference< ConnectClass > MyClass
Definition: users.h:783
Server * server
Definition: users.h:326
insp::intrusive_list< Membership > ChanList
Definition: users.h:277
unsigned int lastping
Definition: users.h:811
unsigned long GetMaxLocal()
Definition: users.h:206
irc::sockets::sockaddrs server_sa
Definition: users.h:796
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:807
std::bitset< 64 > snomasks
Definition: users.h:318
unsigned int maxchans
Definition: users.h:127
unsigned long GetMaxGlobal()
Definition: users.h:213
const std::string uuid
Definition: users.h:304
User(const std::string &uid, Server *srv, UserType objtype)
Definition: users.cpp:66
Definition: users.h:733
unsigned int CommandFloodPenalty
Definition: users.h:828
bool fakelag
Definition: users.h:74
Definition: base.h:31
unsigned int pingtime
Definition: users.h:90
time_t awaytime
Definition: users.h:336
Id GetId() const
Definition: mode.h:241
ChanList chans
Definition: users.h:322
bool maxconnwarn
Definition: users.h:123