This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
clientbuffer.cpp
311 lines (272 loc) · 10.3 KB
/
clientbuffer.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* 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/Utils.h>
#include <znc/User.h>
#include <znc/Chan.h>
#include <znc/znc.h>
#include <znc/version.h>
#include <sys/time.h>
#if (VERSION_MAJOR < 1) || (VERSION_MAJOR == 1 && VERSION_MINOR < 6)
#error The clientbuffer module requires ZNC version 1.6.0 or later.
#endif
class CClientBufferMod : public CModule
{
public:
MODCONSTRUCTOR(CClientBufferMod)
{
AddHelpCommand();
AddCommand("AddClient", static_cast<CModCommand::ModCmdFunc>(&CClientBufferMod::OnAddClientCommand), "<identifier>", "Add a client.");
AddCommand("DelClient", static_cast<CModCommand::ModCmdFunc>(&CClientBufferMod::OnDelClientCommand), "<identifier>", "Delete a client.");
AddCommand("ListClients", static_cast<CModCommand::ModCmdFunc>(&CClientBufferMod::OnListClientsCommand), "", "List known clients.");
}
void OnAddClientCommand(const CString& line);
void OnDelClientCommand(const CString& line);
void OnListClientsCommand(const CString& line);
virtual void OnClientLogin();
virtual EModRet OnUserRaw(CString& line) override;
virtual EModRet OnSendToClient(CString& line, CClient& client) override;
virtual EModRet OnChanBufferStarting(CChan& chan, CClient& client) override;
virtual EModRet OnChanBufferEnding(CChan& chan, CClient& client) override;
virtual EModRet OnChanBufferPlayLine2(CChan& chan, CClient& client, CString& line, const timeval& tv) override;
virtual EModRet OnPrivBufferPlayLine2(CClient& client, CString& line, const timeval& tv) override;
private:
bool AddClient(const CString& identifier);
bool DelClient(const CString& identifier);
bool HasClient(const CString& identifier);
bool ParseMessage(const CString& line, CNick& nick, CString& cmd, CString& target) const;
timeval GetTimestamp(const CString& identifier, const CString& target);
timeval GetTimestamp(const CBuffer& buffer) const;
bool HasSeenTimestamp(const CString& identifier, const CString& target, const timeval& tv);
bool UpdateTimestamp(const CString& identifier, const CString& target, const timeval& tv);
void UpdateTimestamp(const CClient* client, const CString& target);
};
void CClientBufferMod::OnAddClientCommand(const CString& line)
{
const CString identifier = line.Token(1);
if (identifier.empty()) {
PutModule("Usage: AddClient <identifier>");
return;
}
if (HasClient(identifier)) {
PutModule("Client already exists: " + identifier);
return;
}
AddClient(identifier);
PutModule("Client added: " + identifier);
}
void CClientBufferMod::OnDelClientCommand(const CString& line)
{
const CString identifier = line.Token(1);
if (identifier.empty()) {
PutModule("Usage: DelClient <identifier>");
return;
}
if (!HasClient(identifier)) {
PutModule("Unknown client: " + identifier);
return;
}
DelClient(identifier);
PutModule("Client removed: " + identifier);
}
void CClientBufferMod::OnListClientsCommand(const CString& line)
{
const CString& current = GetClient()->GetIdentifier();
CTable table;
table.AddColumn("Client");
table.AddColumn("Connected");
for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) {
if (it->first.Find("/") == CString::npos) {
table.AddRow();
if (it->first == current)
table.SetCell("Client", "*" + it->first);
else
table.SetCell("Client", it->first);
table.SetCell("Connected", CString(!GetNetwork()->FindClients(it->first).empty()));
}
}
if (table.empty())
PutModule("No identified clients");
else
PutModule(table);
}
void CClientBufferMod::OnClientLogin()
{
const CString& current = GetClient()->GetIdentifier();
if (!HasClient(current) && GetArgs().Token(0).Equals("autoadd", CString::CaseInsensitive)) {
AddClient(current);
}
}
CModule::EModRet CClientBufferMod::OnUserRaw(CString& line)
{
CClient* client = GetClient();
if (client) {
CNick nick; CString cmd, target;
// make sure not to update the timestamp for a channel when joining it
if (ParseMessage(line, nick, cmd, target) && !cmd.Equals("JOIN"))
UpdateTimestamp(client, target);
}
return CONTINUE;
}
CModule::EModRet CClientBufferMod::OnSendToClient(CString& line, CClient& client)
{
CIRCNetwork* network = GetNetwork();
if (network) {
CNick nick; CString cmd, target;
// make sure not to update the timestamp for a channel when attaching it
if (ParseMessage(line, nick, cmd, target)) {
CChan* chan = network->FindChan(target);
if (!chan || !chan->IsDetached())
UpdateTimestamp(&client, target);
}
}
return CONTINUE;
}
CModule::EModRet CClientBufferMod::OnChanBufferStarting(CChan& chan, CClient& client)
{
if (client.HasServerTime())
return HALTCORE;
const CString& identifier = client.GetIdentifier();
if (HasClient(identifier)) {
// let "Buffer Playback..." message through?
const CBuffer& buffer = chan.GetBuffer();
if (!buffer.IsEmpty() && HasSeenTimestamp(identifier, chan.GetName(), GetTimestamp(buffer)))
return HALTCORE;
}
return CONTINUE;
}
CModule::EModRet CClientBufferMod::OnChanBufferEnding(CChan& chan, CClient& client)
{
if (client.HasServerTime())
return HALTCORE;
const CString& identifier = client.GetIdentifier();
if (HasClient(identifier)) {
// let "Buffer Complete" message through?
const CBuffer& buffer = chan.GetBuffer();
if (!buffer.IsEmpty() && !UpdateTimestamp(identifier, chan.GetName(), GetTimestamp(buffer)))
return HALTCORE;
}
return CONTINUE;
}
CModule::EModRet CClientBufferMod::OnChanBufferPlayLine2(CChan& chan, CClient& client, CString& line, const timeval& tv)
{
const CString& identifier = client.GetIdentifier();
if (HasClient(identifier) && HasSeenTimestamp(identifier, chan.GetName(), tv))
return HALTCORE;
return CONTINUE;
}
CModule::EModRet CClientBufferMod::OnPrivBufferPlayLine2(CClient& client, CString& line, const timeval& tv)
{
const CString& identifier = client.GetIdentifier();
if (HasClient(identifier)) {
CNick nick; CString cmd, target;
if (ParseMessage(line, nick, cmd, target) && !UpdateTimestamp(identifier, target, tv))
return HALTCORE;
}
return CONTINUE;
}
bool CClientBufferMod::AddClient(const CString& identifier)
{
return SetNV(identifier, "");
}
bool CClientBufferMod::DelClient(const CString& identifier)
{
SCString keys;
for (MCString::iterator it = BeginNV(); it != EndNV(); ++it) {
const CString client = it->first.Token(0, false, "/");
if (client.Equals(identifier))
keys.insert(it->first);
}
bool success = true;
for (const CString& key : keys)
success &= DelNV(key);
return success;
}
bool CClientBufferMod::HasClient(const CString& identifier)
{
return !identifier.empty() && FindNV(identifier) != EndNV();
}
bool CClientBufferMod::ParseMessage(const CString& line, CNick& nick, CString& cmd, CString& target) const
{
// discard message tags
CString msg = line;
if (msg.StartsWith("@"))
msg = msg.Token(1, true);
CString rest;
if (msg.StartsWith(":")) {
nick = CNick(msg.Token(0).TrimPrefix_n());
cmd = msg.Token(1);
rest = msg.Token(2, true);
} else {
cmd = msg.Token(0);
rest = msg.Token(1, true);
}
if (cmd.length() == 3 && isdigit(cmd[0]) && isdigit(cmd[1]) && isdigit(cmd[2])) {
// must block the following numeric replies that are automatically sent on attach:
// RPL_NAMREPLY, RPL_ENDOFNAMES, RPL_TOPIC, RPL_TOPICWHOTIME...
unsigned int num = cmd.ToUInt();
if (num == 353) // RPL_NAMREPLY
target = rest.Token(2);
else
target = rest.Token(1);
} else if (cmd.Equals("PRIVMSG") || cmd.Equals("NOTICE") || cmd.Equals("JOIN") || cmd.Equals("PART") || cmd.Equals("MODE") || cmd.Equals("KICK") || cmd.Equals("TOPIC")) {
target = rest.Token(0).TrimPrefix_n(":");
}
return !target.empty() && !cmd.empty();
}
timeval CClientBufferMod::GetTimestamp(const CString& identifier, const CString& target)
{
timeval tv;
double timestamp = GetNV(identifier + "/" + target).ToDouble();
tv.tv_sec = timestamp;
tv.tv_usec = (timestamp - tv.tv_sec) * 1000000;
return tv;
}
timeval CClientBufferMod::GetTimestamp(const CBuffer& buffer) const
{
return buffer.GetBufLine(buffer.Size() - 1).GetTime();
}
bool CClientBufferMod::HasSeenTimestamp(const CString& identifier, const CString& target, const timeval& tv)
{
const timeval seen = GetTimestamp(identifier, target);
return timercmp(&seen, &tv, >);
}
bool CClientBufferMod::UpdateTimestamp(const CString& identifier, const CString& target, const timeval& tv)
{
if (!HasSeenTimestamp(identifier, target, tv)) {
double timestamp = tv.tv_sec + tv.tv_usec / 1000000.0;
return SetNV(identifier + "/" + target, CString(timestamp));
}
return false;
}
void CClientBufferMod::UpdateTimestamp(const CClient* client, const CString& target)
{
if (client && !client->IsPlaybackActive()) {
const CString& identifier = client->GetIdentifier();
if (HasClient(identifier)) {
timeval tv;
gettimeofday(&tv, NULL);
UpdateTimestamp(identifier, target, tv);
}
}
}
template<> void TModInfo<CClientBufferMod>(CModInfo& info) {
info.SetWikiPage("Clientbuffer");
info.SetHasArgs(true);
}
NETWORKMODULEDEFS(CClientBufferMod, "Client specific buffer playback")