ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
User.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2019 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ZNC_USER_H
18 #define ZNC_USER_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Utils.h>
22 #include <znc/Buffer.h>
23 #include <znc/Nick.h>
24 #include <znc/Translation.h>
25 #include <set>
26 #include <vector>
27 
28 class CModules;
29 class CChan;
30 class CClient;
31 class CConfig;
32 class CFile;
33 class CIRCNetwork;
34 class CIRCSock;
35 class CUserTimer;
36 class CServer;
37 
38 class CUser : private CCoreTranslationMixin {
39  public:
40  CUser(const CString& sUsername);
41  ~CUser();
42 
43  CUser(const CUser&) = delete;
44  CUser& operator=(const CUser&) = delete;
45 
46  bool ParseConfig(CConfig* Config, CString& sError);
47 
48  // TODO refactor this
49  enum eHashType {
53 
55  };
56 
57  // If you change the default hash here and in HASH_DEFAULT,
58  // don't forget CUtils::sDefaultHash!
59  // TODO refactor this
60  static CString SaltedHash(const CString& sPass, const CString& sSalt) {
61  return CUtils::SaltedSHA256Hash(sPass, sSalt);
62  }
63 
64  CConfig ToConfig() const;
65  bool CheckPass(const CString& sPass) const;
66  bool AddAllowedHost(const CString& sHostMask);
67  bool RemAllowedHost(const CString& sHostMask);
68  void ClearAllowedHosts();
69  bool IsHostAllowed(const CString& sHost) const;
70  bool IsValid(CString& sErrMsg, bool bSkipPass = false) const;
71  static bool IsValidUsername(const CString& sUsername);
73  static bool IsValidUserName(const CString& sUsername);
74  static CString MakeCleanUsername(const CString& sUsername);
76  static CString MakeCleanUserName(const CString& sUsername);
77 
78  // Modules
79  CModules& GetModules() { return *m_pModules; }
80  const CModules& GetModules() const { return *m_pModules; }
81  // !Modules
82 
83  // Networks
84  CIRCNetwork* AddNetwork(const CString& sNetwork, CString& sErrorRet);
85  bool DeleteNetwork(const CString& sNetwork);
86  bool AddNetwork(CIRCNetwork* pNetwork);
87  void RemoveNetwork(CIRCNetwork* pNetwork);
88  CIRCNetwork* FindNetwork(const CString& sNetwork) const;
89  const std::vector<CIRCNetwork*>& GetNetworks() const;
90  bool HasSpaceForNewNetwork() const;
91  // !Networks
92 
93  bool PutUser(const CString& sLine, CClient* pClient = nullptr,
94  CClient* pSkipClient = nullptr);
95  bool PutAllUser(const CString& sLine, CClient* pClient = nullptr,
96  CClient* pSkipClient = nullptr);
97  bool PutStatus(const CString& sLine, CClient* pClient = nullptr,
98  CClient* pSkipClient = nullptr);
99  bool PutStatusNotice(const CString& sLine, CClient* pClient = nullptr,
100  CClient* pSkipClient = nullptr);
101  bool PutModule(const CString& sModule, const CString& sLine,
102  CClient* pClient = nullptr, CClient* pSkipClient = nullptr);
103  bool PutModNotice(const CString& sModule, const CString& sLine,
104  CClient* pClient = nullptr,
105  CClient* pSkipClient = nullptr);
106 
107  bool IsUserAttached() const;
108  void UserConnected(CClient* pClient);
109  void UserDisconnected(CClient* pClient);
110 
111  CString GetLocalDCCIP() const;
112 
113  CString ExpandString(const CString& sStr) const;
114  CString& ExpandString(const CString& sStr, CString& sRet) const;
115 
116  CString AddTimestamp(const CString& sStr) const;
117  CString AddTimestamp(time_t tm, const CString& sStr) const;
118  CString AddTimestamp(timeval tv, const CString& sStr) const;
119 
120  void CloneNetworks(const CUser& User);
121  bool Clone(const CUser& User, CString& sErrorRet,
122  bool bCloneNetworks = true);
123  void BounceAllClients();
124 
125  void AddBytesRead(unsigned long long u) { m_uBytesRead += u; }
126  void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; }
127 
128  // Setters
129  void SetNick(const CString& s);
130  void SetAltNick(const CString& s);
131  void SetIdent(const CString& s);
132  void SetRealName(const CString& s);
133  void SetBindHost(const CString& s);
134  void SetDCCBindHost(const CString& s);
135  void SetPass(const CString& s, eHashType eHash, const CString& sSalt = "");
136  void SetMultiClients(bool b);
137  void SetDenyLoadMod(bool b);
138  void SetAdmin(bool b);
139  void SetDenySetBindHost(bool b);
140  bool SetStatusPrefix(const CString& s);
141  void SetDefaultChanModes(const CString& s);
142  void SetClientEncoding(const CString& s);
143  void SetQuitMsg(const CString& s);
144  bool AddCTCPReply(const CString& sCTCP, const CString& sReply);
145  bool DelCTCPReply(const CString& sCTCP);
147  bool SetBufferCount(unsigned int u, bool bForce = false);
148  bool SetChanBufferSize(unsigned int u, bool bForce = false);
149  bool SetQueryBufferSize(unsigned int u, bool bForce = false);
150  void SetAutoClearChanBuffer(bool b);
151  void SetAutoClearQueryBuffer(bool b);
152  bool SetLanguage(const CString& s);
153 
154  void SetBeingDeleted(bool b) { m_bBeingDeleted = b; }
159  void SetTimezone(const CString& s) { m_sTimezone = s; }
160  void SetJoinTries(unsigned int i) { m_uMaxJoinTries = i; }
161  void SetMaxJoins(unsigned int i) { m_uMaxJoins = i; }
162  void SetSkinName(const CString& s) { m_sSkinName = s; }
163  void SetMaxNetworks(unsigned int i) { m_uMaxNetworks = i; }
164  void SetMaxQueryBuffers(unsigned int i) { m_uMaxQueryBuffers = i; }
165  void SetNoTrafficTimeout(unsigned int i) { m_uNoTrafficTimeout = i; }
166  // !Setters
167 
168  // Getters
169  const std::vector<CClient*>& GetUserClients() const { return m_vClients; }
170  std::vector<CClient*> GetAllClients() const;
172  const CString& GetUserName() const;
173  const CString& GetUsername() const;
174  const CString& GetCleanUserName() const;
175  const CString& GetNick(bool bAllowDefault = true) const;
176  const CString& GetAltNick(bool bAllowDefault = true) const;
177  const CString& GetIdent(bool bAllowDefault = true) const;
178  CString GetRealName() const;
179  const CString& GetBindHost() const;
180  const CString& GetDCCBindHost() const;
181  const CString& GetPass() const;
182  eHashType GetPassHashType() const;
183  const CString& GetPassSalt() const;
184  const std::set<CString>& GetAllowedHosts() const;
185  const CString& GetTimestampFormat() const;
186  const CString& GetClientEncoding() const;
187  bool GetTimestampAppend() const;
188  bool GetTimestampPrepend() const;
189 
190  const CString& GetUserPath() const;
191 
192  bool DenyLoadMod() const;
193  bool IsAdmin() const;
194  bool DenySetBindHost() const;
195  bool MultiClients() const;
196  bool AuthOnlyViaModule() const;
197  const CString& GetStatusPrefix() const;
198  const CString& GetDefaultChanModes() const;
200  unsigned int GetPingFrequency() const { return m_uNoTrafficTimeout / 2; }
202  unsigned int GetPingSlack() const { return m_uNoTrafficTimeout / 6; }
206  unsigned int GetNoTrafficTimeout() const { return m_uNoTrafficTimeout; }
207 
208  CString GetQuitMsg() const;
209  const MCString& GetCTCPReplies() const;
211  unsigned int GetBufferCount() const;
212  unsigned int GetChanBufferSize() const;
213  unsigned int GetQueryBufferSize() const;
214  bool AutoClearChanBuffer() const;
215  bool AutoClearQueryBuffer() const;
216  bool IsBeingDeleted() const { return m_bBeingDeleted; }
217  CString GetTimezone() const { return m_sTimezone; }
218  unsigned long long BytesRead() const;
219  unsigned long long BytesWritten() const;
220  unsigned int JoinTries() const { return m_uMaxJoinTries; }
221  unsigned int MaxJoins() const { return m_uMaxJoins; }
222  CString GetSkinName() const;
223  CString GetLanguage() const;
224  unsigned int MaxNetworks() const { return m_uMaxNetworks; }
225  unsigned int MaxQueryBuffers() const { return m_uMaxQueryBuffers; }
226  // !Getters
227 
228  protected:
242 
248 
249  // Paths
251  // !Paths
252 
255  bool m_bAdmin;
263 
264  CUserTimer* m_pUserTimer;
265 
266  std::vector<CIRCNetwork*> m_vIRCNetworks;
267  std::vector<CClient*> m_vClients;
268  std::set<CString> m_ssAllowedHosts;
269  unsigned int m_uChanBufferSize;
270  unsigned int m_uQueryBufferSize;
271  unsigned long long m_uBytesRead;
272  unsigned long long m_uBytesWritten;
273  unsigned int m_uMaxJoinTries;
274  unsigned int m_uMaxNetworks;
275  unsigned int m_uMaxQueryBuffers;
276  unsigned int m_uMaxJoins;
277  unsigned int m_uNoTrafficTimeout;
280 
282 
283  private:
284  void SetKeepBuffer(bool b) {
286  } // XXX compatibility crap, added in 0.207
287  bool LoadModule(const CString& sModName, const CString& sArgs,
288  const CString& sNotice, CString& sError);
289 };
290 
291 #endif // !ZNC_USER_H
void SetJoinTries(unsigned int i)
Definition: User.h:160
Definition: User.h:38
bool IsAdmin() const
eHashType GetPassHashType() const
bool IsBeingDeleted() const
Definition: User.h:216
void SetQuitMsg(const CString &s)
void SetRealName(const CString &s)
unsigned int m_uMaxJoinTries
Definition: User.h:273
bool m_bDenySetBindHost
Definition: User.h:256
CConfig ToConfig() const
const CString & GetBindHost() const
CString ExpandString(const CString &sStr) const
void SetBindHost(const CString &s)
const CString & GetStatusPrefix() const
bool m_bMultiClients
Definition: User.h:253
CString m_sTimezone
Definition: User.h:246
void SetTimezone(const CString &s)
Definition: User.h:159
CString m_sPassSalt
Definition: User.h:238
Definition: User.h:52
void SetTimestampAppend(bool b)
Definition: User.h:156
void SetDenySetBindHost(bool b)
void RemoveNetwork(CIRCNetwork *pNetwork)
CString GetSkinName() const
bool IsValid(CString &sErrMsg, bool bSkipPass=false) const
bool DelCTCPReply(const CString &sCTCP)
CString m_sTimestampFormat
Definition: User.h:245
unsigned int GetNoTrafficTimeout() const
Timeout after which IRC connections are closed.
Definition: User.h:206
bool IsUserAttached() const
const CString & GetTimestampFormat() const
bool RemAllowedHost(const CString &sHostMask)
void AddBytesRead(unsigned long long u)
Definition: User.h:125
const CString & GetUserName() const
Definition: Client.h:99
bool m_bPrependTimestamp
Definition: User.h:261
Definition: User.h:50
std::vector< CIRCNetwork * > m_vIRCNetworks
Definition: User.h:266
CIRCNetwork * AddNetwork(const CString &sNetwork, CString &sErrorRet)
CString m_sStatusPrefix
Definition: User.h:239
MCString m_mssCTCPReplies
Definition: User.h:244
bool AutoClearChanBuffer() const
bool PutStatusNotice(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
eHashType
Definition: User.h:49
bool MultiClients() const
void SetDCCBindHost(const CString &s)
void BounceAllClients()
Definition: Modules.h:1389
bool SetChanBufferSize(unsigned int u, bool bForce=false)
unsigned long long BytesWritten() const
bool AddAllowedHost(const CString &sHostMask)
unsigned int MaxJoins() const
Definition: User.h:221
CString m_sAltNick
Definition: User.h:232
const std::set< CString > & GetAllowedHosts() const
CString AddTimestamp(const CString &sStr) const
const CModules & GetModules() const
Definition: User.h:80
const CString & GetClientEncoding() const
CString m_sNick
Definition: User.h:231
Definition: User.h:54
unsigned int GetQueryBufferSize() const
void SetSkinName(const CString &s)
Definition: User.h:162
unsigned long long BytesRead() const
static CString MakeCleanUserName(const CString &sUsername)
CString m_sRealName
Definition: User.h:234
bool PutStatus(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
bool DeleteNetwork(const CString &sNetwork)
const CString & GetAltNick(bool bAllowDefault=true) const
CString m_sPass
Definition: User.h:237
CString m_sIdent
Definition: User.h:233
Definition: FileUtils.h:30
unsigned int MaxQueryBuffers() const
Definition: User.h:225
CString m_sLanguage
Definition: User.h:279
Definition: IRCNetwork.h:40
const MCString & GetCTCPReplies() const
CString m_sUserPath
Definition: User.h:250
void UserConnected(CClient *pClient)
void SetMultiClients(bool b)
CUserTimer * m_pUserTimer
Definition: User.h:264
bool m_bAutoClearChanBuffer
Definition: User.h:257
String class that is used inside ZNC.
Definition: ZNCString.h:68
bool m_bDenyLoadMod
Definition: User.h:254
const std::vector< CClient * > & GetUserClients() const
Definition: User.h:169
bool CheckPass(const CString &sPass) const
bool SetLanguage(const CString &s)
unsigned int m_uNoTrafficTimeout
Definition: User.h:277
const CString & GetDCCBindHost() const
CString GetLocalDCCIP() const
CString GetQuitMsg() const
const CString & GetDefaultChanModes() const
void SetAltNick(const CString &s)
bool SetStatusPrefix(const CString &s)
void SetAdmin(bool b)
unsigned int m_uMaxJoins
Definition: User.h:276
std::set< CString > m_ssAllowedHosts
Definition: User.h:268
const std::vector< CIRCNetwork * > & GetNetworks() const
void SetDenyLoadMod(bool b)
void SetMaxNetworks(unsigned int i)
Definition: User.h:163
static CString SaltedSHA256Hash(const CString &sPass, const CString &sSalt)
void SetBeingDeleted(bool b)
Definition: User.h:154
unsigned int m_uMaxQueryBuffers
Definition: User.h:275
bool ParseConfig(CConfig *Config, CString &sError)
bool GetTimestampPrepend() const
bool m_bBeingDeleted
Definition: User.h:259
bool PutAllUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
CString m_sClientEncoding
Definition: User.h:241
bool m_bAutoClearQueryBuffer
Definition: User.h:258
void SetNick(const CString &s)
void SetNoTrafficTimeout(unsigned int i)
Definition: User.h:165
const CString & GetPass() const
unsigned int GetChanBufferSize() const
void UserDisconnected(CClient *pClient)
unsigned int GetPingFrequency() const
How long must an IRC connection be idle before ZNC sends a ping.
Definition: User.h:200
CString GetRealName() const
CUser & operator=(const CUser &)=delete
void SetAutoClearChanBuffer(bool b)
unsigned int MaxNetworks() const
Definition: User.h:224
unsigned int m_uChanBufferSize
Definition: User.h:269
bool AddCTCPReply(const CString &sCTCP, const CString &sReply)
static CString MakeCleanUsername(const CString &sUsername)
bool DenyLoadMod() const
CString m_sSkinName
Definition: User.h:278
bool m_bAdmin
Definition: User.h:255
bool m_bAppendTimestamp
Definition: User.h:260
void SetIdent(const CString &s)
CModules * m_pModules
Definition: User.h:281
CString GetLanguage() const
const CString m_sCleanUsername
Definition: User.h:230
CString m_sQuitMsg
Definition: User.h:243
A dictionary for strings.
Definition: ZNCString.h:595
const CString & GetNick(bool bAllowDefault=true) const
std::vector< CClient * > GetAllClients() const
const CString m_sUsername
Definition: User.h:229
void CloneNetworks(const CUser &User)
bool HasSpaceForNewNetwork() const
bool PutUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
const CString & GetUserPath() const
unsigned int m_uMaxNetworks
Definition: User.h:274
bool Clone(const CUser &User, CString &sErrorRet, bool bCloneNetworks=true)
void SetTimestampFormat(const CString &s)
Definition: User.h:155
unsigned int GetPingSlack() const
Time between checks if PINGs need to be sent.
Definition: User.h:202
bool GetTimestampAppend() const
CString m_sBindHost
Definition: User.h:235
const CString & GetCleanUserName() const
bool AuthOnlyViaModule() const
void SetAutoClearQueryBuffer(bool b)
void SetPass(const CString &s, eHashType eHash, const CString &sSalt="")
eHashType m_eHashType
Definition: User.h:247
unsigned int GetBufferCount() const
unsigned long long m_uBytesWritten
Definition: User.h:272
CString m_sDefaultChanModes
Definition: User.h:240
void ClearAllowedHosts()
std::vector< CClient * > m_vClients
Definition: User.h:267
bool IsHostAllowed(const CString &sHost) const
void SetDefaultChanModes(const CString &s)
bool PutModule(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
unsigned int JoinTries() const
Definition: User.h:220
bool PutModNotice(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
unsigned int m_uQueryBufferSize
Definition: User.h:270
bool DenySetBindHost() const
void SetClientEncoding(const CString &s)
CString GetTimezone() const
Definition: User.h:217
Definition: Server.h:23
static bool IsValidUsername(const CString &sUsername)
const CString & GetUsername() const
static CString SaltedHash(const CString &sPass, const CString &sSalt)
Definition: User.h:60
void SetMaxQueryBuffers(unsigned int i)
Definition: User.h:164
void SetAuthOnlyViaModule(bool b)
Definition: User.h:158
void AddBytesWritten(unsigned long long u)
Definition: User.h:126
Definition: IRCSock.h:35
bool SetQueryBufferSize(unsigned int u, bool bForce=false)
const CString & GetIdent(bool bAllowDefault=true) const
CIRCNetwork * FindNetwork(const CString &sNetwork) const
Definition: Chan.h:35
unsigned long long m_uBytesRead
Definition: User.h:271
Definition: Config.h:36
void SetTimestampPrepend(bool b)
Definition: User.h:157
CUser(const CString &sUsername)
CModules & GetModules()
Definition: User.h:79
Definition: User.h:51
static bool IsValidUserName(const CString &sUsername)
void SetMaxJoins(unsigned int i)
Definition: User.h:161
bool SetBufferCount(unsigned int u, bool bForce=false)
const CString & GetPassSalt() const
CString m_sDCCBindHost
Definition: User.h:236
bool AutoClearQueryBuffer() const
bool m_bAuthOnlyViaModule
Definition: User.h:262
Definition: Translation.h:104