summaryrefslogtreecommitdiff
path: root/modules/nickserv/set_misc.cpp
blob: 97f504645dc5b1ea36b0589364158ad8ab92a01d (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
/*
 * Anope IRC Services
 *
 * Copyright (C) 2010-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/set_misc.h"
#include "modules/nickserv/info.h"
#include "modules/nickserv/set.h"

static Anope::map<Anope::string> descriptions;

class NSMiscDataImpl : public NSMiscData
{
	friend class NSMiscDataType;

	Serialize::Storage<NickServ::Account *> account;
	Serialize::Storage<Anope::string> name, data;

 public:
	using NSMiscData::NSMiscData;

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

	Anope::string GetName() override;
	void SetName(const Anope::string &n) override;

	Anope::string GetData() override;
	void SetData(const Anope::string &d) override;
};

class NSMiscDataType : public Serialize::Type<NSMiscDataImpl>
{
 public:
	Serialize::ObjectField<NSMiscDataImpl, NickServ::Account *> owner;
	Serialize::Field<NSMiscDataImpl, Anope::string> name, data;

	NSMiscDataType(Module *me) : Serialize::Type<NSMiscDataImpl>(me)
		, owner(this, "account", &NSMiscDataImpl::account, true)
		, name(this, "name", &NSMiscDataImpl::name)
		, data(this, "data", &NSMiscDataImpl::data)
	{
	}
};

NickServ::Account *NSMiscDataImpl::GetAccount()
{
	return Get(&NSMiscDataType::owner);
}

void NSMiscDataImpl::SetAccount(NickServ::Account *s)
{
	Set(&NSMiscDataType::owner, s);
}

Anope::string NSMiscDataImpl::GetName()
{
	return Get(&NSMiscDataType::name);
}

void NSMiscDataImpl::SetName(const Anope::string &n)
{
	Set(&NSMiscDataType::name, n);
}

Anope::string NSMiscDataImpl::GetData()
{
	return Get(&NSMiscDataType::data);
}

void NSMiscDataImpl::SetData(const Anope::string &d)
{
	Set(&NSMiscDataType::data, d);
}

class CommandNSSetMisc : public Command
{
	Anope::string GetAttribute(const Anope::string &command)
	{
		size_t sp = command.rfind(' ');
		if (sp != Anope::string::npos)
			return command.substr(sp + 1);
		return command;
	}

 public:
	CommandNSSetMisc(Module *creator, const Anope::string &cname = "nickserv/set/misc", size_t min = 0) : Command(creator, cname, min, min + 1)
	{
		this->SetSyntax(_("[\037parameter\037]"));
	}

	void Run(CommandSource &source, const Anope::string &user, const Anope::string &param)
	{
		if (Anope::ReadOnly)
		{
			source.Reply(_("Services are in read-only mode."));
			return;
		}

		NickServ::Nick *na = NickServ::FindNick(user);
		if (!na)
		{
			source.Reply(_("\002{0}\002 isn't registered."), user);
			return;
		}
		NickServ::Account *nc = na->GetAccount();

		EventReturn MOD_RESULT = EventManager::Get()->Dispatch(&Event::SetNickOption::OnSetNickOption, source, this, nc, param);
		if (MOD_RESULT == EVENT_STOP)
			return;

		Anope::string scommand = GetAttribute(source.GetCommand());

		/* remove existing */
		for (NSMiscData *data : nc->GetRefs<NSMiscData *>())
			if (data->GetName() == scommand)
			{
				data->Delete();
				break;
			}

		if (!param.empty())
		{
			NSMiscData *data = Serialize::New<NSMiscData *>();
			data->SetAccount(nc);
			data->SetName(scommand);
			data->SetData(param);

			source.Reply(_("\002{0}\002 for \002{1}\002 set to \002{2}\002."), scommand, nc->GetDisplay(), param);
		}
		else
		{
			source.Reply(_("\002{0}\002 for \002{1}\002 unset."), scommand, nc->GetDisplay());
		}
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		this->Run(source, source.nc->GetDisplay(), !params.empty() ? params[0] : "");
	}

	void OnServHelp(CommandSource &source) override
	{
		if (descriptions.count(source.GetCommand()))
		{
			this->SetDesc(descriptions[source.GetCommand()]);
			Command::OnServHelp(source);
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		if (descriptions.count(source.GetCommand()))
		{
			source.Reply(Language::Translate(source.nc, descriptions[source.GetCommand()]));
			return true;
		}
		return false;
	}
};

class CommandNSSASetMisc : public CommandNSSetMisc
{
 public:
	CommandNSSASetMisc(Module *creator) : CommandNSSetMisc(creator, "nickserv/saset/misc", 1)
	{
		this->ClearSyntax();
		this->SetSyntax(_("\037nickname\037 [\037parameter\037]"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		this->Run(source, params[0], params.size() > 1 ? params[1] : "");
	}
};

class NSSetMisc : public Module
	, public EventHook<Event::NickInfo>
{
	CommandNSSetMisc commandnssetmisc;
	CommandNSSASetMisc commandnssasetmisc;
	NSMiscDataType type;

 public:
	NSSetMisc(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
		, EventHook<Event::NickInfo>(this)
		, commandnssetmisc(this)
		, commandnssasetmisc(this)
		, type(this)
	{
	}

	void OnReload(Configuration::Conf *conf) override
	{
		descriptions.clear();

		for (int i = 0; i < conf->CountBlock("command"); ++i)
		{
			Configuration::Block *block = conf->GetBlock("command", i);

			const Anope::string &cmd = block->Get<Anope::string>("command");

			if (cmd != "nickserv/set/misc" && cmd != "nickserv/saset/misc")
				continue;

			Anope::string cname = block->Get<Anope::string>("name");
			Anope::string desc = block->Get<Anope::string>("misc_description");

			if (cname.empty() || desc.empty())
				continue;

			descriptions[cname] = desc;
		}
	}

	void OnNickInfo(CommandSource &source, NickServ::Nick *na, InfoFormatter &info, bool) override
	{
		for (NSMiscData *data : na->GetAccount()->GetRefs<NSMiscData *>())
			info[data->GetName()] = data->GetData();
	}
};

MODULE_INIT(NSSetMisc)