ZNC  trunk
User.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2024 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  enum eHashType {
53 
54  // This should be kept in sync with CUtils::SaltedHash
55 #if ZNC_HAVE_ARGON
57 #else
59 #endif
60  };
61 
62  static CString SaltedHash(const CString& sPass, const CString& sSalt) {
63  return CUtils::SaltedHash(sPass, sSalt);
64  }
65 
66  CConfig ToConfig() const;
68  bool CheckPass(const CString& sPass);
69  bool AddAllowedHost(const CString& sHostMask);
70  bool RemAllowedHost(const CString& sHostMask);
72  bool IsHostAllowed(const CString& sHost) const;
73  bool IsValid(CString& sErrMsg, bool bSkipPass = false) const;
74  static bool IsValidUsername(const CString& sUsername);
76  static bool IsValidUserName(const CString& sUsername);
77  static CString MakeCleanUsername(const CString& sUsername);
79  static CString MakeCleanUserName(const CString& sUsername);
80 
81  // Modules
82  CModules& GetModules() { return *m_pModules; }
83  const CModules& GetModules() const { return *m_pModules; }
84  // !Modules
85 
86  // Networks
87  CIRCNetwork* AddNetwork(const CString& sNetwork, CString& sErrorRet);
88  bool DeleteNetwork(const CString& sNetwork);
89  bool AddNetwork(CIRCNetwork* pNetwork);
90  void RemoveNetwork(CIRCNetwork* pNetwork);
91  CIRCNetwork* FindNetwork(const CString& sNetwork) const;
92  const std::vector<CIRCNetwork*>& GetNetworks() const;
93  bool HasSpaceForNewNetwork() const;
94  // !Networks
95 
96  bool PutUser(const CString& sLine, CClient* pClient = nullptr,
97  CClient* pSkipClient = nullptr);
98  bool PutAllUser(const CString& sLine, CClient* pClient = nullptr,
99  CClient* pSkipClient = nullptr)
100  ZNC_MSG_DEPRECATED("Use PutUser() instead") {
101  return PutUser(sLine, pClient, pSkipClient);
102  }
103  bool PutStatus(const CString& sLine, CClient* pClient = nullptr,
104  CClient* pSkipClient = nullptr);
105  bool PutStatusNotice(const CString& sLine, CClient* pClient = nullptr,
106  CClient* pSkipClient = nullptr);
107  bool PutModule(const CString& sModule, const CString& sLine,
108  CClient* pClient = nullptr, CClient* pSkipClient = nullptr);
109  bool PutModNotice(const CString& sModule, const CString& sLine,
110  CClient* pClient = nullptr,
111  CClient* pSkipClient = nullptr);
112 
113  bool IsUserAttached() const;
114  void UserConnected(CClient* pClient);
115  void UserDisconnected(CClient* pClient);
116 
118 
119  CString ExpandString(const CString& sStr) const;
120  CString& ExpandString(const CString& sStr, CString& sRet) const;
121 
122  CString AddTimestamp(const CString& sStr) const;
123  CString AddTimestamp(time_t tm, const CString& sStr) const;
124  CString AddTimestamp(timeval tv, const CString& sStr) const;
125 
126  void CloneNetworks(const CUser& User);
127  bool Clone(const CUser& User, CString& sErrorRet,
128  bool bCloneNetworks = true);
130 
131  void AddBytesRead(unsigned long long u) { m_uBytesRead += u; }
132  void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; }
133 
134  // Setters
135  void SetNick(const CString& s);
136  void SetAltNick(const CString& s);
137  void SetIdent(const CString& s);
138  void SetRealName(const CString& s);
139  void SetBindHost(const CString& s);
140  void SetDCCBindHost(const CString& s);
141  void SetPass(const CString& s, eHashType eHash, const CString& sSalt = "");
142  void SetMultiClients(bool b);
143  void SetDenyLoadMod(bool b);
144  void SetAdmin(bool b);
145  void SetDenySetBindHost(bool b);
146  void SetDenySetIdent(bool b);
147  void SetDenySetNetwork(bool b);
148  void SetDenySetRealName(bool b);
149  void SetDenySetQuitMsg(bool b);
150  void SetDenySetCTCPReplies(bool b);
151  bool SetStatusPrefix(const CString& s);
152  void SetDefaultChanModes(const CString& s);
153  void SetClientEncoding(const CString& s);
154  void SetQuitMsg(const CString& s);
155  bool AddCTCPReply(const CString& sCTCP, const CString& sReply);
156  bool DelCTCPReply(const CString& sCTCP);
158  bool SetBufferCount(unsigned int u, bool bForce = false);
159  bool SetChanBufferSize(unsigned int u, bool bForce = false);
160  bool SetQueryBufferSize(unsigned int u, bool bForce = false);
163  bool SetLanguage(const CString& s);
164 
165  void SetBeingDeleted(bool b) { m_bBeingDeleted = b; }
170  void SetTimezone(const CString& s) { m_sTimezone = s; }
171  void SetJoinTries(unsigned int i) { m_uMaxJoinTries = i; }
172  void SetMaxJoins(unsigned int i) { m_uMaxJoins = i; }
173  void SetSkinName(const CString& s) { m_sSkinName = s; }
174  void SetMaxNetworks(unsigned int i) { m_uMaxNetworks = i; }
175  void SetMaxQueryBuffers(unsigned int i) { m_uMaxQueryBuffers = i; }
176  void SetNoTrafficTimeout(unsigned int i) { m_uNoTrafficTimeout = i; }
177  // !Setters
178 
179  // Getters
180  const std::vector<CClient*>& GetUserClients() const { return m_vClients; }
181  std::vector<CClient*> GetAllClients() const;
183  const CString& GetUserName() const;
184  const CString& GetUsername() const;
185  const CString& GetCleanUserName() const;
186  const CString& GetNick(bool bAllowDefault = true) const;
187  const CString& GetAltNick(bool bAllowDefault = true) const;
188  const CString& GetIdent(bool bAllowDefault = true) const;
190  const CString& GetBindHost() const;
191  const CString& GetDCCBindHost() const;
192  const CString& GetPass() const;
194  const CString& GetPassSalt() const;
195  const std::set<CString>& GetAllowedHosts() const;
196  const CString& GetTimestampFormat() const;
197  const CString& GetClientEncoding() const;
198  bool GetTimestampAppend() const;
199  bool GetTimestampPrepend() const;
200 
201  const CString& GetUserPath() const;
202 
203  bool DenyLoadMod() const;
204  bool IsAdmin() const;
205  bool DenySetBindHost() const;
206  bool DenySetIdent() const;
207  bool DenySetNetwork() const;
208  bool DenySetRealName() const;
209  bool DenySetQuitMsg() const;
210  bool DenySetCTCPReplies() const;
211  bool MultiClients() const;
212  bool AuthOnlyViaModule() const;
213  const CString& GetStatusPrefix() const;
214  const CString& GetDefaultChanModes() const;
216  unsigned int GetPingFrequency() const { return m_uNoTrafficTimeout / 2; }
218  unsigned int GetPingSlack() const { return m_uNoTrafficTimeout / 6; }
222  unsigned int GetNoTrafficTimeout() const { return m_uNoTrafficTimeout; }
223 
225  const MCString& GetCTCPReplies() const;
227  unsigned int GetBufferCount() const;
228  unsigned int GetChanBufferSize() const;
229  unsigned int GetQueryBufferSize() const;
230  bool AutoClearChanBuffer() const;
231  bool AutoClearQueryBuffer() const;
232  bool IsBeingDeleted() const { return m_bBeingDeleted; }
233  CString GetTimezone() const { return m_sTimezone; }
234  unsigned long long BytesRead() const;
235  unsigned long long BytesWritten() const;
236  unsigned int JoinTries() const { return m_uMaxJoinTries; }
237  unsigned int MaxJoins() const { return m_uMaxJoins; }
240  unsigned int MaxNetworks() const { return m_uMaxNetworks; }
241  unsigned int MaxQueryBuffers() const { return m_uMaxQueryBuffers; }
242  // !Getters
243 
244  protected:
258 
264 
265  // Paths
267  // !Paths
268 
271  bool m_bAdmin;
284 
285  CUserTimer* m_pUserTimer;
286 
287  std::vector<CIRCNetwork*> m_vIRCNetworks;
288  std::vector<CClient*> m_vClients;
289  std::set<CString> m_ssAllowedHosts;
290  unsigned int m_uChanBufferSize;
291  unsigned int m_uQueryBufferSize;
292  unsigned long long m_uBytesRead;
293  unsigned long long m_uBytesWritten;
294  unsigned int m_uMaxJoinTries;
295  unsigned int m_uMaxNetworks;
296  unsigned int m_uMaxQueryBuffers;
297  unsigned int m_uMaxJoins;
298  unsigned int m_uNoTrafficTimeout;
301 
303 
304  private:
305  void SetKeepBuffer(bool b) {
307  } // XXX compatibility crap, added in 0.207
308  bool LoadModule(const CString& sModName, const CString& sArgs,
309  const CString& sNotice, CString& sError);
310 };
311 
312 #endif // !ZNC_USER_H
#define ZNC_MSG_DEPRECATED(msg)
Definition: Message.h:23
Definition: Chan.h:35
Definition: Client.h:99
Definition: Config.h:36
Definition: Translation.h:103
Definition: FileUtils.h:30
Definition: IRCNetwork.h:40
Definition: IRCSock.h:35
Definition: Modules.h:1464
Definition: Server.h:23
String class that is used inside ZNC.
Definition: ZNCString.h:68
Definition: User.h:38
MCString m_mssCTCPReplies
Definition: User.h:260
const std::vector< CIRCNetwork * > & GetNetworks() const
const CString & GetNick(bool bAllowDefault=true) const
void SetBeingDeleted(bool b)
Definition: User.h:165
void SetAutoClearQueryBuffer(bool b)
const CString m_sUsername
Definition: User.h:245
void SetDenySetBindHost(bool b)
bool DenySetCTCPReplies() const
CIRCNetwork * AddNetwork(const CString &sNetwork, CString &sErrorRet)
CUserTimer * m_pUserTimer
Definition: User.h:285
CString m_sDCCBindHost
Definition: User.h:252
const MCString & GetCTCPReplies() const
bool DenySetBindHost() const
CConfig ToConfig() const
unsigned long long m_uBytesWritten
Definition: User.h:293
void SetDCCBindHost(const CString &s)
bool m_bDenySetCTCPReplies
Definition: User.h:277
bool PutUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
const CString & GetDefaultChanModes() const
void SetPass(const CString &s, eHashType eHash, const CString &sSalt="")
unsigned int m_uNoTrafficTimeout
Definition: User.h:298
static bool IsValidUserName(const CString &sUsername)
bool m_bAutoClearChanBuffer
Definition: User.h:278
CUser(const CUser &)=delete
unsigned int GetPingSlack() const
Time between checks if PINGs need to be sent.
Definition: User.h:218
const CString & GetPassSalt() const
bool m_bDenySetBindHost
Definition: User.h:272
bool AuthOnlyViaModule() const
CString m_sDefaultChanModes
Definition: User.h:256
CString GetLocalDCCIP() const
void BounceAllClients()
void SetDenySetNetwork(bool b)
bool DenyLoadMod() const
CIRCNetwork * FindNetwork(const CString &sNetwork) const
unsigned long long BytesWritten() const
void SetAutoClearChanBuffer(bool b)
void SetDefaultChanModes(const CString &s)
void SetTimestampFormat(const CString &s)
Definition: User.h:166
bool AutoClearQueryBuffer() const
CString m_sLanguage
Definition: User.h:300
std::set< CString > m_ssAllowedHosts
Definition: User.h:289
const std::vector< CClient * > & GetUserClients() const
Definition: User.h:180
bool m_bAdmin
Definition: User.h:271
unsigned long long m_uBytesRead
Definition: User.h:292
std::vector< CClient * > m_vClients
Definition: User.h:288
bool CheckPass(const CString &sPass)
Checks password, may upgrade the hash method.
bool IsValid(CString &sErrMsg, bool bSkipPass=false) const
static CString MakeCleanUsername(const CString &sUsername)
void SetNick(const CString &s)
bool m_bMultiClients
Definition: User.h:269
bool SetChanBufferSize(unsigned int u, bool bForce=false)
bool m_bAuthOnlyViaModule
Definition: User.h:283
void ClearAllowedHosts()
unsigned int MaxQueryBuffers() const
Definition: User.h:241
bool m_bDenyLoadMod
Definition: User.h:270
bool IsHostAllowed(const CString &sHost) const
bool AddAllowedHost(const CString &sHostMask)
bool SetBufferCount(unsigned int u, bool bForce=false)
bool HasSpaceForNewNetwork() const
bool m_bDenySetIdent
Definition: User.h:273
void AddBytesWritten(unsigned long long u)
Definition: User.h:132
CUser(const CString &sUsername)
bool m_bPrependTimestamp
Definition: User.h:282
CString m_sTimezone
Definition: User.h:262
CString m_sUserPath
Definition: User.h:266
std::vector< CClient * > GetAllClients() const
void SetDenySetRealName(bool b)
bool Clone(const CUser &User, CString &sErrorRet, bool bCloneNetworks=true)
void SetAltNick(const CString &s)
CUser & operator=(const CUser &)=delete
bool AddNetwork(CIRCNetwork *pNetwork)
void SetRealName(const CString &s)
void SetMultiClients(bool b)
bool DeleteNetwork(const CString &sNetwork)
void SetDenySetCTCPReplies(bool b)
const CString & GetUserPath() const
bool PutModNotice(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
bool GetTimestampPrepend() const
bool DenySetIdent() const
const CString & GetCleanUserName() const
bool AddCTCPReply(const CString &sCTCP, const CString &sReply)
void AddBytesRead(unsigned long long u)
Definition: User.h:131
CString AddTimestamp(time_t tm, const CString &sStr) const
bool m_bDenySetNetwork
Definition: User.h:274
CString m_sPassSalt
Definition: User.h:254
void SetClientEncoding(const CString &s)
const CString m_sCleanUsername
Definition: User.h:246
CString AddTimestamp(const CString &sStr) const
const CString & GetUserName() const
CModules & GetModules()
Definition: User.h:82
unsigned int GetNoTrafficTimeout() const
Timeout after which IRC connections are closed.
Definition: User.h:222
const CString & GetBindHost() const
void RemoveNetwork(CIRCNetwork *pNetwork)
unsigned int m_uMaxJoinTries
Definition: User.h:294
void SetTimezone(const CString &s)
Definition: User.h:170
CString m_sTimestampFormat
Definition: User.h:261
const CString & GetAltNick(bool bAllowDefault=true) const
CString ExpandString(const CString &sStr) const
unsigned int m_uMaxJoins
Definition: User.h:297
bool DenySetQuitMsg() const
std::vector< CIRCNetwork * > m_vIRCNetworks
Definition: User.h:287
unsigned int GetChanBufferSize() const
bool m_bAppendTimestamp
Definition: User.h:281
void SetDenySetIdent(bool b)
unsigned int m_uMaxNetworks
Definition: User.h:295
void SetMaxQueryBuffers(unsigned int i)
Definition: User.h:175
void SetJoinTries(unsigned int i)
Definition: User.h:171
CString GetQuitMsg() const
bool PutStatusNotice(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
CString GetRealName() const
unsigned int m_uMaxQueryBuffers
Definition: User.h:296
bool m_bBeingDeleted
Definition: User.h:280
unsigned long long BytesRead() const
bool DenySetNetwork() const
static CString SaltedHash(const CString &sPass, const CString &sSalt)
Definition: User.h:62
bool DelCTCPReply(const CString &sCTCP)
CString GetSkinName() const
const CString & GetIdent(bool bAllowDefault=true) const
CString m_sAltNick
Definition: User.h:248
static bool IsValidUsername(const CString &sUsername)
const CModules & GetModules() const
Definition: User.h:83
bool PutStatus(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
void CloneNetworks(const CUser &User)
void SetMaxNetworks(unsigned int i)
Definition: User.h:174
unsigned int GetQueryBufferSize() const
unsigned int GetPingFrequency() const
How long must an IRC connection be idle before ZNC sends a ping.
Definition: User.h:216
CString m_sIdent
Definition: User.h:249
unsigned int m_uChanBufferSize
Definition: User.h:290
const CString & GetUsername() const
CString m_sNick
Definition: User.h:247
unsigned int MaxNetworks() const
Definition: User.h:240
CString m_sRealName
Definition: User.h:250
bool AutoClearChanBuffer() const
void SetQuitMsg(const CString &s)
void SetNoTrafficTimeout(unsigned int i)
Definition: User.h:176
const CString & GetTimestampFormat() const
void SetSkinName(const CString &s)
Definition: User.h:173
unsigned int MaxJoins() const
Definition: User.h:237
void SetTimestampAppend(bool b)
Definition: User.h:167
CString m_sPass
Definition: User.h:253
bool IsBeingDeleted() const
Definition: User.h:232
CString & ExpandString(const CString &sStr, CString &sRet) const
void SetBindHost(const CString &s)
CString GetLanguage() const
CString m_sBindHost
Definition: User.h:251
unsigned int m_uQueryBufferSize
Definition: User.h:291
CString m_sSkinName
Definition: User.h:299
const std::set< CString > & GetAllowedHosts() const
bool IsUserAttached() const
CString AddTimestamp(timeval tv, const CString &sStr) const
bool IsAdmin() const
void UserConnected(CClient *pClient)
void SetIdent(const CString &s)
const CString & GetClientEncoding() const
CString GetTimezone() const
Definition: User.h:233
unsigned int JoinTries() const
Definition: User.h:236
bool m_bDenySetQuitMsg
Definition: User.h:276
bool SetQueryBufferSize(unsigned int u, bool bForce=false)
void SetMaxJoins(unsigned int i)
Definition: User.h:172
const CString & GetStatusPrefix() const
bool SetStatusPrefix(const CString &s)
bool PutAllUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr) ZNC_MSG_DEPRECATED("Use PutUser() instead")
Definition: User.h:98
void SetDenyLoadMod(bool b)
bool PutModule(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
bool ParseConfig(CConfig *Config, CString &sError)
bool DenySetRealName() const
void UserDisconnected(CClient *pClient)
bool SetLanguage(const CString &s)
bool m_bDenySetRealName
Definition: User.h:275
CModules * m_pModules
Definition: User.h:302
bool RemAllowedHost(const CString &sHostMask)
CString m_sClientEncoding
Definition: User.h:257
unsigned int GetBufferCount() const
bool MultiClients() const
bool GetTimestampAppend() const
const CString & GetPass() const
eHashType m_eHashType
Definition: User.h:263
eHashType
Definition: User.h:48
@ HASH_SHA256
Definition: User.h:51
@ HASH_MD5
Definition: User.h:50
@ HASH_ARGON2ID
Definition: User.h:52
@ HASH_DEFAULT
Definition: User.h:58
@ HASH_NONE
Definition: User.h:49
void SetDenySetQuitMsg(bool b)
bool m_bAutoClearQueryBuffer
Definition: User.h:279
void SetAuthOnlyViaModule(bool b)
Definition: User.h:169
CString m_sQuitMsg
Definition: User.h:259
void SetTimestampPrepend(bool b)
Definition: User.h:168
void SetAdmin(bool b)
const CString & GetDCCBindHost() const
CString m_sStatusPrefix
Definition: User.h:255
eHashType GetPassHashType() const
static CString MakeCleanUserName(const CString &sUsername)
static CString SaltedHash(const CString &sPass, const CString &sSalt)
A dictionary for strings.
Definition: ZNCString.h:595