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-2014 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 _CLIENT_H
18 #define _CLIENT_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Socket.h>
22 #include <znc/Utils.h>
23 #include <znc/main.h>
24 
25 // Forward Declarations
26 class CZNC;
27 class CUser;
28 class CIRCNetwork;
29 class CIRCSock;
30 class CClient;
31 // !Forward Declarations
32 
33 class CAuthBase {
34 public:
35  CAuthBase(const CString& sUsername, const CString& sPassword, Csock *pSock) {
36  SetLoginInfo(sUsername, sPassword, pSock);
37  }
38 
39  virtual ~CAuthBase() {}
40 
41  virtual void SetLoginInfo(const CString& sUsername, const CString& sPassword,
42  Csock *pSock) {
43  m_sUsername = sUsername;
44  m_sPassword = sPassword;
45  m_pSock = pSock;
46  }
47 
48  void AcceptLogin(CUser& User);
49  void RefuseLogin(const CString& sReason);
50 
51  const CString& GetUsername() const { return m_sUsername; }
52  const CString& GetPassword() const { return m_sPassword; }
53  Csock *GetSocket() const { return m_pSock; }
54  CString GetRemoteIP() const;
55 
56  // Invalidate this CAuthBase instance which means it will no longer use
57  // m_pSock and AcceptLogin() or RefusedLogin() will have no effect.
58  virtual void Invalidate();
59 
60 protected:
61  virtual void AcceptedLogin(CUser& User) = 0;
62  virtual void RefusedLogin(const CString& sReason) = 0;
63 
64 private:
65  CString m_sUsername;
66  CString m_sPassword;
67  Csock* m_pSock;
68 };
69 
70 
71 class CClientAuth : public CAuthBase {
72 public:
73  CClientAuth(CClient* pClient, const CString& sUsername, const CString& sPassword);
74  virtual ~CClientAuth() {}
75 
77  void AcceptedLogin(CUser& User);
78  void RefusedLogin(const CString& sReason);
79 private:
80 protected:
82 };
83 
84 class CClient : public CZNCSock {
85 public:
86  CClient() : CZNCSock() {
87  m_pUser = NULL;
88  m_pNetwork = NULL;
89  m_bGotPass = false;
90  m_bGotNick = false;
91  m_bGotUser = false;
92  m_bInCap = false;
93  m_bNamesx = false;
94  m_bUHNames = false;
95  m_bAway = false;
96  m_bServerTime = false;
98  // RFC says a line can have 512 chars max, but we are
99  // a little more gentle ;)
100  SetMaxBufferThreshold(1024);
101 
102  SetNick("unknown-nick");
103  }
104 
105  virtual ~CClient();
106 
108  void AcceptLogin(CUser& User);
109  void RefuseLogin(const CString& sReason);
110 
111  CString GetNick(bool bAllowIRCNick = true) const;
112  CString GetNickMask() const;
113  bool HasNamesx() const { return m_bNamesx; }
114  bool HasUHNames() const { return m_bUHNames; }
115  bool IsAway() const { return m_bAway; }
116  bool HasServerTime() const { return m_bServerTime; }
117 
118  void UserCommand(CString& sLine);
119  void UserPortCommand(CString& sLine);
120  void StatusCTCP(const CString& sCommand);
121  void BouncedOff();
122  bool IsAttached() const { return m_pUser != NULL; }
123 
124  void PutIRC(const CString& sLine);
125  void PutClient(const CString& sLine);
126  unsigned int PutStatus(const CTable& table);
127  void PutStatus(const CString& sLine);
128  void PutStatusNotice(const CString& sLine);
129  void PutModule(const CString& sModule, const CString& sLine);
130  void PutModNotice(const CString& sModule, const CString& sLine);
131 
132  bool IsCapEnabled(const CString& sCap) { return 1 == m_ssAcceptedCaps.count(sCap); }
133 
134  virtual void ReadLine(const CString& sData);
135  bool SendMotd();
136  void HelpUser();
137  void AuthUser();
138  virtual void Connected();
139  virtual void Timeout();
140  virtual void Disconnected();
141  virtual void ConnectionRefused();
142  virtual void ReachedMaxBuffer();
143 
144  void SetNick(const CString& s);
145  void SetAway(bool bAway) { m_bAway = bAway; }
146  CUser* GetUser() const { return m_pUser; }
147  void SetNetwork(CIRCNetwork* pNetwork, bool bDisconnect=true, bool bReconnect=true);
148  CIRCNetwork* GetNetwork() const { return m_pNetwork; }
149  std::vector<CClient*>& GetClients();
150  const CIRCSock* GetIRCSock() const;
151  CIRCSock* GetIRCSock();
153 private:
154  void HandleCap(const CString& sLine);
155  void RespondCap(const CString& sResponse);
156 
157 protected:
161  bool m_bInCap;
162  bool m_bNamesx;
164  bool m_bAway;
174 };
175 
176 #endif // !_CLIENT_H
virtual void ReadLine(const CString &sData)
virtual void RefusedLogin(const CString &sReason)=0
CString m_sPass
Definition: Client.h:169
bool SendMotd()
bool m_bServerTime
Definition: Client.h:165
Definition: User.h:37
void AuthUser()
CString GetNick(bool bAllowIRCNick=true) const
SCString m_ssAcceptedCaps
Definition: Client.h:173
virtual void Connected()
Override these functions for an easy interface when using the Socket Manager Don&#39;t bother using these...
bool m_bGotNick
Definition: Client.h:159
void AcceptLogin(CUser &User)
const CString & GetUsername() const
Definition: Client.h:51
void SetNick(const CString &s)
virtual void Timeout()
Override these functions for an easy interface when using the Socket Manager Don&#39;t bother using these...
CString m_sNick
Definition: Client.h:168
Definition: Client.h:71
void SetMaxBufferThreshold(uint32_t iThreshold)
sets the max buffered threshold when EnableReadLine() is enabled
void Invalidate()
Definition: Client.h:76
virtual void Invalidate()
void AcceptedLogin(CUser &User)
Definition: Client.h:84
bool IsAttached() const
Definition: Client.h:122
void RefusedLogin(const CString &sReason)
CString GetNickMask() const
void BouncedOff()
void RefuseLogin(const CString &sReason)
bool IsCapEnabled(const CString &sCap)
Definition: Client.h:132
void PutStatusNotice(const CString &sLine)
void SetAway(bool bAway)
Definition: Client.h:145
bool IsAway() const
Definition: Client.h:115
CSmartPtr< CAuthBase > m_spAuth
Definition: Client.h:172
void SetNetwork(CIRCNetwork *pNetwork, bool bDisconnect=true, bool bReconnect=true)
bool m_bUHNames
Definition: Client.h:163
const CIRCSock * GetIRCSock() const
std::set< CString > SCString
Definition: ZNCString.h:34
Definition: Socket.h:26
void RefuseLogin(const CString &sReason)
void SendRequiredPasswordNotice()
CUser * GetUser() const
Definition: Client.h:146
Definition: znc.h:35
Basic socket class.
Definition: Csocket.h:537
Definition: IRCNetwork.h:36
virtual ~CClient()
virtual void SetLoginInfo(const CString &sUsername, const CString &sPassword, Csock *pSock)
Definition: Client.h:41
virtual void ReachedMaxBuffer()
Override these functions for an easy interface when using the Socket Manager Don&#39;t bother using these...
String class that is used inside ZNC.
Definition: ZNCString.h:67
CString m_sNetwork
Definition: Client.h:171
void PutModule(const CString &sModule, const CString &sLine)
CUser * m_pUser
Definition: Client.h:166
Csock * GetSocket() const
Definition: Client.h:53
CString GetFullName()
void UserPortCommand(CString &sLine)
CClient()
Definition: Client.h:86
CClientAuth(CClient *pClient, const CString &sUsername, const CString &sPassword)
Definition: Client.h:33
CIRCNetwork * m_pNetwork
Definition: Client.h:167
bool m_bGotPass
Definition: Client.h:158
void PutClient(const CString &sLine)
virtual ~CClientAuth()
Definition: Client.h:74
CIRCNetwork * GetNetwork() const
Definition: Client.h:148
void UserCommand(CString &sLine)
bool m_bNamesx
Definition: Client.h:162
bool m_bInCap
Definition: Client.h:161
bool HasNamesx() const
Definition: Client.h:113
const CString & GetPassword() const
Definition: Client.h:52
virtual ~CAuthBase()
Definition: Client.h:39
virtual void ConnectionRefused()
Override these functions for an easy interface when using the Socket Manager Don&#39;t bother using these...
CString GetRemoteIP() const
CClient * m_pClient
Definition: Client.h:81
void PutIRC(const CString &sLine)
void EnableReadLine()
set the value of m_bEnableReadLine to true, we don&#39;t want to store a buffer for ReadLine, unless we want it
std::vector< CClient * > & GetClients()
virtual void Disconnected()
Override these functions for an easy interface when using the Socket Manager Don&#39;t bother using these...
void StatusCTCP(const CString &sCommand)
unsigned int PutStatus(const CTable &table)
bool m_bAway
Definition: Client.h:164
CAuthBase(const CString &sUsername, const CString &sPassword, Csock *pSock)
Definition: Client.h:35
void PutModNotice(const CString &sModule, const CString &sLine)
Definition: IRCSock.h:33
bool HasUHNames() const
Definition: Client.h:114
bool HasServerTime() const
Definition: Client.h:116
Generate a grid-like output from a given input.
Definition: Utils.h:139
bool m_bGotUser
Definition: Client.h:160
CString m_sUser
Definition: Client.h:170
virtual void AcceptedLogin(CUser &User)=0
void HelpUser()
void AcceptLogin(CUser &User)