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
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 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  }
164  CString GetCapLsValue(const CString& sKey,
165  const CString& sDefault = "") const;
166  const MCString& GetISupport() const { return m_mISupport; }
167  CString GetISupport(const CString& sKey,
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
Definition: Message.h:285
bool IsCapAccepted(const CString &sCap)
Definition: IRCSock.h:161
Definition: Message.h:327
Definition: Message.h:257
Definition: User.h:38
void ResumeCap()
If you used PauseCap, call this when CAP negotiation and logging in should be resumed again...
const MCString & GetISupport() const
Definition: IRCSock.h:166
void ConnectionRefused() override
Connection Refused Event.
CIRCNetwork * GetNetwork() const
Definition: IRCSock.h:148
CString m_sPass
Definition: IRCSock.h:220
bool HasAwayNotify() const
Definition: IRCSock.h:151
void SockError(int iErrno, const CString &sDescription) override
bool m_bExtendedJoin
Definition: IRCSock.h:212
CString m_sPerms
Definition: IRCSock.h:214
const CString & GetNick() const
MCString m_mISupport
Definition: IRCSock.h:231
bool IsPermMode(const char c) const
Definition: IRCSock.h:140
VCString m_vsSSLError
Definition: IRCSock.h:238
const std::map< char, EChanModeArgs > & GetChanModes() const
Definition: IRCSock.h:134
CString GetNickMask() const
Definition: IRCSock.h:145
void PauseCap()
You can call this from CModule::OnServerCapResult to suspend sending other CAP requests and CAP END f...
void Quit(const CString &sQuitMsg="")
bool IsAuthed() const
Definition: IRCSock.h:159
short int m_iSendsAllowed
Definition: IRCSock.h:233
bool HasAccountNotify() const
Definition: IRCSock.h:152
Definition: Client.h:99
bool m_bServerTime
Definition: IRCSock.h:213
Base IRC socket for client<->ZNC, and ZNC<->server.
Definition: Socket.h:309
bool HasUHNames() const
Definition: IRCSock.h:150
std::set< char > m_scUserModes
Definition: IRCSock.h:216
std::deque< CMessage > m_vSendQueue
Definition: IRCSock.h:232
Definition: Message.h:302
Definition: Message.h:278
EChanModeArgs GetModeType(char cMode) const
Definition: IRCSock.h:46
const std::set< char > & GetUserModes() const
Definition: IRCSock.h:155
void PutIRCRaw(const CString &sLine)
Sends a raw data line to the server.
unsigned short int m_uFloodBurst
Definition: IRCSock.h:234
char GetPermFromMode(char cMode) const
friend class CCoreCaps
Definition: IRCSock.h:241
Definition: Nick.h:29
bool m_bAwayNotify
Definition: IRCSock.h:210
const CString & GetPass() const
Definition: IRCSock.h:147
CIRCSock(CIRCNetwork *pNetwork)
std::set< CString > SCString
Definition: ZNCString.h:35
Definition: IRCSock.h:49
const CString & GetPermModes() const
Definition: IRCSock.h:144
virtual ~CIRCSock()
Definition: Message.h:240
CNick m_Nick
Definition: IRCSock.h:219
Definition: Message.h:311
Definition: Message.h:229
Definition: IRCNetwork.h:40
std::vector< CString > VCString
Definition: ZNCString.h:38
void PutIRC(const CString &sLine)
Sends a message to the server.
const CString & GetPerms() const
Definition: IRCSock.h:143
void ResetChans()
void Connected() override
Connected event.
String class that is used inside ZNC.
Definition: ZNCString.h:68
SCString m_ssAcceptedCaps
Definition: IRCSock.h:224
void SSLCertError(X509 *pCert) override
static const unsigned int m_uCTCPFloodCount
Definition: IRCSock.h:230
unsigned int m_uMaxNickLen
Definition: IRCSock.h:222
Definition: IRCSock.h:48
bool HasNamesx() const
Definition: IRCSock.h:149
bool HasServerTime() const
Definition: IRCSock.h:154
static const time_t m_uCTCPFloodTime
Definition: IRCSock.h:229
std::map< CString, CChan * > m_msChans
Definition: IRCSock.h:221
std::map< char, EChanModeArgs > m_mceChanModes
Definition: IRCSock.h:217
CString GetCapLsValue(const CString &sKey, const CString &sDefault="") const
bool HasExtendedJoin() const
Definition: IRCSock.h:153
CString m_sPermModes
Definition: IRCSock.h:215
bool m_bAuthed
Definition: IRCSock.h:207
Definition: IRCSock.h:47
static bool IsFloodProtected(double fRate)
time_t m_lastCTCP
Definition: IRCSock.h:227
void SetPass(const CString &s)
Definition: IRCSock.h:127
void Disconnected() override
Disconnected event.
CIRCNetwork * m_pNetwork
Definition: IRCSock.h:218
Definition: Message.h:320
A dictionary for strings.
Definition: ZNCString.h:595
MCString m_msCapLsValues
Definition: IRCSock.h:226
bool IsTagEnabled(const CString &sTag) const
Definition: IRCSock.h:116
Definition: Message.h:270
SCString m_ssSupportedTags
Definition: IRCSock.h:237
const CString & GetNick() const
Definition: IRCSock.h:146
double m_fFloodRate
Definition: IRCSock.h:235
bool m_bAccountNotify
Definition: IRCSock.h:211
CString GetNickMask() const
bool IsPermChar(const char c) const
Definition: IRCSock.h:137
void Timeout() override
Sock Timed out event.
unsigned int m_uNumCTCP
Definition: IRCSock.h:228
unsigned int GetMaxNickLen() const
Definition: IRCSock.h:131
EChanModeArgs
Definition: IRCSock.h:43
void SetTagSupport(const CString &sTag, bool bState)
Registers a tag as being supported or unsupported by the server.
friend class CIRCFloodTimer
Definition: IRCSock.h:240
Definition: Message.h:250
SCString m_ssPendingCaps
Definition: IRCSock.h:225
bool m_bNamesx
Definition: IRCSock.h:208
unsigned int m_uCapPaused
Definition: IRCSock.h:223
bool m_bFloodProtection
Definition: IRCSock.h:236
bool m_bUHNames
Definition: IRCSock.h:209
Definition: IRCSock.h:35
Definition: Chan.h:35
Definition: Message.h:291
const SCString & GetAcceptedCaps() const
Definition: IRCSock.h:160
void PutIRCQuick(const CString &sLine)
Should be used for PONG only.
void ReachedMaxBuffer() override
This WARNING event is called when your buffer for readline exceeds the warning threshold and triggers...
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
CIRCSock & operator=(const CIRCSock &)=delete
void ReadLine(const CString &sData) override