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