-
Notifications
You must be signed in to change notification settings - Fork 6
/
os_swhois.cpp
738 lines (614 loc) · 18.7 KB
/
os_swhois.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
* OperServ SWhois
*
* (C) 2017-2023 - Matt Schatz ([email protected])
* Please refer to the GPL License in use by Anope at:
* https://github.com/anope/anope/blob/master/docs/COPYING
* Based off (but written from scratch for 2.0.x and) the previous OS_SWHOIS by
* azander: https://modules.anope.org/index.php?page=view&id=273
*
* Assign SWhois messages to users. You can configure it to only allow one SWhois per NickCore (Group)
* or to allow a separate SWhois per Nick Alias (the 'useaccount' parameter).
*
* Syntax: SWHOIS ADD nick swhois
* DEL {nick | entry-num | list}
* LIST | VIEW [nick | entry-num | list]
* CLEAR [nick]
*
* Configuration to put into your operserv config:
module { name = "os_swhois"; useaccount = "no"; notifyonadd = "yes"; notifyonlogin = "yes"; }
command { service = "OperServ"; name = "SWHOIS"; command = "operserv/swhois"; permission = "operserv/swhois"; }
*
* Don't forget to add 'operserv/swhois' to your oper permissions
*/
#include "module.h"
namespace
{
bool useaccount;
BotInfo *OperServ;
}
/* Individual SWhois entry */
struct SWhoisEntry : Serializable
{
public:
Anope::string core;
Anope::string nick;
Anope::string swhois;
Anope::string creator;
time_t created;
SWhoisEntry() : Serializable("SWhois") { }
SWhoisEntry(const Anope::string &c, const Anope::string &n, const Anope::string &s, const Anope::string &cr, time_t cd)
: Serializable("SWhois"), core(c), nick(n), swhois(s), creator(cr), created(cd) { }
~SWhoisEntry();
void Serialize(Serialize::Data &data) const anope_override
{
data["core"] << this->core;
data["nick"] << this->nick;
data["swhois"] << this->swhois;
data["creator"] << this->creator;
data["created"] << this->created;
}
static Serializable* Unserialize(Serializable *obj, Serialize::Data &data);
};
/* List of SWhois entries */
class SWhoisList
{
protected:
Serialize::Checker<std::vector<SWhoisEntry *> > entries;
public:
SWhoisList() : entries("SWhois") { }
~SWhoisList()
{
for (unsigned i = entries->size(); i > 0; --i)
delete entries->at(i - 1);
}
void Add(SWhoisEntry *entry)
{
/* Group Nick Aliases together in the list for better listing */
std::vector<SWhoisEntry *>::iterator it;
for (it = entries->begin(); it != entries->end(); ++it)
{
if ((*it)->core == entry->core)
break;
}
if (it != entries->end())
entries->insert(it, entry);
else
entries->insert(entries->begin(), entry);
}
void Del(const SWhoisEntry *entry)
{
std::vector<SWhoisEntry *>::iterator it = std::find(entries->begin(), entries->end(), entry);
if (it == entries->end())
return;
const User *u = User::Find(entry->nick);
if (u)
IRCD->SendSWhois(OperServ, u->nick, "");
entries->erase(it);
}
bool Del(const Anope::string &nick)
{
for (std::vector<SWhoisEntry *>::const_iterator it = entries->begin(), it_end = entries->end(); it != it_end; ++it)
{
if ((*it)->nick == nick)
{
delete (*it);
return true;
}
}
return false;
}
bool Del(const NickCore *nc)
{
bool existed = false;
for (unsigned i = entries->size(); i > 0; --i)
{
const SWhoisEntry *entry = entries->at(i - 1);
if (entry->core == nc->display)
{
delete entry;
existed = true;
}
}
return existed;
}
/* Delete any non-display Nicks from a NickCore's list */
void DelAliases()
{
for (unsigned i = entries->size(); i > 0; --i)
{
const SWhoisEntry *entry = entries->at(i - 1);
if (entry->core != entry->nick)
delete entry;
}
}
void Clear()
{
for (unsigned i = entries->size(); i > 0; --i)
delete (*entries).at(i - 1);
}
unsigned GetCount()
{
return entries->size();
}
void Update(SWhoisEntry *entry)
{
entry->QueueUpdate();
}
SWhoisEntry *GetEntry(const unsigned number)
{
if (number >= entries->size())
return NULL;
return (*entries).at(number);
}
SWhoisEntry *GetEntry(const Anope::string &nick)
{
for (std::vector<SWhoisEntry *>::const_iterator it = entries->begin(), it_end = entries->end(); it != it_end; ++it)
{
if (nick.equals_ci((*it)->nick))
return *it;
}
return NULL;
}
const std::vector<SWhoisEntry *> GetEntriesByCore(const NickCore *nc)
{
std::vector<SWhoisEntry *> core_entries;
for (std::vector<SWhoisEntry *>::const_iterator it = entries->begin(), it_end = entries->end(); it != it_end; ++it)
{
if ((*it)->core == nc->display)
core_entries.push_back(*it);
}
return core_entries;
}
const std::vector<SWhoisEntry *> &GetEntries()
{
return *entries;
}
}
SWhoisList;
SWhoisEntry::~SWhoisEntry()
{
SWhoisList.Del(this);
}
Serializable* SWhoisEntry::Unserialize(Serializable *obj, Serialize::Data &data)
{
SWhoisEntry *entry;
if (obj)
entry = anope_dynamic_static_cast<SWhoisEntry *>(obj);
else
entry = new SWhoisEntry();
data["core"] >> entry->core;
data["nick"] >> entry->nick;
data["swhois"] >> entry->swhois;
data["creator"] >> entry->creator;
data["created"] >> entry->created;
if (!obj)
SWhoisList.Add(entry);
return entry;
}
class SWhoisDelCallback : public NumberList
{
CommandSource &source;
unsigned deleted;
Command *cmd;
public:
SWhoisDelCallback(CommandSource &_source, const Anope::string &numlist, Command *c) : NumberList(numlist, true), source(_source), deleted(0), cmd(c) { }
~SWhoisDelCallback()
{
if (!deleted)
{
source.Reply("No matching entries on the SWhois list.");
return;
}
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
if (deleted == 1)
source.Reply("Deleted 1 entry from the SWhois list.");
else
source.Reply("Deleted %d entries from the SWhois list.", deleted);
}
void HandleNumber(unsigned number) anope_override
{
if (!number || number > SWhoisList.GetCount())
return;
const SWhoisEntry *entry = SWhoisList.GetEntry(number - 1);
if (!entry)
return;
Log(LOG_ADMIN, source, cmd) << "to remove " << entry->nick << " from the list";
delete entry;
++deleted;
}
};
class CommandOSSWhois : public Command
{
private:
void DoAdd(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
if (params.size() < 3)
{
this->OnSyntaxError(source, "ADD");
return;
}
const NickAlias* na = NickAlias::Find(params[1]);
if (!na || !na->nc)
{
source.Reply("Nick %s is not registered.", params[1].c_str());
return;
}
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
const NickCore *nc = na->nc;
Anope::string nick = na->nick;
if (useaccount && nick != nc->display)
nick = nc->display;
const Anope::string &swhois = params[2];
bool created = true;
if (SWhoisList.Del(nick))
created = false;
SWhoisEntry *entry = new SWhoisEntry(nc->display, nick, swhois, source.GetNick(), Anope::CurTime);
SWhoisList.Add(entry);
User *u = User::Find(nick);
if (!u && nick != params[1])
u = User::Find(params[1]);
if (u && u->IsIdentified(true))
{
IRCD->SendSWhois(OperServ, u->nick, swhois);
if (Config->GetModule(this->module)->Get<bool>("notifyonadd", "yes"))
u->SendMessage(OperServ, "A SWhois has been set on you: %s", swhois.c_str());
}
Log(LOG_ADMIN, source, this) << "to " << (created ? "add" : "modify") << " a SWhois message on " << nick;
source.Reply("%s a SWhois message on %s", (created ? "Added" : "Modified"), nick.c_str());
}
void DoDel(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
const Anope::string &match = params.size() > 1 ? params[1] : "";
if (match.empty())
{
this->OnSyntaxError(source, "DEL");
return;
}
if (SWhoisList.GetCount() == 0)
{
source.Reply("The SWhois list is empty.");
return;
}
if (isdigit(match[0]) && match.find_first_not_of("1234567890,-") == Anope::string::npos)
{
SWhoisDelCallback list(source, match, this);
list.Process();
}
else
{
const NickAlias *na = NickAlias::Find(match);
if (!na || !na->nc)
{
source.Reply("%s is not a valid Nick Alias.", match.c_str());
return;
}
if (!SWhoisList.Del(match))
{
source.Reply("The Nick Alias %s was not found on the SWhois list.", match.c_str());
return;
}
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
Log(LOG_ADMIN, source, this) << "to delete " << match << " from the SWhois list";
source.Reply("\002%s\002 deleted from the SWhois list.", match.c_str());
}
}
void ProcessList(CommandSource &source, const std::vector<Anope::string> ¶ms, ListFormatter &list)
{
const Anope::string &match = params.size() > 1 ? params[1] : "";
if (!match.empty() && isdigit(match[0]) && match.find_first_not_of("1234567890,-") == Anope::string::npos)
{
class ListCallback : public NumberList
{
CommandSource &source;
ListFormatter &list;
public:
ListCallback(CommandSource &_source, ListFormatter &_list, const Anope::string &numstr) : NumberList(numstr, false), source(_source), list(_list) { }
void HandleNumber(unsigned number) anope_override
{
if (!number || number > SWhoisList.GetCount())
return;
const SWhoisEntry *s_entry = SWhoisList.GetEntry(number - 1);
if (!s_entry)
return;
ListFormatter::ListEntry entry;
entry["Number"] = stringify(number);
entry["Group"] = s_entry->core;
entry["Nick"] = s_entry->nick;
entry["SWhois"] = s_entry->swhois;
entry["Creator"] = s_entry->creator;
entry["Created"] = Anope::strftime(s_entry->created, source.nc, true);
list.AddEntry(entry);
}
}
nl_list(source, list, match);
nl_list.Process();
}
else
{
const std::vector<SWhoisEntry *> &entries = SWhoisList.GetEntries();
for (unsigned i = 0; i < entries.size(); ++i)
{
const SWhoisEntry *s_entry = entries.at(i);
if (!s_entry)
continue;
if (match.empty() || match.equals_ci(s_entry->nick) || Anope::Match(s_entry->nick, match) || match.equals_ci(s_entry->core) || Anope::Match(s_entry->core, match))
{
ListFormatter::ListEntry entry;
entry["Number"] = stringify(i + 1);
entry["Group"] = s_entry->core;
entry["Nick"] = s_entry->nick;
entry["SWhois"] = s_entry->swhois;
entry["Creator"] = s_entry->creator;
entry["Created"] = Anope::strftime(s_entry->created, source.nc, true);
list.AddEntry(entry);
}
}
}
if (list.IsEmpty())
source.Reply("No matching entries on the SWhois list.");
else
{
source.Reply("Current SWhois list:");
std::vector<Anope::string> replies;
list.Process(replies);
for (unsigned i = 0; i < replies.size(); ++i)
source.Reply(replies[i]);
source.Reply("End of SWhois list.");
}
}
void DoList(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
if (SWhoisList.GetCount() == 0)
{
source.Reply("The SWhois list is empty.");
return;
}
ListFormatter list(source.GetAccount());
list.AddColumn("Number").AddColumn("Group");
if (!useaccount)
list.AddColumn("Nick");
list.AddColumn("SWhois");
this->ProcessList(source, params, list);
}
void DoView(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
if (SWhoisList.GetCount() == 0)
{
source.Reply("The SWhois list is empty.");
return;
}
ListFormatter list(source.GetAccount());
list.AddColumn("Number").AddColumn("Group");
if (!useaccount)
list.AddColumn("Nick");
list.AddColumn("SWhois").AddColumn("Creator").AddColumn("Created");
this->ProcessList(source, params, list);
}
void DoClear(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
if (SWhoisList.GetCount() == 0)
{
source.Reply("The SWhois list is empty.");
return;
}
if (params.size() > 2)
{
this->OnSyntaxError(source, "CLEAR");
return;
}
else if (params.size() == 2)
{
const NickAlias *na = NickAlias::Find(params[1]);
if (!na || !na->nc)
{
source.Reply("%s is not a valid Nick Alias.", params[1].c_str());
return;
}
if (!SWhoisList.Del(na->nc))
{
source.Reply("The group of %s was not found on the SWhois list.", na->nc->display.c_str());
return;
}
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
Log(LOG_ADMIN, source, this) << "to clear the group of " << na->nc->display << " from the SWhois list";
source.Reply("The group of %s has been cleared from the SWhois list.", na->nc->display.c_str());
}
else
{
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
SWhoisList.Clear();
Log(LOG_ADMIN, source, this) << "to clear the SWhois list";
source.Reply("The SWhois list has been cleared.");
}
}
public:
CommandOSSWhois(Module *creator) : Command(creator, "operserv/swhois", 1, 3)
{
this->SetDesc("Manipulate the SWhois list");
this->SetSyntax("ADD \037nick\037 \037swhois\037");
this->SetSyntax("DEL {\037nick\037 | \037entry-num\037 | \037list\037}");
this->SetSyntax("LIST [\037nick\037 | \037entry-num\037 | \037list\037]");
this->SetSyntax("VIEW [\037nick\037 | \037entry-num\037 | \037list\037]");
this->SetSyntax("CLEAR [\037nick\037]");
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
const Anope::string &subcmd = params[0];
if (subcmd.equals_ci("ADD"))
this->DoAdd(source, params);
else if (subcmd.equals_ci("DEL"))
this->DoDel(source, params);
else if (subcmd.equals_ci("LIST"))
this->DoList(source, params);
else if (subcmd.equals_ci("VIEW"))
this->DoView(source, params);
else if (subcmd.equals_ci("CLEAR"))
this->DoClear(source, params);
else
this->OnSyntaxError(source, "");
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply("Manipulate the SWhois messages assigned to nicks.");
if (useaccount)
{
source.Reply("This network restricts the SWhois to one per Nick Group (account).");
source.Reply(" ");
source.Reply("The \002ADD\002 command will assign the given SWhois to the Group that \037nick\037 belongs to.\n"
"The \002DEL\002 command will delete the assigned SWhois from the \037nick\037 Group.\n"
"The \002LIST\002 command with no parameters will list all nick Groups with a SWhois\n"
"assigned to them (and the SWhois message). \002VIEW\002 is more detailed.");
}
else
{
source.Reply("This network allows a SWhois to be assigned to each Nick Alias (grouped nicks).");
source.Reply(" ");
source.Reply("The \002ADD\002 command will assign the given SWhois to the \037nick\037 Alias.\n"
"The \002DEL\002 command will delete the assigned SWhois from the \037nick\037 Alias.\n"
"The \002LIST\002 command with no parameters will list all Nicks with a SWhois\n"
"assigned to them (and the SWhois message). \002VIEW\002 is more detailed.");
}
source.Reply("You can filter this with \037nick\037, \037entry number\037, or a \037list\037 (1-3 or 1,3 format).");
if (useaccount)
source.Reply("The \002CLEAR\002 command clears all assigned SWhois messages.");
else
source.Reply("The \002CLEAR\002 command can be given a \037nick\037 to clear all assigned\n"
"SWhois' from that Nick Group. Otherwise it will clear all SWhois messages.");
return true;
}
void OnSyntaxError(CommandSource &source, const Anope::string &subcommand) anope_override
{
if (subcommand.equals_ci("ADD"))
source.Reply("ADD \037nick\037 \037swhois\037");
else if (subcommand.equals_ci("DEL"))
source.Reply("DEL {\037nick\037 | \037entry-num\037 | \037list\037}");
else if (subcommand.equals_ci("CLEAR"))
source.Reply("CLEAR [\037nick\037]");
else
this->SendSyntax(source);
}
};
class OSSWhois : public Module
{
Serialize::Type swhoisentry_type;
CommandOSSWhois commandosswhois;
void SetSWhois(User *u, const SWhoisEntry *entry)
{
IRCD->SendSWhois(OperServ, u->nick, entry->swhois);
if (Config->GetModule(this)->Get<bool>("notifyonlogin", "yes"))
u->SendMessage(OperServ, "Your SWhois has been set: %s", entry->swhois.c_str());
}
void UnSetSWhois(const User *u)
{
IRCD->SendSWhois(OperServ, u->nick, "");
}
public:
OSSWhois(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, THIRD),
swhoisentry_type("SWhois", SWhoisEntry::Unserialize), commandosswhois(this)
{
if (Anope::VersionMajor() != 2 || Anope::VersionMinor() != 0)
throw ModuleException("Requires version 2.0.x of Anope.");
this->SetAuthor("genius3000");
this->SetVersion("1.0.1");
}
void OnReload(Configuration::Conf *conf) anope_override
{
bool old_useaccount = useaccount;
useaccount = conf->GetModule(this)->Get<bool>("useaccount", "no");
OperServ = conf->GetClient("OperServ");
/* Changed to single per account, remove any not set to the Group display */
if (useaccount && !old_useaccount)
SWhoisList.DelAliases();
}
void OnUserLogin(User *u) anope_override
{
if (u->Quitting())
return;
SWhoisEntry *entry;
if (useaccount)
{
entry = SWhoisList.GetEntry(u->Account()->display);
}
else
{
const NickAlias *na = NickAlias::Find(u->nick);
if (na)
entry = SWhoisList.GetEntry(na->nick);
}
if (entry)
SetSWhois(u, entry);
}
void OnNickLogout(User *u) anope_override
{
if (u->Quitting())
return;
const NickAlias *na = NickAlias::Find(u->nick);
if (na && (SWhoisList.GetEntry(na->nick)))
UnSetSWhois(u);
}
void OnUserNickChange(User *u, const Anope::string &oldnick) anope_override
{
if (u->Quitting() || useaccount)
return;
SWhoisEntry *entry;
const NickAlias *na = NickAlias::Find(u->nick);
const NickAlias *ona = NickAlias::Find(oldnick);
if (na && (entry = SWhoisList.GetEntry(na->nick)))
SetSWhois(u, entry);
else if (ona && (SWhoisList.GetEntry(ona->nick)))
UnSetSWhois(u);
}
void OnDelNick(NickAlias *na) anope_override
{
if (na->nc)
SWhoisList.Del(na->nick);
}
void OnDelCore(NickCore *nc) anope_override
{
SWhoisList.Del(nc);
}
void OnChangeCoreDisplay(NickCore *nc, const Anope::string &newdisplay)
{
if (SWhoisList.GetCount() == 0)
return;
const std::vector<SWhoisEntry *> &entries = SWhoisList.GetEntriesByCore(nc);
if (entries.empty())
return;
for (std::vector<SWhoisEntry *>::const_iterator it = entries.begin(), it_end = entries.end(); it != it_end; ++it)
{
SWhoisEntry *entry = *it;
if (useaccount && entry->nick == entry->core)
entry->nick = newdisplay;
entry->core = newdisplay;
SWhoisList.Update(entry);
}
}
/* Hacky way to catch an Ungroup and update a SWhoisEntry if needed */
void OnNickCoreCreate(NickCore *nc) anope_override
{
if (SWhoisList.GetCount() == 0)
return;
SWhoisEntry *entry = SWhoisList.GetEntry(nc->display);
if (!entry)
return;
entry->core = nc->display;
SWhoisList.Update(entry);
}
void OnNickInfo(CommandSource &source, NickAlias *na, InfoFormatter &info, bool show_hidden) anope_override
{
if (!show_hidden)
return;
const SWhoisEntry *entry = SWhoisList.GetEntry(na->nick);
if (entry)
info["SWhois"] = entry->swhois;
}
};
MODULE_INIT(OSSWhois)