ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IRCSock.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_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;
68  void PutIRCRaw(const CString& sLine);
72  void PutIRC(const CString& sLine);
98  void PutIRC(const CMessage& Message);
99  void PutIRCQuick(const CString& sLine);
100  void ResetChans();
101  void Quit(const CString& sQuitMsg = "");
102 
107  void PauseCap();
111  void ResumeCap();
112 
113  bool IsTagEnabled(const CString& sTag) const {
114  return 1 == m_ssSupportedTags.count(sTag);
115  }
121  void SetTagSupport(const CString& sTag, bool bState);
122 
123  // Setters
124  void SetPass(const CString& s) { m_sPass = s; }
125  // !Setters
126 
127  // Getters
128  unsigned int GetMaxNickLen() const { return m_uMaxNickLen; }
129  EChanModeArgs GetModeType(char cMode) const;
130  char GetPermFromMode(char cMode) const;
131  const std::map<char, EChanModeArgs>& GetChanModes() const {
132  return m_mceChanModes;
133  }
134  bool IsPermChar(const char c) const {
135  return (c != '\0' && GetPerms().find(c) != CString::npos);
136  }
137  bool IsPermMode(const char c) const {
138  return (c != '\0' && GetPermModes().find(c) != CString::npos);
139  }
140  const CString& GetPerms() const { return m_sPerms; }
141  const CString& GetPermModes() const { return m_sPermModes; }
142  CString GetNickMask() const { return m_Nick.GetNickMask(); }
143  const CString& GetNick() const { return m_Nick.GetNick(); }
144  const CString& GetPass() const { return m_sPass; }
145  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
146  bool HasNamesx() const { return m_bNamesx; }
147  bool HasUHNames() const { return m_bUHNames; }
148  bool HasAwayNotify() const { return m_bAwayNotify; }
149  bool HasAccountNotify() const { return m_bAccountNotify; }
150  bool HasExtendedJoin() const { return m_bExtendedJoin; }
151  bool HasServerTime() const { return m_bServerTime; }
152  const std::set<char>& GetUserModes() const {
153  return m_scUserModes;
154  }
155  // This is true if we are past raw 001
156  bool IsAuthed() const { return m_bAuthed; }
157  const SCString& GetAcceptedCaps() const { return m_ssAcceptedCaps; }
158  bool IsCapAccepted(const CString& sCap) {
159  return 1 == m_ssAcceptedCaps.count(sCap);
160  }
161  const MCString& GetISupport() const { return m_mISupport; }
162  CString GetISupport(const CString& sKey,
163  const CString& sDefault = "") const;
164  // !Getters
165 
166  // TODO move this function to CIRCNetwork and make it non-static?
167  static bool IsFloodProtected(double fRate);
168 
169  private:
170  // Message Handlers
171  bool OnAccountMessage(CMessage& Message);
172  bool OnActionMessage(CActionMessage& Message);
173  bool OnAwayMessage(CMessage& Message);
174  bool OnCapabilityMessage(CMessage& Message);
175  bool OnCTCPMessage(CCTCPMessage& Message);
176  bool OnErrorMessage(CMessage& Message);
177  bool OnInviteMessage(CMessage& Message);
178  bool OnJoinMessage(CJoinMessage& Message);
179  bool OnKickMessage(CKickMessage& Message);
180  bool OnModeMessage(CModeMessage& Message);
181  bool OnNickMessage(CNickMessage& Message);
182  bool OnNoticeMessage(CNoticeMessage& Message);
183  bool OnNumericMessage(CNumericMessage& Message);
184  bool OnPartMessage(CPartMessage& Message);
185  bool OnPingMessage(CMessage& Message);
186  bool OnPongMessage(CMessage& Message);
187  bool OnQuitMessage(CQuitMessage& Message);
188  bool OnTextMessage(CTextMessage& Message);
189  bool OnTopicMessage(CTopicMessage& Message);
190  bool OnWallopsMessage(CMessage& Message);
191  bool OnServerCapAvailable(const CString& sCap);
192  // !Message Handlers
193 
194  void SetNick(const CString& sNick);
195  void ParseISupport(const CMessage& Message);
196  // This is called when we connect and the nick we want is already taken
197  void SendAltNick(const CString& sBadNick);
198  void SendNextCap();
199  void TrySend();
200 
201  protected:
202  bool m_bAuthed;
203  bool m_bNamesx;
211  std::set<char> m_scUserModes;
212  std::map<char, EChanModeArgs> m_mceChanModes;
216  std::map<CString, CChan*> m_msChans;
217  unsigned int m_uMaxNickLen;
218  unsigned int m_uCapPaused;
221  time_t m_lastCTCP;
222  unsigned int m_uNumCTCP;
223  static const time_t m_uCTCPFloodTime;
224  static const unsigned int m_uCTCPFloodCount;
226  std::deque<CMessage> m_vSendQueue;
227  short int m_iSendsAllowed;
228  unsigned short int m_uFloodBurst;
229  double m_fFloodRate;
232 
233  friend class CIRCFloodTimer;
234 };
235 
236 #endif // !ZNC_IRCSOCK_H
Definition: Message.h:266
bool IsCapAccepted(const CString &sCap)
Definition: IRCSock.h:158
Definition: Message.h:308
Definition: Message.h:245
const CString & GetPermModes() const
Definition: IRCSock.h:141
Definition: User.h:38
void ResumeCap()
If you used PauseCap, call this when CAP negotiation and logging in should be resumed again...
CString GetNickMask() const
Definition: IRCSock.h:142
void ConnectionRefused() override
Connection Refused Event.
bool HasNamesx() const
Definition: IRCSock.h:146
CString m_sPass
Definition: IRCSock.h:215
CIRCNetwork * GetNetwork() const
Definition: IRCSock.h:145
void SockError(int iErrno, const CString &sDescription) override
bool m_bExtendedJoin
Definition: IRCSock.h:207
CString m_sPerms
Definition: IRCSock.h:209
const CString & GetNick() const
Definition: IRCSock.h:143
MCString m_mISupport
Definition: IRCSock.h:225
bool IsPermMode(const char c) const
Definition: IRCSock.h:137
void PauseCap()
You can call this from CModule::OnServerCapResult to suspend sending other CAP requests and CAP END f...
void Quit(const CString &sQuitMsg="")
short int m_iSendsAllowed
Definition: IRCSock.h:227
bool IsPermChar(const char c) const
Definition: IRCSock.h:134
Definition: Client.h:99
bool m_bServerTime
Definition: IRCSock.h:208
Base IRC socket for client<->ZNC, and ZNC<->server.
Definition: Socket.h:305
std::set< char > m_scUserModes
Definition: IRCSock.h:211
std::deque< CMessage > m_vSendQueue
Definition: IRCSock.h:226
Definition: Message.h:283
Definition: Message.h:259
const CString & GetNick() const
const SCString & GetAcceptedCaps() const
Definition: IRCSock.h:157
Definition: IRCSock.h:46
void PutIRCRaw(const CString &sLine)
Sends a raw data line to the server.
unsigned short int m_uFloodBurst
Definition: IRCSock.h:228
EChanModeArgs GetModeType(char cMode) const
Definition: Nick.h:29
bool m_bAwayNotify
Definition: IRCSock.h:205
bool HasAwayNotify() const
Definition: IRCSock.h:148
CIRCSock(CIRCNetwork *pNetwork)
std::set< CString > SCString
Definition: ZNCString.h:35
Definition: IRCSock.h:49
CString GetNickMask() const
virtual ~CIRCSock()
Definition: Message.h:228
const std::map< char, EChanModeArgs > & GetChanModes() const
Definition: IRCSock.h:131
CNick m_Nick
Definition: IRCSock.h:214
Definition: Message.h:292
Definition: Message.h:217
Definition: IRCNetwork.h:40
bool HasExtendedJoin() const
Definition: IRCSock.h:150
void PutIRC(const CString &sLine)
Sends a message to the server.
void ResetChans()
void Connected() override
Connected event.
String class that is used inside ZNC.
Definition: ZNCString.h:68
bool HasAccountNotify() const
Definition: IRCSock.h:149
SCString m_ssAcceptedCaps
Definition: IRCSock.h:219
static const unsigned int m_uCTCPFloodCount
Definition: IRCSock.h:224
unsigned int m_uMaxNickLen
Definition: IRCSock.h:217
Definition: IRCSock.h:48
const CString & GetPass() const
Definition: IRCSock.h:144
bool IsAuthed() const
Definition: IRCSock.h:156
static const time_t m_uCTCPFloodTime
Definition: IRCSock.h:223
std::map< CString, CChan * > m_msChans
Definition: IRCSock.h:216
std::map< char, EChanModeArgs > m_mceChanModes
Definition: IRCSock.h:212
CString m_sPermModes
Definition: IRCSock.h:210
bool m_bAuthed
Definition: IRCSock.h:202
Definition: IRCSock.h:47
static bool IsFloodProtected(double fRate)
time_t m_lastCTCP
Definition: IRCSock.h:221
void SetPass(const CString &s)
Definition: IRCSock.h:124
void Disconnected() override
Disconnected event.
CIRCNetwork * m_pNetwork
Definition: IRCSock.h:213
Definition: Message.h:301
bool HasUHNames() const
Definition: IRCSock.h:147
A dictionary for strings.
Definition: ZNCString.h:595
unsigned int GetMaxNickLen() const
Definition: IRCSock.h:128
Definition: Message.h:251
SCString m_ssSupportedTags
Definition: IRCSock.h:231
double m_fFloodRate
Definition: IRCSock.h:229
bool m_bAccountNotify
Definition: IRCSock.h:206
bool HasServerTime() const
Definition: IRCSock.h:151
const MCString & GetISupport() const
Definition: IRCSock.h:161
void Timeout() override
Sock Timed out event.
unsigned int m_uNumCTCP
Definition: IRCSock.h:222
char GetPermFromMode(char cMode) const
EChanModeArgs
Definition: IRCSock.h:43
const CString & GetPerms() const
Definition: IRCSock.h:140
void SetTagSupport(const CString &sTag, bool bState)
Registers a tag as being supported or unsupported by the server.
friend class CIRCFloodTimer
Definition: IRCSock.h:233
Definition: Message.h:238
bool IsTagEnabled(const CString &sTag) const
Definition: IRCSock.h:113
SCString m_ssPendingCaps
Definition: IRCSock.h:220
bool m_bNamesx
Definition: IRCSock.h:203
unsigned int m_uCapPaused
Definition: IRCSock.h:218
bool m_bFloodProtection
Definition: IRCSock.h:230
bool m_bUHNames
Definition: IRCSock.h:204
Definition: IRCSock.h:35
Definition: Chan.h:35
Definition: Message.h:272
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:63
const std::set< char > & GetUserModes() const
Definition: IRCSock.h:152
CIRCSock & operator=(const CIRCSock &)=delete
void ReadLine(const CString &sData) override