ZNC trunk
Loading...
Searching...
No Matches
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
29class CZNC;
30class CUser;
31class CIRCNetwork;
32class CIRCSock;
33class CClient;
34class CMessage;
35class 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
78class 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;
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
99class 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;
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 IsAway() const { return m_bAway; }
123 bool HasServerTime() const { return m_bServerTime; }
124 bool HasBatch() const { return m_bBatch; }
125 bool HasEchoMessage() const { return m_bEchoMessage; }
126 bool HasSelfMessage() const { return m_bSelfMessage; }
127
128 static bool IsValidIdentifier(const CString& sIdentifier);
129
130 void UserCommand(CString& sLine);
132 void StatusCTCP(const CString& sCommand);
134 bool IsAttached() const { return m_pUser != nullptr; }
135
136 bool IsPlaybackActive() const { return m_bPlaybackActive; }
137 void SetPlaybackActive(bool bActive) { m_bPlaybackActive = bActive; }
138
139 void PutIRC(const CString& sLine);
152 bool PutClientRaw(const CString& sLine);
156 void PutClient(const CString& sLine);
206 bool PutClient(const CMessage& Message);
207 unsigned int PutStatus(const CTable& table);
208 void PutStatus(const CString& sLine);
209 void PutStatusNotice(const CString& sLine);
210 void PutModule(const CString& sModule, const CString& sLine);
211 void PutModNotice(const CString& sModule, const CString& sLine);
212
213 bool IsCapEnabled(const CString& sCap) const {
214 return 1 == m_ssAcceptedCaps.count(sCap);
215 }
216
217 bool IsTagEnabled(const CString& sTag) const {
218 return 1 == m_ssSupportedTags.count(sTag);
219 }
220
225 void SetTagSupport(const CString& sTag, bool bState);
226
229 void NotifyServerDependentCap(const CString& sCap, bool bValue, const CString& sValue);
230
231 void ReadLine(const CString& sData) override;
232 bool SendMotd();
233 void HelpUser(const CString& sFilter = "");
234 void AuthUser();
235 void Connected() override;
236 void Timeout() override;
237 void Disconnected() override;
238 void ConnectionRefused() override;
239 void ReachedMaxBuffer() override;
240
241 void SetNick(const CString& s);
242 void SetAway(bool bAway) { m_bAway = bAway; }
243 CUser* GetUser() const { return m_pUser; }
244 void SetNetwork(CIRCNetwork* pNetwork, bool bDisconnect = true,
245 bool bReconnect = true);
246 CIRCNetwork* GetNetwork() const { return m_pNetwork; }
247 const std::vector<CClient*>& GetClients() const;
248 const CIRCSock* GetIRCSock() const;
251
252 private:
253 void HandleCap(const CMessage& Message);
254 void RespondCap(const CString& sResponse);
255 void ParsePass(const CString& sAuthLine);
256 void ParseUser(const CString& sAuthLine);
257 void ParseIdentifier(const CString& sAuthLine);
258
259 template <typename T>
260 void AddBuffer(const T& Message);
261 void EchoMessage(const CMessage& Message);
262
263 std::set<CChan*> MatchChans(const CString& sPatterns) const;
264 unsigned int AttachChans(const std::set<CChan*>& sChans);
265 unsigned int DetachChans(const std::set<CChan*>& sChans);
266
267 bool OnActionMessage(CActionMessage& Message);
268 bool OnCTCPMessage(CCTCPMessage& Message);
269 bool OnJoinMessage(CJoinMessage& Message);
270 bool OnModeMessage(CModeMessage& Message);
271 bool OnNoticeMessage(CNoticeMessage& Message);
272 bool OnPartMessage(CPartMessage& Message);
273 bool OnPingMessage(CMessage& Message);
274 bool OnPongMessage(CMessage& Message);
275 bool OnQuitMessage(CQuitMessage& Message);
276 bool OnTextMessage(CTextMessage& Message);
277 bool OnTopicMessage(CTopicMessage& Message);
278 bool OnOtherMessage(CMessage& Message);
279
280 protected:
284 unsigned short int m_uCapVersion;
305 std::shared_ptr<CAuthBase> m_spAuth;
308 // The capabilities supported by the ZNC core - capability names mapped to
309 // change handler. Note: this lists caps which don't require support on IRC
310 // server.
311 static const std::map<CString, std::function<void(CClient*, bool bVal)>>&
313
314 friend class ClientTest;
315 friend class CCoreCaps;
316};
317
318#endif // !ZNC_CLIENT_H
std::set< CString > SCString
Definition ZNCString.h:37
Definition Message.h:229
virtual void AcceptedLogin(CUser &User)=0
const CString & GetPassword() const
Definition Client.h:60
void AcceptLogin(CUser &User)
CAuthBase(const CAuthBase &)=delete
CAuthBase & operator=(const CAuthBase &)=delete
const CString & GetUsername() const
Definition Client.h:59
virtual ~CAuthBase()
Definition Client.h:44
virtual void RefusedLogin(const CString &sReason)=0
virtual void Invalidate()
CAuthBase(const CString &sUsername, const CString &sPassword, CZNCSock *pSock)
Definition Client.h:40
Csock * GetSocket() const
Definition Client.h:61
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:240
Definition Chan.h:35
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:126
bool IsTagEnabled(const CString &sTag) const
Definition Client.h:217
void AuthUser()
void SetNetwork(CIRCNetwork *pNetwork, bool bDisconnect=true, bool bReconnect=true)
CIRCSock * GetIRCSock()
CString GetFullName() const
bool m_bEchoMessage
Definition Client.h:295
CIRCNetwork * m_pNetwork
Definition Client.h:299
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:300
const CIRCSock * GetIRCSock() const
bool m_bAccountNotify
Definition Client.h:288
CClient & operator=(const CClient &)=delete
static bool IsValidIdentifier(const CString &sIdentifier)
bool m_bAway
Definition Client.h:292
bool HasCapNotify() const
Definition Client.h:116
void PutClient(const CString &sLine)
Sends a message to the client.
CString m_sNetwork
Definition Client.h:303
bool m_bBatch
Definition Client.h:294
void PutIRC(const CString &sLine)
std::shared_ptr< CAuthBase > m_spAuth
Definition Client.h:305
CClient(const CClient &)=delete
void BouncedOff()
CString GetNick(bool bAllowIRCNick=true) const
unsigned int PutStatus(const CTable &table)
bool m_bInCap
Definition Client.h:285
void StatusCTCP(const CString &sCommand)
CString GetIdentifier() const
Definition Client.h:113
bool m_bGotNick
Definition Client.h:282
bool HasCap302() const
Definition Client.h:115
void ReadLine(const CString &sData) override
bool IsPlaybackActive() const
Definition Client.h:136
CString m_sPass
Definition Client.h:301
SCString m_ssAcceptedCaps
Definition Client.h:306
bool m_bGotPass
Definition Client.h:281
bool IsAttached() const
Definition Client.h:134
bool HasServerTime() const
Definition Client.h:123
void HelpUser(const CString &sFilter="")
bool HasBatch() const
Definition Client.h:124
bool HasUHNames() const
Definition Client.h:121
bool HasAwayNotify() const
Definition Client.h:117
void PutStatusNotice(const CString &sLine)
void SetPlaybackActive(bool bActive)
Definition Client.h:137
unsigned short int m_uCapVersion
Definition Client.h:284
bool m_bServerTime
Definition Client.h:293
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:307
bool IsCapEnabled(const CString &sCap) const
Definition Client.h:213
void PutStatus(const CString &sLine)
void Timeout() override
Sock Timed out event.
bool m_bGotUser
Definition Client.h:283
void SetAway(bool bAway)
Definition Client.h:242
bool m_bSelfMessage
Definition Client.h:296
CUser * m_pUser
Definition Client.h:298
void SetNick(const CString &s)
friend class CCoreCaps
Definition Client.h:315
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:297
bool IsAway() const
Definition Client.h:122
bool m_bNamesx
Definition Client.h:290
friend class ClientTest
Definition Client.h:314
bool HasEchoMessage() const
Definition Client.h:125
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:302
bool m_bAwayNotify
Definition Client.h:287
static const std::map< CString, std::function< void(CClient *, bool bVal)> > & CoreCaps()
bool m_bExtendedJoin
Definition Client.h:289
bool m_bCapNotify
Definition Client.h:286
bool HasNamesx() const
Definition Client.h:120
void ConnectionRefused() override
Connection Refused Event.
CUser * GetUser() const
Definition Client.h:243
CIRCNetwork * GetNetwork() const
Definition Client.h:246
unsigned short int CapVersion() const
Definition Client.h:114
const std::vector< CClient * > & GetClients() const
bool m_bUHNames
Definition Client.h:291
bool PutClient(const CMessage &Message)
Sends a message to the client.
CString m_sIdentifier
Definition Client.h:304
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:250
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:257
Definition Message.h:278
Definition Message.h:302
Definition Message.h:311
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:320
Definition Message.h:327
Definition User.h:38
Definition Socket.h:27
Definition znc.h:38
Basic socket class.
Definition Csocket.h:564