ZNC  trunk
IRCNetwork.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_IRCNETWORK_H
18 #define ZNC_IRCNETWORK_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/ZNCString.h>
22 #include <znc/Buffer.h>
23 #include <znc/Nick.h>
24 #include <znc/znc.h>
25 
26 class CModules;
27 class CUser;
28 class CFile;
29 class CConfig;
30 class CClient;
31 class CConfig;
32 class CChan;
33 class CQuery;
34 class CServer;
35 class CIRCSock;
36 class CIRCNetworkPingTimer;
37 class CIRCNetworkJoinTimer;
38 class CMessage;
39 
41  public:
42  static bool IsValidNetwork(const CString& sNetwork);
43 
44  CIRCNetwork(CUser* pUser, const CString& sName);
45  CIRCNetwork(CUser* pUser, const CIRCNetwork& Network);
47 
48  CIRCNetwork(const CIRCNetwork&) = delete;
49  CIRCNetwork& operator=(const CIRCNetwork&) = delete;
50 
51  void Clone(const CIRCNetwork& Network, bool bCloneName = true);
52 
54 
55  void DelServers();
56 
57  bool ParseConfig(CConfig* pConfig, CString& sError, bool bUpgrade = false);
58  CConfig ToConfig() const;
59 
61 
62  bool IsUserAttached() const { return !m_vClients.empty(); }
63  bool IsUserOnline() const;
64  void ClientConnected(CClient* pClient);
65  void ClientDisconnected(CClient* pClient);
66 
67  CUser* GetUser() const;
68  const CString& GetName() const;
69  bool IsNetworkAttached() const { return !m_vClients.empty(); }
70  const std::vector<CClient*>& GetClients() const { return m_vClients; }
71  std::vector<CClient*> FindClients(const CString& sIdentifier) const;
72 
73  void SetUser(CUser* pUser);
74  bool SetName(const CString& sName);
75 
76  // Modules
77  CModules& GetModules() { return *m_pModules; }
78  const CModules& GetModules() const { return *m_pModules; }
79  // !Modules
80 
81  bool PutUser(const CString& sLine, CClient* pClient = nullptr,
82  CClient* pSkipClient = nullptr);
83  bool PutUser(const CMessage& Message, CClient* pClient = nullptr,
84  CClient* pSkipClient = nullptr);
85  bool PutStatus(const CString& sLine, CClient* pClient = nullptr,
86  CClient* pSkipClient = nullptr);
87  bool PutModule(const CString& sModule, const CString& sLine,
88  CClient* pClient = nullptr, CClient* pSkipClient = nullptr);
89 
90  const std::vector<CChan*>& GetChans() const;
91  CChan* FindChan(CString sName) const;
92  std::vector<CChan*> FindChans(const CString& sWild) const;
93  bool AddChan(CChan* pChan);
94  bool AddChan(const CString& sName, bool bInConfig);
95  bool DelChan(const CString& sName);
96  bool MoveChan(const CString& sChan, unsigned int index, CString& sError);
97  bool SwapChans(const CString& sChan1, const CString& sChan2,
98  CString& sError);
99  void JoinChans();
100  void JoinChans(std::set<CChan*>& sChans);
101 
102  const std::vector<CQuery*>& GetQueries() const;
103  CQuery* FindQuery(const CString& sName) const;
104  std::vector<CQuery*> FindQueries(const CString& sWild) const;
105  CQuery* AddQuery(const CString& sName);
106  bool DelQuery(const CString& sName);
107 
108  const CString& GetChanPrefixes() const { return m_sChanPrefixes; }
109  void SetChanPrefixes(const CString& s) { m_sChanPrefixes = s; }
110  bool IsChan(const CString& sChan) const;
111 
112  const std::vector<CServer*>& GetServers() const;
113  bool HasServers() const { return !m_vServers.empty(); }
114  CServer* FindServer(const CString& sName) const;
115  bool DelServer(const CString& sName, unsigned short uPort,
116  const CString& sPass);
117  bool AddServer(const CString& sName);
118  bool AddServer(const CString& sName, unsigned short uPort,
119  const CString& sPass = "", bool bSSL = false);
120  CServer* GetNextServer(bool bAdvance = true);
122  void SetIRCServer(const CString& s);
123  bool SetNextServer(const CServer* pServer);
124  bool IsLastServer() const;
125 
128  }
129  void AddTrustedFingerprint(const CString& sFP) {
132  }
133  void DelTrustedFingerprint(const CString& sFP) {
134  m_ssTrustedFingerprints.erase(sFP);
135  }
137 
138  void SetIRCConnectEnabled(bool b);
140 
142  const CIRCSock* GetIRCSock() const { return m_pIRCSock; }
143  const CString& GetIRCServer() const;
144  const CNick& GetIRCNick() const;
145  void SetIRCNick(const CNick& n);
147  bool IsIRCAway() const { return m_bIRCAway; }
148  void SetIRCAway(bool b) { m_bIRCAway = b; }
149 
150  bool Connect();
154  bool IsIRCConnected() const;
155  void SetIRCSocket(CIRCSock* pIRCSock);
156  void IRCConnected();
159  void NotifyClientsAboutServerDependentCap(const CString& sCap, bool bValue);
160  bool IsServerCapAccepted(const CString& sCap) const;
161 
162  bool PutIRC(const CString& sLine);
163  bool PutIRC(const CMessage& Message);
164 
165  // Buffers
166  void AddRawBuffer(const CMessage& Format, const CString& sText = "") {
167  m_RawBuffer.AddLine(Format, sText);
168  }
169  void UpdateRawBuffer(const CString& sCommand, const CMessage& Format,
170  const CString& sText = "") {
171  m_RawBuffer.UpdateLine(sCommand, Format, sText);
172  }
173  void UpdateExactRawBuffer(const CMessage& Format,
174  const CString& sText = "") {
175  m_RawBuffer.UpdateExactLine(Format, sText);
176  }
178 
180  void AddRawBuffer(const CString& sFormat, const CString& sText = "") {
181  m_RawBuffer.AddLine(sFormat, sText);
182  }
184  void UpdateRawBuffer(const CString& sMatch, const CString& sFormat,
185  const CString& sText = "") {
186  m_RawBuffer.UpdateLine(sMatch, sFormat, sText);
187  }
189  void UpdateExactRawBuffer(const CString& sFormat,
190  const CString& sText = "") {
191  m_RawBuffer.UpdateExactLine(sFormat, sText);
192  }
193 
194  void AddMotdBuffer(const CMessage& Format, const CString& sText = "") {
195  m_MotdBuffer.AddLine(Format, sText);
196  }
197  void UpdateMotdBuffer(const CString& sCommand, const CMessage& Format,
198  const CString& sText = "") {
199  m_MotdBuffer.UpdateLine(sCommand, Format, sText);
200  }
202 
204  void AddMotdBuffer(const CString& sFormat, const CString& sText = "") {
205  m_MotdBuffer.AddLine(sFormat, sText);
206  }
208  void UpdateMotdBuffer(const CString& sMatch, const CString& sFormat,
209  const CString& sText = "") {
210  m_MotdBuffer.UpdateLine(sMatch, sFormat, sText);
211  }
212 
213  void AddNoticeBuffer(const CMessage& Format, const CString& sText = "") {
214  m_NoticeBuffer.AddLine(Format, sText);
215  }
216  void UpdateNoticeBuffer(const CString& sCommand, const CMessage& Format,
217  const CString& sText = "") {
218  m_NoticeBuffer.UpdateLine(sCommand, Format, sText);
219  }
221 
223  void AddNoticeBuffer(const CString& sFormat, const CString& sText = "") {
224  m_NoticeBuffer.AddLine(sFormat, sText);
225  }
227  void UpdateNoticeBuffer(const CString& sMatch, const CString& sFormat,
228  const CString& sText = "") {
229  m_NoticeBuffer.UpdateLine(sMatch, sFormat, sText);
230  }
231 
233  // !Buffers
234 
235  // la
236  const CString& GetNick(const bool bAllowDefault = true) const;
237  const CString& GetAltNick(const bool bAllowDefault = true) const;
238  const CString& GetIdent(const bool bAllowDefault = true) const;
240  const CString& GetBindHost() const;
241  const CString& GetEncoding() const;
243 
244  void SetNick(const CString& s);
245  void SetAltNick(const CString& s);
246  void SetIdent(const CString& s);
247  void SetRealName(const CString& s);
248  void SetBindHost(const CString& s);
249  void SetEncoding(const CString& s);
250  void SetQuitMsg(const CString& s);
251 
252  double GetFloodRate() const { return m_fFloodRate; }
253  unsigned short int GetFloodBurst() const { return m_uFloodBurst; }
254  void SetFloodRate(double fFloodRate) { m_fFloodRate = fFloodRate; }
255  void SetFloodBurst(unsigned short int uFloodBurst) {
256  m_uFloodBurst = uFloodBurst;
257  }
258 
259  unsigned short int GetJoinDelay() const { return m_uJoinDelay; }
260  void SetJoinDelay(unsigned short int uJoinDelay) {
261  m_uJoinDelay = uJoinDelay;
262  }
263 
264  void SetTrustAllCerts(const bool bTrustAll = false) {
265  m_bTrustAllCerts = bTrustAll;
266  }
267  bool GetTrustAllCerts() const { return m_bTrustAllCerts; }
268 
269  void SetTrustPKI(const bool bTrustPKI = true) { m_bTrustPKI = bTrustPKI; }
270  bool GetTrustPKI() const { return m_bTrustPKI; }
271 
272  unsigned long long BytesRead() const { return m_uBytesRead; }
273  unsigned long long BytesWritten() const { return m_uBytesWritten; }
274 
275  void AddBytesRead(unsigned long long u) { m_uBytesRead += u; }
276  void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; }
277 
278  CString ExpandString(const CString& sStr) const;
279  CString& ExpandString(const CString& sStr, CString& sRet) const;
280 
281  private:
282  bool JoinChan(CChan* pChan);
283  bool LoadModule(const CString& sModName, const CString& sArgs,
284  const CString& sNotice, CString& sError);
285 
286  protected:
289 
298 
300 
301  std::vector<CClient*> m_vClients;
302 
304 
305  std::vector<CChan*> m_vChans;
306  std::vector<CQuery*> m_vQueries;
307 
309 
314  std::vector<CServer*> m_vServers;
315  size_t m_uServerIdx;
316 
319 
320  double m_fFloodRate;
321  unsigned short int m_uFloodBurst;
322 
326 
327  CIRCNetworkPingTimer* m_pPingTimer;
328  CIRCNetworkJoinTimer* m_pJoinTimer;
329 
330  unsigned short int m_uJoinDelay;
331  unsigned long long m_uBytesRead;
332  unsigned long long m_uBytesWritten;
333 };
334 
335 #endif // !ZNC_IRCNETWORK_H
std::set< CString > SCString
Definition: ZNCString.h:35
Definition: Buffer.h:74
size_type UpdateLine(const CString &sCommand, const CMessage &Format, const CString &sText="")
size_type AddLine(const CMessage &Format, const CString &sText="")
void Clear()
Definition: Buffer.h:100
size_type UpdateExactLine(const CMessage &Format, const CString &sText="")
Definition: Chan.h:35
Definition: Client.h:99
Definition: Config.h:36
Definition: Translation.h:103
Definition: FileUtils.h:30
Definition: IRCNetwork.h:40
void BounceAllClients()
void DelTrustedFingerprint(const CString &sFP)
Definition: IRCNetwork.h:133
const CString & GetEncoding() const
void SetBindHost(const CString &s)
CString m_sNick
Definition: IRCNetwork.h:290
bool SetNextServer(const CServer *pServer)
void DelServers()
void SetQuitMsg(const CString &s)
const CIRCSock * GetIRCSock() const
Definition: IRCNetwork.h:142
unsigned long long BytesWritten() const
Definition: IRCNetwork.h:273
const CNick & GetIRCNick() const
CString m_sRealName
Definition: IRCNetwork.h:293
void AddNoticeBuffer(const CMessage &Format, const CString &sText="")
Definition: IRCNetwork.h:213
bool IsLastServer() const
void IRCDisconnected()
void AddTrustedFingerprint(const CString &sFP)
Definition: IRCNetwork.h:129
CServer * FindServer(const CString &sName) const
void AddBytesWritten(unsigned long long u)
Definition: IRCNetwork.h:276
CUser * m_pUser
Definition: IRCNetwork.h:288
bool IsUserAttached() const
Definition: IRCNetwork.h:62
void ClearTrustedFingerprints()
Definition: IRCNetwork.h:136
void AddBytesRead(unsigned long long u)
Definition: IRCNetwork.h:275
CQuery * AddQuery(const CString &sName)
void SetIRCSocket(CIRCSock *pIRCSock)
CString ExpandString(const CString &sStr) const
void SetAltNick(const CString &s)
CIRCSock * GetIRCSock()
Definition: IRCNetwork.h:141
CServer * GetCurrentServer() const
CServer * GetNextServer(bool bAdvance=true)
CChan * FindChan(CString sName) const
CString m_sEncoding
Definition: IRCNetwork.h:295
void UpdateExactRawBuffer(const CMessage &Format, const CString &sText="")
Definition: IRCNetwork.h:173
CIRCNetwork(const CIRCNetwork &)=delete
static bool IsValidNetwork(const CString &sNetwork)
bool IsNetworkAttached() const
Definition: IRCNetwork.h:69
const std::vector< CQuery * > & GetQueries() const
bool IsIRCAway() const
Definition: IRCNetwork.h:147
void ClearNoticeBuffer()
Definition: IRCNetwork.h:220
void JoinChans()
bool IsUserOnline() const
unsigned short int m_uJoinDelay
Definition: IRCNetwork.h:330
void AddRawBuffer(const CString &sFormat, const CString &sText="")
Definition: IRCNetwork.h:180
CModules & GetModules()
Definition: IRCNetwork.h:77
void SetIdent(const CString &s)
bool PutStatus(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
bool m_bIRCAway
Definition: IRCNetwork.h:318
void ClientConnected(CClient *pClient)
void SetNick(const CString &s)
bool m_bTrustAllCerts
Definition: IRCNetwork.h:311
double GetFloodRate() const
Definition: IRCNetwork.h:252
CBuffer m_RawBuffer
Definition: IRCNetwork.h:323
const CString & GetBindHost() const
void AddNoticeBuffer(const CString &sFormat, const CString &sText="")
Definition: IRCNetwork.h:223
CString m_sName
Definition: IRCNetwork.h:287
bool IsChan(const CString &sChan) const
bool DelQuery(const CString &sName)
CIRCNetworkJoinTimer * m_pJoinTimer
Definition: IRCNetwork.h:328
CBuffer m_MotdBuffer
Definition: IRCNetwork.h:324
const std::vector< CClient * > & GetClients() const
Definition: IRCNetwork.h:70
SCString m_ssTrustedFingerprints
Definition: IRCNetwork.h:297
CString m_sBindHost
Definition: IRCNetwork.h:294
void UpdateRawBuffer(const CString &sMatch, const CString &sFormat, const CString &sText="")
Definition: IRCNetwork.h:184
CIRCNetwork(CUser *pUser, const CString &sName)
void SetFloodBurst(unsigned short int uFloodBurst)
Definition: IRCNetwork.h:255
CString GetCurNick() const
void SetTrustAllCerts(const bool bTrustAll=false)
Definition: IRCNetwork.h:264
const std::vector< CChan * > & GetChans() const
void SetIRCAway(bool b)
Definition: IRCNetwork.h:148
CNick m_IRCNick
Definition: IRCNetwork.h:317
bool GetTrustPKI() const
Definition: IRCNetwork.h:270
CIRCNetworkPingTimer * m_pPingTimer
Definition: IRCNetwork.h:327
CString m_sChanPrefixes
Definition: IRCNetwork.h:308
unsigned long long m_uBytesRead
Definition: IRCNetwork.h:331
CIRCSock * m_pIRCSock
Definition: IRCNetwork.h:303
CString GetQuitMsg() const
unsigned short int GetJoinDelay() const
Definition: IRCNetwork.h:259
void AddRawBuffer(const CMessage &Format, const CString &sText="")
Definition: IRCNetwork.h:166
const CString & GetIRCServer() const
bool HasServers() const
Definition: IRCNetwork.h:113
CBuffer m_NoticeBuffer
Definition: IRCNetwork.h:325
void ClearMotdBuffer()
Definition: IRCNetwork.h:201
CString & ExpandString(const CString &sStr, CString &sRet) const
const CString & GetNick(const bool bAllowDefault=true) const
bool PutModule(const CString &sModule, const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
void AddMotdBuffer(const CMessage &Format, const CString &sText="")
Definition: IRCNetwork.h:194
bool PutUser(const CString &sLine, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
void SetUser(CUser *pUser)
std::vector< CChan * > m_vChans
Definition: IRCNetwork.h:305
std::vector< CQuery * > FindQueries(const CString &sWild) const
bool MoveChan(const CString &sChan, unsigned int index, CString &sError)
unsigned long long BytesRead() const
Definition: IRCNetwork.h:272
void SetTrustPKI(const bool bTrustPKI=true)
Definition: IRCNetwork.h:269
std::vector< CServer * > m_vServers
Definition: IRCNetwork.h:314
CString GetRealName() const
const CString & GetIdent(const bool bAllowDefault=true) const
void NotifyClientsAboutServerDependentCap(const CString &sCap, bool bValue)
std::vector< CClient * > FindClients(const CString &sIdentifier) const
CIRCNetwork(CUser *pUser, const CIRCNetwork &Network)
const CModules & GetModules() const
Definition: IRCNetwork.h:78
size_t m_uServerIdx
Index in m_vServers of our current server + 1.
Definition: IRCNetwork.h:315
CConfig ToConfig() const
void SetIRCServer(const CString &s)
void UpdateMotdBuffer(const CString &sCommand, const CMessage &Format, const CString &sText="")
Definition: IRCNetwork.h:197
CIRCNetwork & operator=(const CIRCNetwork &)=delete
void CheckIRCConnect()
void UpdateRawBuffer(const CString &sCommand, const CMessage &Format, const CString &sText="")
Definition: IRCNetwork.h:169
unsigned short int GetFloodBurst() const
Definition: IRCNetwork.h:253
std::vector< CChan * > FindChans(const CString &sWild) const
bool ParseConfig(CConfig *pConfig, CString &sError, bool bUpgrade=false)
const CString & GetAltNick(const bool bAllowDefault=true) const
bool DelServer(const CString &sName, unsigned short uPort, const CString &sPass)
CUser * GetUser() const
CString m_sIRCServer
Definition: IRCNetwork.h:313
void SetJoinDelay(unsigned short int uJoinDelay)
Definition: IRCNetwork.h:260
bool AddChan(const CString &sName, bool bInConfig)
const std::vector< CServer * > & GetServers() const
CString m_sIdent
Definition: IRCNetwork.h:292
CString m_sAltNick
Definition: IRCNetwork.h:291
void SetIRCNick(const CNick &n)
const CString & GetName() const
bool PutIRC(const CMessage &Message)
bool GetTrustAllCerts() const
Definition: IRCNetwork.h:267
bool PutIRC(const CString &sLine)
void ClientDisconnected(CClient *pClient)
void ClearRawBuffer()
Definition: IRCNetwork.h:177
bool AddServer(const CString &sName, unsigned short uPort, const CString &sPass="", bool bSSL=false)
bool m_bIRCConnectEnabled
Definition: IRCNetwork.h:310
unsigned short int m_uFloodBurst
Definition: IRCNetwork.h:321
bool AddServer(const CString &sName)
bool m_bTrustPKI
Definition: IRCNetwork.h:312
bool SetName(const CString &sName)
unsigned long long m_uBytesWritten
Definition: IRCNetwork.h:332
std::vector< CQuery * > m_vQueries
Definition: IRCNetwork.h:306
void UpdateExactRawBuffer(const CString &sFormat, const CString &sText="")
Definition: IRCNetwork.h:189
void IRCConnected()
CQuery * FindQuery(const CString &sName) const
bool PutUser(const CMessage &Message, CClient *pClient=nullptr, CClient *pSkipClient=nullptr)
CModules * m_pModules
Definition: IRCNetwork.h:299
CString GetNetworkPath() const
void UpdateNoticeBuffer(const CString &sCommand, const CMessage &Format, const CString &sText="")
Definition: IRCNetwork.h:216
CString m_sQuitMsg
Definition: IRCNetwork.h:296
bool IsServerCapAccepted(const CString &sCap) const
void UpdateMotdBuffer(const CString &sMatch, const CString &sFormat, const CString &sText="")
Definition: IRCNetwork.h:208
void SetIRCConnectEnabled(bool b)
bool IsIRCConnected() const
This method will return whether the user is connected and authenticated to an IRC server.
void SetChanPrefixes(const CString &s)
Definition: IRCNetwork.h:109
bool DelChan(const CString &sName)
void JoinChans(std::set< CChan * > &sChans)
bool GetIRCConnectEnabled() const
Definition: IRCNetwork.h:139
void SetFloodRate(double fFloodRate)
Definition: IRCNetwork.h:254
void UpdateNoticeBuffer(const CString &sMatch, const CString &sFormat, const CString &sText="")
Definition: IRCNetwork.h:227
void Clone(const CIRCNetwork &Network, bool bCloneName=true)
void SetEncoding(const CString &s)
bool AddChan(CChan *pChan)
void SetRealName(const CString &s)
bool Connect()
double m_fFloodRate
Set to -1 to disable protection.
Definition: IRCNetwork.h:320
void AddMotdBuffer(const CString &sFormat, const CString &sText="")
Definition: IRCNetwork.h:204
void ClearQueryBuffer()
std::vector< CClient * > m_vClients
Definition: IRCNetwork.h:301
const SCString & GetTrustedFingerprints() const
Definition: IRCNetwork.h:126
const CString & GetChanPrefixes() const
Definition: IRCNetwork.h:108
bool SwapChans(const CString &sChan1, const CString &sChan2, CString &sError)
Definition: IRCSock.h:35
Here is a small explanation of how messages on IRC work, and how you can use this class to get useful...
Definition: Message.h:57
Definition: Modules.h:1464
Definition: Nick.h:29
Definition: Query.h:29
Definition: Server.h:23
String class that is used inside ZNC.
Definition: ZNCString.h:68
@ EHEXCOLON
Definition: ZNCString.h:78
CString Escape_n(EEscape eFrom, EEscape eTo) const
Definition: User.h:38