summaryrefslogtreecommitdiff
path: root/modules/operserv/info.cpp
blob: f4785ed88d64309f3175f60e7c4c653a56e9dac1 (plain)
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
/*
 * Anope IRC Services
 *
 * Copyright (C) 2013-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"
#include "modules/nickserv/info.h"
#include "modules/chanserv/info.h"
#include "modules/operserv/info.h"

class OperInfoImpl : public OperInfo
{
	friend class OperInfoType;

	Serialize::Storage<NickServ::Account *> account;
	Serialize::Storage<ChanServ::Channel *> channel;
	Serialize::Storage<Anope::string> info, creator;
	Serialize::Storage<time_t> created;

 public:
	using OperInfo::OperInfo;

	NickServ::Account *GetAccount() override;
	void SetAccount(NickServ::Account *) override;

	ChanServ::Channel *GetChannel() override;
	void SetChannel(ChanServ::Channel *) override;

	Anope::string GetInfo() override;
	void SetInfo(const Anope::string &) override;

	Anope::string GetCreator() override;
	void SetCreator(const Anope::string &) override;

	time_t GetCreated() override;
	void SetCreated(const time_t &) override;
};

class OperInfoType : public Serialize::Type<OperInfoImpl>
{
 public:
	Serialize::ObjectField<OperInfoImpl, NickServ::Account *> account;
	Serialize::ObjectField<OperInfoImpl, ChanServ::Channel *> channel;
	Serialize::Field<OperInfoImpl, Anope::string> info, creator;
	Serialize::Field<OperInfoImpl, time_t> created;

	OperInfoType(Module *c) : Serialize::Type<OperInfoImpl>(c)
		, account(this, "account", &OperInfoImpl::account, true)
		, channel(this, "channel", &OperInfoImpl::channel, true)
		, info(this, "info", &OperInfoImpl::info)
		, creator(this, "adder", &OperInfoImpl::creator)
		, created(this, "created", &OperInfoImpl::created)
	{
	}
};

NickServ::Account *OperInfoImpl::GetAccount()
{
	return Get(&OperInfoType::account);
}

void OperInfoImpl::SetAccount(NickServ::Account *acc)
{
	Set(&OperInfoType::account, acc);
}

ChanServ::Channel *OperInfoImpl::GetChannel()
{
	return Get(&OperInfoType::channel);
}

void OperInfoImpl::SetChannel(ChanServ::Channel *c)
{
	Set(&OperInfoType::channel, c);
}

Anope::string OperInfoImpl::GetInfo()
{
	return Get(&OperInfoType::info);
}

void OperInfoImpl::SetInfo(const Anope::string &i)
{
	Set(&OperInfoType::info, i);
}

Anope::string OperInfoImpl::GetCreator()
{
	return Get(&OperInfoType::creator);
}

void OperInfoImpl::SetCreator(const Anope::string &c)
{
	Set(&OperInfoType::creator, c);
}

time_t OperInfoImpl::GetCreated()
{
	return Get(&OperInfoType::created);
}

void OperInfoImpl::SetCreated(const time_t &t)
{
	Set(&OperInfoType::created, t);
}

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> &params) override
	{
		const Anope::string &cmd = params[0], target = params[1], info = params.size() > 2 ? params[2] : "";

		NickServ::Account *account = nullptr;
		ChanServ::Channel *channel = nullptr;
		Serialize::Object *e;

		if (IRCD->IsChannelValid(target))
		{
			ChanServ::Channel *ci = ChanServ::Find(target);
			if (!ci)
			{
				source.Reply(_("Channel \002{0}\002 isn't registered."), target);
				return;
			}

			channel = ci;
			e = ci;
		}
		else
		{
			NickServ::Nick *na = NickServ::FindNick(target);
			if (!na)
			{
				source.Reply(_("\002{0}\002 isn't registered."), target);
				return;
			}

			account = na->GetAccount();
			e = account;
		}

		if (cmd.equals_ci("ADD"))
		{
			if (info.empty())
			{
				this->OnSyntaxError(source, cmd);
				return;
			}

			std::vector<OperInfo *> oinfos = e->GetRefs<OperInfo *>();
			if (oinfos.size() >= Config->GetModule(this->module)->Get<unsigned int>("max", "10"))
			{
				source.Reply(_("The oper info list for \002{0}\002 is full."), target);
				return;
			}

			for (OperInfo *o : oinfos)
				if (o->GetInfo().equals_ci(info))
				{
					source.Reply(_("The oper info already exists on \002{0}\002."), target);
					return;
				}

			OperInfo *o = Serialize::New<OperInfo *>();
			o->SetAccount(account);
			o->SetChannel(channel);
			o->SetInfo(info);
			o->SetCreator(source.GetNick());
			o->SetCreated(Anope::CurTime);

			source.Reply(_("Added info to \002{0}\002."), target);

			logger.Admin(source, _("{source} used {command} to add oper information to {0}: {1}"), target, info);

                	if (Anope::ReadOnly)
				source.Reply(_("Services are in read-only mode. Any changes made may not persist."));
		}
		else if (cmd.equals_ci("DEL"))
		{
			if (info.empty())
			{
				this->OnSyntaxError(source, cmd);
				return;
			}

			std::vector<OperInfo *> oinfos = e->GetRefs<OperInfo *>();
			if (oinfos.empty())
			{
				source.Reply(_("Oper info list for \002{0}\002 is empty."), target);
				return;
			}

			for (OperInfo *o : oinfos)
			{
				if (o->GetInfo().equals_ci(info))
				{
					o->Delete();

					source.Reply(_("Deleted info from \002{0}\002."), target);

					logger.Admin(source, _("{source} used {command} to remove oper information from {0}: {1}"), target, info);

	        	        	if (Anope::ReadOnly)
						source.Reply(_("Services are in read-only mode. Any changes made may not persist."));
					return;
				}
			}

			source.Reply(_("No such info \002{0}\002 on \002{1}\002."), info, target);
		}
		else if (cmd.equals_ci("CLEAR"))
		{
			std::vector<OperInfo *> oinfos = e->GetRefs<OperInfo *>();

			if (oinfos.empty())
			{
				source.Reply(_("Oper info list for \002{0}\002 is empty."), target);
				return;
			}

			for (OperInfo *o : oinfos)
				o->Delete();

			source.Reply(_("Cleared info from \002{0}\002."), target);

			logger.Admin(source, _("{source} used {command} to clear oper information for {0}"), target);

                	if (Anope::ReadOnly)
				source.Reply(_("Services are in read-only mode. Any changes made may not persist."));
		}
		else
		{
			this->OnSyntaxError(source, cmd);
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		source.Reply(_("Add or delete oper information for a given account or channel. This information will show to opers in the respective info command for the account or channel."));
		return true;
	}
};

class OSInfo : public Module
	, public EventHook<Event::NickInfo>
	, public EventHook<Event::ChanInfo>
{
	CommandOSInfo commandosinfo;
	OperInfoType oinfotype;

	void OnInfo(CommandSource &source, Serialize::Object *e, InfoFormatter &info)
	{
		if (!source.IsOper())
			return;

		for (OperInfo *o : e->GetRefs<OperInfo *>())
			info[_("Oper Info")] = Anope::printf(_("(by %s on %s) %s"), o->GetCreator().c_str(), Anope::strftime(o->GetCreated(), source.GetAccount(), true).c_str(), o->GetInfo().c_str());
	}

 public:
	OSInfo(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
		, EventHook<Event::NickInfo>(this)
		, EventHook<Event::ChanInfo>(this)
		, commandosinfo(this)
		, oinfotype(this)
	{
	}

	void OnNickInfo(CommandSource &source, NickServ::Nick *na, InfoFormatter &info, bool show_hidden) override
	{
		OnInfo(source, na->GetAccount(), info);
	}

	void OnChanInfo(CommandSource &source, ChanServ::Channel *ci, InfoFormatter &info, bool show_hidden) override
	{
		OnInfo(source, ci, info);
	}
};

MODULE_INIT(OSInfo)