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