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
|
/* OperServ core functions
*
* (C) 2003-2019 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*/
#include "module.h"
struct OperInfo : Serializable
{
Anope::string target;
Anope::string info;
Anope::string adder;
time_t created;
OperInfo() : Serializable("OperInfo"), created(0) { }
OperInfo(const Anope::string &t, const Anope::string &i, const Anope::string &a, time_t c) :
Serializable("OperInfo"), target(t), info(i), adder(a), created(c) { }
~OperInfo();
void Serialize(Serialize::Data &data) const anope_override
{
data["target"] << target;
data["info"] << info;
data["adder"] << adder;
data["created"] << created;
}
static Serializable *Unserialize(Serializable *obj, Serialize::Data &data);
};
struct OperInfos : Serialize::Checker<std::vector<OperInfo *> >
{
OperInfos(Extensible *) : Serialize::Checker<std::vector<OperInfo *> >("OperInfo") { }
~OperInfos()
{
for (unsigned i = (*this)->size(); i > 0; --i)
delete (*this)->at(i - 1);
}
static Extensible *Find(const Anope::string &target)
{
NickAlias *na = NickAlias::Find(target);
if (na)
return na->nc;
return ChannelInfo::Find(target);
}
};
OperInfo::~OperInfo()
{
Extensible *e = OperInfos::Find(target);
if (e)
{
OperInfos *op = e->GetExt<OperInfos>("operinfo");
if (op)
{
std::vector<OperInfo *>::iterator it = std::find((*op)->begin(), (*op)->end(), this);
if (it != (*op)->end())
(*op)->erase(it);
}
}
}
Serializable *OperInfo::Unserialize(Serializable *obj, Serialize::Data &data)
{
Anope::string starget;
data["target"] >> starget;
Extensible *e = OperInfos::Find(starget);
if (!e)
return NULL;
OperInfos *oi = e->Require<OperInfos>("operinfo");
OperInfo *o;
if (obj)
o = anope_dynamic_static_cast<OperInfo *>(obj);
else
{
o = new OperInfo();
o->target = starget;
}
data["info"] >> o->info;
data["adder"] >> o->adder;
data["created"] >> o->created;
if (!obj)
(*oi)->push_back(o);
return o;
}
class CommandOSInfo : public Command
{
public:
CommandOSInfo(Module *creator) : Command(creator, "operserv/info", 2, 3)
{
this->SetDesc(_("Associate oper info with a nick or channel"));
this->SetSyntax(_("ADD \037target\037 \037info\037"));
this->SetSyntax(_("DEL \037target\037 \037info\037"));
this->SetSyntax(_("CLEAR \037target\037"));
}
void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) anope_override
{
const Anope::string &cmd = params[0], target = params[1], info = params.size() > 2 ? params[2] : "";
Extensible *e;
if (IRCD->IsChannelValid(target))
{
ChannelInfo *ci = ChannelInfo::Find(target);
if (!ci)
{
source.Reply(CHAN_X_NOT_REGISTERED, target.c_str());
return;
}
e = ci;
}
else
{
NickAlias *na = NickAlias::Find(target);
if (!na)
{
source.Reply(NICK_X_NOT_REGISTERED, target.c_str());
return;
}
e = na->nc;
}
if (cmd.equals_ci("ADD"))
{
if (info.empty())
{
this->OnSyntaxError(source, cmd);
return;
}
OperInfos *oi = e->Require<OperInfos>("operinfo");
if ((*oi)->size() >= Config->GetModule(this->module)->Get<unsigned>("max", "10"))
{
source.Reply(_("The oper info list for \002%s\002 is full."), target.c_str());
return;
}
for (unsigned i = 0; i < (*oi)->size(); ++i)
{
OperInfo *o = (*oi)->at(i);
if (o->info.equals_ci(info))
{
source.Reply(_("The oper info already exists on \002%s\002."), target.c_str());
return;
}
}
(*oi)->push_back(new OperInfo(target, info, source.GetNick(), Anope::CurTime));
source.Reply(_("Added info to \002%s\002."), target.c_str());
Log(LOG_ADMIN, source, this) << "to add information to " << target;
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
}
else if (cmd.equals_ci("DEL"))
{
if (info.empty())
{
this->OnSyntaxError(source, cmd);
return;
}
OperInfos *oi = e->GetExt<OperInfos>("operinfo");
if (!oi)
{
source.Reply(_("Oper info list for \002%s\002 is empty."), target.c_str());
return;
}
bool found = false;
for (unsigned i = (*oi)->size(); i > 0; --i)
{
OperInfo *o = (*oi)->at(i - 1);
if (o->info.equals_ci(info))
{
delete o;
found = true;
break;
}
}
if (!found)
{
source.Reply(_("No such info \"%s\" on \002%s\002."), info.c_str(), target.c_str());
}
else
{
if ((*oi)->empty())
e->Shrink<OperInfos>("operinfo");
source.Reply(_("Deleted info from \002%s\002."), target.c_str());
Log(LOG_ADMIN, source, this) << "to remove information from " << target;
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
}
}
else if (cmd.equals_ci("CLEAR"))
{
OperInfos *oi = e->GetExt<OperInfos>("operinfo");
if (!oi)
{
source.Reply(_("Oper info list for \002%s\002 is empty."), target.c_str());
return;
}
e->Shrink<OperInfos>("operinfo");
source.Reply(_("Cleared info from \002%s\002."), target.c_str());
Log(LOG_ADMIN, source, this) << "to clear information for " << target;
if (Anope::ReadOnly)
source.Reply(READ_ONLY_MODE);
}
else
{
this->OnSyntaxError(source, cmd);
}
}
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
{
this->SendSyntax(source);
source.Reply(" ");
source.Reply(_("Add or delete oper information for a given nick or channel.\n"
"This will show to opers in the respective info command for\n"
"the nick or channel."));
return true;
}
};
class OSInfo : public Module
{
CommandOSInfo commandosinfo;
ExtensibleItem<OperInfos> oinfo;
Serialize::Type oinfo_type;
void OnInfo(CommandSource &source, Extensible *e, InfoFormatter &info)
{
if (!source.IsOper())
return;
OperInfos *oi = oinfo.Get(e);
if (!oi)
return;
for (unsigned i = 0; i < (*oi)->size(); ++i)
{
OperInfo *o = (*oi)->at(i);
info[_("Oper Info")] = Anope::printf(_("(by %s on %s) %s"), o->adder.c_str(), Anope::strftime(o->created, source.GetAccount(), true).c_str(), o->info.c_str());
}
}
public:
OSInfo(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
commandosinfo(this), oinfo(this, "operinfo"), oinfo_type("OperInfo", OperInfo::Unserialize)
{
}
void OnNickInfo(CommandSource &source, NickAlias *na, InfoFormatter &info, bool show_hidden) anope_override
{
OnInfo(source, na->nc, info);
}
void OnChanInfo(CommandSource &source, ChannelInfo *ci, InfoFormatter &info, bool show_hidden) anope_override
{
OnInfo(source, ci, info);
}
};
MODULE_INIT(OSInfo)
|