summaryrefslogtreecommitdiff
path: root/modules/botserv/bot.cpp
blob: 65a89183910acae4398227b0fc9a70842ff8e6c3 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * 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/bot.h"

class CommandBSBot : public Command
{
	void DoAdd(CommandSource &source, const std::vector<Anope::string> &params)
	{
		const Anope::string &nick = params[1];
		const Anope::string &user = params[2];
		const Anope::string &host = params[3];
		const Anope::string &real = params[4];

		if (ServiceBot::Find(nick, true))
		{
			source.Reply(_("Bot \002{0}\002 already exists."), nick);
			return;
		}

		Configuration::Block *networkinfo = Config->GetBlock("networkinfo");

		if (nick.length() > networkinfo->Get<unsigned>("nicklen"))
		{
			source.Reply(_("Bot nicks may only be \002{0}\002 characters long."), networkinfo->Get<unsigned>("nicklen"));
			return;
		}

		if (user.length() > networkinfo->Get<unsigned>("userlen"))
		{
			source.Reply(_("Bot idents may only be \002{0}\002 characters long."), networkinfo->Get<unsigned>("userlen"));
			return;
		}

		if (host.length() > networkinfo->Get<unsigned>("hostlen"))
		{
			source.Reply(_("Bot hosts may only be \002{0}\002 characters long."), networkinfo->Get<unsigned>("hostlen"));
			return;
		}

		if (!IRCD->IsNickValid(nick))
		{
			source.Reply(_("Bot nicks may only contain valid nick characters."));
			return;
		}

		if (!IRCD->IsIdentValid(user))
		{
			source.Reply(_("Bot idents may only contain valid ident characters."));
			return;
		}

		if (!IRCD->IsHostValid(host))
		{
			source.Reply(_("Bot hosts may only contain valid host characters."));
			return;
		}

		/* We check whether the nick is registered, and inform the user
		* if so. You need to drop the nick manually before you can use
		* it as a bot nick from now on -GD
		*/
		NickServ::Nick *na = NickServ::FindNick(nick);
		if (na)
		{
			source.Reply(_("\002{0}\002 is already registered!"), na->GetNick());
			return;
		}

		User *targ = User::Find(nick, true);
		if (targ)
		{
			source.Reply(_("\002{0}\002 is currently in use."), targ->nick);
			return;
		}

		ServiceBot *bi = new ServiceBot(nick, user, host, real);

		BotInfo *botinfo = Serialize::New<BotInfo *>();
		botinfo->SetNick(nick);
		botinfo->SetUser(user);
		botinfo->SetHost(host);
		botinfo->SetRealName(real);
		botinfo->SetCreated(Anope::CurTime);

		bi->bi = botinfo;
		botinfo->bot = bi;

		logger.Admin(source, _("{source} used {command} to add bot {0} {1}"), bi->GetMask(), bi->realname);

		source.Reply(_("\002{0}!{1}@{2}\002 (\002{3}\002) added to the bot list."), bi->nick, bi->GetIdent(), bi->host, bi->realname);

		EventManager::Get()->Dispatch(&Event::BotCreate::OnBotCreate, bi);
	}

