ZNC  trunk
Client.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_CLIENT_H
18 #define ZNC_CLIENT_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Socket.h>
22 #include <znc/Utils.h>
23 #include <znc/Message.h>
24 #include <znc/main.h>
25 #include <memory>
26 #include <functional>
27 
28 // Forward Declarations
29 class CZNC;
30 class CUser;
31 class CIRCNetwork;
32 class CIRCSock;
33 class CClient;
34 class CMessage;
35 class CChan;
36 // !Forward Declarations
37 
39  public:
40  CAuthBase(const CString& sUsername, const CString& sPassword,
41  CZNCSock* pSock)
42  : m_sUsername(sUsername), m_sPassword(sPassword), m_pSock(pSock) {}
43 
44  virtual ~CAuthBase() {}
45 
46  CAuthBase(const CAuthBase&) = delete;
47  CAuthBase& operator=(const CAuthBase&) = delete;
48 
49  virtual void SetLoginInfo(const CString& sUsername,
50  const CString& sPassword, CZNCSock* pSock) {
51  m_sUsername = sUsername;
52  m_sPassword = sPassword;
53  m_pSock = pSock;
54  }
55 
56  void AcceptLogin(CUser& User);
57  void RefuseLogin(const CString& sReason);
58 
59  const CString& GetUsername() const { return m_sUsername; }
60  const CString& GetPassword() const { return m_sPassword; }
61  Csock* GetSocket() const { return m_pSock; }
63 
64  // Invalidate this CAuthBase instance which means it will no longer use
65  // m_pSock and AcceptLogin() or RefusedLogin() will have no effect.
66  virtual void Invalidate();
67 
68  protected:
69  virtual void AcceptedLogin(CUser& User) = 0;
70  virtual void RefusedLogin(const CString& sReason) = 0;
71 
72  private:
73  CString m_sUsername;
74  CString m_sPassword;
75  CZNCSock* m_pSock;
76 };
77 
78 class CClientAuth : public CAuthBase {
79  public:
80  CClientAuth(CClient* pClient, const CString& sUsername,
81  const CString& sPassword);
82  virtual ~CClientAuth() {}
83 
84  CClientAuth(const CClientAuth&) = delete;
85  CClientAuth& operator=(const CClientAuth&) = delete;
86 
87  void Invalidate() override {
88  m_pClient = nullptr;
90  }
91  void AcceptedLogin(CUser& User) override;
92  void RefusedLogin(const CString& sReason) override;
93 
94  private:
95  protected:
97 };
98 
99 class CClient : public CIRCSocket {
100  public:
102  virtual ~CClient();
103 
104  CClient(const CClient&) = delete;
105  CClient& operator=(const CClient&) = delete;
106 
108  void AcceptLogin(CUser& User);
109  void RefuseLogin(const CString& sReason);
110 
111  CString GetNick(bool bAllowIRCNick = true) const;
113  CString GetIdentifier() const { return m_sIdentifier; }
114  unsigned short int CapVersion() const { return m_uCapVersion; }
115  bool HasCap302() const { return CapVersion() >= 302; }
116  bool HasCapNotify() const { return m_bCapNotify; }
117  bool HasAwayNotify() const { return m_bAwayNotify; }
118  bool HasAccountNotify() const { return m_bAccountNotify; }
119  bool HasExtendedJoin() const { return m_bExtendedJoin; }
120  bool HasNamesx() const { return m_bNamesx; }
121  bool HasUHNames() const { return m_bUHNames; }
122  bool HasChgHost() const { return m_bChgHost; }
123  bool IsAway() const { return m_bAway; }
124  bool HasServerTime() const { return m_bServerTime; }
125  bool HasBatch() const { return m_bBatch; }
126  bool HasEchoMessage() const { return m_bEchoMessage; }
127  bool HasSelfMessage() const { return m_bSelfMessage; }
128 
129  static bool IsValidIdentifier(const CString& sIdentifier);
130 
131  void UserCommand(CString& sLine);
132  void UserPortCommand(CString& sLine);
133  void StatusCTCP(const CString& sCommand);
134  void BouncedOff();
135  bool IsAttached() const { return m_pUser != nullptr; }
136 
137  bool IsPlaybackActive() const { return m_bPlaybackActive; }
138  void SetPlaybackActive(bool bActive) { m_bPlaybackActive = bActive; }
139 
140  void PutIRC(const CString& sLine);
153  bool PutClientRaw(const CString& sLine);
157  void PutClient(const CString& sLine);
207  bool PutClient(const CMessage& Message);
208  unsigned int PutStatus(const CTable& table);
209  void PutStatus(const CString& sLine);
210  void PutStatusNotice(const CString& sLine);
211  void PutModule(const CString& sModule, const CString& sLine);
212  void PutModNotice(const CString& sModule, const CString& sLine);
213 
214  bool IsCapEnabled(const CString& sCap) const {
215  return 1 == m_ssAcceptedCaps.count(sCap);
216  }
217 
218  bool IsTagEnabled(const CString& sTag) const {
219  return 1 == m_ssSupportedTags.count(sTag);
220  }
226  void SetTagSupport(const CString& sTag, bool bState);
227 
230  void NotifyServerDependentCap(const CString& sCap, bool bValue, const CString& sValue);
231 
232  void ReadLine(const CString& sData) override;
233  bool SendMotd();
234  void HelpUser(const CString& sFilter = "");
235  void AuthUser();
236  void Connected() override;
237  void Timeout() override;
238  void Disconnected() override;
239  void ConnectionRefused() override;
240  void ReachedMaxBuffer() override;
241 
242  void SetNick(const CString& s);
243  void SetAway(bool bAway) { m_bAway = bAway; }
244  CUser* GetUser() const { return m_pUser; }
245  void SetNetwork(CIRCNetwork* pNetwork, bool bDisconnect = true,
246  bool bReconnect = true);
247  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
248  const std::vector<CClient*>& GetClients() const;
249  const CIRCSock* GetIRCSock() const;
252 
253  private:
254  void HandleCap(const CMessage& Message);
255  void RespondCap(const CString& sResponse);
256  void ParsePass(const CString& sAuthLine);
257  void ParseUser(const CString& sAuthLine);
258  void ParseIdentifier(const CString& sAuthLine);
259 
260  template <typename T>
261  void AddBuffer(const T& Message);
262  void EchoMessage(const CMessage& Message);
263 
264  std::set<CChan*> MatchChans(const CString& sPatterns) const;
265  unsigned int AttachChans(const std::set<CChan*>& sChans);
266  unsigned int DetachChans(const std::set<CChan*>& sChans);
267 
268  bool OnActionMessage(CActionMessage& Message);
269  bool OnCTCPMessage(CCTCPMessage& Message);
270  bool OnJoinMessage(CJoinMessage& Message);
271  bool OnModeMessage(CModeMessage& Message);
272  bool OnNoticeMessage(CNoticeMessage& Message);
273  bool OnPartMessage(CPartMessage& Message);
274  bool OnPingMessage(CMessage& Message);
275  bool OnPongMessage(CMessage& Message);
276  bool OnQuitMessage(CQuitMessage& Message);
277  bool OnTextMessage(CTextMessage& Message);
278  bool OnTopicMessage(CTopicMessage& Message);
279  bool OnOtherMessage(CMessage& Message);
280 
281  protected:
285  unsigned short int m_uCapVersion;
286  bool m_bInCap;
291  bool m_bNamesx;
294  bool m_bAway;
296  bool m_bBatch;
307  std::shared_ptr<CAuthBase> m_spAuth;
310  // The capabilities supported by the ZNC core - capability names mapped to
311  // change handler. Note: this lists caps which don't require support on IRC
312  // server.
313  static const std::map<CString, std::function<void(CClient*, bool bVal)>>&
315 
316  friend class ClientTest;
317  friend class CCoreCaps;
318 };
319 
320 #endif // !ZNC_CLIENT_H
std::set< CString > SCString
Definition: ZNCString.h:35
Definition: Message.h:231
Definition: Client.h:38
virtual void AcceptedLogin(CUser &User)=0
const CString & GetUsername() const
Definition: Client.h:59
void AcceptLogin(CUser &User)
CAuthBase(const CAuthBase &)=delete
CAuthBase & operator=(const CAuthBase &)=delete
const CString & GetPassword() const
Definition: Client.h:60
virtual ~CAuthBase()
Definition: Client.h:44
virtual void RefusedLogin(const CString &sReason)=0
virtual void Invalidate()
Csock * GetSocket() const
Definition: Client.h:61
CAuthBase(const CString &sUsername, const CString &sPassword, CZNCSock *pSock)
Definition: Client.h:40
virtual void SetLoginInfo(const CString &sUsername, const CString &sPassword, CZNCSock *pSock)
Definition: Client.h:49
void RefuseLogin(const CString &sReason)
CString GetRemoteIP() const
Definition: Message.h:242
Definition: Chan.h:35
Definition: Client.h:78
CClientAuth(CClient *pClient, const CString &sUsername, const CString &sPassword)
void Invalidate() override
Definition: Client.h:87
CClientAuth(const CClientAuth &)=delete
CClientAuth & operator=(const CClientAuth &)=delete
virtual ~CClientAuth()
Definition: Client.h:82
CClient * m_pClient
Definition: Client.h:96
void AcceptedLogin(CUser &User) override
void RefusedLogin(const CString &sReason) override
Definition: Client.h:99
CString GetNickMask() const
bool HasSelfMessage() const
Definition: Client.h:127
bool IsTagEnabled(const CString &sTag) const
Definition: Client.h:218
void AuthUser()
void SetNetwork(CIRCNetwork *pNetwork, bool bDisconnect=true, bool bReconnect=true)
CString GetFullName() const
bool m_bEchoMessage
Definition: Client.h:297
CIRCNetwork * m_pNetwork
Definition: Client.h:301
void SendRequiredPasswordNotice()
bool PutClientRaw(const CString &sLine)
Sends a raw data line to the client.
void SetTagSupport(const CString &sTag, bool bState)
Registers a tag as being supported or unsupported by the client.
void UserCommand(CString &sLine)
CString m_sNick
Definition: Client.h:302
bool m_bAccountNotify
Definition: Client.h:289
static bool IsValidIdentifier(const CString &sIdentifier)
bool m_bAway
Definition: Client.h:294
bool HasCapNotify() const
Definition: Client.h:116
void PutClient(const CString &sLine)
Sends a message to the client.
CString m_sNetwork
Definition: Client.h:305
CClient & operator=(const CClient &)=delete
bool m_bBatch
Definition: Client.h:296
void PutIRC(const CString &sLine)
bool m_bChgHost
Definition: Client.h:293
std::shared_ptr< CAuthBase > m_spAuth
Definition: Client.h:307
CIRCSock * GetIRCSock()
CClient(const CClient &)=delete
void BouncedOff()
CString GetNick(bool bAllowIRCNick=true) const
unsigned int PutStatus(const CTable &table)
bool m_bInCap
Definition: Client.h:286
void StatusCTCP(const CString &sCommand)
CString GetIdentifier() const
Definition: Client.h:113
bool m_bGotNick
Definition: Client.h:283
bool HasCap302() const
Definition: Client.h:115
void ReadLine(const CString &sData) override
bool IsPlaybackActive() const
Definition: Client.h:137
CString m_sPass
Definition: Client.h:303
SCString m_ssAcceptedCaps
Definition: Client.h:308
bool m_bGotPass
Definition: Client.h:282
bool IsAttached() const
Definition: Client.h:135
bool HasServerTime() const
Definition: Client.h:124
const std::vector< CClient * > & GetClients() const
void HelpUser(const CString &sFilter="")
bool HasBatch() const
Definition: Client.h:125
bool HasUHNames() const
Definition: Client.h:121
bool HasAwayNotify() const
Definition: Client.h:117
bool HasChgHost() const
Definition: Client.h:122
void PutStatusNotice(const CString &sLine)
void SetPlaybackActive(bool bActive)
Definition: Client.h:138
unsigned short int m_uCapVersion
Definition: Client.h:285
const CIRCSock * GetIRCSock() const
bool m_bServerTime
Definition: Client.h:295
void ReachedMaxBuffer() override
This WARNING event is called when your buffer for readline exceeds the warning threshold and triggers...
SCString m_ssSupportedTags
Definition: Client.h:309
bool IsCapEnabled(const CString &sCap) const
Definition: Client.h:214
void PutStatus(const CString &sLine)
static const std::map< CString, std::function< void(CClient *, bool bVal)> > & CoreCaps()
void Timeout() override
Sock Timed out event.
bool m_bGotUser
Definition: Client.h:284
void SetAway(bool bAway)
Definition: Client.h:243
bool m_bSelfMessage
Definition: Client.h:298
CUser * m_pUser
Definition: Client.h:300
void SetNick(const CString &s)
friend class CCoreCaps
Definition: Client.h:317
void Disconnected() override
Disconnected event.
void UserPortCommand(CString &sLine)
void Connected() override
Connected event.
void PutModNotice(const CString &sModule, const CString &sLine)
bool m_bPlaybackActive
Definition: Client.h:299
bool IsAway() const
Definition: Client.h:123
bool m_bNamesx
Definition: Client.h:291
CUser * GetUser() const
Definition: Client.h:244
friend class ClientTest
Definition: Client.h:316
bool HasEchoMessage() const
Definition: Client.h:126
bool HasAccountNotify() const
Definition: Client.h:118
bool HasExtendedJoin() const
Definition: Client.h:119
void PutModule(const CString &sModule, const CString &sLine)
void NotifyServerDependentCap(const CString &sCap, bool bValue, const CString &sValue)
Notifies client about one specific cap which server has just notified us about.
void AcceptLogin(CUser &User)
bool SendMotd()
CString m_sUser
Definition: Client.h:304
bool m_bAwayNotify
Definition: Client.h:288
bool m_bExtendedJoin
Definition: Client.h:290
bool m_bCapNotify
Definition: Client.h:287
bool HasNamesx() const
Definition: Client.h:120
void ConnectionRefused() override
Connection Refused Event.
CIRCNetwork * GetNetwork() const
Definition: Client.h:247
unsigned short int CapVersion() const
Definition: Client.h:114
bool m_bUHNames
Definition: Client.h:292
bool PutClient(const CMessage &Message)
Sends a message to the client.
CString m_sIdentifier
Definition: Client.h:306
virtual ~CClient()
void RefuseLogin(const CString &sReason)
Definition: Translation.h:103
Definition: IRCNetwork.h:40
Definition: IRCSock.h:35
Base IRC socket for client<->ZNC, and ZNC<->server.
Definition: Socket.h:309
Definition: Message.h:252
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:259
Definition: Message.h:280
Definition: Message.h:304
Definition: Message.h:313
String class that is used inside ZNC.
Definition: ZNCString.h:68
Generate a grid-like or list-like output from a given input.
Definition: Utils.h:172
Definition: Message.h:322
Definition: Message.h:329
Definition: User.h:38
Definition: Socket.h:27
Definition: znc.h:38
Basic socket class.
Definition: Csocket.h:564