ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Client.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_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; }
62  CString GetRemoteIP() const;
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  : CIRCSocket(),
103  m_bGotPass(false),
104  m_bGotNick(false),
105  m_bGotUser(false),
106  m_bInCap(false),
107  m_bCapNotify(false),
108  m_bAwayNotify(false),
109  m_bAccountNotify(false),
110  m_bExtendedJoin(false),
111  m_bNamesx(false),
112  m_bUHNames(false),
113  m_bAway(false),
114  m_bServerTime(false),
115  m_bBatch(false),
116  m_bEchoMessage(false),
117  m_bSelfMessage(false),
118  m_bPlaybackActive(false),
119  m_pUser(nullptr),
120  m_pNetwork(nullptr),
121  m_sNick("unknown-nick"),
122  m_sPass(""),
123  m_sUser(""),
124  m_sNetwork(""),
125  m_sIdentifier(""),
126  m_spAuth(),
129  m_mCoreCaps({
130  {"multi-prefix",
131  {false, [this](bool bVal) { m_bNamesx = bVal; }}},
132  {"userhost-in-names",
133  {false, [this](bool bVal) { m_bUHNames = bVal; }}},
134  {"echo-message",
135  {false, [this](bool bVal) { m_bEchoMessage = bVal; }}},
136  {"server-time",
137  {false, [this](bool bVal) {
138  m_bServerTime = bVal;
139  SetTagSupport("time", bVal);
140  }}},
141  {"batch", {false, [this](bool bVal) {
142  m_bBatch = bVal;
143  SetTagSupport("batch", bVal);
144  }}},
145  {"cap-notify",
146  {false, [this](bool bVal) { m_bCapNotify = bVal; }}},
147  {"away-notify",
148  {true, [this](bool bVal) { m_bAwayNotify = bVal; }}},
149  {"account-notify",
150  {true, [this](bool bVal) { m_bAccountNotify = bVal; }}},
151  {"extended-join",
152  {true, [this](bool bVal) { m_bExtendedJoin = bVal; }}},
153  }) {
154  EnableReadLine();
155  // RFC says a line can have 512 chars max, but we are
156  // a little more gentle ;)
157  SetMaxBufferThreshold(1024);
158 
159  // For compatibility with older clients
160  m_mCoreCaps["znc.in/server-time-iso"] = m_mCoreCaps["server-time"];
161  m_mCoreCaps["znc.in/batch"] = m_mCoreCaps["batch"];
162  m_mCoreCaps["znc.in/self-message"] = {
163  false, [this](bool bVal) { m_bSelfMessage = bVal; }};
164  }
165 
166  virtual ~CClient();
167 
168  CClient(const CClient&) = delete;
169  CClient& operator=(const CClient&) = delete;
170 
172  void AcceptLogin(CUser& User);
173  void RefuseLogin(const CString& sReason);
174 
175  CString GetNick(bool bAllowIRCNick = true) const;
176  CString GetNickMask() const;
177  CString GetIdentifier() const { return m_sIdentifier; }
178  bool HasCapNotify() const { return m_bCapNotify; }
179  bool HasAwayNotify() const { return m_bAwayNotify; }
180  bool HasAccountNotify() const { return m_bAccountNotify; }
181  bool HasExtendedJoin() const { return m_bExtendedJoin; }
182  bool HasNamesx() const { return m_bNamesx; }
183  bool HasUHNames() const { return m_bUHNames; }
184  bool IsAway() const { return m_bAway; }
185  bool HasServerTime() const { return m_bServerTime; }
186  bool HasBatch() const { return m_bBatch; }
187  bool HasEchoMessage() const { return m_bEchoMessage; }
188  bool HasSelfMessage() const { return m_bSelfMessage; }
189 
190  static bool IsValidIdentifier(const CString& sIdentifier);
191 
192  void UserCommand(CString& sLine);
193  void UserPortCommand(CString& sLine);
194  void StatusCTCP(const CString& sCommand);
195  void BouncedOff();
196  bool IsAttached() const { return m_pUser != nullptr; }
197 
198  bool IsPlaybackActive() const { return m_bPlaybackActive; }
199  void SetPlaybackActive(bool bActive) { m_bPlaybackActive = bActive; }
200 
201  void PutIRC(const CString& sLine);
214  bool PutClientRaw(const CString& sLine);
218  void PutClient(const CString& sLine);
268  bool PutClient(const CMessage& Message);
269  unsigned int PutStatus(const CTable& table);
270  void PutStatus(const CString& sLine);
271  void PutStatusNotice(const CString& sLine);
272  void PutModule(const CString& sModule, const CString& sLine);
273  void PutModNotice(const CString& sModule, const CString& sLine);
274 
275  bool IsCapEnabled(const CString& sCap) const {
276  return 1 == m_ssAcceptedCaps.count(sCap);
277  }
278 
279  bool IsTagEnabled(const CString& sTag) const {
280  return 1 == m_ssSupportedTags.count(sTag);
281  }
287  void SetTagSupport(const CString& sTag, bool bState);
288 
289  void NotifyServerDependentCaps(const SCString& ssCaps);
291 
292  void ReadLine(const CString& sData) override;
293  bool SendMotd();
294  void HelpUser(const CString& sFilter = "");
295  void AuthUser();
296  void Connected() override;
297  void Timeout() override;
298  void Disconnected() override;
299  void ConnectionRefused() override;
300  void ReachedMaxBuffer() override;
301 
302  void SetNick(const CString& s);
303  void SetAway(bool bAway) { m_bAway = bAway; }
304  CUser* GetUser() const { return m_pUser; }
305  void SetNetwork(CIRCNetwork* pNetwork, bool bDisconnect = true,
306  bool bReconnect = true);
307  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
308  const std::vector<CClient*>& GetClients() const;
309  const CIRCSock* GetIRCSock() const;
310  CIRCSock* GetIRCSock();
311  CString GetFullName() const;
312 
313  private:
314  void HandleCap(const CMessage& Message);
315  void RespondCap(const CString& sResponse);
316  void ParsePass(const CString& sAuthLine);
317  void ParseUser(const CString& sAuthLine);
318  void ParseIdentifier(const CString& sAuthLine);
319 
320  template <typename T>
321  void AddBuffer(const T& Message);
322  void EchoMessage(const CMessage& Message);
323 
324  std::set<CChan*> MatchChans(const CString& sPatterns) const;
325  unsigned int AttachChans(const std::set<CChan*>& sChans);
326  unsigned int DetachChans(const std::set<CChan*>& sChans);
327 
328  bool OnActionMessage(CActionMessage& Message);
329  bool OnCTCPMessage(CCTCPMessage& Message);
330  bool OnJoinMessage(CJoinMessage& Message);
331  bool OnModeMessage(CModeMessage& Message);
332  bool OnNoticeMessage(CNoticeMessage& Message);
333  bool OnPartMessage(CPartMessage& Message);
334  bool OnPingMessage(CMessage& Message);
335  bool OnPongMessage(CMessage& Message);
336  bool OnQuitMessage(CQuitMessage& Message);
337  bool OnTextMessage(CTextMessage& Message);
338  bool OnTopicMessage(CTopicMessage& Message);
339  bool OnOtherMessage(CMessage& Message);
340 
341  protected:
345  bool m_bInCap;
350  bool m_bNamesx;
352  bool m_bAway;
354  bool m_bBatch;
365  std::shared_ptr<CAuthBase> m_spAuth;
368  // The capabilities supported by the ZNC core - capability names mapped
369  // to a pair which contains a bool describing whether the capability is
370  // server-dependent, and a capability value change handler.
371  std::map<CString, std::pair<bool, std::function<void(bool bVal)>>>
373  // A subset of CIRCSock::GetAcceptedCaps(), the caps that can be listed
374  // in CAP LS and may be notified to the client with CAP NEW (cap-notify).
376 
377  friend class ClientTest;
378 };
379 
380 #endif // !ZNC_CLIENT_H
virtual void RefusedLogin(const CString &sReason)=0
void Invalidate() override
Definition: Client.h:87
bool m_bCapNotify
Definition: Client.h:346
Definition: Message.h:308
Definition: Message.h:245
CString m_sPass
Definition: Client.h:361
bool SendMotd()
bool m_bServerTime
Definition: Client.h:353
Definition: User.h:38
bool IsTagEnabled(const CString &sTag) const
Definition: Client.h:279
void AuthUser()
CString GetNick(bool bAllowIRCNick=true) const
bool HasAccountNotify() const
Definition: Client.h:180
SCString m_ssAcceptedCaps
Definition: Client.h:366
bool m_bSelfMessage
Definition: Client.h:356
std::map< CString, std::pair< bool, std::function< void(bool bVal)> > > m_mCoreCaps
Definition: Client.h:372
bool m_bGotNick
Definition: Client.h:343
SetMaxBufferThreshold(1024)
CAuthBase(const CString &sUsername, const CString &sPassword, CZNCSock *pSock)
Definition: Client.h:40
bool HasBatch() const
Definition: Client.h:186
void RefusedLogin(const CString &sReason) override
void AcceptLogin(CUser &User)
const CString & GetUsername() const
Definition: Client.h:59
const std::vector< CClient * > & GetClients() const
void SetNick(const CString &s)
CString m_sNick
Definition: Client.h:360
Definition: Client.h:78
virtual void Invalidate()
Definition: Client.h:99
Base IRC socket for client<->ZNC, and ZNC<->server.
Definition: Socket.h:305
bool IsAttached() const
Definition: Client.h:196
bool HasCapNotify() const
Definition: Client.h:178
CString GetNickMask() const
CClient & operator=(const CClient &)=delete
Definition: Message.h:283
void Connected() override
Connected event.
Definition: Message.h:259
void BouncedOff()
void RefuseLogin(const CString &sReason)
bool HasSelfMessage() const
Definition: Client.h:188
void PutStatusNotice(const CString &sLine)
void SetAway(bool bAway)
Definition: Client.h:303
void ClearServerDependentCaps()
bool IsAway() const
Definition: Client.h:184
void ReadLine(const CString &sData) override
void SetNetwork(CIRCNetwork *pNetwork, bool bDisconnect=true, bool bReconnect=true)
bool m_bUHNames
Definition: Client.h:351
const CIRCSock * GetIRCSock() const
std::set< CString > SCString
Definition: ZNCString.h:35
bool PutClientRaw(const CString &sLine)
Sends a raw data line to the client.
Definition: Socket.h:27
bool HasExtendedJoin() const
Definition: Client.h:181
Definition: Message.h:228
void ConnectionRefused() override
Connection Refused Event.
void RefuseLogin(const CString &sReason)
bool m_bEchoMessage
Definition: Client.h:355
bool HasEchoMessage() const
Definition: Client.h:187
Definition: Message.h:292
void SendRequiredPasswordNotice()
CUser * GetUser() const
Definition: Client.h:304
Definition: znc.h:38
Definition: Message.h:217
Basic socket class.
Definition: Csocket.h:564
Definition: IRCNetwork.h:40
virtual ~CClient()
bool HasAwayNotify() const
Definition: Client.h:179
static bool IsValidIdentifier(const CString &sIdentifier)
String class that is used inside ZNC.
Definition: ZNCString.h:68
void SetTagSupport(const CString &sTag, bool bState)
Registers a tag as being supported or unsupported by the client.
bool m_bPlaybackActive
Definition: Client.h:357
CString m_sNetwork
Definition: Client.h:363
void HelpUser(const CString &sFilter="")
void AcceptedLogin(CUser &User) override
bool m_bAccountNotify
Definition: Client.h:348
void PutModule(const CString &sModule, const CString &sLine)
void Disconnected() override
Disconnected event.
CUser * m_pUser
Definition: Client.h:358
CString GetIdentifier() const
Definition: Client.h:177
Csock * GetSocket() const
Definition: Client.h:61
friend class ClientTest
Definition: Client.h:377
void UserPortCommand(CString &sLine)
bool m_bExtendedJoin
Definition: Client.h:349
CClient()
Definition: Client.h:101
CClientAuth(CClient *pClient, const CString &sUsername, const CString &sPassword)
Definition: Client.h:38
virtual void SetLoginInfo(const CString &sUsername, const CString &sPassword, CZNCSock *pSock)
Definition: Client.h:49
SCString m_ssServerDependentCaps
Definition: Client.h:375
CIRCNetwork * m_pNetwork
Definition: Client.h:359
bool m_bGotPass
Definition: Client.h:342
void PutClient(const CString &sLine)
Sends a message to the client.
bool IsPlaybackActive() const
Definition: Client.h:198
virtual ~CClientAuth()
Definition: Client.h:82
CIRCNetwork * GetNetwork() const
Definition: Client.h:307
Definition: Message.h:301
void UserCommand(CString &sLine)
bool m_bAwayNotify
Definition: Client.h:347
bool m_bNamesx
Definition: Client.h:350
bool m_bInCap
Definition: Client.h:345
bool HasNamesx() const
Definition: Client.h:182
const CString & GetPassword() const
Definition: Client.h:60
m_mCoreCaps["znc.in/server-time-iso"]
Definition: Client.h:160
virtual ~CAuthBase()
Definition: Client.h:44
CString GetRemoteIP() const
CAuthBase & operator=(const CAuthBase &)=delete
CClientAuth & operator=(const CClientAuth &)=delete
CClient * m_pClient
Definition: Client.h:96
void PutIRC(const CString &sLine)
void SetPlaybackActive(bool bActive)
Definition: Client.h:199
void EnableReadLine()
set the value of m_bEnableReadLine to true, we don't want to store a buffer for ReadLine, unless we want it
CString GetFullName() const
std::shared_ptr< CAuthBase > m_spAuth
Definition: Client.h:365
CString m_sIdentifier
Definition: Client.h:364
void StatusCTCP(const CString &sCommand)
unsigned int PutStatus(const CTable &table)
Definition: Message.h:238
void Timeout() override
Sock Timed out event.
bool m_bAway
Definition: Client.h:352
void ReachedMaxBuffer() override
This WARNING event is called when your buffer for readline exceeds the warning threshold and triggers...
void PutModNotice(const CString &sModule, const CString &sLine)
Definition: IRCSock.h:35
bool HasUHNames() const
Definition: Client.h:183
bool m_bBatch
Definition: Client.h:354
void NotifyServerDependentCaps(const SCString &ssCaps)
bool HasServerTime() const
Definition: Client.h:185
Definition: Chan.h:35
Generate a grid-like output from a given input.
Definition: Utils.h:156
SCString m_ssSupportedTags
Definition: Client.h:367
bool IsCapEnabled(const CString &sCap) const
Definition: Client.h:275
bool m_bGotUser
Definition: Client.h:344
CString m_sUser
Definition: Client.h:362
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
virtual void AcceptedLogin(CUser &User)=0
void AcceptLogin(CUser &User)
Definition: Translation.h:104