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;
59 
60  void PutIRC(const CString& sLine);
61  void PutIRCQuick(const CString& sLine);
62  void ResetChans();
63  void Quit(const CString& sQuitMsg = "");
64 
69  void PauseCap();
73  void ResumeCap();
74 
75  // Setters
76  void SetPass(const CString& s) { m_sPass = s; }
77  // !Setters
78 
79  // Getters
80  unsigned int GetMaxNickLen() const { return m_uMaxNickLen; }
81  EChanModeArgs GetModeType(char cMode) const;
82  char GetPermFromMode(char cMode) const;
83  const std::map<char, EChanModeArgs>& GetChanModes() const {
84  return m_mceChanModes;
85  }
86  bool IsPermChar(const char c) const {
87  return (c != '\0' && GetPerms().find(c) != CString::npos);
88  }
89  bool IsPermMode(const char c) const {
90  return (c != '\0' && GetPermModes().find(c) != CString::npos);
91  }
92  const CString& GetPerms() const { return m_sPerms; }
93  const CString& GetPermModes() const { return m_sPermModes; }
94  CString GetNickMask() const { return m_Nick.GetNickMask(); }
95  const CString& GetNick() const { return m_Nick.GetNick(); }
96  const CString& GetPass() const { return m_sPass; }
97  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
98  bool HasNamesx() const { return m_bNamesx; }
99  bool HasUHNames() const { return m_bUHNames; }
100  bool HasAwayNotify() const { return m_bAwayNotify; }
101  bool HasAccountNotify() const { return m_bAccountNotify; }
102  bool HasExtendedJoin() const { return m_bExtendedJoin; }
103  bool HasServerTime() const { return m_bServerTime; }
104  const std::set<char>& GetUserModes() const {
105  return m_scUserModes;
106  }
107  // This is true if we are past raw 001
108  bool IsAuthed() const { return m_bAuthed; }
109  const SCString& GetAcceptedCaps() const { return m_ssAcceptedCaps; }
110  bool IsCapAccepted(const CString& sCap) {
111  return 1 == m_ssAcceptedCaps.count(sCap);
112  }
113  const MCString& GetISupport() const { return m_mISupport; }
114  CString GetISupport(const CString& sKey,
115  const CString& sDefault = "") const;
116  // !Getters
117 
118  // TODO move this function to CIRCNetwork and make it non-static?
119  static bool IsFloodProtected(double fRate);
120 
121  private:
122  // Message Handlers
123  bool OnAccountMessage(CMessage& Message);
124  bool OnActionMessage(CActionMessage& Message);
125  bool OnAwayMessage(CMessage& Message);
126  bool OnCapabilityMessage(CMessage& Message);
127  bool OnCTCPMessage(CCTCPMessage& Message);
128  bool OnErrorMessage(CMessage& Message);
129  bool OnInviteMessage(CMessage& Message);
130  bool OnJoinMessage(CJoinMessage& Message);
131  bool OnKickMessage(CKickMessage& Message);
132  bool OnModeMessage(CModeMessage& Message);
133  bool OnNickMessage(CNickMessage& Message);
134  bool OnNoticeMessage(CNoticeMessage& Message);
135  bool OnNumericMessage(CNumericMessage& Message);
136  bool OnPartMessage(CPartMessage& Message);
137  bool OnPingMessage(CMessage& Message);
138  bool OnPongMessage(CMessage& Message);
139  bool OnQuitMessage(CQuitMessage& Message);
140  bool OnTextMessage(CTextMessage& Message);
141  bool OnTopicMessage(CTopicMessage& Message);
142  bool OnWallopsMessage(CMessage& Message);
143  bool OnServerCapAvailable(const CString& sCap);
144  // !Message Handlers
145 
146  void SetNick(const CString& sNick);
147  void ParseISupport(const CMessage& Message);
148  // This is called when we connect and the nick we want is already taken
149  void SendAltNick(const CString& sBadNick);
150  void SendNextCap();
151  void TrySend();
152 
153  protected:
154  bool m_bAuthed;
155  bool m_bNamesx;
163  std::set<char> m_scUserModes;
164  std::map<char, EChanModeArgs> m_mceChanModes;
168  std::map<CString, CChan*> m_msChans;
169  unsigned int m_uMaxNickLen;
170  unsigned int m_uCapPaused;
173  time_t m_lastCTCP;
174  unsigned int m_uNumCTCP;
175  static const time_t m_uCTCPFloodTime;
176  static const unsigned int m_uCTCPFloodCount;
178  std::deque<CString> m_vsSendQueue;
179  short int m_iSendsAllowed;
180  unsigned short int m_uFloodBurst;
181  double m_fFloodRate;
183 
184  friend class CIRCFloodTimer;
185 };
186 
187 #endif // !ZNC_IRCSOCK_H
Definition: Message.h:266
bool IsCapAccepted(const CString &sCap)
Definition: IRCSock.h:110
Definition: Message.h:308
Definition: Message.h:245
const CString & GetPermModes() const
Definition: IRCSock.h:93
Definition: User.h:37
void ResumeCap()
If you used PauseCap, call this when CAP negotiation and logging in should be resumed again...
CString GetNickMask() const
Definition: IRCSock.h:94
void ConnectionRefused() override
Connection Refused Event.
bool HasNamesx() const
Definition: IRCSock.h:98
CString m_sPass
Definition: IRCSock.h:167
CIRCNetwork * GetNetwork() const
Definition: IRCSock.h:97
void SockError(int iErrno, const CString &sDescription) override
bool m_bExtendedJoin
Definition: IRCSock.h:159
CString m_sPerms
Definition: IRCSock.h:161
const CString & GetNick() const
Definition: IRCSock.h:95
MCString m_mISupport
Definition: IRCSock.h:177
bool IsPermMode(const char c) const
Definition: IRCSock.h:89
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:179
bool IsPermChar(const char c) const
Definition: IRCSock.h:86
Definition: Client.h:99
bool m_bServerTime
Definition: IRCSock.h:160
Base IRC socket for client<->ZNC, and ZNC<->server.
Definition: Socket.h:304
std::set< char > m_scUserModes
Definition: IRCSock.h:163
Definition: Message.h:283
Definition: Message.h:259
const CString & GetNick() const
const SCString & GetAcceptedCaps() const
Definition: IRCSock.h:109
Definition: IRCSock.h:46
unsigned short int m_uFloodBurst
Definition: IRCSock.h:180
EChanModeArgs GetModeType(char cMode) const
Definition: Nick.h:29
bool m_bAwayNotify
Definition: IRCSock.h:157
bool HasAwayNotify() const
Definition: IRCSock.h:100
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:83
CNick m_Nick
Definition: IRCSock.h:166
Definition: Message.h:292
Definition: Message.h:217
Definition: IRCNetwork.h:40
bool HasExtendedJoin() const
Definition: IRCSock.h:102
void PutIRC(const CString &sLine)
void ResetChans()
void Connected() override
Connected event.
String class that is used inside ZNC.
Definition: ZNCString.h:68
bool HasAccountNotify() const
Definition: IRCSock.h:101
std::deque< CString > m_vsSendQueue
Definition: IRCSock.h:178
SCString m_ssAcceptedCaps
Definition: IRCSock.h:171
static const unsigned int m_uCTCPFloodCount
Definition: IRCSock.h:176
unsigned int m_uMaxNickLen
Definition: IRCSock.h:169
Definition: IRCSock.h:48
const CString & GetPass() const
Definition: IRCSock.h:96
bool IsAuthed() const
Definition: IRCSock.h:108
static const time_t m_uCTCPFloodTime
Definition: IRCSock.h:175
std::map< CString, CChan * > m_msChans
Definition: IRCSock.h:168
std::map< char, EChanModeArgs > m_mceChanModes
Definition: IRCSock.h:164
CString m_sPermModes
Definition: IRCSock.h:162
bool m_bAuthed
Definition: IRCSock.h:154
Definition: IRCSock.h:47
static bool IsFloodProtected(double fRate)
time_t m_lastCTCP
Definition: IRCSock.h:173
void SetPass(const CString &s)
Definition: IRCSock.h:76
void Disconnected() override
Disconnected event.
CIRCNetwork * m_pNetwork
Definition: IRCSock.h:165
Definition: Message.h:301
bool HasUHNames() const
Definition: IRCSock.h:99
A dictionary for strings.
Definition: ZNCString.h:595
unsigned int GetMaxNickLen() const
Definition: IRCSock.h:80
Definition: Message.h:251
double m_fFloodRate
Definition: IRCSock.h:181
bool m_bAccountNotify
Definition: IRCSock.h:158
bool HasServerTime() const
Definition: IRCSock.h:103
const MCString & GetISupport() const
Definition: IRCSock.h:113
void Timeout() override
Sock Timed out event.
unsigned int m_uNumCTCP
Definition: IRCSock.h:174
char GetPermFromMode(char cMode) const
EChanModeArgs
Definition: IRCSock.h:43
const CString & GetPerms() const
Definition: IRCSock.h:92
friend class CIRCFloodTimer
Definition: IRCSock.h:184
Definition: Message.h:238
SCString m_ssPendingCaps
Definition: IRCSock.h:172
bool m_bNamesx
Definition: IRCSock.h:155
unsigned int m_uCapPaused
Definition: IRCSock.h:170
bool m_bFloodProtection
Definition: IRCSock.h:182
bool m_bUHNames
Definition: IRCSock.h:156
Definition: IRCSock.h:35
Definition: Chan.h:34
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:104
CIRCSock & operator=(const CIRCSock &)=delete
void ReadLine(const CString &sData) override