-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_only_detached.cpp
562 lines (457 loc) · 15.2 KB
/
watch_only_detached.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* Copyright (C) 2004-2009 See the AUTHORS file for details.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "Chan.h"
#include "User.h"
#include <list>
using std::list;
class CWatchSource {
public:
CWatchSource(const CString& sSource, bool bNegated) {
m_sSource = sSource;
m_bNegated = bNegated;
}
virtual ~CWatchSource() {}
// Getters
const CString& GetSource() const { return m_sSource; }
bool IsNegated() const { return m_bNegated; }
// !Getters
// Setters
// !Setters
private:
protected:
bool m_bNegated;
CString m_sSource;
};
class CWatchEntry {
public:
CWatchEntry(const CString& sHostMask, const CString& sTarget, const CString& sPattern) {
m_bDisabled = false;
m_sPattern = (sPattern.size()) ? sPattern : "*";
CNick Nick;
Nick.Parse(sHostMask);
m_sHostMask = (Nick.GetNick().size()) ? Nick.GetNick() : "*";
m_sHostMask += "!";
m_sHostMask += (Nick.GetIdent().size()) ? Nick.GetIdent() : "*";
m_sHostMask += "@";
m_sHostMask += (Nick.GetHost().size()) ? Nick.GetHost() : "*";
if (sTarget.size()) {
m_sTarget = sTarget;
} else {
m_sTarget = "$";
m_sTarget += Nick.GetNick();
}
}
virtual ~CWatchEntry() {}
bool IsMatch(const CNick& Nick, const CString& sText, const CString& sSource, const CUser* pUser) {
if (IsDisabled()) {
return false;
}
bool bGoodSource = true;
if (sSource.size() && m_vsSources.size()) {
bGoodSource = false;
for (unsigned int a = 0; a < m_vsSources.size(); a++) {
const CWatchSource& WatchSource = m_vsSources[a];
if (sSource.AsLower().WildCmp(WatchSource.GetSource().AsLower())) {
if (WatchSource.IsNegated()) {
return false;
} else {
bGoodSource = true;
}
}
}
}
if (!bGoodSource)
return false;
if (!Nick.GetHostMask().AsLower().WildCmp(m_sHostMask.AsLower()))
return false;
if (!sText.AsLower().WildCmp(pUser->ExpandString(m_sPattern).AsLower()))
return false;
return true;
}
bool operator ==(const CWatchEntry& WatchEntry) {
return (GetHostMask().Equals(WatchEntry.GetHostMask())
&& GetTarget().Equals(WatchEntry.GetTarget())
&& GetPattern().Equals(WatchEntry.GetPattern())
);
}
// Getters
const CString& GetHostMask() const { return m_sHostMask; }
const CString& GetTarget() const { return m_sTarget; }
const CString& GetPattern() const { return m_sPattern; }
bool IsDisabled() const { return m_bDisabled; }
const vector<CWatchSource>& GetSources() const { return m_vsSources; }
CString GetSourcesStr() const {
CString sRet;
for (unsigned int a = 0; a < m_vsSources.size(); a++) {
const CWatchSource& WatchSource = m_vsSources[a];
if (a) {
sRet += " ";
}
if (WatchSource.IsNegated()) {
sRet += "!";
}
sRet += WatchSource.GetSource();
}
return sRet;
}
// !Getters
// Setters
void SetHostMask(const CString& s) { m_sHostMask = s; }
void SetTarget(const CString& s) { m_sTarget = s; }
void SetPattern(const CString& s) { m_sPattern = s; }
void SetDisabled(bool b = true) { m_bDisabled = b; }
void SetSources(const CString& sSources) {
unsigned int uIdx = 1;
CString sSrc = sSources.Token(0);
m_vsSources.clear();
while (sSrc.size()) {
if (sSrc[0] == '!') {
if (sSrc.size() > 1) {
m_vsSources.push_back(CWatchSource(sSrc.substr(1), true));
}
} else {
m_vsSources.push_back(CWatchSource(sSrc, false));
}
sSrc = sSources.Token(uIdx++);
}
}
// !Setters
private:
protected:
CString m_sHostMask;
CString m_sTarget;
CString m_sPattern;
bool m_bDisabled;
vector<CWatchSource> m_vsSources;
};
class CWatcherMod : public CModule {
public:
MODCONSTRUCTOR(CWatcherMod) {
m_Buffer.SetLineCount(500);
Load();
}
virtual ~CWatcherMod() {}
virtual void OnRawMode(const CNick& OpNick, CChan& Channel, const CString& sModes, const CString& sArgs) {
Process(OpNick, "* " + OpNick.GetNick() + " sets mode: " + sModes + " " +
sArgs + " on " + Channel.GetName(), Channel.GetName());
}
virtual void OnClientLogin() {
CString sBufLine;
while (m_Buffer.GetNextLine(m_pUser->GetCurNick(), sBufLine)) {
PutUser(sBufLine);
}
m_Buffer.Clear();
}
virtual void OnKick(const CNick& OpNick, const CString& sKickedNick, CChan& Channel, const CString& sMessage) {
Process(OpNick, "* " + OpNick.GetNick() + " kicked " + sKickedNick + " from " +
Channel.GetName() + " because [" + sMessage + "]", Channel.GetName());
}
virtual void OnQuit(const CNick& Nick, const CString& sMessage, const vector<CChan*>& vChans) {
Process(Nick, "* Quits: " + Nick.GetNick() + " (" + Nick.GetIdent() + "@" + Nick.GetHost() + ") "
"(" + sMessage + ")", "");
}
virtual void OnJoin(const CNick& Nick, CChan& Channel) {
Process(Nick, "* " + Nick.GetNick() + " (" + Nick.GetIdent() + "@" + Nick.GetHost() + ") joins " +
Channel.GetName(), Channel.GetName());
}
virtual void OnPart(const CNick& Nick, CChan& Channel) {
Process(Nick, "* " + Nick.GetNick() + " (" + Nick.GetIdent() + "@" + Nick.GetHost() + ") parts " +
Channel.GetName(), Channel.GetName());
}
virtual void OnNick(const CNick& OldNick, const CString& sNewNick, const vector<CChan*>& vChans) {
Process(OldNick, "* " + OldNick.GetNick() + " is now known as " + sNewNick, "");
}
virtual EModRet OnCTCPReply(CNick& Nick, CString& sMessage) {
Process(Nick, "* CTCP: " + Nick.GetNick() + " reply [" + sMessage + "]", "priv");
return CONTINUE;
}
virtual EModRet OnPrivCTCP(CNick& Nick, CString& sMessage) {
Process(Nick, "* CTCP: " + Nick.GetNick() + " [" + sMessage + "]", "priv");
return CONTINUE;
}
virtual EModRet OnChanCTCP(CNick& Nick, CChan& Channel, CString& sMessage) {
Process(Nick, "* CTCP: " + Nick.GetNick() + " [" + sMessage + "] to "
"[" + Channel.GetName() + "]", Channel.GetName());
return CONTINUE;
}
virtual EModRet OnPrivNotice(CNick& Nick, CString& sMessage) {
Process(Nick, "-" + Nick.GetNick() + "- " + sMessage, "priv");
return CONTINUE;
}
virtual EModRet OnChanNotice(CNick& Nick, CChan& Channel, CString& sMessage) {
Process(Nick, "-" + Nick.GetNick() + ":" + Channel.GetName() + "- " + sMessage, Channel.GetName());
return CONTINUE;
}
virtual EModRet OnPrivMsg(CNick& Nick, CString& sMessage) {
Process(Nick, "<" + Nick.GetNick() + "> " + sMessage, "priv");
return CONTINUE;
}
virtual EModRet OnChanMsg(CNick& Nick, CChan& Channel, CString& sMessage) {
Process(Nick, "<" + Nick.GetNick() + ":" + Channel.GetName() + "> " + sMessage, Channel.GetName());
return CONTINUE;
}
virtual void OnModCommand(const CString& sCommand) {
CString sCmdName = sCommand.Token(0);
if (sCmdName.Equals("ADD") || sCmdName.Equals("WATCH")) {
Watch(sCommand.Token(1), sCommand.Token(2), sCommand.Token(3, true));
} else if (sCmdName.Equals("HELP")) {
Help();
} else if (sCmdName.Equals("LIST")) {
List();
} else if (sCmdName.Equals("DUMP")) {
Dump();
} else if (sCmdName.Equals("ENABLE")) {
CString sTok = sCommand.Token(1);
if (sTok == "*") {
SetDisabled(~0, false);
} else {
SetDisabled(sTok.ToUInt(), false);
}
} else if (sCmdName.Equals("DISABLE")) {
CString sTok = sCommand.Token(1);
if (sTok == "*") {
SetDisabled(~0, true);
} else {
SetDisabled(sTok.ToUInt(), true);
}
} else if (sCmdName.Equals("SETSOURCES")) {
SetSources(sCommand.Token(1).ToUInt(), sCommand.Token(2, true));
} else if (sCmdName.Equals("CLEAR")) {
m_lsWatchers.clear();
PutModule("All entries cleared.");
Save();
} else if (sCmdName.Equals("BUFFER")) {
CString sCount = sCommand.Token(1);
if (sCount.size()) {
m_Buffer.SetLineCount(sCount.ToUInt());
}
PutModule("Buffer count is set to [" + CString(m_Buffer.GetLineCount()) + "]");
} else if (sCmdName.Equals("DEL")) {
Remove(sCommand.Token(1).ToUInt());
} else {
PutModule("Unknown command: [" + sCmdName + "]");
}
}
private:
void Process(const CNick& Nick, const CString& sMessage, const CString& sSource) {
// Only watch if no user is connected
if (m_pUser->IsUserAttached())
return;
for (list<CWatchEntry>::iterator it = m_lsWatchers.begin(); it != m_lsWatchers.end(); it++) {
CWatchEntry& WatchEntry = *it;
if (WatchEntry.IsMatch(Nick, sMessage, sSource, m_pUser)) {
if (m_pUser->IsUserAttached()) {
m_pUser->PutUser(":" + WatchEntry.GetTarget() + "!watch@znc.in PRIVMSG " +
m_pUser->GetCurNick() + " :" + sMessage);
} else {
m_Buffer.AddLine(":" + WatchEntry.GetTarget() + "!watch@znc.in PRIVMSG ",
" :" + m_pUser->AddTimestamp(sMessage));
}
}
}
}
void SetDisabled(unsigned int uIdx, bool bDisabled) {
if (uIdx == (unsigned int) ~0) {
for (list<CWatchEntry>::iterator it = m_lsWatchers.begin(); it != m_lsWatchers.end(); it++) {
(*it).SetDisabled(bDisabled);
}
PutModule(((bDisabled) ? "Disabled all entries." : "Enabled all entries."));
Save();
return;
}
uIdx--; // "convert" index to zero based
if (uIdx >= m_lsWatchers.size()) {
PutModule("Invalid Id");
return;
}
list<CWatchEntry>::iterator it = m_lsWatchers.begin();
for (unsigned int a = 0; a < uIdx; a++)
it++;
(*it).SetDisabled(bDisabled);
PutModule("Id " + CString(uIdx +1) + ((bDisabled) ? " Disabled" : " Enabled"));
Save();
}
void List() {
CTable Table;
Table.AddColumn("Id");
Table.AddColumn("HostMask");
Table.AddColumn("Target");
Table.AddColumn("Pattern");
Table.AddColumn("Sources");
Table.AddColumn("Off");
unsigned int uIdx = 1;
for (list<CWatchEntry>::iterator it = m_lsWatchers.begin(); it != m_lsWatchers.end(); it++, uIdx++) {
CWatchEntry& WatchEntry = *it;
Table.AddRow();
Table.SetCell("Id", CString(uIdx));
Table.SetCell("HostMask", WatchEntry.GetHostMask());
Table.SetCell("Target", WatchEntry.GetTarget());
Table.SetCell("Pattern", WatchEntry.GetPattern());
Table.SetCell("Sources", WatchEntry.GetSourcesStr());
Table.SetCell("Off", (WatchEntry.IsDisabled()) ? "Off" : "");
}
if (Table.size()) {
PutModule(Table);
} else {
PutModule("You have no entries.");
}
}
void Dump() {
if (!m_lsWatchers.size()) {
PutModule("You have no entries.");
return;
}
PutModule("---------------");
PutModule("/msg " + GetModNick() + " CLEAR");
unsigned int uIdx = 1;
for (list<CWatchEntry>::iterator it = m_lsWatchers.begin(); it != m_lsWatchers.end(); it++, uIdx++) {
CWatchEntry& WatchEntry = *it;
PutModule("/msg " + GetModNick() + " ADD " + WatchEntry.GetHostMask() + " " +
WatchEntry.GetTarget() + " " + WatchEntry.GetPattern());
if (WatchEntry.GetSourcesStr().size()) {
PutModule("/msg " + GetModNick() + " SETSOURCES " + CString(uIdx) + " " +
WatchEntry.GetSourcesStr());
}
if (WatchEntry.IsDisabled()) {
PutModule("/msg " + GetModNick() + " DISABLE " + CString(uIdx));
}
}
PutModule("---------------");
}
void SetSources(unsigned int uIdx, const CString& sSources) {
uIdx--; // "convert" index to zero based
if (uIdx >= m_lsWatchers.size()) {
PutModule("Invalid Id");
return;
}
list<CWatchEntry>::iterator it = m_lsWatchers.begin();
for (unsigned int a = 0; a < uIdx; a++)
it++;
(*it).SetSources(sSources);
PutModule("Sources set for Id " + CString(uIdx +1) + ".");
Save();
}
void Remove(unsigned int uIdx) {
uIdx--; // "convert" index to zero based
if (uIdx >= m_lsWatchers.size()) {
PutModule("Invalid Id");
return;
}
list<CWatchEntry>::iterator it = m_lsWatchers.begin();
for (unsigned int a = 0; a < uIdx; a++)
it++;
m_lsWatchers.erase(it);
PutModule("Id " + CString(uIdx +1) + " Removed.");
Save();
}
void Help() {
CTable Table;
Table.AddColumn("Command");
Table.AddColumn("Description");
Table.AddRow();
Table.SetCell("Command", "Add <HostMask> [Target] [Pattern]");
Table.SetCell("Description", "Used to add an entry to watch for.");
Table.AddRow();
Table.SetCell("Command", "List");
Table.SetCell("Description", "List all entries being watched.");
Table.AddRow();
Table.SetCell("Command", "Dump");
Table.SetCell("Description", "Dump a list of all current entries to be used later.");
Table.AddRow();
Table.SetCell("Command", "Del <Id>");
Table.SetCell("Description", "Deletes Id from the list of watched entries.");
Table.AddRow();
Table.SetCell("Command", "Clear");
Table.SetCell("Description", "Delete all entries.");
Table.AddRow();
Table.SetCell("Command", "Enable <Id | *>");
Table.SetCell("Description", "Enable a disabled entry.");
Table.AddRow();
Table.SetCell("Command", "Disable <Id | *>");
Table.SetCell("Description", "Disable (but don't delete) an entry.");
Table.AddRow();
Table.SetCell("Command", "Buffer [Count]");
Table.SetCell("Description", "Show/Set the amount of buffered lines while detached.");
Table.AddRow();
Table.SetCell("Command", "SetSources <Id> [#chan priv #foo* !#bar]");
Table.SetCell("Description", "Set the source channels that you care about.");
Table.AddRow();
Table.SetCell("Command", "Help");
Table.SetCell("Description", "This help.");
PutModule(Table);
}
void Watch(const CString& sHostMask, const CString& sTarget, const CString& sPattern, bool bNotice = false) {
CString sMessage;
if (sHostMask.size()) {
CWatchEntry WatchEntry(sHostMask, sTarget, sPattern);
bool bExists = false;
for (list<CWatchEntry>::iterator it = m_lsWatchers.begin(); it != m_lsWatchers.end(); it++) {
if (*it == WatchEntry) {
sMessage = "Entry for [" + WatchEntry.GetHostMask() + "] already exists.";
bExists = true;
break;
}
}
if (!bExists) {
sMessage = "Adding entry: [" + WatchEntry.GetHostMask() + "] watching for "
"[" + WatchEntry.GetPattern() + "] -> [" + WatchEntry.GetTarget() + "]";
m_lsWatchers.push_back(WatchEntry);
}
} else {
sMessage = "Watch: Not enough arguments. Try Help";
}
if (bNotice) {
PutModNotice(sMessage);
} else {
PutModule(sMessage);
}
Save();
}
void Save() {
ClearNV(false);
for (list<CWatchEntry>::iterator it = m_lsWatchers.begin(); it != m_lsWatchers.end(); it++) {
CWatchEntry& WatchEntry = *it;
CString sSave;
sSave = WatchEntry.GetHostMask() + "\n";
sSave += WatchEntry.GetTarget() + "\n";
sSave += WatchEntry.GetPattern() + "\n";
sSave += (WatchEntry.IsDisabled() ? "disabled\n" : "enabled\n");
sSave += WatchEntry.GetSourcesStr();
// Without this, loading fails if GetSourcesStr()
// returns an empty string
sSave += " ";
SetNV(sSave, "", false);
}
SaveRegistry();
}
void Load() {
// Just to make sure we dont mess up badly
m_lsWatchers.clear();
bool bWarn = false;
for (MCString::iterator it = BeginNV(); it != EndNV(); it++) {
VCString vList;
it->first.Split("\n", vList);
if (vList.size() != 5) {
bWarn = true;
continue;
}
CWatchEntry WatchEntry(vList[0], vList[1], vList[2]);
if (vList[3].Equals("disabled"))
WatchEntry.SetDisabled(true);
else
WatchEntry.SetDisabled(false);
WatchEntry.SetSources(vList[4]);
m_lsWatchers.push_back(WatchEntry);
}
if (bWarn)
PutModule("WARNING: malformed entry found while loading");
}
list<CWatchEntry> m_lsWatchers;
CBuffer m_Buffer;
};
MODULEDEFS(CWatcherMod, "Copy activity from a specific user into a separate window")