ZNC
Versions: latest readthedocs-test
User.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2017 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 <set>
25 #include <vector>
26 
27 class CModules;
28 class CChan;
29 class CClient;
30 class CConfig;
31 class CFile;
32 class CIRCNetwork;
33 class CIRCSock;
34 class CUserTimer;
35 class CServer;
36 
37 class CUser {
38  public:
39  CUser(const CString& sUserName);
40  ~CUser();
41 
42  CUser(const CUser&) = delete;
43  CUser& operator=(const CUser&) = delete;
44 
45  bool ParseConfig(CConfig* Config, CString& sError);
46 
47  // TODO refactor this
48  enum eHashType {
52 
54  };
55 
56  // If you change the default hash here and in HASH_DEFAULT,
57  // don't forget CUtils::sDefaultHash!
58  // TODO refactor this
59  static CString SaltedHash(const CString& sPass, const CString& sSalt) {
60  return CUtils::SaltedSHA256Hash(sPass, sSalt);
61  }
62 
63  CConfig ToConfig() const;
64  bool CheckPass(const CString& sPass) const;
65  bool AddAllowedHost(const CString& sHostMask);
66  bool RemAllowedHost(const CString& sHostMask);
67  void ClearAllowedHosts();
68  bool IsHostAllowed(const CString& sHost) const;
69  bool IsValid(CString& sErrMsg, bool bSkipPass = false) const;
70  static bool IsValidUserName(const CString& sUserName);
71  static CString MakeCleanUserName(const CString& sUserName);
72 
73  // Modules
74  CModules& GetModules() { return *m_pModules; }
75  const CModules& GetModules() const { return *m_pModules; }
76  // !Modules
77 
78  // Networks
79  CIRCNetwork* AddNetwork(const CString& sNetwork, CString& sErrorRet);
80  bool DeleteNetwork(const CString& sNetwork);
81  bool AddNetwork(CIRCNetwork* pNetwork);
82  void RemoveNetwork(CIRCNetwork* pNetwork);
83  CIRCNetwork* FindNetwork(const CString& sNetwork) const;
84  const std::vector<CIRCNetwork*>& GetNetworks() const;
85  bool HasSpaceForNewNetwork() const;
86  // !Networks
87 
88  bool PutUser(const CString& sLine, CClient* pClient = nullptr,
89  CClient* pSkipClient = nullptr);
90  bool PutAllUser(const CString& sLine, CClient* pClient = nullptr,
91  CClient* pSkipClient = nullptr);
92  bool PutStatus(const CString& sLine, CClient* pClient = nullptr,
93  CClient* pSkipClient = nullptr);
94  bool PutStatusNotice(const CString& sLine, CClient* pClient = nullptr,
95  CClient* pSkipClient = nullptr);
96  bool PutModule(const CString& sModule, const CString& sLine,
97  CClient* pClient = nullptr, CClient* pSkipClient = nullptr);
98  bool PutModNotice(const CString& sModule, const CString& sLine,
99  CClient* pClient = nullptr,
100  CClient* pSkipClient = nullptr);
101 
102  bool IsUserAttached() const;
103  void UserConnected(CClient* pClient);
104  void UserDisconnected(CClient* pClient);
105 
106  CString GetLocalDCCIP() const;
107 
108  CString ExpandString(const CString& sStr) const;
109  CString& ExpandString(const CString& sStr, CString& sRet) const;
110 
111  CString AddTimestamp(const CString& sStr) const;
112  CString AddTimestamp(time_t tm, const CString& sStr) const;
113 
114  void CloneNetworks(const CUser& User);
115  bool Clone(const CUser& User, CString& sErrorRet,
116  bool bCloneNetworks = true);
117  void BounceAllClients();
118 
119  void AddBytesRead(unsigned long long u) { m_uBytesRead += u; }
120  void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; }
121 
122  // Setters
123  void SetNick(const CString& s);
124  void SetAltNick(const CString& s);
125  void SetIdent(const CString& s);
126  void SetRealName(const CString& s);
127  void SetBindHost(const CString& s);
128  void SetDCCBindHost(const CString& s);
129  void SetPass(const CString& s, eHashType eHash, const CString& sSalt = "");
130  void SetMultiClients(bool b);
131  void SetDenyLoadMod(bool b);
132  void SetAdmin(bool b);
133  void SetDenySetBindHost(bool b);
134  bool SetStatusPrefix(const CString& s);
135  void SetDefaultChanModes(const CString& s);
136  void SetClientEncoding(const CString& s);
137  void SetQuitMsg(const CString& s);
138  bool AddCTCPReply(const CString& sCTCP, const CString& sReply);
139  bool DelCTCPReply(const CString& sCTCP);
141  bool SetBufferCount(unsigned int u, bool bForce = false);
142  bool SetChanBufferSize(unsigned int u, bool bForce = false);
143  bool SetQueryBufferSize(unsigned int u, bool bForce = false);
144  void SetAutoClearChanBuffer(bool b);
145  void SetAutoClearQueryBuffer(bool b);
146  bool SetLanguage(const CString& s);
147 
148  void SetBeingDeleted(bool b) { m_bBeingDeleted = b; }
152  void SetTimezone(const CString& s) { m_sTimezone = s; }
153  void SetJoinTries(unsigned int i) { m_uMaxJoinTries = i; }
154  void SetMaxJoins(unsigned int i) { m_uMaxJoins = i; }
155  void SetSkinName(const CString& s) { m_sSkinName = s; }
156  void SetMaxNetworks(unsigned int i) { m_uMaxNetworks = i; }
157  void SetMaxQueryBuffers(unsigned int i) { m_uMaxQueryBuffers = i; }
158  void SetNoTrafficTimeout(unsigned int i) { m_uNoTrafficTimeout = i; }
159  // !Setters
160 
161  // Getters
162  const std::vector<CClient*>& GetUserClients() const { return m_vClients; }
163  std::vector<CClient*> GetAllClients() const;
164  const CString& GetUserName() const;
165  const CString& GetCleanUserName() const;
166  const CString& GetNick(bool bAllowDefault = true) const;
167  const CString& GetAltNick(bool bAllowDefault = true) const;
168  const CString& GetIdent(bool bAllowDefault = true) const;
169  CString GetRealName() const;
170  const CString& GetBindHost() const;
171  const CString& GetDCCBindHost() const;
172  const CString& GetPass() const;
173  eHashType GetPassHashType() const;
174  const CString& GetPassSalt() const;
175  const std::set<CString>& GetAllowedHosts() const;
176  const CString& GetTimestampFormat() const;
177  const CString& GetClientEncoding() const;
178  bool GetTimestampAppend() const;
179  bool GetTimestampPrepend() const;
180 
181  const CString& GetUserPath() const;
182 
183  bool DenyLoadMod() const;
184  bool IsAdmin() const;
185  bool DenySetBindHost() const;
186  bool MultiClients() const;
187  const CString& GetStatusPrefix() const;
188  const CString& GetDefaultChanModes() const;
190  unsigned int GetPingFrequency() const { return m_uNoTrafficTimeout / 2; }
192  unsigned int GetPingSlack() const { return m_uNoTrafficTimeout / 6; }
196  unsigned int GetNoTrafficTimeout() const { return m_uNoTrafficTimeout; }
197 
198  CString GetQuitMsg() const;
199  const MCString& GetCTCPReplies() const;
201  unsigned int GetBufferCount() const;
202  unsigned int GetChanBufferSize() const;
203  unsigned int GetQueryBufferSize() const;
204  bool AutoClearChanBuffer() const;
205  bool AutoClearQueryBuffer() const;
206  bool IsBeingDeleted() const { return m_bBeingDeleted; }
207  CString GetTimezone() const { return m_sTimezone; }
208  unsigned long long BytesRead() const;
209  unsigned long long BytesWritten() const;
210  unsigned int JoinTries() const { return m_uMaxJoinTries; }
211  unsigned int MaxJoins() const { return m_uMaxJoins; }
212  CString GetSkinName() const;
213  CString GetLanguage() const;
214  unsigned int MaxNetworks() const { return m_uMaxNetworks; }
215  unsigned int MaxQueryBuffers() const { return m_uMaxQueryBuffers; }
216  // !Getters
217 
218  protected:
232 
238 
239  // Paths
241  // !Paths
242 
245  bool m_bAdmin;
252 
253  CUserTimer* m_pUserTimer;
254 
255  std::vector<CIRCNetwork*> m_vIRCNetworks;
256  std::vector<CClient*> m_vClients;
257  std::set<CString> m_ssAllowedHosts;
258  unsigned int m_uChanBufferSize;
259  unsigned int m_uQueryBufferSize;
260  unsigned long long m_uBytesRead;
261  unsigned long long m_uBytesWritten;
262  unsigned int m_uMaxJoinTries;
263  unsigned int m_uMaxNetworks;
264  unsigned int m_uMaxQueryBuffers;
265  unsigned int m_uMaxJoins;
266  unsigned int m_uNoTrafficTimeout;
269 
271 
272  private:
273  void SetKeepBuffer(bool b) {
275  } // XXX compatibility crap, added in 0.207
276  bool LoadModule(const CString& sModName, const CString& sArgs,
277  const CString& sNotice, CString& sError);
278 };
279 
280 #endif // !ZNC_USER_H
void SetJoinTries(unsigned int i)
Definition: User.h:153
Definition: User.h:37
bool IsAdmin() const
eHashType GetPassHashType() const
bool IsBeingDeleted() const
Definition: User.h:206
void SetQuitMsg(const CString &s)
void SetRealName(const CString &s)
unsigned int m_uMaxJoinTries
Definition: User.h:262
bool m_bDenySetBindHost
Definition: User.h:246
CConfig ToConfig() const
const CString m_sUserName
Definition: User.h:219
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:243
CString m_sTimezone
Definition: User.h:236
void SetTimezone(const CString &s)
Definition: User.h:152
CString m_sPassSalt
Definition: User.h:228
Definition: User.h:51
void SetTimestampAppend(bool b)
Definition: User.h:150
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:235
static CString MakeCleanUserName(const CString &sUserName)
unsigned int GetNoTrafficTimeout() const
Timeout after which IRC connections are closed.
Definition: User.h:196
bool IsUserAttached() const
const CString & GetTimestampFormat() const
bool RemAllowedHost(const CString &sHostMask)
void AddBytesRead(unsigned long long u)
Definition: User.h:119
const CString & GetUserName() const
Definition: Client.h:98
bool m_bPrependTimestamp
Definition: User.h:251
Definition: User.h:49
std::vector< CIRCNetwork * > m_vIRCNetworks
Definition: User.h:255
CIRCNetwork * AddNetwork(const CString &sNetwork, CString &sErrorRet)
CString m_sStatusPrefix
Definition: User.h:229
MCString m_mssCTCPReplies
Definition: User.h:234
bool AutoClearChanBuffer() const
bool PutStatusNotice(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
eHashType
Definition: User.h:48
bool MultiClients() const
void SetDCCBindHost(const CString &s)
void BounceAllClients()
Definition: Modules.h:1379
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:211
CString m_sAltNick
Definition: User.h:222
const std::set< CString > & GetAllowedHosts() const
CString AddTimestamp(const CString &sStr) const
const CModules & GetModules() const
Definition: User.h:75
const CString & GetClientEncoding() const
CString m_sNick
Definition: User.h:221
Definition: User.h:53
unsigned int GetQueryBufferSize() const
void SetSkinName(const CString &s)
Definition: User.h:155
unsigned long long BytesRead() const
CString m_sRealName
Definition: User.h:224
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:227
CString m_sIdent
Definition: User.h:223
Definition: FileUtils.h:30
unsigned int MaxQueryBuffers() const
Definition: User.h:215
CString m_sLanguage
Definition: User.h:268
Definition: IRCNetwork.h:40
const MCString & GetCTCPReplies() const
CString m_sUserPath
Definition: User.h:240
void UserConnected(CClient *pClient)
void SetMultiClients(bool b)
CUserTimer * m_pUserTimer
Definition: User.h:253
bool m_bAutoClearChanBuffer
Definition: User.h:247
String class that is used inside ZNC.
Definition: ZNCString.h:68
bool m_bDenyLoadMod
Definition: User.h:244
const std::vector< CClient * > & GetUserClients() const
Definition: User.h:162
bool CheckPass(const CString &sPass) const
bool SetLanguage(const CString &s)
unsigned int m_uNoTrafficTimeout
Definition: User.h:266
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:265
static bool IsValidUserName(const CString &sUserName)
std::set< CString > m_ssAllowedHosts
Definition: User.h:257
const std::vector< CIRCNetwork * > & GetNetworks() const
void SetDenyLoadMod(bool b)
void SetMaxNetworks(unsigned int i)
Definition: User.h:156
static CString SaltedSHA256Hash(const CString &sPass, const CString &sSalt)
void SetBeingDeleted(bool b)
Definition: User.h:148
unsigned int m_uMaxQueryBuffers
Definition: User.h:264
bool ParseConfig(CConfig *Config, CString &sError)
bool GetTimestampPrepend() const
bool m_bBeingDeleted
Definition: User.h:249
bool PutAllUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
CString m_sClientEncoding
Definition: User.h:231
bool m_bAutoClearQueryBuffer
Definition: User.h:248
void SetNick(const CString &s)
void SetNoTrafficTimeout(unsigned int i)
Definition: User.h:158
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:190
CString GetRealName() const
CUser & operator=(const CUser &)=delete
void SetAutoClearChanBuffer(bool b)
unsigned int MaxNetworks() const
Definition: User.h:214
unsigned int m_uChanBufferSize
Definition: User.h:258
bool AddCTCPReply(const CString &sCTCP, const CString &sReply)
bool DenyLoadMod() const
CString m_sSkinName
Definition: User.h:267
bool m_bAdmin
Definition: User.h:245
bool m_bAppendTimestamp
Definition: User.h:250
void SetIdent(const CString &s)
CModules * m_pModules
Definition: User.h:270
CString GetLanguage() const
CUser(const CString &sUserName)
CString m_sQuitMsg
Definition: User.h:233
const CString m_sCleanUserName
Definition: User.h:220
A dictionary for strings.
Definition: ZNCString.h:595
const CString & GetNick(bool bAllowDefault=true) const
std::vector< CClient * > GetAllClients() const
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:263
bool Clone(const CUser &User, CString &sErrorRet, bool bCloneNetworks=true)
void SetTimestampFormat(const CString &s)
Definition: User.h:149
unsigned int GetPingSlack() const
Time between checks if PINGs need to be sent.
Definition: User.h:192
bool GetTimestampAppend() const
CString m_sBindHost
Definition: User.h:225
const CString & GetCleanUserName() const
void SetAutoClearQueryBuffer(bool b)
void SetPass(const CString &s, eHashType eHash, const CString &sSalt="")
eHashType m_eHashType
Definition: User.h:237
unsigned int GetBufferCount() const
unsigned long long m_uBytesWritten
Definition: User.h:261
CString m_sDefaultChanModes
Definition: User.h:230
void ClearAllowedHosts()
std::vector< CClient * > m_vClients
Definition: User.h:256
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:210
bool PutModNotice(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
unsigned int m_uQueryBufferSize
Definition: User.h:259
bool DenySetBindHost() const
void SetClientEncoding(const CString &s)
CString GetTimezone() const
Definition: User.h:207
Definition: Server.h:23
static CString SaltedHash(const CString &sPass, const CString &sSalt)
Definition: User.h:59
void SetMaxQueryBuffers(unsigned int i)
Definition: User.h:157
void AddBytesWritten(unsigned long long u)
Definition: User.h:120
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:34
unsigned long long m_uBytesRead
Definition: User.h:260
Definition: Config.h:36
void SetTimestampPrepend(bool b)
Definition: User.h:151
CModules & GetModules()
Definition: User.h:74
Definition: User.h:50
void SetMaxJoins(unsigned int i)
Definition: User.h:154
bool SetBufferCount(unsigned int u, bool bForce=false)
const CString & GetPassSalt() const
CString m_sDCCBindHost
Definition: User.h:226
bool AutoClearQueryBuffer() const