	void DoChange(CommandSource &source, const std::vector<Anope::string> &params)
	{
		const Anope::string &oldnick = params[1];
		const Anope::string &nick = params.size() > 2 ? params[2] : "";
		const Anope::string &user = params.size() > 3 ? params[3] : "";
		const Anope::string &host = params.size() > 4 ? params[4] : "";
		const Anope::string &real = params.size() > 5 ? params[5] : "";

		if (oldnick.empty() || nick.empty())
		{
			this->OnSyntaxError(source, "CHANGE");
			return;
		}

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

		if (bi->bi && bi->bi->conf)
		{
			source.Reply(_("Bot \002{0}\002 is not changeable because it is configured in services configuration."), bi->nick.c_str());
			return;
		}

		Configuration::Block *networkinfo = Config->GetBlock("networkinfo");

		if (nick.length() > networkinfo->Get<unsigned>("nicklen"))
		{
			source.Reply(_("Bot nicknames may only be \002{0}\002 characters long."), networkinfo->Get<unsigned>("nicklen"));
			return;
		}

		if (user.length() > networkinfo->Get<unsigned>("userlen"))
		{
			source.Reply(_("Bot usernames may only be \002{0}\002 characters long."), networkinfo->Get<unsigned>("userlen"));
			return;
		}

		if (host.length() > networkinfo->Get<unsigned>("hostlen"))
		{
			source.Reply(_("Bot hostnames may only be \002{0}\002 characters long."), networkinfo->Get<unsigned>("hostlen"));
			return;
		}

		/* Checks whether there *are* changes.
		* Case sensitive because we may want to change just the case.
		* And we must finally check that the nick is not already
		* taken by another bot.
		*/
		if (nick.equals_cs(bi->nick)
			&& (user.empty() || user.equals_cs(bi->GetIdent()))
			&& (host.empty() || host.equals_cs(bi->host))
			&& (real.empty() || real.equals_cs(bi->realname)))
		{
			source.Reply(_("There is no difference between the current settings and the new settings."));
			return;
		}

		if (!IRCD->IsNickValid(nick))
		{
			source.Reply(_("Bot nicknames may only contain valid nickname characters."));
			return;
		}

		if (!user.empty() && !IRCD->IsIdentValid(user))
		{
			source.Reply(_("Bot uesrnames may only contain valid username characters."));
			return;
		}

		if (!host.empty() && !IRCD->IsHostValid(host))
		{
			source.Reply(_("Bot hostnames may only contain valid hostname characters."));
			return;
		}

		ServiceBot *newbi = ServiceBot::Find(nick, true);
		if (newbi && bi != newbi)
		{
			source.Reply(_("Bot \002{0}\002 already exists."), newbi->nick);
			return;
		}

		User *target = User::Find(nick, true);
		if (target)
		{
			source.Reply(_("\002{0}\002 is currently in use."), target->nick);
			return;
		}

		if (!nick.equals_ci(bi->nick))
		{
			/* We check whether the nick is registered, and inform the user
			* if so. You need to drop the nick manually before you can use
			* it as a bot nick from now on -GD
			*/
			NickServ::Nick *na = NickServ::FindNick(nick);
			if (na)
			{
				source.Reply(_("\002{0}\002 is already registered."), na->GetNick());
				return;
			}

#warning "xline on the stack"
			/* The new nick is really different, so we remove the Q line for the old nick. */
			//XLine x_del(bi->nick);
			//IRCD->SendSQLineDel(&x_del);

			/* Add a Q line for the new nick */
			//XLine x(nick, "Reserved for services");
			//IRCD->SendSQLine(NULL, &x);
		}

		if (!user.empty())
		{
			IRCD->SendQuit(bi, "Quit: Be right back");
			bi->introduced = false;
		}
		else
		{
			IRCD->Send<messages::NickChange>(bi, nick, Anope::CurTime);
			bi->timestamp = Anope::CurTime;
		}

		if (!nick.equals_cs(bi->nick))
		{
			bi->SetNewNick(nick);
			if (bi->bi != nullptr)
				bi->bi->SetNick(nick);
		}

		if (!user.equals_cs(bi->GetIdent()))
		{
			bi->SetIdent(user);
			if (bi->bi != nullptr)
				bi->bi->SetUser(user);
		}
		if (!host.equals_cs(bi->host))
		{
			bi->host = host;
			if (bi->bi != nullptr)
				bi->bi->SetHost(host);
		}
		if (real.equals_cs(bi->realname))
		{
			bi->realname = real;
			if (bi->bi != nullptr)
				bi->bi->SetRealName(real);
		}

		if (!user.empty())
			bi->OnKill();

		logger.Admin(source, _("{source} used {command} to change bot {0} to {1} {2}"),
				oldnick, bi->GetMask(), bi->realname);

		source.Reply(_("Bot \002{0}\002 has been changed to \002{1}!{2}@{3}\002 (\002{4}\002)."), oldnick, bi->nick, bi->GetIdent(), bi->host, bi->realname);

		EventManager::Get()->Dispatch(&Event::BotChange::OnBotChange, bi);
	}

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

		if (nick.empty())
		{
			this->OnSyntaxError(source, "DEL");
			return;
		}

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

		if (bi->bi && bi->bi->conf)
		{
			source.Reply(_("Bot \002{0}\002 is can not be deleted because it is configured in services configuration."), bi->nick);
			return;
		}

		EventManager::Get()->Dispatch(&Event::BotDelete::OnBotDelete, bi);

		logger.Admin(source, _("{source} used {command} to delete bot {0}"), bi->nick);

