~ubuntu-branches/ubuntu/trusty/znc/trusty

« back to all changes in this revision

Viewing changes to src/Buffer.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-05-06 09:18:27 UTC
  • mfrom: (21.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20130506091827-08sixjiyy3hjfx6b
Tags: 1.0-4
* Change section from znc-tcl to interpreters.
* Uploading to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004-2012  See the AUTHORS file for details.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 2 as published
 
6
 * by the Free Software Foundation.
 
7
 */
 
8
 
 
9
#include <znc/Buffer.h>
 
10
#include <znc/znc.h>
 
11
#include <znc/Client.h>
 
12
#include <znc/User.h>
 
13
 
 
14
CBufLine::CBufLine(const CString& sFormat, const CString& sText, const timeval* ts) {
 
15
        m_sFormat = sFormat;
 
16
        m_sText = sText;
 
17
        if (ts == NULL)
 
18
                UpdateTime();
 
19
        else
 
20
                m_time = *ts;
 
21
}
 
22
 
 
23
CBufLine::~CBufLine() {}
 
24
 
 
25
void CBufLine::UpdateTime() {
 
26
        if (0 == gettimeofday(&m_time, NULL)) {
 
27
                return;
 
28
        }
 
29
        m_time.tv_sec = time(NULL);
 
30
        m_time.tv_usec = 0;
 
31
}
 
32
 
 
33
CString CBufLine::GetLine(const CClient& Client, const MCString& msParams) const {
 
34
        MCString msThisParams = msParams;
 
35
 
 
36
        if (Client.HasServerTime()) {
 
37
                msThisParams["text"] = m_sText;
 
38
                CString sStr = CString::NamedFormat(m_sFormat, msThisParams);
 
39
                CString s_msec(m_time.tv_usec / 1000);
 
40
                while (s_msec.length() < 3) {
 
41
                        s_msec = "0" + s_msec;
 
42
                }
 
43
                // TODO support message-tags properly
 
44
                return "@time=" + CString(m_time.tv_sec) + "." + s_msec + " " + sStr;
 
45
        } else {
 
46
                msThisParams["text"] = Client.GetUser()->AddTimestamp(m_time.tv_sec, m_sText);
 
47
                return CString::NamedFormat(m_sFormat, msThisParams);
 
48
        }
 
49
}
 
50
 
 
51
CBuffer::CBuffer(unsigned int uLineCount) {
 
52
        m_uLineCount = uLineCount;
 
53
}
 
54
 
 
55
CBuffer::~CBuffer() {}
 
56
 
 
57
CBuffer::size_type CBuffer::AddLine(const CString& sFormat, const CString& sText, const timeval* ts) {
 
58
        if (!m_uLineCount) {
 
59
                return 0;
 
60
        }
 
61
 
 
62
        while (size() >= m_uLineCount) {
 
63
                erase(begin());
 
64
        }
 
65
 
 
66
        push_back(CBufLine(sFormat, sText, ts));
 
67
        return size();
 
68
}
 
69
 
 
70
CBuffer::size_type CBuffer::UpdateLine(const CString& sMatch, const CString& sFormat, const CString& sText) {
 
71
        for (iterator it = begin(); it != end(); ++it) {
 
72
                if (it->GetFormat().compare(0, sMatch.length(), sMatch) == 0) {
 
73
                        it->SetFormat(sFormat);
 
74
                        it->SetText(sText);
 
75
                        it->UpdateTime();
 
76
                        return size();
 
77
                }
 
78
        }
 
79
 
 
80
        return AddLine(sFormat, sText);
 
81
}
 
82
 
 
83
CBuffer::size_type CBuffer::UpdateExactLine(const CString& sFormat, const CString& sText) {
 
84
        for (iterator it = begin(); it != end(); ++it) {
 
85
                if (it->GetFormat() == sFormat && it->GetText() == sText) {
 
86
                        return size();
 
87
                }
 
88
        }
 
89
 
 
90
        return AddLine(sFormat, sText);
 
91
}
 
92
 
 
93
const CBufLine& CBuffer::GetBufLine(unsigned int uIdx) const {
 
94
        return (*this)[uIdx];
 
95
}
 
96
 
 
97
CString CBuffer::GetLine(size_type uIdx, const CClient& Client, const MCString& msParams) const {
 
98
        return (*this)[uIdx].GetLine(Client, msParams);
 
99
}
 
100
 
 
101
bool CBuffer::SetLineCount(unsigned int u, bool bForce) {
 
102
        if (!bForce && u > CZNC::Get().GetMaxBufferSize()) {
 
103
                return false;
 
104
        }
 
105
 
 
106
        m_uLineCount = u;
 
107
 
 
108
        // We may need to shrink the buffer if the allowed size got smaller
 
109
        while (size() > m_uLineCount) {
 
110
                erase(begin());
 
111
        }
 
112
 
 
113
        return true;
 
114
}