forked from genius3000/anope-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_expiring.cpp
269 lines (230 loc) · 8.42 KB
/
os_expiring.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
/*
* OperServ Expiring
*
* (C) 2016 - genius3000 (genius3000@g3k.solutions)
* Please refer to the GPL License in use by Anope at:
* https://github.com/anope/anope/blob/master/docs/COPYING
*
* Check registered nick and/or channel list for any soon to be expire
*
* Syntax: EXPIRING [NICK|CHAN] [time]
* Both nick and channels will be listed if not specified
* Configured defaults will be used if no time is specified
*
* Configuration to put into your operserv config:
module { name = "os_expiring" }
command { service = "OperServ"; name = "EXPIRING"; command = "operserv/expiring"; permission = "operserv/expiring"; }
*
* Don't forget to add 'operserv/expiring' to your oper permissions
*/
#include "module.h"
class CommandOSExpiring : public Command
{
private:
void ProcessNickList(CommandSource &source, Anope::string range)
{
unsigned nnicks = 0;
time_t trange;
time_t nick_expiry = Config->GetModule("nickserv")->Get<time_t>("expire", "21d");
time_t nick_uc_expiry = Config->GetModule("ns_register")->Get<time_t>("unconfirmedexpire", "1d");
unsigned listmax = Config->GetModule("nickserv")->Get<unsigned>("listmax", "50");
if (range.equals_ci("default"))
{
trange = nick_expiry / 4;
range = Anope::Duration(trange, source.GetAccount());
}
else
{
trange = Anope::DoTime(range);
range = Anope::Duration(trange, source.GetAccount());
}
float nick_percent = static_cast<float>(trange) / static_cast<float>(nick_expiry) * 100.0;
if (nick_percent > 90)
return source.Reply(_("The range of %s is too close to (or greater than) the nick default expiry (%s). Not running a list."), range.c_str(), Anope::Duration(nick_expiry, source.GetAccount()).c_str());
trange += Anope::CurTime;
ListFormatter list(source.GetAccount());
list.AddColumn(_("Nick")).AddColumn(_("Expires"));
Anope::map<NickAlias *> ordered_map;
for (nickalias_map::const_iterator it = NickAliasList->begin(), it_end = NickAliasList->end(); it != it_end; ++it)
ordered_map[it->first] = it->second;
for (Anope::map<NickAlias *>::const_iterator it = ordered_map.begin(), it_end = ordered_map.end(); it != it_end; ++it)
{
const NickAlias *na = it->second;
time_t this_expires;
if (na->HasExt("NS_NO_EXPIRE"))
continue;
if (na->nc->HasExt("UNCONFIRMED"))
this_expires = na->last_seen + nick_uc_expiry;
else
this_expires = na->last_seen + nick_expiry;
if (this_expires <= trange)
{
if (++nnicks <= listmax)
{
ListFormatter::ListEntry entry;
entry["Nick"] = na->nick;
Anope::string expires = Anope::strftime(this_expires, source.GetAccount());
if (na->nc->HasExt("NS_SUSPENDED"))
entry["Expires"] = expires + Language::Translate(source.GetAccount(), _(" [Suspended]"));
else if (na->nc->HasExt("UNCONFIRMED"))
entry["Expires"] = expires + Language::Translate(source.GetAccount(), _(" [Unconfirmed]"));
else
entry["Expires"] = expires;
list.AddEntry(entry);
}
}
}
if (list.IsEmpty())
source.Reply(_("No nicks will expire within %s."), range.c_str());
else
{
source.Reply(_("List of nicks expiring within %s:"), range.c_str());
std::vector<Anope::string> replies;
list.Process(replies);
for (unsigned i = 0; i < replies.size(); ++i)
source.Reply(replies[i]);
source.Reply(_("End of list - %d/%d matches shown."), nnicks > listmax ? listmax : nnicks, nnicks);
}
return;
}
void ProcessChanList(CommandSource &source, Anope::string range)
{
unsigned nchans = 0;
time_t trange;
time_t chan_expiry = Config->GetModule("chanserv")->Get<time_t>("expire", "14d");
unsigned listmax = Config->GetModule("chanserv")->Get<unsigned>("listmax", "50");
if (range.equals_ci("default"))
{
trange = chan_expiry / 4;
range = Anope::Duration(trange, source.GetAccount());
}
else
{
trange = Anope::DoTime(range);
range = Anope::Duration(trange, source.GetAccount());
}
float chan_percent = static_cast<float>(trange) / static_cast<float>(chan_expiry) * 100.0;
if (chan_percent > 90)
return source.Reply(_("The range of %s is too close to (or greather than) the channel default expiry (%s). Not running a list."), range.c_str(), Anope::Duration(chan_expiry, source.GetAccount()).c_str());
trange += Anope::CurTime;
ListFormatter list(source.GetAccount());
list.AddColumn(_("Name")).AddColumn(_("Expires"));
Anope::map<ChannelInfo *> ordered_map;
for (registered_channel_map::const_iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end; ++it)
ordered_map[it->first] = it->second;
for (Anope::map<ChannelInfo *>::const_iterator it = ordered_map.begin(), it_end = ordered_map.end(); it != it_end; ++it)
{
const ChannelInfo *ci = it->second;
time_t this_expires;
if (ci->HasExt("CS_NO_EXPIRE"))
continue;
this_expires = ci->last_used + chan_expiry;
if (this_expires <= trange)
{
if (++nchans <= listmax)
{
ListFormatter::ListEntry entry;
entry["Name"] = ci->name;
Anope::string expires = Anope::strftime(this_expires, source.GetAccount());
if (ci->HasExt("CS_SUSPENDED"))
entry["Expires"] = expires + Language::Translate(source.GetAccount(), _(" [Suspended]"));
else
entry["Expires"] = expires;
list.AddEntry(entry);
}
}
}
if (list.IsEmpty())
source.Reply(_("No channels will expire within %s."), range.c_str());
else
{
source.Reply(_("List of channels expiring within %s:"), range.c_str());
std::vector<Anope::string> replies;
list.Process(replies);
for (unsigned i = 0; i < replies.size(); ++i)
source.Reply(replies[i]);
source.Reply(_("End of list - %d/%d matches shown."), nchans > listmax ? listmax : nchans, nchans);
}
return;
}
void DoList(CommandSource &source, const std::vector<Anope::string> ¶ms)
{
Anope::string choice, range;
if (params.size() < 1)
{
choice = "nickchan";
range = "default";
}
else if (params.size() == 1)
{
if (isdigit(params[0][0]))
{
choice = "nickchan";
range = params[0];
}
else if (params[0].equals_ci("nick") || params[0].equals_ci("chan"))
{
choice = params[0];
range = "default";
}
else
return this->OnSyntaxError(source, "");
}
else if (params.size() == 2 && (params[0].equals_ci("nick") || params[0].equals_ci("chan")))
{
choice = params[0];
range = params[1];
}
else
return this->OnSyntaxError(source, "");
// Range is in days if not specified
if (isdigit(range[range.length() - 1]))
range += "d";
if (choice.equals_ci("nickchan") || choice.equals_ci("nick"))
this->ProcessNickList(source, range);
if (choice.equals_ci("nickchan") || choice.equals_ci("chan"))
this->ProcessChanList(source, range);
}
public:
CommandOSExpiring(Module *creator) : Command(creator, "operserv/expiring", 0, 2)
{
this->SetDesc(_("Check registered nick and/or channel list for any soon to expire"));
this->SetSyntax(_("[\037nick\037 | \037chan\037] [\037time\037]"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
return this->DoList(source, params);
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("Let's you check the registered nick and/or channel list\n"
"for any that are expiring in the time range specified.\n"));
source.Reply(" ");
source.Reply(_("\002EXPIRING\002 will list both nicks and channels.\n"
"\002EXPIRING NICK\002 will list just the nicks.\n"
"\002EXPIRING CHAN\002 will list just the channels.\n"
"\037time\037 is specified as an integer followed by one of \037d\037\n"
"(days), \037h\037 (hours), or \037m\037 (minutes). Combinations (such as\n"
"\0371h30m\037) are not permitted. If a unit specifier is not\n"
"included, the default is days (so \03730\037 by itself means 30\n"
"days). If a time range is not given, it will use one quarter\n"
"of the default expiry time (for either list).\n"));
return true;
}
};
class OSExpiring : public Module
{
CommandOSExpiring commandosexpiring;
public:
OSExpiring(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, THIRD),
commandosexpiring(this)
{
if(Anope::VersionMajor() != 2 || Anope::VersionMinor() != 0)
throw ModuleException("Requires version 2.0.x of Anope.");
this->SetAuthor("genius3000");
this->SetVersion("1.0.4");
}
};
MODULE_INIT(OSExpiring)