This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.cpp
127 lines (106 loc) · 3.39 KB
/
Buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (C) 2004-2013 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <znc/znc.h>
#include <znc/User.h>
CBufLine::CBufLine(const CString& sFormat, const CString& sText, const timeval* ts) {
m_sFormat = sFormat;
m_sText = sText;
if (ts == NULL)
UpdateTime();
else
m_time = *ts;
}
CBufLine::~CBufLine() {}
void CBufLine::UpdateTime() {
if (0 == gettimeofday(&m_time, NULL)) {
return;
}
m_time.tv_sec = time(NULL);
m_time.tv_usec = 0;
}
CString CBufLine::GetLine(const CClient& Client, const MCString& msParams) const {
MCString msThisParams = msParams;
if (Client.HasServerTime()) {
msThisParams["text"] = m_sText;
CString sStr = CString::NamedFormat(m_sFormat, msThisParams);
CString s_msec(m_time.tv_usec / 1000);
while (s_msec.length() < 3) {
s_msec = "0" + s_msec;
}
// TODO support leap seconds properly
// TODO support message-tags properly
struct tm stm;
memset(&stm, 0, sizeof(stm));
const time_t secs = m_time.tv_sec; // OpenBSD has tv_sec as int, so explicitly convert it to time_t to make gmtime_r() happy
gmtime_r(&secs, &stm);
char sTime[20] = {};
strftime(sTime, sizeof(sTime), "%Y-%m-%dT%H:%M:%S", &stm);
return "@time=" + CString(sTime) + "." + s_msec + "Z " + sStr;
} else {
msThisParams["text"] = Client.GetUser()->AddTimestamp(m_time.tv_sec, m_sText);
return CString::NamedFormat(m_sFormat, msThisParams);
}
}
CBuffer::CBuffer(unsigned int uLineCount) {
m_uLineCount = uLineCount;
}
CBuffer::~CBuffer() {}
CBuffer::size_type CBuffer::AddLine(const CString& sFormat, const CString& sText, const timeval* ts) {
if (!m_uLineCount) {
return 0;
}
while (size() >= m_uLineCount) {
erase(begin());
}
push_back(CBufLine(sFormat, sText, ts));
return size();
}
CBuffer::size_type CBuffer::UpdateLine(const CString& sMatch, const CString& sFormat, const CString& sText) {
for (iterator it = begin(); it != end(); ++it) {
if (it->GetFormat().compare(0, sMatch.length(), sMatch) == 0) {
it->SetFormat(sFormat);
it->SetText(sText);
it->UpdateTime();
return size();
}
}
return AddLine(sFormat, sText);
}
CBuffer::size_type CBuffer::UpdateExactLine(const CString& sFormat, const CString& sText) {
for (iterator it = begin(); it != end(); ++it) {
if (it->GetFormat() == sFormat && it->GetText() == sText) {
return size();
}
}
return AddLine(sFormat, sText);
}
const CBufLine& CBuffer::GetBufLine(unsigned int uIdx) const {
return (*this)[uIdx];
}
CString CBuffer::GetLine(size_type uIdx, const CClient& Client, const MCString& msParams) const {
return (*this)[uIdx].GetLine(Client, msParams);
}
bool CBuffer::SetLineCount(unsigned int u, bool bForce) {
if (!bForce && u > CZNC::Get().GetMaxBufferSize()) {
return false;
}
m_uLineCount = u;
// We may need to shrink the buffer if the allowed size got smaller
while (size() > m_uLineCount) {
erase(begin());
}
return true;
}