ZNC  trunk
IRCSock.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_IRCSOCK_H
18 #define ZNC_IRCSOCK_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Socket.h>
22 #include <znc/Nick.h>
23 #include <znc/Message.h>
24 
25 #include <deque>
26 
27 // Forward Declarations
28 class CChan;
29 class CUser;
30 class CIRCNetwork;
31 class CClient;
32 // !Forward Declarations
33 
34 // TODO: This class needs new name
35 class CIRCSock : public CIRCSocket {
36  public:
37  CIRCSock(CIRCNetwork* pNetwork);
38  virtual ~CIRCSock();
39 
40  CIRCSock(const CIRCSock&) = delete;
41  CIRCSock& operator=(const CIRCSock&) = delete;
42 
43  typedef enum {
44  // These values must line up with their position in the CHANMODE
45  // argument to raw 005
46  ListArg = 0,
47  HasArg = 1,
49  NoArg = 3
51 
52  void ReadLine(const CString& sData) override;
53  void Connected() override;
54  void Disconnected() override;
55  void ConnectionRefused() override;
56  void SockError(int iErrno, const CString& sDescription) override;
57  void Timeout() override;
58  void ReachedMaxBuffer() override;
59 #ifdef HAVE_LIBSSL
60  void SSLCertError(X509* pCert) override;
61 #endif
71  void PutIRCRaw(const CString& sLine);
75  void PutIRC(const CString& sLine);
101  void PutIRC(const CMessage& Message);
102  void PutIRCQuick(const CString& sLine);
103  void ResetChans();
104  void Quit(const CString& sQuitMsg = "");
105 
110  void PauseCap();
114  void ResumeCap();
115 
116  bool IsTagEnabled(const CString& sTag) const {
117  return 1 == m_ssSupportedTags.count(sTag);
118  }
124  void SetTagSupport(const CString& sTag, bool bState);
125 
126  // Setters
127  void SetPass(const CString& s) { m_sPass = s; }
128  // !Setters
129 
130  // Getters
131  unsigned int GetMaxNickLen() const { return m_uMaxNickLen; }
132  EChanModeArgs GetModeType(char cMode) const;
133  char GetPermFromMode(char cMode) const;
134  const std::map<char, EChanModeArgs>& GetChanModes() const {
135  return m_mceChanModes;
136  }
137  bool IsPermChar(const char c) const {
138  return (c != '\0' && GetPerms().find(c) != CString::npos);
139  }
140  bool IsPermMode(const char c) const {
141  return (c != '\0' && GetPermModes().find(c) != CString::npos);
142  }
143  const CString& GetPerms() const { return m_sPerms; }
144  const CString& GetPermModes() const { return m_sPermModes; }
145  CString GetNickMask() const { return m_Nick.GetNickMask(); }
146  const CString& GetNick() const { return m_Nick.GetNick(); }
147  const CString& GetPass() const { return m_sPass; }
148  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
149  bool HasNamesx() const { return m_bNamesx; }
150  bool HasUHNames() const { return m_bUHNames; }
151  bool HasAwayNotify() const { return m_bAwayNotify; }
152  bool HasAccountNotify() const { return m_bAccountNotify; }
153  bool HasExtendedJoin() const { return m_bExtendedJoin; }
154  bool HasServerTime() const { return m_bServerTime; }
155  const std::set<char>& GetUserModes() const {
156  return m_scUserModes;
157  }
158  // This is true if we are past raw 001
159  bool IsAuthed() const { return m_bAuthed; }
160  const SCString& GetAcceptedCaps() const { return m_ssAcceptedCaps; }
161  bool IsCapAccepted(const CString& sCap) {
162  return 1 == m_ssAcceptedCaps.count(sCap);
163  }
165  const CString& sDefault = "") const;
166  const MCString& GetISupport() const { return m_mISupport; }
168  const CString& sDefault = "") const;
169  // !Getters
170 
171  // TODO move this function to CIRCNetwork and make it non-static?
172  static bool IsFloodProtected(double fRate);
173 
174  private:
175  // Message Handlers
176  bool OnAccountMessage(CMessage& Message);
177  bool OnActionMessage(CActionMessage& Message);
178  bool OnAwayMessage(CMessage& Message);
179  bool OnCapabilityMessage(CMessage& Message);
180  bool OnCTCPMessage(CCTCPMessage& Message);
181  bool OnErrorMessage(CMessage& Message);
182  bool OnInviteMessage(CMessage& Message);
183  bool OnJoinMessage(CJoinMessage& Message);
184  bool OnKickMessage(CKickMessage& Message);
185  bool OnModeMessage(CModeMessage& Message);
186  bool OnNickMessage(CNickMessage& Message);
187  bool OnNoticeMessage(CNoticeMessage& Message);
188  bool OnNumericMessage(CNumericMessage& Message);
189  bool OnPartMessage(CPartMessage& Message);
190  bool OnPingMessage(CMessage& Message);
191  bool OnPongMessage(CMessage& Message);
192  bool OnQuitMessage(CQuitMessage& Message);
193  bool OnTextMessage(CTextMessage& Message);
194  bool OnTopicMessage(CTopicMessage& Message);
195  bool OnWallopsMessage(CMessage& Message);
196  bool OnServerCapAvailable(const CString& sCap, const CString& sValue);
197  // !Message Handlers
198 
199  void SetNick(const CString& sNick);
200  void ParseISupport(const CMessage& Message);
201  // This is called when we connect and the nick we want is already taken
202  void SendAltNick(const CString& sBadNick);
203  void SendNextCap();
204  void TrySend();
205 
206  protected:
207  bool m_bAuthed;
208  bool m_bNamesx;
216  std::set<char> m_scUserModes;
217  std::map<char, EChanModeArgs> m_mceChanModes;
221  std::map<CString, CChan*> m_msChans;
222  unsigned int m_uMaxNickLen;
223  unsigned int m_uCapPaused;
227  time_t m_lastCTCP;
228  unsigned int m_uNumCTCP;
229  static const time_t m_uCTCPFloodTime;
230  static const unsigned int m_uCTCPFloodCount;
232  std::deque<CMessage> m_vSendQueue;
233  short int m_iSendsAllowed;
234  unsigned short int m_uFloodBurst;
235  double m_fFloodRate;
239 
240  friend class CIRCFloodTimer;
241  friend class CCoreCaps;
242 };
243 
244 #endif // !ZNC_IRCSOCK_H
std::set< CString > SCString
Definition: ZNCString.h:35
std::vector< CString > VCString
Definition: ZNCString.h:38
Definition: Message.h:229
Definition: Message.h:240
Definition: Chan.h:35
Definition: Client.h:99
Definition: IRCNetwork.h:40
Definition: IRCSock.h:35
VCString m_vsSSLError
Definition: IRCSock.h:238
void ReachedMaxBuffer() override
This WARNING event is called when your buffer for readline exceeds the warning threshold and triggers...
CString m_sPermModes
Definition: IRCSock.h:215
const MCString & GetISupport() const
Definition: IRCSock.h:166
SCString m_ssPendingCaps
Definition: IRCSock.h:225
CIRCSock & operator=(const CIRCSock &)=delete
std::set< char > m_scUserModes
Definition: IRCSock.h:216
CIRCSock(const CIRCSock &)=delete
void PutIRCRaw(const CString &sLine)
Sends a raw data line to the server.
bool m_bExtendedJoin
Definition: IRCSock.h:212
EChanModeArgs GetModeType(char cMode) const
CString m_sPerms
Definition: IRCSock.h:214
static const time_t m_uCTCPFloodTime
Definition: IRCSock.h:229
void SetTagSupport(const CString &sTag, bool bState)
Registers a tag as being supported or unsupported by the server.
void SetPass(const CString &s)
Definition: IRCSock.h:127
bool IsPermMode(const char c) const
Definition: IRCSock.h:140
CIRCNetwork * GetNetwork() const
Definition: IRCSock.h:148
short int m_iSendsAllowed
Definition: IRCSock.h:233
bool m_bUHNames
Definition: IRCSock.h:209
const CString & GetPerms() const
Definition: IRCSock.h:143
bool IsAuthed() const
Definition: IRCSock.h:159
bool IsCapAccepted(const CString &sCap)
Definition: IRCSock.h:161
char GetPermFromMode(char cMode) const
bool m_bAuthed
Definition: IRCSock.h:207
void Quit(const CString &sQuitMsg="")
CNick m_Nick
Definition: IRCSock.h:219
const SCString & GetAcceptedCaps() const
Definition: IRCSock.h:160
void SSLCertError(X509 *pCert) override
bool IsTagEnabled(const CString &sTag) const
Definition: IRCSock.h:116
void PutIRCQuick(const CString &sLine)
Should be used for PONG only.
void Timeout() override
Sock Timed out event.
unsigned int m_uNumCTCP
Definition: IRCSock.h:228
const CString & GetNick() const
Definition: IRCSock.h:146
bool m_bNamesx
Definition: IRCSock.h:208
unsigned int m_uMaxNickLen
Definition: IRCSock.h:222
CIRCNetwork * m_pNetwork
Definition: IRCSock.h:218
void Connected() override
Connected event.
void ResetChans()
bool m_bFloodProtection
Definition: IRCSock.h:236
MCString m_msCapLsValues
Definition: IRCSock.h:226
void PauseCap()
You can call this from CModule::OnServerCapResult to suspend sending other CAP requests and CAP END f...
time_t m_lastCTCP
Definition: IRCSock.h:227
bool m_bServerTime
Definition: IRCSock.h:213
SCString m_ssAcceptedCaps
Definition: IRCSock.h:224
static const unsigned int m_uCTCPFloodCount
Definition: IRCSock.h:230
CString GetCapLsValue(const CString &sKey, const CString &sDefault="") const
EChanModeArgs
Definition: IRCSock.h:43
@ HasArg
Definition: IRCSock.h:47
@ ListArg
Definition: IRCSock.h:46
@ ArgWhenSet
Definition: IRCSock.h:48
@ NoArg
Definition: IRCSock.h:49
void ReadLine(const CString &sData) override
void SockError(int iErrno, const CString &sDescription) override
bool HasAccountNotify() const
Definition: IRCSock.h:152
SCString m_ssSupportedTags
Definition: IRCSock.h:237
CIRCSock(CIRCNetwork *pNetwork)
bool m_bAccountNotify
Definition: IRCSock.h:211
std::map< char, EChanModeArgs > m_mceChanModes
Definition: IRCSock.h:217
friend class CCoreCaps
Definition: IRCSock.h:241
CString GetNickMask() const
Definition: IRCSock.h:145
const CString & GetPass() const
Definition: IRCSock.h:147
bool HasExtendedJoin() const
Definition: IRCSock.h:153
unsigned int GetMaxNickLen() const
Definition: IRCSock.h:131
friend class CIRCFloodTimer
Definition: IRCSock.h:240
CString GetISupport(const CString &sKey, const CString &sDefault="") const
bool IsPermChar(const char c) const
Definition: IRCSock.h:137
MCString m_mISupport
Definition: IRCSock.h:231
void ResumeCap()
If you used PauseCap, call this when CAP negotiation and logging in should be resumed again.
const std::map< char, EChanModeArgs > & GetChanModes() const
Definition: IRCSock.h:134
bool HasUHNames() const
Definition: IRCSock.h:150
bool HasServerTime() const
Definition: IRCSock.h:154
std::deque< CMessage > m_vSendQueue
Definition: IRCSock.h:232
unsigned int m_uCapPaused
Definition: IRCSock.h:223
std::map< CString, CChan * > m_msChans
Definition: IRCSock.h:221
double m_fFloodRate
Definition: IRCSock.h:235
bool HasNamesx() const
Definition: IRCSock.h:149
void Disconnected() override
Disconnected event.
void PutIRC(const CMessage &Message)
Sends a message to the server.
virtual ~CIRCSock()
static bool IsFloodProtected(double fRate)
bool HasAwayNotify() const
Definition: IRCSock.h:151
unsigned short int m_uFloodBurst
Definition: IRCSock.h:234
void ConnectionRefused() override
Connection Refused Event.
const std::set< char > & GetUserModes() const
Definition: IRCSock.h:155
const CString & GetPermModes() const
Definition: IRCSock.h:144
void PutIRC(const CString &sLine)
Sends a message to the server.
bool m_bAwayNotify
Definition: IRCSock.h:210
CString m_sPass
Definition: IRCSock.h:220
Base IRC socket for client<->ZNC, and ZNC<->server.
Definition: Socket.h:309
Definition: Message.h:250
Definition: Message.h:291
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: Message.h:257
Definition: Message.h:270
Definition: Nick.h:29
const CString & GetNick() const
CString GetNickMask() const
Definition: Message.h:278
Definition: Message.h:285
Definition: Message.h:302
Definition: Message.h:311
String class that is used inside ZNC.
Definition: ZNCString.h:68
Definition: Message.h:320
Definition: Message.h:327
Definition: User.h:38
A dictionary for strings.
Definition: ZNCString.h:595