ZNC  trunk
WebModules.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_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;
30 class CWebSubPage;
31 
32 typedef std::shared_ptr<CWebSubPage> TWebSubPage;
33 typedef std::vector<TWebSubPage> VWebSubPages;
34 
36  public:
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);
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;
62 
64  m_pUser = p;
65  return m_pUser;
66  }
67 
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);
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 
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
std::vector< TWebSubPage > VWebSubPages
Definition: WebModules.h:33
std::shared_ptr< CWebSubPage > TWebSubPage
Definition: WebModules.h:30
std::vector< std::pair< CString, CString > > VPair
Definition: ZNCString.h:39
std::vector< CString > VCString
Definition: ZNCString.h:38
Definition: Client.h:38
Definition: HTTPSock.h:26
bool PrintErrorPage(unsigned int uStatusId, const CString &sStatusMsg, const CString &sMessage)
The base class for your own ZNC modules.
Definition: Modules.h:420
Definition: Translation.h:85
CString Resolve() const
Definition: Translation.h:90
String class that is used inside ZNC.
Definition: ZNCString.h:68
Definition: Template.h:28
Definition: Template.h:129
Definition: User.h:38
Definition: WebModules.h:120
void FinishUserSessions(const CUser &User)
CWebSessionMap(unsigned int uTTL=5000)
Definition: WebModules.h:122
Definition: WebModules.h:47
CWebSession & operator=(const CWebSession &)=delete
bool IsAdmin() const
size_t AddError(const CString &sMessage)
size_t AddSuccess(const CString &sMessage)
CWebSession(const CWebSession &)=delete
const CString & GetId() const
Definition: WebModules.h:55
void FillMessageLoops(CTemplate &Tmpl)
CUser * SetUser(CUser *p)
Definition: WebModules.h:63
CUser * GetUser() const
Definition: WebModules.h:57
CWebSession(const CString &sId, const CString &sIP)
time_t GetLastActive() const
Definition: WebModules.h:58
void UpdateLastActive()
void ClearMessageLoops()
const CString & GetIP() const
Definition: WebModules.h:56
bool IsLoggedIn() const
Definition: WebModules.h:59
Definition: WebModules.h:127
bool ForceLogin() override
bool SendCookie(const CString &sKey, const CString &sValue)
void SetVars()
CString GetRequestCookie(const CString &sKey)
EPageReqResult PrintTemplate(const CString &sPageName, CString &sPageRet, CModule *pModule=nullptr)
void SetPaths(CModule *pModule, bool bIsTemplate=false)
CWebSock(const CString &sURIPrefix)
std::shared_ptr< CWebSession > GetSession()
virtual ~CWebSock()
void PrintErrorPage(const CString &sMessage)
bool ValidateCSRFCheck(const CString &sURI)
bool OnLogin(const CString &sUser, const CString &sPass, bool bBasic) override
CString FindTmpl(CModule *pModule, const CString &sName)
static CString GetSkinPath(const CString &sSkinName)
Csock * GetSockObj(const CString &sHost, unsigned short uPort) override
EPageReqResult
Definition: WebModules.h:129
@ PAGE_NOTFOUND
Definition: WebModules.h:130
@ PAGE_DONE
Definition: WebModules.h:134
@ PAGE_PRINT
Definition: WebModules.h:131
@ PAGE_DEFERRED
Definition: WebModules.h:132
bool AddModLoop(const CString &sLoopName, CModule &Module, CTemplate *pTemplate=nullptr)
VCString GetDirs(CModule *pModule, bool bIsTemplate)
static void FinishUserSessions(const CUser &User)
EPageReqResult PrintStaticFile(const CString &sPath, CString &sPageRet, CModule *pModule=nullptr)
CString GetSkinName()
void OnPageRequest(const CString &sURI) override
void GetAvailSkins(VCString &vRet) const
CString GetCSRFCheck()
Definition: WebModules.h:82
CString GetTitle() const
Definition: WebModules.h:110
virtual ~CWebSubPage()
Definition: WebModules.h:95
@ F_ADMIN
Definition: WebModules.h:97
const VPair & GetParams() const
Definition: WebModules.h:111
CWebSubPage(const CString &sName, const CString &sTitle="", unsigned int uFlags=0)
Definition: WebModules.h:84
const CString & GetName() const
Definition: WebModules.h:109
CWebSubPage(const CString &sName, const COptionalTranslation &Title, const VPair &vParams, unsigned int uFlags=0)
Definition: WebModules.h:88
bool RequiresAdmin() const
Definition: WebModules.h:107
void SetTitle(const COptionalTranslation &s)
Definition: WebModules.h:100
void SetName(const CString &s)
Definition: WebModules.h:99
void AddParam(const CString &sName, const CString &sValue)
Definition: WebModules.h:103
Definition: WebModules.h:35
virtual ~CZNCTagHandler()
Definition: WebModules.h:38
CZNCTagHandler(CWebSock &pWebSock)
bool HandleTag(CTemplate &Tmpl, const CString &sName, const CString &sArgs, CString &sOutput) override
Basic socket class.
Definition: Csocket.h:564
Insert an object with a time-to-live and check later if it still exists.
Definition: Utils.h:291