		source.Reply(_("Bot \002{0}\002 has been deleted."), bi->nick);
		delete bi;
	}

 public:
	CommandBSBot(Module *creator) : Command(creator, "botserv/bot", 1, 6)
	{
		this->SetDesc(_("Maintains network bot list"));
		this->SetSyntax(_("\002ADD \037nicknae\037 \037username\037 \037hostname\037 \037realname\037\002"));
		this->SetSyntax(_("\002CHANGE \037oldnickname\037 \037newnickname\037 [\037username\037 [\037hostname\037 [\037realname\037]]]\002"));
		this->SetSyntax(_("\002DEL \037nickname\037\002"));
	}

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

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

		if (cmd.equals_ci("ADD"))
		{
			if (!source.HasCommand("botserv/bot/add"))
			{
				source.Reply(_("Access denied. You do not have access to the operator command \002{0}\002."), "botserv/bot/add");
				return;
			}

			// ADD nick user host real - 5
			if (params.size() < 5)
			{
				this->OnSyntaxError(source, "ADD");
				return;
			}

			std::vector<Anope::string> tempparams = params;
			// ADD takes less params than CHANGE, so we need to take 6 if given and append it with a space to 5.
			if (tempparams.size() >= 6)
				tempparams[4] = tempparams[4] + " " + tempparams[5];

			this->DoAdd(source, tempparams);
		}
		else if (cmd.equals_ci("CHANGE"))
		{
			// CHANGE oldn newn user host real - 6
			// but only oldn and newn are required
			if (!source.HasCommand("botserv/bot/change"))
			{
				source.Reply(_("Access denied. You do not have access to the operator command \002{0}\002."), "botserv/bot/change");
				return;
			}

			if (params.size() < 3)
			{
				this->OnSyntaxError(source, "CHANGE");
				return;
			}

			this->DoChange(source, params);
		}
		else if (cmd.equals_ci("DEL"))
		{
			// DEL nick
			if (!source.HasCommand("botserv/bot/del"))
			{
				source.Reply(_("Access denied. You do not have access to the operator command \002{0}\002."), "botserv/bot/del");
				return;
			}

			if (params.size() < 1)
			{
				this->OnSyntaxError(source, "DEL");
				return;
			}

			this->DoDel(source, params);
		}
		else
		{
			this->OnSyntaxError(source);
		}
	}

	bool OnHelp(CommandSource &source, const Anope::string &subcommand) override
	{
		if (subcommand.equals_ci("ADD"))
			source.Reply(_("\002{command} ADD\002 adds a bot with the given \037nickname\037, \037username\037, \037hostname\037 and \037realname\037."
			               " You can not create a bot with a nickname that is currently registered. If an unregistered user is currently using the nick, they will be killed.\n"
			               " Once a bot is created, users will be able to assign the bot to their channels. This command requires the opererator privilege for command \002{0}\002."
			               "\n"
			               "Example:\n"
			               "         {command} ADD Botox Botox services.anope.org Botox\n"
			               "         Adds a service bot with nickname \"Botox\", username \"Botox\", hostname \"services.anope.org\", and realname \"Botox\" to the bot list."),
			               "botserv/bot/add", "command"_kw = source.GetCommand());
		else if (subcommand.equals_ci("CHANGE"))
			source.Reply(_("\002{command} CHANGE\002 allows changing the \037nickname\037, \037username\037, \037hostname\037 and \037realname\037 of bot \037oldnickname\037."
			               " If a new username, hostname, or realname is specified, then the bot \037nickname\037 will quit and rejoin all of its channels using the new mask."
			               " Otherwise, the bot simply change nick to \037newnickname\037. All settings on the bot, such as channels and no-bot, are retained."
			               " This command requires the operator privilege for command \002{0}\002."
			               "\n"
			               "Example:\n"
			               "         {command} CHANGE Botox peer connection reset.by peer\n"
			               "          Renames the bot \"Botox\" to \"peer\" with the given username, hostname, and realname."),
			               "botserv/bot/change", "command"_kw = source.GetCommand());
		else if (subcommand.equals_ci("DEL"))
			source.Reply(_("\002{command} DEL\002 removes the bot \037nickname\037 from the bot list. The bot will quit from any channels it is in, and will not be replaced."
			               " This command requires the operator privilege for command \002{0}\002.\n"
			               "\n"
			               "Example:\n"
			               "         {command} DEL peer\n"
			               "         Removes the bot \"peer\" from the bot list."),
			               "botserv/bot/del", "command"_kw = source.GetCommand());
		else
		{
			source.Reply(_("Allows Services Operators to create, modify, and delete bots that users will be able to use on their channels."));

			CommandInfo *help = source.service->FindCommand("generic/help");
			if (help)
				source.Reply(_("\n"
			                       "The \002ADD\002 command adds a bot with the given \037nickname\037, \037username\037, \037hostname\037 and \037realname\037 to the bot list.\n"
			                       "\002{msg}{service} {help} {command} ADD\002 for more information.\n"
			                       "\n"
			                       "The \002CHANGE\002 command allows changing the \037nickname\037, \037username\037, \037hostname\037 and \037realname\037 of bot \037oldnickname\037.\n"
			                       "\002{msg}{service} {help} {command} CHANGE\002 for more information.\n"
			                       "\n"
			                       "The \002{command} DEL\002 removes the bot \037nickname\037 from the bot list.\n"
			                       "\002{msg}{service} {help} {command} DEL\002 for more information."),
			                       "msg"_kw = Config->StrictPrivmsg, "help"_kw = help->cname, "command"_kw = source.GetCommand());
		}
		return true;
	}
};

class BSBot : public Module
{
	CommandBSBot commandbsbot;

 public:
	BSBot(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR)
		, commandbsbot(this)
	{

	}
};

MODULE_INIT(BSBot)