-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmonitor.cpp
More file actions
375 lines (335 loc) · 12.7 KB
/
monitor.cpp
File metadata and controls
375 lines (335 loc) · 12.7 KB
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
// Initial version created by "KindOne @ irc.libera.chat".
//
// Module to support MONITOR.
// http://ircv3.org/specification/monitor-3.2
//
// TODO ...
//
//
// Input:
// - Adding:
// - Sanitizing checking.
// - Make the checking case insensitive.
// - Prevent nicks from starting with numbers/invalid characters.
// - Allow adding via Webadmin.
// - Don't go over "MONITOR=" limit.
// - Deleting:
// - Sanitizing checking.
// - Make the checking case insensitive.
// - Allow deleting via Webadmin.
//
// Output:
// - Allow people to customize the output of the module, without modifying/recompiling the code.
// - PutModule() from raw/numeric 730
//
// IRCd:
// - Only load/start on networks that support MONITOR.
// - Maybe: <*monitor> Error. This %network% does not support MONITOR.
// - When connecting/loading, add multiple nicks per "MONITOR +", instead of one per line. Excess floods.
// - Honor MONITOR=, from the 005, warn if we have more entries than the IRCd can allow.
// - Maybe only "MONITOR + ..." the first however many the IRCd supports.
//
// OnLoad/OnIRCConnected/OnModUnload:
// - Add option to enable/disable the "MONITOR C". Some people might already have nicks added, before loading.
// - OnLoad/OnIRCConnected send the same commands, combine them?
//
// Other:
// - Other things that I might of missed and/or can't think of.
//
#include <znc/IRCNetwork.h>
#include <znc/Modules.h>
using std::vector;
class CMonitor : public CModule {
public:
MODCONSTRUCTOR(CMonitor) {
AddHelpCommand();
AddCommand("Add", static_cast<CModCommand::ModCmdFunc>(&CMonitor::Add),
"<nick1>[,nick2[,...]]", "Add one or more nicks to monitor");
AddCommand("Del", static_cast<CModCommand::ModCmdFunc>(&CMonitor::Del),
"<id | nick1>[,nick2[,...]]", "Delete by ID or by nick list");
AddCommand("List", static_cast<CModCommand::ModCmdFunc>(&CMonitor::List),
"", "List all monitored nicks");
AddCommand("Reload", static_cast<CModCommand::ModCmdFunc>(&CMonitor::Reload),
"", "Reload nicks from storage and update server");
AddCommand("Swap", static_cast<CModCommand::ModCmdFunc>(&CMonitor::Swap),
"<id1> <id2>", "Swap two nicks by ID");
AddCommand("Sort", static_cast<CModCommand::ModCmdFunc>(&CMonitor::Sort),
"", "Sort nicks alphabetically and update server");
AddCommand("Clear", static_cast<CModCommand::ModCmdFunc>(&CMonitor::Clear),
"", "Remove all nicks from the list and server");
}
virtual ~CMonitor() {
}
bool OnLoad(const CString& sArgs, CString& sMessage) override {
GetNV("Monitor").Split("\n", m_vMonitor, false);
if (m_pNetwork->IsIRCConnected()) {
SendMonitorClear();
SendMonitorAdd(m_vMonitor);
}
return true;
}
void OnModuleUnloading() {
if (m_pNetwork->IsIRCConnected()) {
SendMonitorClear();
}
}
void OnIRCConnected() override {
SendMonitorAdd(m_vMonitor);
}
void OnISupport(CMessage& msg) {
for (const auto& param : msg.GetParams()) {
if (param.Token(0, false, "=").Equals("NICKLEN")) {
m_uNickLen = param.Token(1, true, "=").ToUInt();
if (m_uNickLen == 0) m_uNickLen = 30;
} else if (param.Token(0, false, "=").Equals("MONITOR")) {
m_uMonitorLimit = param.Token(1, true, "=").ToUInt();
}
}
}
EModRet OnNumericMessage(CNumericMessage& numeric) override {
switch (numeric.GetCode()) {
case 730: { // RPL_MONONLINE
// :irc.libera.chat 730 KindOne :EvilOne!KindOne@1.2.3.4
// Uncomment / comment the other one if you want to switch the output.
// Online: EvilOne!KindOne@1.2.3.4
// PutModule("Online: " + sLine.Token(3).TrimPrefix_n() + "");
// Online: EvilOne KindOne@1.2.3.4
PutModule("Online: " + numeric.GetParam(1).TrimPrefix_n().Replace_n("!", " "));
return HALT;
}
case 731: { // RPL_MONOFFLINE
// :irc.libera.chat 731 KindOne :EvilOne
// Offine: EvilOne
PutModule("Offline: " + numeric.GetParam(1).TrimPrefix_n());
return HALT;
}
case 732: { // RPL_MONLIST
// :irc.libera.chat 732 KindOne :EvilOne,EpicOne,KindTwo
VCString vsNicks;
numeric.GetParam(1).TrimPrefix_n().Split(",", vsNicks, false);
for (const CString& sNick : vsNicks) {
PutModule("List: " + sNick);
}
return HALT;
}
case 733: { // RPL_ENDOFMONLIST
// :irc.libera.chat 733 KindOne :End of MONITOR list
PutModule("End of MONITOR list.");
return HALT;
}
case 734: { // ERR_MONLISTFULL
// :irc.libera.chat 734 KindOne 100 Nick101,Nick102 :Monitor list is full
CString sMsg = "Error: Monitor list full. Cannot add: " +
numeric.GetParam(2).Replace_n(",", " ");
PutModule(sMsg);
return HALT;
}
return CONTINUE;
}
return CONTINUE;
}
private:
void Add(const CString& sCommand) {
CString sArgs = sCommand.Token(1, true);
if (sArgs.empty()) {
PutModule("Usage: add <nick>[,nick2[,...]]");
return;
}
VCString vsNewNicks;
sArgs.Split(",", vsNewNicks, false);
VCString vsValid;
VCString vsErrors;
for (CString& sNick : vsNewNicks) {
sNick.Trim();
if (std::find_if(m_vMonitor.begin(), m_vMonitor.end(),
[&](const CString& a) { return a.Equals(sNick); }) != m_vMonitor.end()) {
vsErrors.push_back(sNick + " (already in list)");
continue;
}
if (m_uMonitorLimit > 0 && m_vMonitor.size() + vsValid.size() >= m_uMonitorLimit) {
vsErrors.push_back(sNick + " (would exceed server limit)");
continue;
}
vsValid.push_back(sNick);
}
for (const CString& sNick : vsValid) {
m_vMonitor.push_back(sNick);
PutModule("Added " + sNick);
}
for (const CString& sErr : vsErrors) {
PutModule("Failed: " + sErr);
}
if (m_pNetwork->IsIRCConnected() && !vsValid.empty()) {
SendMonitorAdd(vsValid);
}
Save();
}
void Del(const CString& sCommand) {
CString sArgs = sCommand.Token(1, true);
if (sArgs.empty()) {
PutModule("Usage: del <id | nick1>[,nick2[,...]]");
return;
}
VCString vsNicks;
sArgs.Split(",", vsNicks, false);
// Trim all nicks and remove empty ones
VCString vsTrimmed;
for (CString& nick : vsNicks) {
nick.Trim();
if (!nick.empty())
vsTrimmed.push_back(nick);
}
if (vsTrimmed.empty()) {
PutModule("No valid nicks/IDs provided.");
return;
}
// If multiple items or the single item is not all digits, treat as nick list
bool bIsMulti = (vsTrimmed.size() > 1);
if (!bIsMulti && vsTrimmed.size() == 1) {
// Single item: could be ID (all digits) or a nick
const CString& sItem = vsTrimmed[0];
if (sItem.find_first_not_of("0123456789") == CString::npos) {
// All digits -> treat as ID
u_int iNum = sItem.ToUInt();
if (iNum == 0 || iNum > m_vMonitor.size()) {
PutModule("Invalid # requested");
return;
}
CString sRemoved = m_vMonitor[iNum - 1];
m_vMonitor.erase(m_vMonitor.begin() + iNum - 1);
if (m_pNetwork->IsIRCConnected())
SendMonitorDel({sRemoved});
PutModule("Removed " + sRemoved);
Save();
return;
}
// else fall through to nick deletion
}
// Delete by nick list (multi or single nick)
VCString vsRemoved;
VCString vsNotFound;
for (const CString& sNick : vsTrimmed) {
auto it = std::find_if(m_vMonitor.begin(), m_vMonitor.end(),
[&](const CString& a) { return a.Equals(sNick); });
if (it != m_vMonitor.end()) {
vsRemoved.push_back(*it);
m_vMonitor.erase(it);
} else {
vsNotFound.push_back(sNick);
}
}
if (!vsRemoved.empty()) {
if (m_pNetwork->IsIRCConnected())
SendMonitorDel(vsRemoved);
for (const CString& s : vsRemoved)
PutModule("Removed " + s);
}
for (const CString& s : vsNotFound)
PutModule("Not found: " + s);
Save();
}
void List(const CString& sCommand) {
CTable Table;
Table.AddColumn("Id");
Table.AddColumn("Nick");
unsigned int idx = 1;
for (const CString& sNick : m_vMonitor) {
Table.AddRow();
Table.SetCell("Id", CString(idx++));
Table.SetCell("Nick", sNick);
}
if (!PutModule(Table))
PutModule("No nicks found.");
}
void Reload(const CString& sCommand) {
m_vMonitor.clear();
GetNV("Monitor").Split("\n", m_vMonitor, false);
if (m_pNetwork->IsIRCConnected()) {
SendMonitorClear();
SendMonitorAdd(m_vMonitor);
}
PutModule("Reloaded " + CString(m_vMonitor.size()) + " nicks.");
}
void Swap(const CString& sCommand) {
u_int iA = sCommand.Token(1).ToUInt();
u_int iB = sCommand.Token(2).ToUInt();
if (iA == 0 || iA > m_vMonitor.size() || iB == 0 || iB > m_vMonitor.size()) {
PutModule("Illegal # requested");
return;
}
std::iter_swap(m_vMonitor.begin() + iA - 1, m_vMonitor.begin() + iB - 1);
PutModule("Swapped " + m_vMonitor[iA - 1] + " and " + m_vMonitor[iB - 1]);
Save();
}
void Sort(const CString& sCommand) {
std::sort(m_vMonitor.begin(), m_vMonitor.end(),
[](const CString& a, const CString& b) {
return a.AsLower() < b.AsLower();
});
PutModule("Nicks sorted.");
Save();
}
void Clear(const CString& sCommand) {
m_vMonitor.clear();
if (m_pNetwork->IsIRCConnected())
SendMonitorClear();
Save();
PutModule("Monitor list cleared.");
}
static const size_t MAX_LIST_LEN = 500; // 510 - len("MONITOR + ")
void SendMonitorAdd(const VCString& nicks) {
if (nicks.empty()) return;
CString current;
for (const CString& nick : nicks) {
size_t additional = nick.size() + (current.empty() ? 0 : 1); // comma if needed
if (current.size() + additional <= MAX_LIST_LEN) {
if (!current.empty()) current += ",";
current += nick;
} else {
// current batch is full, send it
PutIRC("MONITOR + " + current);
current = nick; // start new batch
}
}
if (!current.empty()) {
PutIRC("MONITOR + " + current);
}
}
void SendMonitorDel(const VCString& nicks) {
if (nicks.empty()) return;
CString current;
for (const CString& nick : nicks) {
size_t additional = nick.size() + (current.empty() ? 0 : 1);
if (current.size() + additional <= MAX_LIST_LEN) {
if (!current.empty()) current += ",";
current += nick;
} else {
PutIRC("MONITOR - " + current);
current = nick;
}
}
if (!current.empty()) {
PutIRC("MONITOR - " + current);
}
}
void SendMonitorClear() {
PutIRC("MONITOR C");
}
void Save() {
CString sBuffer;
for (const CString& sNick : m_vMonitor) {
sBuffer += sNick + "\n";
}
SetNV("Monitor", sBuffer);
}
VCString m_vMonitor;
unsigned int m_uNickLen = 30;
unsigned int m_uMonitorLimit = 0;
};
template <>
void TModInfo<CMonitor>(CModInfo& Info) {
// Info.SetWikiPage("monitor");
Info.SetHasArgs(false);
Info.AddType(CModInfo::NetworkModule);
}
NETWORKMODULEDEFS(CMonitor, "IRCv3 MONITOR support.")