Coverage Report

Created: 2026-01-10 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/znc/include/znc/WebModules.h
Line
Count
Source
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
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
35
class CZNCTagHandler : public CTemplateTagHandler {
36
  public:
37
    CZNCTagHandler(CWebSock& pWebSock);
38
0
    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
0
    const CString& GetId() const { return m_sId; }
56
0
    const CString& GetIP() const { return m_sIP; }
57
0
    CUser* GetUser() const { return m_pUser; }
58
0
    time_t GetLastActive() const { return m_tmLastActive; }
59
0
    bool IsLoggedIn() const { return m_pUser != nullptr; }
60
    bool IsAdmin() const;
61
    void UpdateLastActive();
62
63
0
    CUser* SetUser(CUser* p) {
64
0
        m_pUser = p;
65
0
        return m_pUser;
66
0
    }
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
0
        : 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
0
          m_vParams(vParams) {}
94
95
0
    virtual ~CWebSubPage() {}
96
97
    enum { F_ADMIN = 1 };
98
99
0
    void SetName(const CString& s) { m_sName = s; }
100
0
    void SetTitle(const COptionalTranslation& s) {
101
0
        m_Title = s;
102
0
    }
103
0
    void AddParam(const CString& sName, const CString& sValue) {
104
0
        m_vParams.push_back(make_pair(sName, sValue));
105
0
    }
106
107
0
    bool RequiresAdmin() const { return m_uFlags & F_ADMIN; }
108
109
0
    const CString& GetName() const { return m_sName; }
110
0
    CString GetTitle() const { return m_Title.Resolve(); }
111
0
    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
2
        : TCacheMap<CString, std::shared_ptr<CWebSession>>(uTTL) {}
124
    void FinishUserSessions(const CUser& User);
125
};
126
127
class CWebSock : public CHTTPSock {
128
  public:
129
    enum EPageReqResult {
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;
159
    CString GetSkinName();
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
166
    CString GetCSRFCheck();
167
    bool ValidateCSRFCheck(const CString& sURI);
168
169
  protected:
170
    using CHTTPSock::PrintErrorPage;
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