1 /*
2  * Copyright (C) 2004-2020 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_BUFFER_H
18 #define ZNC_BUFFER_H
19 
20 #include <znc/zncconfig.h>
21 #include <znc/ZNCString.h>
22 #include <znc/Message.h>
23 #include <sys/time.h>
24 #include <deque>
25 
26 // Forward Declarations
27 class CClient;
28 // !Forward Declarations
29 
30 class CBufLine {
31   public:
CBufLine()32     CBufLine() : CBufLine("") {
33         throw 0;
34     }  // shouldn't be called, but is needed for compilation
35     CBufLine(const CMessage& Format, const CString& sText = "");
36     /// @deprecated
37     CBufLine(const CString& sFormat, const CString& sText = "",
38              const timeval* ts = nullptr,
39              const MCString& mssTags = MCString::EmptyMap);
40     ~CBufLine();
41     CMessage ToMessage(const CClient& Client, const MCString& mssParams) const;
42     /// @deprecated Use ToMessage() instead
43     CString GetLine(const CClient& Client, const MCString& mssParams) const;
44     /// @deprecated
45     void UpdateTime();
46 
Equals(const CMessage & Format)47     bool Equals(const CMessage& Format) const {
48         return m_Message.Equals(Format);
49     }
50 
51     // Setters
SetFormat(const CString & sFormat)52     void SetFormat(const CString& sFormat) { m_Message.Parse(sFormat); }
SetText(const CString & sText)53     void SetText(const CString& sText) { m_sText = sText; }
SetTime(const timeval & ts)54     void SetTime(const timeval& ts) { m_Message.SetTime(ts); }
SetTags(const MCString & mssTags)55     void SetTags(const MCString& mssTags) { m_Message.SetTags(mssTags); }
56     // !Setters
57 
58     // Getters
GetCommand()59     const CString& GetCommand() const { return m_Message.GetCommand(); }
GetFormat()60     CString GetFormat() const {
61         return m_Message.ToString(CMessage::ExcludeTags);
62     }
GetText()63     const CString& GetText() const { return m_sText; }
GetTime()64     timeval GetTime() const { return m_Message.GetTime(); }
GetTags()65     const MCString& GetTags() const { return m_Message.GetTags(); }
66     // !Getters
67 
68   private:
69   protected:
70     CMessage m_Message;
71     CString m_sText;
72 };
73 
74 class CBuffer : private std::deque<CBufLine> {
75   public:
76     CBuffer(unsigned int uLineCount = 100);
77     ~CBuffer();
78 
79     size_type AddLine(const CMessage& Format, const CString& sText = "");
80     size_type UpdateLine(const CString& sCommand, const CMessage& Format,
81                          const CString& sText = "");
82     size_type UpdateExactLine(const CMessage& Format,
83                               const CString& sText = "");
84 
85     size_type AddLine(const CString& sFormat, const CString& sText = "",
86                       const timeval* ts = nullptr,
87                       const MCString& mssTags = MCString::EmptyMap);
88     /// Same as AddLine, but replaces a line whose format string starts with sMatch if there is one.
89     size_type UpdateLine(const CString& sMatch, const CString& sFormat,
90                          const CString& sText = "");
91     /// Same as UpdateLine, but does nothing if this exact line already exists.
92     /// We need this because "/version" sends us the 005 raws again
93     size_type UpdateExactLine(const CString& sFormat,
94                               const CString& sText = "");
95     const CBufLine& GetBufLine(unsigned int uIdx) const;
96     CString GetLine(size_type uIdx, const CClient& Client,
97                     const MCString& msParams = MCString::EmptyMap) const;
Size()98     size_type Size() const { return size(); }
IsEmpty()99     bool IsEmpty() const { return empty(); }
Clear()100     void Clear() { clear(); }
101 
102     // Setters
103     bool SetLineCount(unsigned int u, bool bForce = false);
104     // !Setters
105 
106     // Getters
GetLineCount()107     unsigned int GetLineCount() const { return m_uLineCount; }
108     // !Getters
109   private:
110   protected:
111     unsigned int m_uLineCount;
112 };
113 
114 #endif  // !ZNC_BUFFER_H
115