summaryrefslogtreecommitdiff
path: root/modules/botserv/assign.cpp
blob: 26a44821689f6fc9d73d0305cd4d862d1e0c3bf6 (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
/*
 * Anope IRC Services
 *
 * Copyright (C) 2003-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/botserv/info.h"

class CommandBSAssign : public Command
{
 public:
	CommandBSAssign(Module *creator) : Command(creator, "botserv/assign", 2, 2)
	{
		this->SetDesc(_("Assigns a bot to a channel"));
		this->SetSyntax(_("\037channel\037 \037nickname\037"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		const Anope::string &chan = params[0];
		const Anope::string &nick = params[1];

		if (Anope::ReadOnly)
		{
			source.Reply(_("Sorry, bot assignment is temporarily disabled."));
			return;
		}

		ChanServ::Channel *ci = ChanServ::Find(chan);
		if (ci == NULL)
		{
			source.Reply(_("Channel \002{0}\002 isn't registered."), chan);
			return;
		}

		ServiceBot *bi = ServiceBot::Find(nick, true);
		if (!bi)
		{
			source.Reply(_("Bot \002{0}\002 does not exist."), nick);
			return;
		}

		ChanServ::AccessGroup access = source.AccessFor(ci);
		if (!access.HasPriv("ASSIGN") && !source.HasOverridePriv("botserv/administration"))
		{
			source.Reply(_("Access denied. You do not have privilege \002{0}\002 on \002{1}\002."), "ASSIGN", ci->GetName());
			return;
		}

		if (ci->HasFieldS("BS_NOBOT"))
		{
			source.Reply(_("Access denied. \002{0}\002 may not have a bot assigned to it because a Services Operator has disallowed it."), ci->GetName());
			return;
		}

		if (bi->bi && bi->bi->GetOperOnly() && !source.HasPriv("botserv/administration"))
		{
			source.Reply(_("Access denied. Bot \002{0}\002 is for operators only."), bi->nick);
			return;
		}

		if (ci->GetBot() == bi)
		{
			source.Reply(_("Bot \002{0}\002 is already assigned to \002{1}\002."), ci->GetBot()->nick, ci->GetName());
			return;
		}

		logger.Command(source, ci, _("{source} used {command} on {channel} to assign {0}"), bi->nick);

		bi->Assign(source.GetUser(), ci);
		source.Reply(_("Bot \002{0}\002 has been assigned to \002{1}\002."), bi->nick, ci->GetName());
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		source.Reply(_("Assigns the bot \037nickname\037 to \037channel\037."
		               " You can then configure the bot for the channel so it fits your needs.\n"
		               "\n"
		               "Use of this command requires the \002{0}\002 privilege on \037channel\037."
		               "\n"
		               "Example:\n"
			       "         {command} #anope Botox\n"
			       "         Assigns the bot Botox to #anope.\n"),
		               "ASSIGN", "command"_kw = source.GetCommand());
		return true;
	}
};

class CommandBSUnassign : public Command
{
 public:
	CommandBSUnassign(Module *creator) : Command(creator, "botserv/unassign", 1, 1)
	{
		this->SetDesc(_("Unassigns a bot from a channel"));
		this->SetSyntax(_("\037channel\037"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		const Anope::string &chan = params[0];

		if (Anope::ReadOnly)
		{
			source.Reply(_("Sorry, bot assignment is temporarily disabled."));
			return;
		}

		ChanServ::Channel *ci = ChanServ::Find(chan);
		if (ci == NULL)
		{
			source.Reply(_("Channel \002{0}\002 isn't registered."), chan);
			return;
		}

		ChanServ::AccessGroup access = source.AccessFor(ci);
		if (!access.HasPriv("ASSIGN") && !source.HasOverridePriv("botserv/administration"))
		{
			source.Reply(_("Access denied. You do not have privilege \002{0}\002 on \002{1}\002."), "ASSIGN", ci->GetName());
			return;
		}

		if (!ci->GetBot())
		{
			source.Reply(_("There is no bot assigned to \002{0}\002."), ci->GetName());
			return;
		}

		if (ci->IsPersist() && !ModeManager::FindChannelModeByName("PERM"))
		{
			source.Reply(_("You cannot unassign bots while persist is set on the channel."));
			return;
		}

		logger.Command(source, ci, _("{source} used {command} on {channel} to unassign {0}"), ci->GetBot()->nick);

		ServiceBot *bi = ci->GetBot();
		bi->UnAssign(source.GetUser(), ci);
		source.Reply(_("Bot \002{0}\002 has been unassigned from \002{1}\002."), bi->nick, ci->GetName());
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		source.Reply(_("Unassigns a bot from \037channel\037."
		               "Bot configuration is kept, so you will always be able to reassign a bot later without losing your settings.\n"
		               "\n"
		               "Use of this command requires the \002{0}\002 privilege on \037channel\037."
		               "\n"
		               "Example:\n"
			       "         {command} #anope\n"
			       "         Unassigns the current bot from #anope.\n"),
		               "ASSIGN", "command"_kw = source.GetCommand());
		return true;
	}
};

class CommandBSSetNoBot : public Command
{
 public:
	CommandBSSetNoBot(Module *creator, const Anope::string &sname = "botserv/set/nobot") : Command(creator, sname, 2, 2)
	{
		this->SetDesc(_("Prevent a bot from being assigned to a channel"));
		this->SetSyntax(_("\037channel\037 {\037ON|OFF\037}"));
	}

	void Execute(CommandSource &source, const std::vector<Anope::string> &params) override
	{
		const Anope::string &chan = params[0];
		const Anope::string &value = params[1];

		ChanServ::Channel *ci = ChanServ::Find(chan);
		if (ci == NULL)
		{
			source.Reply(_("Channel \002{0}\002 isn't registered."), chan);
			return;
		}

		if (value.equals_ci("ON"))
		{
			logger.Admin(source, ci, _("{source} used {command} on {channel} to enable nobot"));

			ci->SetS<bool>("BS_NOBOT", true);
			if (ci->GetBot())
				ci->GetBot()->UnAssign(source.GetUser(), ci);
			source.Reply(_("No-bot mode is now \002on\002 for \002{0}\002."), ci->GetName());
		}
		else if (value.equals_ci("OFF"))
		{
			logger.Admin(source, ci, _("{source} used {command} on {channel} to disable nobot"));

			ci->UnsetS<bool>("BS_NOBOT");
			source.Reply(_("No-bot mode is now \002off\002 for \002{0}\002."), ci->GetName());
		}
		else
		{
			this->OnSyntaxError(source, source.GetCommand());
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &) override
	{
		source.Reply(_("If no-bot is set on a channel, then the channel will be unabled to have a bot assigned to it."
		               " If a bot is already assigned to the channel, it is unassigned automatically when no-bot is enabled."
		               "\n"
		               "Example:\n"
			       "         {command} #anope on\n"
			       "         Prevents a service bot from being assigned to #anope.\n"),
		               "command"_kw = source.GetCommand());
		return true;
	}
};

class BSAssign : public Module
	, public EventHook<Event::Invite>
	, public EventHook<Event::ServiceBotEvent>
{
	Serialize::Field<BotInfo, bool> nobot;

	CommandBSAssign commandbsassign;
	CommandBSUnassign commandbsunassign;
	CommandBSSetNoBot commandbssetnobot;

 public:
	BSAssign(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
		, EventHook<Event::Invite>(this)
		, EventHook<Event::ServiceBotEvent>(this)

		, nobot(this, "BS_NOBOT")

		, commandbsassign(this)
		, commandbsunassign(this)
		, commandbssetnobot(this)
	{
	}

	void OnInvite(User *source, Channel *c, User *targ) override
	{
		ServiceBot *bi;
		if (Anope::ReadOnly || !c->ci || targ->server != Me || !(bi = ServiceBot::Find(targ->nick, true)))
			return;

		ChanServ::AccessGroup access = c->ci->AccessFor(source);
		if (!access.HasPriv("ASSIGN") && !source->HasPriv("botserv/administration"))
		{
			targ->SendMessage(bi, _("Access denied. You do not have privilege \002ASSIGN\002 on \002{0}\002."), c->ci->GetName());
			return;
		}

		if (nobot.HasExt(c->ci))
		{
			targ->SendMessage(bi, _("Access denied. \002{0}\002 may not have a bot assigned to it because a Services Operator has disallowed it."), c->ci->GetName());
			return;
		}

		if (bi->bi && bi->bi->GetOperOnly() && !source->HasPriv("botserv/administration"))
		{
			targ->SendMessage(bi, _("Access denied. Bot \002{0}\002 is for operators only."), bi->nick);
			return;
		}

		if (c->ci->GetBot() == bi)
		{
			targ->SendMessage(bi, _("Bot \002{0}\002 is already assigned to \002{1}\002."), bi->nick, c->ci->GetName());
			return;
		}

		bi->Assign(source, c->ci);
		targ->SendMessage(bi, _("Bot \002{0}\002 has been assigned to \002{1}\002."), bi->nick, c->ci->GetName());
	}

	void OnServiceBot(CommandSource &source, ServiceBot *bi, ChanServ::Channel *ci, InfoFormatter &info) override
	{
		if (nobot.HasExt(ci))
			info.AddOption(_("No bot"));
	}
};

MODULE_INIT(BSAssign)