ZNC  trunk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WebModules.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_WEBMODULES_H
18 #define ZNC_WEBMODULES_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/Template.h>
22 #include <znc/HTTPSock.h>
23 #include <znc/Utils.h>
24 #include <znc/Translation.h>
25 
26 class CAuthBase;
27 class CUser;
28 class CWebSock;
29 class CModule;
31 
32 typedef std::shared_ptr<CWebSubPage> TWebSubPage;
33 typedef std::vector<TWebSubPage> VWebSubPages;
34 
36  public:
37  CZNCTagHandler(CWebSock& pWebSock);
38  virtual ~CZNCTagHandler() {}
39 
40  bool HandleTag(CTemplate& Tmpl, const CString& sName, const CString& sArgs,
41  CString& sOutput) override;
42 
43  private:
44  CWebSock& m_WebSock;
45 };
46 
47 class CWebSession {
48  public:
49  CWebSession(const CString& sId, const CString& sIP);
50  ~CWebSession();
51 
52  CWebSession(const CWebSession&) = delete;
53  CWebSession& operator=(const CWebSession&) = delete;
54 
55  const CString& GetId() const { return m_sId; }
56  const CString& GetIP() const { return m_sIP; }
57  CUser* GetUser() const { return m_pUser; }
58  time_t GetLastActive() const { return m_tmLastActive; }
59  bool IsLoggedIn() const { return m_pUser != nullptr; }
60  bool IsAdmin() const;
61  void UpdateLastActive();
62 
64  m_pUser = p;
65  return m_pUser;
66  }
67 
68  void ClearMessageLoops();
69  void FillMessageLoops(CTemplate& Tmpl);
70  size_t AddError(const CString& sMessage);
71  size_t AddSuccess(const CString& sMessage);
72 
73  private:
74  CString m_sId;
75  CString m_sIP;
76  CUser* m_pUser;
77  VCString m_vsErrorMsgs;
78  VCString m_vsSuccessMsgs;
79  time_t m_tmLastActive;
80 };
81 
82 class CWebSubPage {
83  public:
84  CWebSubPage(const CString& sName, const CString& sTitle = "",
85  unsigned int uFlags = 0)
86  : m_uFlags(uFlags), m_sName(sName), m_Title(sTitle), m_vParams() {}
87 
88  CWebSubPage(const CString& sName, const COptionalTranslation& Title,
89  const VPair& vParams, unsigned int uFlags = 0)
90  : m_uFlags(uFlags),
91  m_sName(sName),
92  m_Title(Title),
93  m_vParams(vParams) {}
94 
95  virtual ~CWebSubPage() {}
96 
97  enum { F_ADMIN = 1 };
98 
99  void SetName(const CString& s) { m_sName = s; }
101  m_Title = s;
102  }
103  void AddParam(const CString& sName, const CString& sValue) {
104  m_vParams.push_back(make_pair(sName, sValue));
105  }
106 
107  bool RequiresAdmin() const { return m_uFlags & F_ADMIN; }
108 
109  const CString& GetName() const { return m_sName; }
110  CString GetTitle() const { return m_Title.Resolve(); }
111  const VPair& GetParams() const { return m_vParams; }
112 
113  private:
114  unsigned int m_uFlags;
115  CString m_sName;
116  COptionalTranslation m_Title;
117  VPair m_vParams;
118 };
119 
120 class CWebSessionMap : public TCacheMap<CString, std::shared_ptr<CWebSession>> {
121  public:
122  CWebSessionMap(unsigned int uTTL = 5000)
123  : TCacheMap<CString, std::shared_ptr<CWebSession>>(uTTL) {}
124  void FinishUserSessions(const CUser& User);
125 };
126 
127 class CWebSock : public CHTTPSock {
128  public:
130  PAGE_NOTFOUND, // print 404 and Close()
131  PAGE_PRINT, // print page contents and Close()
132  PAGE_DEFERRED, // async processing, Close() will be called from a
133  // different place
134  PAGE_DONE // all stuff has been done
135  };
136 
137  CWebSock(const CString& sURIPrefix);
138  virtual ~CWebSock();
139 
140  bool ForceLogin() override;
141  bool OnLogin(const CString& sUser, const CString& sPass,
142  bool bBasic) override;
143  void OnPageRequest(const CString& sURI) override;
144 
145  EPageReqResult PrintTemplate(const CString& sPageName, CString& sPageRet,
146  CModule* pModule = nullptr);
147  EPageReqResult PrintStaticFile(const CString& sPath, CString& sPageRet,
148  CModule* pModule = nullptr);
149 
150  CString FindTmpl(CModule* pModule, const CString& sName);
151 
152  void PrintErrorPage(const CString& sMessage);
153 
154  std::shared_ptr<CWebSession> GetSession();
155 
156  Csock* GetSockObj(const CString& sHost, unsigned short uPort) override;
157  static CString GetSkinPath(const CString& sSkinName);
158  void GetAvailSkins(VCString& vRet) const;
160 
161  CString GetRequestCookie(const CString& sKey);
162  bool SendCookie(const CString& sKey, const CString& sValue);
163 
164  static void FinishUserSessions(const CUser& User);
165 
167  bool ValidateCSRFCheck(const CString& sURI);
168 
169  protected:
171 
172  bool AddModLoop(const CString& sLoopName, CModule& Module,
173  CTemplate* pTemplate = nullptr);
174  VCString GetDirs(CModule* pModule, bool bIsTemplate);
175  void SetPaths(CModule* pModule, bool bIsTemplate = false);
176  void SetVars();
177 
178  private:
179  EPageReqResult OnPageRequestInternal(const CString& sURI,
180  CString& sPageRet);
181 
182  bool m_bPathsSet;
183  CTemplate m_Template;
184  std::shared_ptr<CAuthBase> m_spAuth;
185  CString m_sModName;
186  CString m_sPath;
187  CString m_sPage;
188  std::shared_ptr<CWebSession> m_spSession;
189 
190  static const unsigned int m_uiMaxSessions;
191 };
192 
193 #endif // !ZNC_WEBMODULES_H
void UpdateLastActive()
bool ValidateCSRFCheck(const CString &sURI)
Definition: User.h:37
bool HandleTag(CTemplate &Tmpl, const CString &sName, const CString &sArgs, CString &sOutput) override
virtual ~CZNCTagHandler()
Definition: WebModules.h:38
VCString GetDirs(CModule *pModule, bool bIsTemplate)
static CString GetSkinPath(const CString &sSkinName)
void SetVars()
Definition: WebModules.h:97
const CString & GetIP() const
Definition: WebModules.h:56
CWebSubPage(const CString &sName, const CString &sTitle="", unsigned int uFlags=0)
Definition: WebModules.h:84
CString FindTmpl(CModule *pModule, const CString &sName)
Definition: WebModules.h:47
void ClearMessageLoops()
size_t AddSuccess(const CString &sMessage)
Definition: WebModules.h:132
EPageReqResult
Definition: WebModules.h:129
bool SendCookie(const CString &sKey, const CString &sValue)
void OnPageRequest(const CString &sURI) override
void SetTitle(const COptionalTranslation &s)
Definition: WebModules.h:100
CString GetTitle() const
Definition: WebModules.h:110
void GetAvailSkins(VCString &vRet) const
const CString & GetId() const
Definition: WebModules.h:55
virtual ~CWebSubPage()
Definition: WebModules.h:95
void SetName(const CString &s)
Definition: WebModules.h:99
const CString & GetName() const
Definition: WebModules.h:109
Definition: Translation.h:84
bool PrintErrorPage(unsigned int uStatusId, const CString &sStatusMsg, const CString &sMessage)
CString Resolve() const
Definition: Translation.h:91
std::vector< TWebSubPage > VWebSubPages
Definition: WebModules.h:33
bool AddModLoop(const CString &sLoopName, CModule &Module, CTemplate *pTemplate=nullptr)
Definition: WebModules.h:134
void SetPaths(CModule *pModule, bool bIsTemplate=false)
Definition: Template.h:28
EPageReqResult PrintStaticFile(const CString &sPath, CString &sPageRet, CModule *pModule=nullptr)
Basic socket class.
Definition: Csocket.h:564
std::shared_ptr< CWebSubPage > TWebSubPage
Definition: WebModules.h:30
Definition: Template.h:129
Definition: WebModules.h:82
bool IsLoggedIn() const
Definition: WebModules.h:59
std::vector< CString > VCString
Definition: ZNCString.h:38
String class that is used inside ZNC.
Definition: ZNCString.h:68
bool ForceLogin() override
size_t AddError(const CString &sMessage)
CWebSubPage(const CString &sName, const COptionalTranslation &Title, const VPair &vParams, unsigned int uFlags=0)
Definition: WebModules.h:88
CUser * SetUser(CUser *p)
Definition: WebModules.h:63
The base class for your own ZNC modules.
Definition: Modules.h:407
CWebSession(const CString &sId, const CString &sIP)
void PrintErrorPage(const CString &sMessage)
time_t GetLastActive() const
Definition: WebModules.h:58
static void FinishUserSessions(const CUser &User)
Definition: WebModules.h:127
EPageReqResult PrintTemplate(const CString &sPageName, CString &sPageRet, CModule *pModule=nullptr)
Definition: Client.h:38
void AddParam(const CString &sName, const CString &sValue)
Definition: WebModules.h:103
CWebSession & operator=(const CWebSession &)=delete
const VPair & GetParams() const
Definition: WebModules.h:111
CString GetSkinName()
Insert an object with a time-to-live and check later if it still exists.
Definition: Utils.h:265
bool OnLogin(const CString &sUser, const CString &sPass, bool bBasic) override
void FinishUserSessions(const CUser &User)
void FillMessageLoops(CTemplate &Tmpl)
CString GetCSRFCheck()
std::vector< std::pair< CString, CString > > VPair
Definition: ZNCString.h:39
Definition: WebModules.h:35
bool RequiresAdmin() const
Definition: WebModules.h:107
Definition: WebModules.h:130
virtual ~CWebSock()
bool IsAdmin() const
CWebSock(const CString &sURIPrefix)
CString GetRequestCookie(const CString &sKey)
CUser * GetUser() const
Definition: WebModules.h:57
CZNCTagHandler(CWebSock &pWebSock)
Definition: WebModules.h:120
Csock * GetSockObj(const CString &sHost, unsigned short uPort) override
std::shared_ptr< CWebSession > GetSession()
Definition: WebModules.h:131
Definition: HTTPSock.h:26
CWebSessionMap(unsigned int uTTL=5000)
Definition: WebModules.h:122