1 /* 2 * Copyright (C) 2004-2020 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 : private CCoreTranslationMixin { 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 IsUserAttached()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; IsNetworkAttached()69 bool IsNetworkAttached() const { return !m_vClients.empty(); } GetClients()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 GetModules()77 CModules& GetModules() { return *m_pModules; } GetModules()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 GetChanPrefixes()105 const CString& GetChanPrefixes() const { return m_sChanPrefixes; } SetChanPrefixes(const CString & s)106 void SetChanPrefixes(const CString& s) { m_sChanPrefixes = s; } 107 bool IsChan(const CString& sChan) const; 108 109 const std::vector<CServer*>& GetServers() const; HasServers()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 GetTrustedFingerprints()123 const SCString& GetTrustedFingerprints() const { 124 return m_ssTrustedFingerprints; 125 } AddTrustedFingerprint(const CString & sFP)126 void AddTrustedFingerprint(const CString& sFP) { 127 m_ssTrustedFingerprints.insert( 128 sFP.Escape_n(CString::EHEXCOLON, CString::EHEXCOLON)); 129 } DelTrustedFingerprint(const CString & sFP)130 void DelTrustedFingerprint(const CString& sFP) { 131 m_ssTrustedFingerprints.erase(sFP); 132 } ClearTrustedFingerprints()133 void ClearTrustedFingerprints() { m_ssTrustedFingerprints.clear(); } 134 135 void SetIRCConnectEnabled(bool b); GetIRCConnectEnabled()136 bool GetIRCConnectEnabled() const { return m_bIRCConnectEnabled; } 137 GetIRCSock()138 CIRCSock* GetIRCSock() { return m_pIRCSock; } GetIRCSock()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; IsIRCAway()144 bool IsIRCAway() const { return m_bIRCAway; } SetIRCAway(bool b)145 void SetIRCAway(bool b) { m_bIRCAway = b; } 146 147 bool Connect(); 148 /** This method will return whether the user is connected and authenticated 149 * to an IRC server. 150 */ 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 bool PutIRC(const CMessage& Message); 159 160 // Buffers 161 void AddRawBuffer(const CMessage& Format, const CString& sText = "") { 162 m_RawBuffer.AddLine(Format, sText); 163 } 164 void UpdateRawBuffer(const CString& sCommand, const CMessage& Format, 165 const CString& sText = "") { 166 m_RawBuffer.UpdateLine(sCommand, Format, sText); 167 } 168 void UpdateExactRawBuffer(const CMessage& Format, 169 const CString& sText = "") { 170 m_RawBuffer.UpdateExactLine(Format, sText); 171 } ClearRawBuffer()172 void ClearRawBuffer() { m_RawBuffer.Clear(); } 173 174 /// @deprecated 175 void AddRawBuffer(const CString& sFormat, const CString& sText = "") { 176 m_RawBuffer.AddLine(sFormat, sText); 177 } 178 /// @deprecated 179 void UpdateRawBuffer(const CString& sMatch, const CString& sFormat, 180 const CString& sText = "") { 181 m_RawBuffer.UpdateLine(sMatch, sFormat, sText); 182 } 183 /// @deprecated 184 void UpdateExactRawBuffer(const CString& sFormat, 185 const CString& sText = "") { 186 m_RawBuffer.UpdateExactLine(sFormat, sText); 187 } 188 189 void AddMotdBuffer(const CMessage& Format, const CString& sText = "") { 190 m_MotdBuffer.AddLine(Format, sText); 191 } 192 void UpdateMotdBuffer(const CString& sCommand, const CMessage& Format, 193 const CString& sText = "") { 194 m_MotdBuffer.UpdateLine(sCommand, Format, sText); 195 } ClearMotdBuffer()196 void ClearMotdBuffer() { m_MotdBuffer.Clear(); } 197 198 /// @deprecated 199 void AddMotdBuffer(const CString& sFormat, const CString& sText = "") { 200 m_MotdBuffer.AddLine(sFormat, sText); 201 } 202 /// @deprecated 203 void UpdateMotdBuffer(const CString& sMatch, const CString& sFormat, 204 const CString& sText = "") { 205 m_MotdBuffer.UpdateLine(sMatch, sFormat, sText); 206 } 207 208 void AddNoticeBuffer(const CMessage& Format, const CString& sText = "") { 209 m_NoticeBuffer.AddLine(Format, sText); 210 } 211 void UpdateNoticeBuffer(const CString& sCommand, const CMessage& Format, 212 const CString& sText = "") { 213 m_NoticeBuffer.UpdateLine(sCommand, Format, sText); 214 } ClearNoticeBuffer()215 void ClearNoticeBuffer() { m_NoticeBuffer.Clear(); } 216 217 /// @deprecated 218 void AddNoticeBuffer(const CString& sFormat, const CString& sText = "") { 219 m_NoticeBuffer.AddLine(sFormat, sText); 220 } 221 /// @deprecated 222 void UpdateNoticeBuffer(const CString& sMatch, const CString& sFormat, 223 const CString& sText = "") { 224 m_NoticeBuffer.UpdateLine(sMatch, sFormat, sText); 225 } 226 227 void ClearQueryBuffer(); 228 // !Buffers 229 230 // la 231 const CString& GetNick(const bool bAllowDefault = true) const; 232 const CString& GetAltNick(const bool bAllowDefault = true) const; 233 const CString& GetIdent(const bool bAllowDefault = true) const; 234 CString GetRealName() const; 235 const CString& GetBindHost() const; 236 const CString& GetEncoding() const; 237 CString GetQuitMsg() const; 238 239 void SetNick(const CString& s); 240 void SetAltNick(const CString& s); 241 void SetIdent(const CString& s); 242 void SetRealName(const CString& s); 243 void SetBindHost(const CString& s); 244 void SetEncoding(const CString& s); 245 void SetQuitMsg(const CString& s); 246 GetFloodRate()247 double GetFloodRate() const { return m_fFloodRate; } GetFloodBurst()248 unsigned short int GetFloodBurst() const { return m_uFloodBurst; } SetFloodRate(double fFloodRate)249 void SetFloodRate(double fFloodRate) { m_fFloodRate = fFloodRate; } SetFloodBurst(unsigned short int uFloodBurst)250 void SetFloodBurst(unsigned short int uFloodBurst) { 251 m_uFloodBurst = uFloodBurst; 252 } 253 GetJoinDelay()254 unsigned short int GetJoinDelay() const { return m_uJoinDelay; } SetJoinDelay(unsigned short int uJoinDelay)255 void SetJoinDelay(unsigned short int uJoinDelay) { 256 m_uJoinDelay = uJoinDelay; 257 } 258 259 void SetTrustAllCerts(const bool bTrustAll = false) { 260 m_bTrustAllCerts = bTrustAll; 261 } GetTrustAllCerts()262 bool GetTrustAllCerts() const { return m_bTrustAllCerts; } 263 264 void SetTrustPKI(const bool bTrustPKI = true) { m_bTrustPKI = bTrustPKI; } GetTrustPKI()265 bool GetTrustPKI() const { return m_bTrustPKI; } 266 BytesRead()267 unsigned long long BytesRead() const { return m_uBytesRead; } BytesWritten()268 unsigned long long BytesWritten() const { return m_uBytesWritten; } 269 AddBytesRead(unsigned long long u)270 void AddBytesRead(unsigned long long u) { m_uBytesRead += u; } AddBytesWritten(unsigned long long u)271 void AddBytesWritten(unsigned long long u) { m_uBytesWritten += u; } 272 273 CString ExpandString(const CString& sStr) const; 274 CString& ExpandString(const CString& sStr, CString& sRet) const; 275 276 private: 277 bool JoinChan(CChan* pChan); 278 bool LoadModule(const CString& sModName, const CString& sArgs, 279 const CString& sNotice, CString& sError); 280 281 protected: 282 CString m_sName; 283 CUser* m_pUser; 284 285 CString m_sNick; 286 CString m_sAltNick; 287 CString m_sIdent; 288 CString m_sRealName; 289 CString m_sBindHost; 290 CString m_sEncoding; 291 CString m_sQuitMsg; 292 SCString m_ssTrustedFingerprints; 293 294 CModules* m_pModules; 295 296 std::vector<CClient*> m_vClients; 297 298 CIRCSock* m_pIRCSock; 299 300 std::vector<CChan*> m_vChans; 301 std::vector<CQuery*> m_vQueries; 302 303 CString m_sChanPrefixes; 304 305 bool m_bIRCConnectEnabled; 306 bool m_bTrustAllCerts; 307 bool m_bTrustPKI; 308 CString m_sIRCServer; 309 std::vector<CServer*> m_vServers; 310 size_t m_uServerIdx; ///< Index in m_vServers of our current server + 1 311 312 CNick m_IRCNick; 313 bool m_bIRCAway; 314 315 double m_fFloodRate; ///< Set to -1 to disable protection. 316 unsigned short int m_uFloodBurst; 317 318 CBuffer m_RawBuffer; 319 CBuffer m_MotdBuffer; 320 CBuffer m_NoticeBuffer; 321 322 CIRCNetworkPingTimer* m_pPingTimer; 323 CIRCNetworkJoinTimer* m_pJoinTimer; 324 325 unsigned short int m_uJoinDelay; 326 unsigned long long m_uBytesRead; 327 unsigned long long m_uBytesWritten; 328 }; 329 330 #endif // !ZNC_IRCNETWORK_H 331