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
|
/*
* Anope IRC Services
*
* Copyright (C) 2017 Anope Team <team@anope.org>
*
* This file is part of Anope. Anope is free software; you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see see <http://www.gnu.org/licenses/>.
*/
#include "module.h"
class BanImpl : public HostServ::Ban
{
friend class BanType;
Serialize::Storage<NickServ::Account *> account;
Serialize::Storage<Anope::string> creator, reason;
Serialize::Storage<time_t> created, expires;
public:
using HostServ::Ban::Ban;
NickServ::Account *GetAccount() override;
void SetAccount(NickServ::Account *) override;
Anope::string GetCreator() override;
void SetCreator(const Anope::string &) override;
time_t GetCreated() override;
void SetCreated(time_t) override;
time_t GetExpires() override;
void SetExpires(time_t) override;
Anope::string GetReason() override;
void SetReason(const Anope::string &) override;
};
class BanType : public Serialize::Type<BanImpl>
{
public:
Serialize::ObjectField<BanImpl, NickServ::Account *> account;
Serialize::Field<BanImpl, Anope::string> creator;
Serialize::Field<BanImpl, time_t> created, expires;
Serialize::Field<BanImpl, Anope::string> reason;
BanType(Module *me) : Serialize::Type<BanImpl>(me)
, account(this, "account", &BanImpl::account, true)
, creator(this, "creator", &BanImpl::creator)
, created(this, "created", &BanImpl::created)
, expires(this, "expires", &BanImpl::expires)
, reason(this, "reason", &BanImpl::reason)
{
}
};
NickServ::Account *BanImpl::GetAccount()
{
return Get(&BanType::account);
}
void BanImpl::SetAccount(NickServ::Account *acc)
{
Set(&BanType::account, acc);
}
Anope::string BanImpl::GetCreator()
{
return Get(&BanType::creator);
}
void BanImpl::SetCreator(const Anope::string &c)
{
Set(&BanType::creator, c);
}
time_t BanImpl::GetCreated()
{
return Get(&BanType::created);
}
void BanImpl::SetCreated(time_t cr)
{
Set(&BanType::created, cr);
}
time_t BanImpl::GetExpires()
{
return Get(&BanType::expires);
}
void BanImpl::SetExpires(time_t ex)
{
Set(&BanType::expires, ex);
}
Anope::string BanImpl::GetReason()
{
return Get(&BanType::reason);
}
void BanImpl::SetReason(const Anope::string &r)
{
Set(&BanType::reason, r);
}
class CommandHSBan : public Command
{
public:
CommandHSBan(Module *creator) : Command(creator, "hostserv/ban", 2, 3)
{
this->SetDesc(_("Ban a user from requesting vhosts"));
this->SetSyntax(_("\037user\037 [+\037expiry\037] \037reason\037"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
const Anope::string &user = params[0];
Anope::string expiry, reason;
if (params[1][0] == '+')
{
expiry = params[1];
reason = params.size() > 2 ? params[2] : "";
}
else
{
reason = params[2];
if (params.size() > 3)
reason += " " + params[3];
}
time_t expires = !expiry.empty() ? Anope::DoTime(expiry) : Config->GetModule(this->GetOwner())->Get<time_t>("eexpiry", "30d");
if (reason.empty())
{
this->OnSyntaxError(source, "");
return;
}
if (expires && expires < 60)
{
source.Reply(_("Invalid expiry time \002{0}\002."), expiry);
return;
}
if (expires > 0)
{
expires += Anope::CurTime;
}
NickServ::Nick *nick = NickServ::FindNick(user);
if (nick == nullptr)
{
source.Reply(_("\002{0}\002 isn't registered."), user);
return;
}
HostServ::Ban *ban = nick->GetAccount()->GetRef<HostServ::Ban *>();
if (ban != nullptr)
{
source.Reply(_("\002{0}\002 is already banned from requesting vhosts."), nick->GetAccount()->GetDisplay());
return;
}
ban = Serialize::New<HostServ::Ban *>();
ban->SetAccount(nick->GetAccount());
ban->SetCreator(source.GetNick());
ban->SetCreated(Anope::CurTime);
ban->SetExpires(expires);
ban->SetReason(reason);
logger.Command(LogType::ADMIN, source, _("{source} used {command} to ban {0} from requesting new vhosts"), nick->GetAccount()->GetDisplay());
source.Reply(_("\002{0}\002 has been banned from requesting new vhosts."), nick->GetAccount()->GetDisplay());
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
{
source.Reply(_("Ban the given \037user\037 from requesting vhosts for the given duration."));
return true;
}
};
class CommandHSUnban : public Command
{
public:
CommandHSUnban(Module *creator) : Command(creator, "hostserv/unban", 1, 1)
{
this->SetDesc(_("Unban a user from requesting vhosts"));
this->SetSyntax(_("\037user\037"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
const Anope::string &user = params[0];
if (user.find_first_not_of("1234567890,-") == Anope::string::npos)
{
std::vector<HostServ::Ban *> bans = Serialize::GetObjects<HostServ::Ban *>();
unsigned int deleted = 0;
Anope::string nicks;
NumberList(user, true,
[&](unsigned int num)
{
if (num == 0 || num > bans.size())
return;
HostServ::Ban *ban = bans[num - 1];
if (!nicks.empty())
nicks.append(", ");
nicks.append(ban->GetAccount()->GetDisplay());
ban->Delete();
++deleted;
},
[&]()
{
if (deleted == 0)
{
source.Reply(_("No matching entries on ban list."));
return;
}
logger.Command(LogType::ADMIN, source, _("{source} used {command} to unban {0} from requesting new vhosts"), nicks);
source.Reply(_("Deleted \002{0}\002 entries from the ban list."), deleted);
});
}
else
{
NickServ::Nick *nick = NickServ::FindNick(user);
if (nick == nullptr)
{
source.Reply(_("\002{0}\002 isn't registered."), user);
return;
}
HostServ::Ban *ban = nick->GetAccount()->GetRef<HostServ::Ban *>();
if (ban == nullptr)
{
source.Reply(_("\002{0}\002 isn't banned."), nick->GetNick());
return;
}
logger.Command(LogType::ADMIN, source, _("{source} used {command} to unban {0} from requesting new vhosts"), nick->GetAccount()->GetDisplay());
source.Reply(_("\002{0}\002 has been unbanned from requesting new vhosts."), nick->GetAccount()->GetDisplay());
ban->Delete();
}
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
{
source.Reply(_("Unban the given \037user\037 from requesting vhosts."));
return true;
}
};
class CommandHSBanlist : public Command
{
public:
CommandHSBanlist(Module *creator) : Command(creator, "hostserv/banlist", 0, 1)
{
this->SetDesc(_("View the list of banned users"));
this->SetSyntax(_("[\037search\037]"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) override
{
const Anope::string &search = params.size() > 0 ? params[0] : "";
ListFormatter list(source.GetAccount());
list.AddColumn(_("Number"))
.AddColumn(_("Nick"))
.AddColumn(_("Expires"))
.AddColumn(_("Creator"))
.AddColumn(_("Created"))
.AddColumn(_("Reason"));
unsigned int count = 0, displayed = 0;
for (HostServ::Ban *ban : Serialize::GetObjects<HostServ::Ban *>())
{
++count;
if (!search.empty() && !Anope::Match(ban->GetAccount()->GetDisplay(), search))
continue;
++displayed;
ListFormatter::ListEntry entry;
entry["Number"] = stringify(count);
entry["Nick"] = ban->GetAccount()->GetDisplay();
entry["Expires"] = Anope::Expires(ban->GetExpires(), source.GetAccount());
entry["Creator"] = ban->GetCreator();
entry["Created"] = Anope::strftime(ban->GetCreated(), NULL, true);
entry["Reason"] = ban->GetReason();
list.AddEntry(entry);
}
if (count == 0)
{
source.Reply(_("Ban list is empty."));
return;
}
if (displayed == 0)
{
source.Reply(_("No entries match \002{0}\002."), search);
return;
}
std::vector<Anope::string> replies;
list.Process(replies);
for (const Anope::string &reply : replies)
source.Reply(reply);
source.Reply(_("Displayed \002{0}\002/\002{1}\002 records."), displayed, count);
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
{
source.Reply(_("Lists the users banned from requesting vhosts."));
return true;
}
};
class HSBan : public Module
, public EventHook<Event::VhostRequest>
{
CommandHSBan commandhsban;
CommandHSUnban commandhsunban;
CommandHSBanlist commandhsbanlist;
BanType bantype;
public:
HSBan(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
, EventHook<Event::VhostRequest>(this)
, commandhsban(this)
, commandhsunban(this)
, commandhsbanlist(this)
, bantype(this)
{
}
EventReturn OnVhostRequest(CommandSource *source, NickServ::Account *account, const Anope::string &user, const Anope::string &host) override
{
HostServ::Ban *ban = account->GetRef<HostServ::Ban *>();
if (ban == nullptr)
return EVENT_CONTINUE;
if (source)
{
if (ban->GetExpires() > 0)
source->Reply(_("You are banned from requesting vhosts until {0}. Reason: {1}"),
Anope::strftime(ban->GetExpires(), account), ban->GetReason());
else
source->Reply(_("You are banned from requesting vhosts. Reason: {0}"),
ban->GetReason());
}
return EVENT_STOP;
}
};
MODULE_INIT(HSBan)
|