ZNC  trunk
IRCSock.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2021 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
50  } EChanModeArgs;
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
62 
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 HasAccountTag() const { return m_bAccountTag; }
154  bool HasExtendedJoin() const { return m_bExtendedJoin; }
155  bool HasServerTime() const { return m_bServerTime; }
156  const std::set<char>& GetUserModes() const {
157  return m_scUserModes;
158  }
159  // This is true if we are past raw 001
160  bool IsAuthed() const { return m_bAuthed; }
161  const SCString& GetAcceptedCaps() const { return m_ssAcceptedCaps; }
162  bool IsCapAccepted(const CString& sCap) {
163  return 1 == m_ssAcceptedCaps.count(sCap);
164  }
165  const MCString& GetISupport() const { return m_mISupport; }
166  CString GetISupport(const CString& sKey,
167  const CString& sDefault = "") const;
168  // !Getters
169 
170  // TODO move this function to CIRCNetwork and make it non-static?
171  static bool IsFloodProtected(double fRate);
172 
173  private:
174  // Message Handlers
175  bool OnAccountMessage(CMessage& Message);
176  bool OnActionMessage(CActionMessage& Message);
177  bool OnAwayMessage(CMessage& Message);
178  bool OnCapabilityMessage(CMessage& Message);
179  bool OnCTCPMessage(CCTCPMessage& Message);
180  bool OnErrorMessage(CMessage& Message);
181  bool OnInviteMessage(CMessage& Message);
182  bool OnJoinMessage(CJoinMessage& Message);
183  bool OnKickMessage(CKickMessage& Message);
184  bool OnModeMessage(CModeMessage& Message);
185  bool OnNickMessage(CNickMessage& Message);
186  bool OnNoticeMessage(CNoticeMessage& Message);
187  bool OnNumericMessage(CNumericMessage& Message);
188  bool OnPartMessage(CPartMessage& Message);
189  bool OnPingMessage(CMessage& Message);
190  bool OnPongMessage(CMessage& Message);
191  bool OnQuitMessage(CQuitMessage& Message);
192  bool OnTextMessage(CTextMessage& Message);
193  bool OnTopicMessage(CTopicMessage& Message);
194  bool OnWallopsMessage(CMessage& Message);
195  bool OnServerCapAvailable(const CString& sCap);
196  // !Message Handlers
197 
198  void SetNick(const CString& sNick);
199  void ParseISupport(const CMessage& Message);
200  // This is called when we connect and the nick we want is already taken
201  void SendAltNick(const CString& sBadNick);
202  void SendNextCap();
203  void TrySend();
204 
205  protected:
206  bool m_bAuthed;
207  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;
226  time_t m_lastCTCP;
227  unsigned int m_uNumCTCP;
228  static const time_t m_uCTCPFloodTime;
229  static const unsigned int m_uCTCPFloodCount;
231  std::deque<CMessage> m_vSendQueue;
232  short int m_iSendsAllowed;
233  unsigned short int m_uFloodBurst;
234  double m_fFloodRate;
238 
239  friend class CIRCFloodTimer;
240 };
241 
242 #endif // !ZNC_IRCSOCK_H
CIRCSock::m_Nick
CNick m_Nick
Definition: IRCSock.h:219
CIRCSock::m_uCapPaused
unsigned int m_uCapPaused
Definition: IRCSock.h:223
CIRCSock::m_sPass
CString m_sPass
Definition: IRCSock.h:220
CIRCSock::SSLCertError
void SSLCertError(X509 *pCert) override
CIRCSock::m_bExtendedJoin
bool m_bExtendedJoin
Definition: IRCSock.h:212
CIRCSock::SockError
void SockError(int iErrno, const CString &sDescription) override
CIRCSock::m_uFloodBurst
unsigned short int m_uFloodBurst
Definition: IRCSock.h:233
CIRCSock::m_msChans
std::map< CString, CChan * > m_msChans
Definition: IRCSock.h:221
CIRCSock::GetISupport
const MCString & GetISupport() const
Definition: IRCSock.h:165
Nick.h
CIRCSock::m_uCTCPFloodCount
static const unsigned int m_uCTCPFloodCount
Definition: IRCSock.h:229
CClient
Definition: Client.h:99
CIRCSock::NoArg
@ NoArg
Definition: IRCSock.h:49
CIRCSock::GetNick
const CString & GetNick() const
Definition: IRCSock.h:146
CIRCSock::m_bAwayNotify
bool m_bAwayNotify
Definition: IRCSock.h:209
CIRCSock::IsAuthed
bool IsAuthed() const
Definition: IRCSock.h:160
CIRCSock::HasUHNames
bool HasUHNames() const
Definition: IRCSock.h:150
CIRCSock::HasExtendedJoin
bool HasExtendedJoin() const
Definition: IRCSock.h:154
CTopicMessage
Definition: Message.h:340
CIRCSock::HasAccountNotify
bool HasAccountNotify() const
Definition: IRCSock.h:152
CIRCSock::m_bAuthed
bool m_bAuthed
Definition: IRCSock.h:206
CIRCSock::m_lastCTCP
time_t m_lastCTCP
Definition: IRCSock.h:226
CIRCSock::Disconnected
void Disconnected() override
Disconnected event.
MCString
A dictionary for strings.
Definition: ZNCString.h:595
CActionMessage
Definition: Message.h:242
CQuitMessage
Definition: Message.h:324
CIRCSock::IsPermMode
bool IsPermMode(const char c) const
Definition: IRCSock.h:140
CIRCSock::m_mceChanModes
std::map< char, EChanModeArgs > m_mceChanModes
Definition: IRCSock.h:217
CIRCSock::IsPermChar
bool IsPermChar(const char c) const
Definition: IRCSock.h:137
CIRCSock::GetUserModes
const std::set< char > & GetUserModes() const
Definition: IRCSock.h:156
CIRCSock::m_sPermModes
CString m_sPermModes
Definition: IRCSock.h:215
CIRCSock::EChanModeArgs
EChanModeArgs
Definition: IRCSock.h:43
CIRCSock::GetNickMask
CString GetNickMask() const
Definition: IRCSock.h:145
CCTCPMessage
Definition: Message.h:253
CIRCSock::m_fFloodRate
double m_fFloodRate
Definition: IRCSock.h:234
SCString
std::set< CString > SCString
Definition: ZNCString.h:35
CIRCSock::ArgWhenSet
@ ArgWhenSet
Definition: IRCSock.h:48
CIRCSock::m_sPerms
CString m_sPerms
Definition: IRCSock.h:214
CIRCSock::Timeout
void Timeout() override
Sock Timed out event.
CIRCSocket
Base IRC socket for client<->ZNC, and ZNC<->server.
Definition: Socket.h:309
CIRCSock::m_bNamesx
bool m_bNamesx
Definition: IRCSock.h:207
CIRCSock::operator=
CIRCSock & operator=(const CIRCSock &)=delete
CMessage
Here is a small explanation of how messages on IRC work, and how you can use this class to get useful...
Definition: Message.h:70
CIRCSock::PutIRCRaw
void PutIRCRaw(const CString &sLine)
Sends a raw data line to the server.
CString
String class that is used inside ZNC.
Definition: ZNCString.h:68
CNickMessage
Definition: Message.h:283
CIRCSock::m_ssSupportedTags
SCString m_ssSupportedTags
Definition: IRCSock.h:236
CIRCSock::ConnectionRefused
void ConnectionRefused() override
Connection Refused Event.
CIRCSock::ListArg
@ ListArg
Definition: IRCSock.h:46
CIRCSock::HasArg
@ HasArg
Definition: IRCSock.h:47
CIRCSock::GetPerms
const CString & GetPerms() const
Definition: IRCSock.h:143
CIRCSock::CIRCFloodTimer
friend class CIRCFloodTimer
Definition: IRCSock.h:239
CIRCSock::Connected
void Connected() override
Connected event.
CIRCSock::PutIRC
void PutIRC(const CString &sLine)
Sends a message to the server.
CTextMessage
Definition: Message.h:333
Socket.h
CIRCSock::GetPermModes
const CString & GetPermModes() const
Definition: IRCSock.h:144
CIRCSock::GetNetwork
CIRCNetwork * GetNetwork() const
Definition: IRCSock.h:148
CChan
Definition: Chan.h:35
CIRCSock::ReachedMaxBuffer
void ReachedMaxBuffer() override
This WARNING event is called when your buffer for readline exceeds the warning threshold and triggers...
CIRCSock
Definition: IRCSock.h:35
CIRCSock::m_bFloodProtection
bool m_bFloodProtection
Definition: IRCSock.h:235
CIRCSock::SetTagSupport
void SetTagSupport(const CString &sTag, bool bState)
Registers a tag as being supported or unsupported by the server.
CIRCSock::HasNamesx
bool HasNamesx() const
Definition: IRCSock.h:149
CJoinMessage
Definition: Message.h:263
CIRCSock::CIRCSock
CIRCSock(CIRCNetwork *pNetwork)
CIRCSock::IsTagEnabled
bool IsTagEnabled(const CString &sTag) const
Definition: IRCSock.h:116
CIRCSock::HasServerTime
bool HasServerTime() const
Definition: IRCSock.h:155
CIRCSock::GetAcceptedCaps
const SCString & GetAcceptedCaps() const
Definition: IRCSock.h:161
CIRCSock::m_ssAcceptedCaps
SCString m_ssAcceptedCaps
Definition: IRCSock.h:224
CIRCSock::HasAwayNotify
bool HasAwayNotify() const
Definition: IRCSock.h:151
CIRCSock::GetChanModes
const std::map< char, EChanModeArgs > & GetChanModes() const
Definition: IRCSock.h:134
CIRCSock::m_bAccountTag
bool m_bAccountTag
Definition: IRCSock.h:211
CIRCSock::ResumeCap
void ResumeCap()
If you used PauseCap, call this when CAP negotiation and logging in should be resumed again.
CIRCSock::m_mISupport
MCString m_mISupport
Definition: IRCSock.h:230
CIRCSock::IsCapAccepted
bool IsCapAccepted(const CString &sCap)
Definition: IRCSock.h:162
CKickMessage
Definition: Message.h:304
CIRCSock::HasAccountTag
bool HasAccountTag() const
Definition: IRCSock.h:153
CIRCSock::Quit
void Quit(const CString &sQuitMsg="")
CIRCSock::GetPermFromMode
char GetPermFromMode(char cMode) const
CIRCSock::PauseCap
void PauseCap()
You can call this from CModule::OnServerCapResult to suspend sending other CAP requests and CAP END f...
CIRCSock::m_bAccountNotify
bool m_bAccountNotify
Definition: IRCSock.h:210
CModeMessage
Definition: Message.h:270
CIRCSock::m_iSendsAllowed
short int m_iSendsAllowed
Definition: IRCSock.h:232
CIRCSock::m_bServerTime
bool m_bServerTime
Definition: IRCSock.h:213
CIRCSock::m_uNumCTCP
unsigned int m_uNumCTCP
Definition: IRCSock.h:227
CUser
Definition: User.h:38
CIRCSock::m_uCTCPFloodTime
static const time_t m_uCTCPFloodTime
Definition: IRCSock.h:228
CNick::GetNickMask
CString GetNickMask() const
CIRCSock::IsFloodProtected
static bool IsFloodProtected(double fRate)
CIRCSock::SetPass
void SetPass(const CString &s)
Definition: IRCSock.h:127
CIRCSock::m_pNetwork
CIRCNetwork * m_pNetwork
Definition: IRCSock.h:218
CIRCSock::m_vSendQueue
std::deque< CMessage > m_vSendQueue
Definition: IRCSock.h:231
CIRCNetwork
Definition: IRCNetwork.h:40
CIRCSock::GetPass
const CString & GetPass() const
Definition: IRCSock.h:147
CIRCSock::m_scUserModes
std::set< char > m_scUserModes
Definition: IRCSock.h:216
CIRCSock::~CIRCSock
virtual ~CIRCSock()
VCString
std::vector< CString > VCString
Definition: ZNCString.h:38
CIRCSock::ReadLine
void ReadLine(const CString &sData) override
CNick
Definition: Nick.h:29
CIRCSock::m_ssPendingCaps
SCString m_ssPendingCaps
Definition: IRCSock.h:225
CIRCSock::GetModeType
EChanModeArgs GetModeType(char cMode) const
CIRCSock::PutIRCQuick
void PutIRCQuick(const CString &sLine)
Should be used for PONG only.
CIRCSock::GetMaxNickLen
unsigned int GetMaxNickLen() const
Definition: IRCSock.h:131
CNoticeMessage
Definition: Message.h:291
CIRCSock::ResetChans
void ResetChans()
CNumericMessage
Definition: Message.h:298
CIRCSock::m_vsSSLError
VCString m_vsSSLError
Definition: IRCSock.h:237
CIRCSock::m_bUHNames
bool m_bUHNames
Definition: IRCSock.h:208
CPartMessage
Definition: Message.h:315
CNick::GetNick
const CString & GetNick() const
CIRCSock::m_uMaxNickLen
unsigned int m_uMaxNickLen
Definition: IRCSock.h:222
Message.h