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);
71  void ClearAllowedHosts();
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 
117  CString GetLocalDCCIP() const;
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);
129  void BounceAllClients();
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);
161  void SetAutoClearChanBuffer(bool b);
162  void SetAutoClearQueryBuffer(bool b);
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;
189  CString GetRealName() const;
190  const CString& GetBindHost() const;
191  const CString& GetDCCBindHost() const;
192  const CString& GetPass() const;
193  eHashType GetPassHashType() 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 
224  CString GetQuitMsg() const;
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; }
238  CString GetSkinName() const;
239  CString GetLanguage() const;
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
const std::vector< CClient * > & GetUserClients() const
Definition: User.h:180
#define ZNC_MSG_DEPRECATED(msg)
Definition: Message.h:23
void SetJoinTries(unsigned int i)
Definition: User.h:171
const std::set< CString > & GetAllowedHosts() const
Definition: User.h:38
void SetQuitMsg(const CString &s)
void SetRealName(const CString &s)
unsigned int m_uMaxJoinTries
Definition: User.h:294
bool m_bDenySetBindHost
Definition: User.h:272
bool m_bDenySetRealName
Definition: User.h:275
const CString & GetCleanUserName() const
void SetBindHost(const CString &s)
const CString & GetClientEncoding() const
CIRCNetwork * FindNetwork(const CString &sNetwork) const
bool m_bMultiClients
Definition: User.h:269
CString m_sTimezone
Definition: User.h:262
CConfig ToConfig() const
void SetTimezone(const CString &s)
Definition: User.h:170
CString m_sPassSalt
Definition: User.h:254
Definition: User.h:51
void SetTimestampAppend(bool b)
Definition: User.h:167
void SetDenySetBindHost(bool b)
const CString & GetNick(bool bAllowDefault=true) const
void RemoveNetwork(CIRCNetwork *pNetwork)
const CString & GetAltNick(bool bAllowDefault=true) const
bool DelCTCPReply(const CString &sCTCP)
bool IsHostAllowed(const CString &sHost) const
CString m_sTimestampFormat
Definition: User.h:261
bool PutAllUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr) ZNC_MSG_DEPRECATED("Use PutUser() instead")
Definition: User.h:98
const CString & GetDefaultChanModes() const
CString ExpandString(const CString &sStr) const
const CString & GetIdent(bool bAllowDefault=true) const
unsigned int GetChanBufferSize() const
bool RemAllowedHost(const CString &sHostMask)
void AddBytesRead(unsigned long long u)
Definition: User.h:131
bool CheckPass(const CString &sPass)
Checks password, may upgrade the hash method.
bool MultiClients() const
Definition: Client.h:99
bool m_bPrependTimestamp
Definition: User.h:282
Definition: User.h:49
std::vector< CIRCNetwork * > m_vIRCNetworks
Definition: User.h:287
CIRCNetwork * AddNetwork(const CString &sNetwork, CString &sErrorRet)
CString m_sStatusPrefix
Definition: User.h:255
MCString m_mssCTCPReplies
Definition: User.h:260
bool IsBeingDeleted() const
Definition: User.h:232
unsigned int JoinTries() const
Definition: User.h:236
const CString & GetTimestampFormat() const
bool PutStatusNotice(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
eHashType
Definition: User.h:48
void SetDCCBindHost(const CString &s)
void SetDenySetIdent(bool b)
void BounceAllClients()
Definition: Modules.h:1465
bool SetChanBufferSize(unsigned int u, bool bForce=false)
const CString & GetUserPath() const
void SetDenySetCTCPReplies(bool b)
bool AddAllowedHost(const CString &sHostMask)
unsigned int MaxJoins() const
Definition: User.h:237
const CString & GetPassSalt() const
bool m_bDenySetCTCPReplies
Definition: User.h:277
bool GetTimestampPrepend() const
CString m_sAltNick
Definition: User.h:248
unsigned int MaxQueryBuffers() const
Definition: User.h:241
CString m_sNick
Definition: User.h:247
Definition: User.h:58
void SetSkinName(const CString &s)
Definition: User.h:173
static CString MakeCleanUserName(const CString &sUsername)
CString m_sRealName
Definition: User.h:250
bool PutStatus(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
bool DeleteNetwork(const CString &sNetwork)
CString m_sPass
Definition: User.h:253
bool DenySetIdent() const
CString m_sIdent
Definition: User.h:249
Definition: FileUtils.h:30
CString m_sLanguage
Definition: User.h:300
Definition: IRCNetwork.h:40
CString m_sUserPath
Definition: User.h:266
void UserConnected(CClient *pClient)
const CString & GetBindHost() const
bool AutoClearChanBuffer() const
bool IsUserAttached() const
void SetMultiClients(bool b)
CUserTimer * m_pUserTimer
Definition: User.h:285
eHashType GetPassHashType() const
bool m_bAutoClearChanBuffer
Definition: User.h:278
String class that is used inside ZNC.
Definition: ZNCString.h:68
bool m_bDenyLoadMod
Definition: User.h:270
Definition: User.h:52
unsigned long long BytesWritten() const
bool SetLanguage(const CString &s)
const CString & GetDCCBindHost() const
unsigned int m_uNoTrafficTimeout
Definition: User.h:298
void SetAltNick(const CString &s)
const CString & GetPass() const
bool SetStatusPrefix(const CString &s)
void SetAdmin(bool b)
unsigned int m_uMaxJoins
Definition: User.h:297
std::set< CString > m_ssAllowedHosts
Definition: User.h:289
std::vector< CClient * > GetAllClients() const
bool DenySetCTCPReplies() const
const CString & GetUserName() const
void SetDenyLoadMod(bool b)
void SetMaxNetworks(unsigned int i)
Definition: User.h:174
bool DenyLoadMod() const
bool AutoClearQueryBuffer() const
void SetDenySetQuitMsg(bool b)
void SetBeingDeleted(bool b)
Definition: User.h:165
bool DenySetNetwork() const
unsigned int m_uMaxQueryBuffers
Definition: User.h:296
const CString & GetStatusPrefix() const
bool ParseConfig(CConfig *Config, CString &sError)
bool m_bBeingDeleted
Definition: User.h:280
CString m_sClientEncoding
Definition: User.h:257
bool m_bAutoClearQueryBuffer
Definition: User.h:279
bool IsAdmin() const
static CString SaltedHash(const CString &sPass, const CString &sSalt)
void SetNick(const CString &s)
void SetNoTrafficTimeout(unsigned int i)
Definition: User.h:176
CString GetTimezone() const
Definition: User.h:233
unsigned long long BytesRead() const
void UserDisconnected(CClient *pClient)
unsigned int MaxNetworks() const
Definition: User.h:240
bool IsValid(CString &sErrMsg, bool bSkipPass=false) const
CUser & operator=(const CUser &)=delete
void SetAutoClearChanBuffer(bool b)
void SetDenySetNetwork(bool b)
unsigned int m_uChanBufferSize
Definition: User.h:290
bool AddCTCPReply(const CString &sCTCP, const CString &sReply)
static CString MakeCleanUsername(const CString &sUsername)
CString m_sSkinName
Definition: User.h:299
bool m_bAdmin
Definition: User.h:271
bool m_bAppendTimestamp
Definition: User.h:281
void SetIdent(const CString &s)
CModules * m_pModules
Definition: User.h:302
unsigned int GetNoTrafficTimeout() const
Timeout after which IRC connections are closed.
Definition: User.h:222
bool DenySetBindHost() const
const CString & GetUsername() const
const CString m_sCleanUsername
Definition: User.h:246
unsigned int GetQueryBufferSize() const
CString m_sQuitMsg
Definition: User.h:259
A dictionary for strings.
Definition: ZNCString.h:595
const MCString & GetCTCPReplies() const
bool m_bDenySetIdent
Definition: User.h:273
const CString m_sUsername
Definition: User.h:245
void CloneNetworks(const CUser &User)
bool PutUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
void SetDenySetRealName(bool b)
bool GetTimestampAppend() const
bool m_bDenySetQuitMsg
Definition: User.h:276
unsigned int m_uMaxNetworks
Definition: User.h:295
bool DenySetQuitMsg() const
bool Clone(const CUser &User, CString &sErrorRet, bool bCloneNetworks=true)
unsigned int GetPingSlack() const
Time between checks if PINGs need to be sent.
Definition: User.h:218
CString GetLocalDCCIP() const
CString AddTimestamp(const CString &sStr) const
void SetTimestampFormat(const CString &s)
Definition: User.h:166
CString m_sBindHost
Definition: User.h:251
void SetAutoClearQueryBuffer(bool b)
CString GetRealName() const
void SetPass(const CString &s, eHashType eHash, const CString &sSalt="")
unsigned int GetBufferCount() const
eHashType m_eHashType
Definition: User.h:263
unsigned long long m_uBytesWritten
Definition: User.h:293
unsigned int GetPingFrequency() const
How long must an IRC connection be idle before ZNC sends a ping.
Definition: User.h:216
bool AuthOnlyViaModule() const
CString m_sDefaultChanModes
Definition: User.h:256
void ClearAllowedHosts()
std::vector< CClient * > m_vClients
Definition: User.h:288
void SetDefaultChanModes(const CString &s)
bool PutModule(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
bool PutModNotice(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
unsigned int m_uQueryBufferSize
Definition: User.h:291
CString GetQuitMsg() const
bool DenySetRealName() const
CString GetSkinName() const
void SetClientEncoding(const CString &s)
Definition: Server.h:23
static bool IsValidUsername(const CString &sUsername)
const CModules & GetModules() const
Definition: User.h:83
static CString SaltedHash(const CString &sPass, const CString &sSalt)
Definition: User.h:62
void SetMaxQueryBuffers(unsigned int i)
Definition: User.h:175
void SetAuthOnlyViaModule(bool b)
Definition: User.h:169
CString GetLanguage() const
void AddBytesWritten(unsigned long long u)
Definition: User.h:132
Definition: IRCSock.h:35
bool SetQueryBufferSize(unsigned int u, bool bForce=false)
bool m_bDenySetNetwork
Definition: User.h:274
Definition: Chan.h:35
unsigned long long m_uBytesRead
Definition: User.h:292
bool HasSpaceForNewNetwork() const
Definition: Config.h:36
void SetTimestampPrepend(bool b)
Definition: User.h:168
CUser(const CString &sUsername)
CModules & GetModules()
Definition: User.h:82
Definition: User.h:50
const std::vector< CIRCNetwork * > & GetNetworks() const
static bool IsValidUserName(const CString &sUsername)
void SetMaxJoins(unsigned int i)
Definition: User.h:172
bool SetBufferCount(unsigned int u, bool bForce=false)
CString m_sDCCBindHost
Definition: User.h:252
bool m_bAuthOnlyViaModule
Definition: User.h:283
Definition: Translation.h:103