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