InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
users.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6  * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7  * Copyright (C) 2003-2007 Craig Edwards <craigedwards@brainbox.cc>
8  * Copyright (C) 2007 Burlex <???@???>
9  * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
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 #ifndef USERS_H
26 #define USERS_H
27 
28 #include "socket.h"
29 #include "inspsocket.h"
30 #include "dns.h"
31 #include "mode.h"
32 #include "membership.h"
33 
36 enum ClassTypes {
38  CC_ALLOW = 0,
40  CC_DENY = 1,
42  CC_NAMED = 2
43 };
44 
47 enum UserModes {
49  UM_SNOMASK = 's' - 65,
51  UM_WALLOPS = 'w' - 65,
53  UM_INVISIBLE = 'i' - 65,
55  UM_OPERATOR = 'o' - 65
56 };
57 
61 enum RegistrationState {
62 
63 #ifndef _WIN32 // Burlex: This is already defined in win32, luckily it is still 0.
64  REG_NONE = 0, /* Has sent nothing */
65 #endif
66 
67  REG_USER = 1, /* Has sent USER */
68  REG_NICK = 2, /* Has sent NICK */
69  REG_NICKUSER = 3, /* Bitwise combination of REG_NICK and REG_USER */
70  REG_ALL = 7 /* REG_NICKUSER plus next bit along */
71 };
72 
73 enum UserType {
74  USERTYPE_LOCAL = 1,
75  USERTYPE_REMOTE = 2,
76  USERTYPE_SERVER = 3
77 };
78 
81 struct CoreExport ConnectClass : public refcountbase
82 {
83  reference<ConfigTag> config;
86  char type;
87 
89  bool fakelag;
90 
93  std::string name;
94 
97  unsigned int registration_timeout;
98 
101  std::string host;
102 
105  unsigned int pingtime;
106 
110  unsigned long softsendqmax;
111 
115  unsigned long hardsendqmax;
116 
119  unsigned long recvqmax;
120 
123  unsigned int penaltythreshold;
124 
126  unsigned int commandrate;
127 
130  unsigned long maxlocal;
131 
134  unsigned long maxglobal;
135 
139 
142  unsigned int maxchans;
143 
147  unsigned long limit;
148 
151  ConnectClass(ConfigTag* tag, char type, const std::string& mask);
154  ConnectClass(ConfigTag* tag, char type, const std::string& mask, const ConnectClass& parent);
155 
157  void Update(const ConnectClass* newSettings);
158 
159  const std::string& GetName() { return name; }
160  const std::string& GetHost() { return host; }
161 
164  time_t GetRegTimeout()
165  {
166  return (registration_timeout ? registration_timeout : 90);
167  }
168 
171  unsigned int GetPingTime()
172  {
173  return (pingtime ? pingtime : 120);
174  }
175 
179  unsigned long GetSendqSoftMax()
180  {
181  return (softsendqmax ? softsendqmax : 4096);
182  }
183 
186  unsigned long GetSendqHardMax()
187  {
188  return (hardsendqmax ? hardsendqmax : 0x100000);
189  }
190 
193  unsigned long GetRecvqMax()
194  {
195  return (recvqmax ? recvqmax : 4096);
196  }
197 
200  unsigned int GetPenaltyThreshold()
201  {
202  return penaltythreshold ? penaltythreshold : (fakelag ? 10 : 20);
203  }
204 
205  unsigned int GetCommandRate()
206  {
207  return commandrate ? commandrate : 1000;
208  }
209 
212  unsigned long GetMaxLocal()
213  {
214  return maxlocal;
215  }
216 
219  unsigned long GetMaxGlobal()
220  {
221  return maxglobal;
222  }
223 };
224 
230 class CoreExport User : public Extensible
231 {
232  private:
235  std::string cached_fullhost;
236 
239  std::string cached_hostip;
240 
243  std::string cached_makehost;
244 
247  std::string cached_fullrealhost;
248 
251  std::string cachedip;
252 
253  public:
254 
258  std::string host;
259 
262  time_t age;
263 
268  time_t signon;
269 
272  time_t idle_lastmsg;
273 
279 
284  std::string nick;
285 
289  const std::string uuid;
290 
294  std::string ident;
295 
299  std::string dhost;
300 
303  std::string fullname;
304 
315  std::bitset<64> modes;
316 
320  std::bitset<64> snomasks;
321 
324  UserChanList chans;
325 
328  const std::string server;
329 
333  std::string awaymsg;
334 
338  time_t awaytime;
339 
343 
348  unsigned int registered:3;
349 
354  unsigned int dns_done:1;
355 
358  unsigned int quietquit:1;
359 
365  unsigned int quitting:1;
366 
371  unsigned int quitting_sendq:1;
372 
375  unsigned int exempt:1;
376 
379  unsigned int lastping:1;
380 
382  const unsigned int usertype:2;
383 
387  const char* GetIPString();
388 
391  irc::sockets::cidr_mask GetCIDRMask();
392 
396  virtual bool SetClientIP(const char* sip, bool recheck_eline = true);
397 
398  virtual void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true);
399 
403  User(const std::string &uid, const std::string& srv, int objtype);
404 
409  bool CheckLines(bool doZline = false);
410 
416  virtual const std::string& GetFullHost();
417 
424  virtual const std::string& GetFullRealHost();
425 
429  void InvalidateCache();
430 
434  const char* FormatNoticeMasks();
435 
442  std::string ProcessNoticeMasks(const char *sm);
443 
448  bool IsNoticeMaskSet(unsigned char sm);
449 
454  void SetNoticeMask(unsigned char sm, bool value);
455 
459  const char* FormatModes(bool showparameters = false);
460 
465  bool IsModeSet(unsigned char m);
466 
471  void SetMode(unsigned char m, bool value);
472 
479  virtual bool HasPermission(const std::string &command);
480 
489  virtual bool HasPrivPermission(const std::string &privstr, bool noisy = false);
490 
498  virtual bool HasModePermission(unsigned char mode, ModeType type);
499 
504  char* MakeWildHost();
505 
510  const std::string& MakeHost();
511 
516  const std::string& MakeHostIP();
517 
520  void AddToWhoWas();
521 
525  void Oper(OperInfo* info);
526 
534  inline bool ForceNickChange(const char* newnick) { return ChangeNick(newnick, true); }
535 
539  void UnOper();
540 
544  virtual void Write(const std::string &text);
545 
551  virtual void Write(const char *text, ...) CUSTOM_PRINTF(2, 3);
552 
557  void WriteServ(const std::string& text);
558 
564  void WriteServ(const char* text, ...) CUSTOM_PRINTF(2, 3);
565 
566  void WriteNumeric(unsigned int numeric, const char* text, ...) CUSTOM_PRINTF(3, 4);
567 
568  void WriteNumeric(unsigned int numeric, const std::string &text);
569 
574  void WriteFrom(User *user, const std::string &text);
575 
581  void WriteFrom(User *user, const char* text, ...) CUSTOM_PRINTF(3, 4);
582 
587  void WriteTo(User *dest, const std::string &data);
588 
594  void WriteTo(User *dest, const char *data, ...) CUSTOM_PRINTF(3, 4);
595 
600  void WriteCommonRaw(const std::string &line, bool include_self = true);
601 
606  void WriteCommon(const char* text, ...) CUSTOM_PRINTF(2, 3);
607 
612  void WriteCommonExcept(const char* text, ...) CUSTOM_PRINTF(2, 3);
613 
619  void WriteCommonQuit(const std::string &normal_text, const std::string &oper_text);
620 
625  void SendText(const std::string &LinePrefix, std::stringstream &TextStream);
626 
629  virtual void SendText(const std::string& line) = 0;
630 
633  void SendText(const char* text, ...) CUSTOM_PRINTF(2, 3);
634 
639  bool SharesChannelWith(User *other);
640 
647  void DoHostCycle(const std::string &quitline);
648 
657  bool ChangeDisplayedHost(const char* host);
658 
666  bool ChangeIdent(const char* newident);
667 
675  bool ChangeName(const char* gecos);
676 
682  bool ChangeNick(const std::string& newnick, bool force = false);
683 
692  void SendAll(const char* command, const char* text, ...) CUSTOM_PRINTF(3, 4);
693 
699  std::string ChannelList(User* source, bool spy);
700 
706  void SplitChanList(User* dest, const std::string &cl);
707 
711  void PurgeEmptyChannels();
712 
716  virtual ConnectClass* GetClass();
717 
720  virtual ~User();
721  virtual CullResult cull();
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 InviteBase
743 {
744  public:
746  CullResult cull();
747 
748  UserIOHandler eh;
749 
752  LocalUserList::iterator localuseriter;
753 
756  unsigned int bytes_in;
757 
760  unsigned int bytes_out;
761 
764  unsigned int cmds_in;
765 
768  unsigned int cmds_out;
769 
774  std::string password;
775 
779 
781 
784  void CheckClass();
785 
789 
793  int GetServerPort();
794 
797  time_t nping;
798 
802  unsigned int CommandFloodPenalty;
803 
804  static already_sent_t already_sent_id;
805  already_sent_t already_sent;
806 
809  std::string stored_host;
810 
815  void StartDNSLookup();
816 
820  void FullConnect();
821 
826  void SetClass(const std::string &explicit_name = "");
827 
828  bool SetClientIP(const char* sip, bool recheck_eline = true);
829 
830  void SetClientIP(const irc::sockets::sockaddrs& sa, bool recheck_eline = true);
831 
832  void SendText(const std::string& line);
833  void Write(const std::string& text);
834  void Write(const char*, ...) CUSTOM_PRINTF(2, 3);
835 
839  InviteList& GetInviteList();
840 
845  bool IsInvited(const irc::string &channel);
846 
851  void InviteTo(const irc::string &channel, time_t timeout);
852 
858  void RemoveInvite(const irc::string &channel);
859 
860  void RemoveExpiredInvites();
861 
868  bool HasPermission(const std::string &command);
869 
878  bool HasPrivPermission(const std::string &privstr, bool noisy = false);
879 
887  bool HasModePermission(unsigned char mode, ModeType type);
888 };
889 
890 class CoreExport RemoteUser : public User
891 {
892  public:
893  RemoteUser(const std::string& uid, const std::string& srv) : User(uid, srv, USERTYPE_REMOTE)
894  {
895  }
896  virtual void SendText(const std::string& line);
897 };
898 
899 class CoreExport FakeUser : public User
900 {
901  public:
902  FakeUser(const std::string &uid, const std::string& srv) : User(uid, srv, USERTYPE_SERVER)
903  {
904  nick = srv;
905  }
906 
907  virtual CullResult cull();
908  virtual void SendText(const std::string& line);
909  virtual const std::string& GetFullHost();
910  virtual const std::string& GetFullRealHost();
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 }
930 #define IS_OPER(x) (x->oper)
931 
932 #define IS_AWAY(x) (!x->awaymsg.empty())
933 
936 class CoreExport UserResolver : public Resolver
937 {
938  private:
940  std::string uuid;
943  bool fwd;
944  public:
951  UserResolver(LocalUser* user, std::string to_resolve, QueryType qt, bool &cache);
952 
958  void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached);
959 
964  void OnError(ResolverError e, const std::string &errormessage);
965 };
966 
967 #endif
time_t GetRegTimeout()
Definition: users.h:164
bool ForceNickChange(const char *newnick)
Definition: users.h:534
virtual const std::string & GetFullRealHost()
Definition: users.cpp:314
unsigned int bytes_in
Definition: users.h:756
unsigned int penaltythreshold
Definition: users.h:123
std::string awaymsg
Definition: users.h:333
std::string ident
Definition: users.h:294
std::string host
Definition: users.h:101
virtual bool SetClientIP(const char *sip, bool recheck_eline=true)
Definition: users.cpp:1002
unsigned int bytes_out
Definition: users.h:760
unsigned long GetSendqHardMax()
Definition: users.h:186
unsigned long GetSendqSoftMax()
Definition: users.h:179
unsigned long maxglobal
Definition: users.h:134
Definition: configreader.h:37
const std::string server
Definition: users.h:328
time_t signon
Definition: users.h:268
std::bitset< 64 > modes
Definition: users.h:315
unsigned long recvqmax
Definition: users.h:119
virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)=0
Definition: extensible.h:76
unsigned int commandrate
Definition: users.h:126
Definition: configreader.h:149
time_t age
Definition: users.h:262
void SendText(const std::string &LinePrefix, std::stringstream &TextStream)
Definition: users.cpp:1322
unsigned int registration_timeout
Definition: users.h:97
unsigned long maxlocal
Definition: users.h:130
unsigned long hardsendqmax
Definition: users.h:115
Definition: users.h:230
unsigned long softsendqmax
Definition: users.h:110
Definition: users.h:81
Definition: users.h:742
Definition: socket.h:58
unsigned int cmds_in
Definition: users.h:764
unsigned int GetPingTime()
Definition: users.h:171
time_t nping
Definition: users.h:797
std::string stored_host
Definition: users.h:809
char type
Definition: users.h:86
UserChanList chans
Definition: users.h:324
Definition: inspsocket.h:103
std::string name
Definition: users.h:93
unsigned long limit
Definition: users.h:147
User(const std::string &uid, const std::string &srv, int objtype)
Definition: users.cpp:201
std::string fullname
Definition: users.h:303
std::string dhost
Definition: users.h:299
Definition: users.h:890
Definition: base.h:86
Definition: socket.h:75
reference< OperInfo > oper
Definition: users.h:342
unsigned int cmds_out
Definition: users.h:768
std::string password
Definition: users.h:774
std::string nick
Definition: users.h:284
virtual void OnError(ResolverError e, const std::string &errormessage)
Definition: dns.cpp:995
virtual const std::string & GetFullHost()
Definition: users.cpp:280
Definition: users.h:899
virtual CullResult cull()
Definition: users.cpp:530
irc::sockets::sockaddrs client_sa
Definition: users.h:278
std::string host
Definition: users.h:258
reference< ConnectClass > MyClass
Definition: users.h:778
time_t idle_lastmsg
Definition: users.h:272
LocalUserList::iterator localuseriter
Definition: users.h:752
unsigned long GetMaxLocal()
Definition: users.h:212
irc::sockets::sockaddrs server_sa
Definition: users.h:788
unsigned int GetPenaltyThreshold()
Definition: users.h:200
unsigned long GetRecvqMax()
Definition: users.h:193
virtual void Write(const std::string &text)
Definition: users.cpp:1041
std::bitset< 64 > snomasks
Definition: users.h:320
unsigned int maxchans
Definition: users.h:142
unsigned long GetMaxGlobal()
Definition: users.h:219
const std::string uuid
Definition: users.h:289
Definition: membership.h:38
const unsigned int usertype
Definition: users.h:382
Definition: users.h:724
unsigned int CommandFloodPenalty
Definition: users.h:802
virtual ConnectClass * GetClass()
Definition: users.cpp:1689
bool fakelag
Definition: users.h:89
Definition: base.h:32
unsigned int pingtime
Definition: users.h:105
time_t awaytime
Definition: users.h:338
bool maxconnwarn
Definition: users.h:138
Definition: users.h:936
Definition: dns.h:164