forked from jpnurmi/znc-playback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playback.cpp
288 lines (261 loc) · 9.3 KB
/
playback.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (C) 2015 J-P Nurmi
*
* 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/Modules.h>
#include <znc/IRCNetwork.h>
#include <znc/Client.h>
#include <znc/Buffer.h>
#include <znc/Utils.h>
#include <znc/Query.h>
#include <znc/Chan.h>
#include <znc/znc.h>
#include <znc/version.h>
#include <sys/time.h>
#include <cfloat>
#if (VERSION_MAJOR < 1) || (VERSION_MAJOR == 1 && VERSION_MINOR < 6)
#error The playback module requires ZNC version 1.6.0 or later.
#endif
static const char* PlaybackCap = "znc.in/playback";
class CPlaybackMod : public CModule
{
public:
MODCONSTRUCTOR(CPlaybackMod)
{
m_play = false;
AddHelpCommand();
AddCommand("Clear", static_cast<CModCommand::ModCmdFunc>(&CPlaybackMod::ClearCommand), "<buffer(s)>", "Clears playback for given buffers.");
AddCommand("Play", static_cast<CModCommand::ModCmdFunc>(&CPlaybackMod::PlayCommand), "<buffer(s)> [from] [to]", "Sends playback for given buffers.");
AddCommand("List", static_cast<CModCommand::ModCmdFunc>(&CPlaybackMod::ListCommand), "[buffer(s)]", "Lists available/matching buffers.");
AddCommand("Limit", static_cast<CModCommand::ModCmdFunc>(&CPlaybackMod::LimitCommand), "<client> [limit]", "Get/set the buffer limit (<= 0 to clear) for the given client.");
}
void OnClientCapLs(CClient* client, SCString& caps) override
{
caps.insert(PlaybackCap);
}
bool IsClientCapSupported(CClient* client, const CString& cap, bool state) override
{
return cap.Equals(PlaybackCap);
}
EModRet OnChanBufferStarting(CChan& chan, CClient& client) override
{
if (!m_play && client.IsCapEnabled(PlaybackCap))
return HALTCORE;
return CONTINUE;
}
EModRet OnChanBufferPlayLine(CChan& chan, CClient& client, CString& line) override
{
if (!m_play && client.IsCapEnabled(PlaybackCap))
return HALTCORE;
return CONTINUE;
}
EModRet OnChanBufferEnding(CChan& chan, CClient& client) override
{
if (!m_play && client.IsCapEnabled(PlaybackCap))
return HALTCORE;
return CONTINUE;
}
EModRet OnPrivBufferPlayLine(CClient& client, CString& line) override
{
if (!m_play && client.IsCapEnabled(PlaybackCap))
return HALTCORE;
return CONTINUE;
}
void ClearCommand(const CString& line)
{
// CLEAR <buffer(s)>
const CString arg = line.Token(1);
if (arg.empty() || !line.Token(2).empty())
return;
std::vector<CChan*> chans = FindChans(arg);
for (CChan* chan : chans)
chan->ClearBuffer();
std::vector<CQuery*> queries = FindQueries(arg);
for (CQuery* query : queries)
query->ClearBuffer();
}
void PlayCommand(const CString& line)
{
// PLAY <buffer(s)> [from] [to]
const CString arg = line.Token(1);
if (arg.empty() || !line.Token(4).empty())
return;
double from = line.Token(2).ToDouble();
double to = DBL_MAX;
if (!line.Token(3).empty())
to = line.Token(3).ToDouble();
int limit = -1;
if (CClient* client = GetClient())
limit = GetLimit(client->GetIdentifier());
std::vector<CChan*> chans = FindChans(arg);
for (CChan* chan : chans) {
if (chan->IsOn() && !chan->IsDetached()) {
CBuffer lines = GetLines(chan->GetBuffer(), from, to, limit);
m_play = true;
chan->SendBuffer(GetClient(), lines);
m_play = false;
}
}
std::vector<CQuery*> queries = FindQueries(arg);
for (CQuery* query : queries) {
CBuffer lines = GetLines(query->GetBuffer(), from, to, limit);
m_play = true;
query->SendBuffer(GetClient(), lines);
m_play = false;
}
}
void ListCommand(const CString& line)
{
// LIST [buffer(s)]
CString arg = line.Token(1);
if (arg.empty())
arg = "*";
std::vector<CChan*> chans = FindChans(arg);
for (CChan* chan : chans) {
if (chan->IsOn() && !chan->IsDetached()) {
CBuffer buffer = chan->GetBuffer();
if (!buffer.IsEmpty()) {
timeval from = UniversalTime(buffer.GetBufLine(0).GetTime());
timeval to = UniversalTime(buffer.GetBufLine(buffer.Size() - 1).GetTime());
PutModule(chan->GetName() + " " + CString(Timestamp(from)) + " " + CString(Timestamp(to)));
}
}
}
std::vector<CQuery*> queries = FindQueries(arg);
for (CQuery* query : queries) {
CBuffer buffer = query->GetBuffer();
if (!buffer.IsEmpty()) {
timeval from = UniversalTime(buffer.GetBufLine(0).GetTime());
timeval to = UniversalTime(buffer.GetBufLine(buffer.Size() - 1).GetTime());
PutModule(query->GetName() + " " + CString(Timestamp(from)) + " " + CString(Timestamp(to)));
}
}
}
void LimitCommand(const CString& line)
{
// LIMIT <client> [limit]
const CString client = line.Token(1);
if (client.empty()) {
PutModule("Usage: LIMIT <client> [limit]");
return;
}
const CString arg = line.Token(2);
int limit = GetLimit(client);
if (!arg.empty()) {
limit = arg.ToInt();
SetLimit(client, limit);
}
if (limit <= 0)
PutModule(client + " buffer limit: -");
else
PutModule(client + " buffer limit: " + CString(limit));
}
EModRet OnSendToClient(CString& line, CClient& client) override
{
if (client.IsAttached() && client.IsCapEnabled(PlaybackCap) && !line.Token(0).Equals("CAP")) {
MCString tags = CUtils::GetMessageTags(line);
if (tags.find("time") == tags.end()) {
// CUtils::FormatServerTime() converts to UTC
tags["time"] = CUtils::FormatServerTime(LocalTime());
CUtils::SetMessageTags(line, tags);
}
}
return CONTINUE;
}
private:
static double Timestamp(timeval tv)
{
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
static timeval LocalTime()
{
timeval tv;
if (gettimeofday(&tv, NULL) == -1) {
tv.tv_sec = time(NULL);
tv.tv_usec = 0;
}
return tv;
}
static timeval UniversalTime(timeval tv = LocalTime())
{
tm stm;
memset(&stm, 0, sizeof(stm));
const time_t secs = tv.tv_sec; // OpenBSD has tv_sec as int, so explicitly convert it to time_t to make gmtime_r() happy
gmtime_r(&secs, &stm);
const char* tz = getenv("TZ");
setenv("TZ", "UTC", 1);
tzset();
tv.tv_sec = mktime(&stm);
if (tz)
setenv("TZ", tz, 1);
else
unsetenv("TZ");
tzset();
return tv;
}
std::vector<CChan*> FindChans(const CString& arg) const
{
std::vector<CChan*> chans;
CIRCNetwork* network = GetNetwork();
if (network) {
VCString vargs;
arg.Split(",", vargs, false);
for (const CString& name : vargs) {
std::vector<CChan*> found = network->FindChans(name);
chans.insert(chans.end(), found.begin(), found.end());
}
}
return chans;
}
std::vector<CQuery*> FindQueries(const CString& arg) const
{
std::vector<CQuery*> queries;
CIRCNetwork* network = GetNetwork();
if (network) {
VCString vargs;
arg.Split(",", vargs, false);
for (const CString& name : vargs) {
std::vector<CQuery*> found = network->FindQueries(name);
queries.insert(queries.end(), found.begin(), found.end());
}
}
return queries;
}
int GetLimit(const CString& client) const
{
return GetNV(client).ToInt();
}
void SetLimit(const CString& client, int limit)
{
if (limit > 0)
SetNV(client, CString(limit));
else
DelNV(client);
}
static CBuffer GetLines(const CBuffer& buffer, double from, double to, int limit)
{
CBuffer lines(buffer.Size());
for (size_t i = 0; i < buffer.Size(); ++i) {
const CBufLine& line = buffer.GetBufLine(i);
timeval tv = UniversalTime(line.GetTime());
if (from < Timestamp(tv) && to >= Timestamp(tv))
lines.AddLine(line.GetFormat(), line.GetText(), &tv);
}
if (limit > 0)
lines.SetLineCount(limit);
return lines;
}
bool m_play;
};
GLOBALMODULEDEFS(CPlaybackMod, "An advanced playback module for ZNC")