aboutsummaryrefslogtreecommitdiff
path: root/modules/sacommands.cpp
blob: 4450b5608986b2fdffef4a413dc3e1270e8616d4 (about) (plain) (blame)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2026 Sadie Powell <sadie@witchery.services>
 *
 * This file is part of InspIRCd.  InspIRCd 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 <http://www.gnu.org/licenses/>.
 */


#include "inspircd.h"

#include "inspircd.h"
#include "clientprotocolmsg.h"
#include "numerichelper.h"
#include "utility/string.h"

struct SharedData final
{
	// A reference to the +k user mode from the servicdes.
	UserModeReference servprotectmode;

	// Whether to sign messages with the executing operator's nick.
	bool sign = true;

	SharedData(const WeakModulePtr& mod)
		: servprotectmode(mod, "protect")
	{
	}
};

class CommandSAJoin final
	: public Command
{
private:
	SharedData& data;

public:
	CommandSAJoin(const WeakModulePtr& mod, SharedData& sd)
		: Command(mod, "SAJOIN", 1)
		, data(sd)
	{
		accepts_multiple_targets = true;
		access_needed = CmdAccess::OPERATOR;
		syntax = { "[<nick>] <channel>[,<channel>]+" };
		translation = { TranslateType::NICK, TranslateType::TEXT };
	}

	CmdResult Handle(User* user, const Params& parameters) override
	{
		const unsigned int channelindex = (parameters.size() > 1) ? 1 : 0;
		if (CommandParser::LoopCall(user, this, parameters, channelindex))
			return CmdResult::FAILURE;

		User* target = user;
		if (parameters.size() > 1)
		{
			const auto& targetnick = parameters[0];
			target = ServerInstance->Users.Find(targetnick, true);
			if (!target)
			{
				user->WriteRemoteNumeric(Numerics::NoSuchNick(targetnick));
				return CmdResult::FAILURE;
			}
		}

		if (user != target)
		{
			// Force joining another user requires a special permission.
			if (!user->HasPrivPermission("users/sajoin-others"))
			{
				user->WriteRemoteNumeric(Numerics::NoPrivileges("your server operator account does not have the users/sajoin-others privilege"));
				return CmdResult::FAILURE;
			}
			if (target->IsModeSet(data.servprotectmode))
			{
				user->WriteRemoteNumeric(Numerics::NoPrivileges("you can not use the {} command on a protected service", this->service_name));
				return CmdResult::FAILURE;
			}
		}

		const auto& targetchan = parameters[channelindex];
		if (user->IsLocal() && !ServerInstance->Channels.IsChannel(targetchan))
		{
			user->WriteRemoteNumeric(ERR_BADCHANMASK, targetchan, "Invalid channel name");
			return CmdResult::FAILURE;
		}

		auto* chan = ServerInstance->Channels.Find(targetchan);
		if (chan && chan->HasUser(target))
		{
			user->WriteRemoteNumeric(ERR_USERONCHANNEL, target->nick, chan->name, "is already on channel");
			return CmdResult::FAILURE;
		}

		auto* ltarget = target->AsLocal();
		if (!ltarget)
			return CmdResult::SUCCESS; // User will be handled by their own server.

		auto* memb = Channel::JoinUser(ltarget, targetchan, true);
		if (!memb)
		{
			user->WriteRemoteNotice("*** {}: could not join {} to {}.", this->service_name, target->nick, targetchan);
			return CmdResult::FAILURE;
		}

		ServerInstance->SNO.WriteGlobalSno('a', "{} ({}) used {} to make {} join {}", user->nick,
			user->oper->GetName(), this->service_name, target->nick, memb->chan->name);

		return CmdResult::SUCCESS;
	}

	RouteDescriptor GetRouting(User* user, const Params& parameters) override
	{
		return ROUTE_OPT_UCAST(parameters[0]);
	}
};

class CommandSAKick final
	: public Command
{
private:
	SharedData& data;

public:
	CommandSAKick(const WeakModulePtr& mod, SharedData& sd)
		: Command(mod, "SAKICK", 2, 3)
		, data(sd)
	{
		access_needed = CmdAccess::OPERATOR;
		allow_empty_last_param = true;
		syntax = { "<channel> <nick> [:<reason>]" };
		translation = { TranslateType::TEXT, TranslateType::NICK, TranslateType::TEXT };
	}

	CmdResult Handle(User* user, const Params& parameters) override
	{
		const auto& targetnick = parameters[1];
		auto* target = ServerInstance->Users.Find(targetnick, true);
		if (!target)
		{
			user->WriteRemoteNumeric(Numerics::NoSuchNick(targetnick));
			return CmdResult::FAILURE;
		}
		if (target->IsModeSet(data.servprotectmode))
		{
			user->WriteRemoteNumeric(Numerics::NoPrivileges("you can not use the {} command on a protected service", this->service_name));
			return CmdResult::FAILURE;
		}

		const auto& targetchan = parameters[1];
		auto* chan = ServerInstance->Channels.Find(targetchan);
		if (!chan)
		{
			user->WriteRemoteNumeric(Numerics::NoSuchChannel(targetchan));
			return CmdResult::FAILURE;
		}
		if (!chan->HasUser(target))
		{
			user->WriteRemoteNumeric(ERR_USERNOTINCHANNEL, target->nick, chan->name, "They are not on that channel");
			return CmdResult::FAILURE;
		}

		if (!target->IsLocal())
			return CmdResult::SUCCESS; // User will be handled by their own server.

		const std::string reason(parameters.size() > 2 ? parameters.back() : target->nick, 0, ServerInstance->Config->Limits.MaxKick);
		chan->KickUser(user, target, data.sign ? FMT::format("{} ({})", reason, user->nick) : reason);

		ServerInstance->SNO.WriteGlobalSno('a', "{} ({}) used {} to kick {} from {} ({})", user->nick,
			user->oper->GetName(), this->service_name, target->nick, chan->name, reason);

		return CmdResult::SUCCESS;
	}

	RouteDescriptor GetRouting(User* user, const Params& parameters) override
	{
		return ROUTE_OPT_UCAST(parameters[1]);
	}
};

class CommandSAMode final
	: public Command
{
private:
	SharedData& data;
	bool logged = false;

public:
	bool active = false;

	CommandSAMode(const WeakModulePtr& mod, SharedData& sd)
		: Command(mod, "SAMODE", 2)
		, data(sd)
	{
		access_needed = CmdAccess::OPERATOR;
		syntax = { "<target> (+|-)<modes> [<mode-parameters>]" };
	}

	CmdResult Handle(User* user, const Params& parameters) override
	{
		const auto& targetstr = parameters[0];
		if (!ServerInstance->Channels.IsPrefix(targetstr[0]))
		{
			auto* target = ServerInstance->Users.FindNick(targetstr, true);
			if (!target)
			{
				user->WriteNumeric(Numerics::NoSuchNick(targetstr));
				return CmdResult::FAILURE;
			}
			if (target->IsModeSet(data.servprotectmode))
			{
				user->WriteRemoteNumeric(Numerics::NoPrivileges("you can not use the {} command on a protected service", this->service_name));
				return CmdResult::FAILURE;
			}

			// Changing the modes of another user requires a special permission.
			if (target != user && !user->HasPrivPermission("users/samode-usermodes"))
			{
				user->WriteRemoteNumeric(Numerics::NoPrivileges("your server operator account does not have the users/samode-usermodes privilege"));
				return CmdResult::FAILURE;
			}
		}

		// XXX: Make ModeParser clear LastParse
		Modes::ChangeList emptychangelist;
		ServerInstance->Modes.ProcessSingle(ServerInstance->FakeClient, nullptr, ServerInstance->FakeClient, emptychangelist);

		this->logged = false;

		this->active = true;
		ServerInstance->Parser.CallHandler("MODE", parameters, user);
		this->active = false;

		if (!this->logged)
		{
			// If we haven't logged anything yet then the client queried the list of a listmode
			// (e.g. /SAMODE #chan b), which was handled internally by the MODE command handler.
			//
			// Viewing the modes of a user or a channel could also result in this, but that is not
			// possible with /SAMODE because we require at least 2 parameters.
			this->LogUsage(user, targetstr, insp::join(insp::iterator_range(parameters.begin() + 1, parameters.end())));
		}

		return CmdResult::SUCCESS;
	}

	void LogUsage(const User* user, const std::string& target, const std::string& modes)
	{
		this->logged = true;
		ServerInstance->SNO.WriteGlobalSno('a', "{} ({}) used {} to change the modes of {} to {}", user->nick,
			user->oper->GetName(), this->service_name, target, modes);
	}
};

class CommandSANick final
	: public Command
{
private:
	SharedData& data;

public:
	CommandSANick(const WeakModulePtr& mod, SharedData& sd)
		: Command(mod, "SANICK", 2)
		, data(sd)
	{
		access_needed = CmdAccess::OPERATOR;
		allow_empty_last_param = true;
		syntax = { "<nick> <newnick>" };
		translation = { TranslateType::NICK, TranslateType::TEXT };
	}

	CmdResult Handle(User* user, const Params& parameters) override
	{
		const auto& targetnick = parameters[0];
		auto* target = ServerInstance->Users.Find(targetnick, true);
		if (!target)
		{
			user->WriteRemoteNumeric(Numerics::NoSuchNick(targetnick));
			return CmdResult::FAILURE;
		}
		if (target->IsModeSet(data.servprotectmode))
		{
			user->WriteRemoteNumeric(Numerics::NoPrivileges("you can not use the {} command on a protected service", this->service_name));
			return CmdResult::FAILURE;
		}

		auto targetnewnick = parameters[1];
		if (targetnewnick.empty())
		{
			user->WriteRemoteNumeric(ERR_NONICKNAMEGIVEN, "No nickname given");
			return CmdResult::FAILURE;
		}

		if (targetnewnick == "0")
			targetnewnick = target->uuid;
		else if (!ServerInstance->Users.IsNick(targetnewnick))
		{
			user->WriteRemoteNumeric(ERR_ERRONEUSNICKNAME, targetnewnick, "Erroneous nickname");
			return CmdResult::FAILURE;
		}

		if (ServerInstance->Users.FindNick(targetnewnick))
		{
			user->WriteRemoteNumeric(ERR_NICKNAMEINUSE, targetnewnick, "Nickname is already in use.");
			return CmdResult::FAILURE;
		}

		if (!target->IsLocal())
			return CmdResult::SUCCESS; // User will be handled by their own server.

		if (!target->ChangeNick(targetnewnick))
		{
			user->WriteRemoteNotice("*** {}: could not change the nick of {} to {}.", this->service_name,
				targetnick, targetnewnick);
			return CmdResult::FAILURE;
		}

		ServerInstance->SNO.WriteGlobalSno('a', "{} ({}) used {} to change the nick of {} to {}", user->nick,
			user->oper->GetName(), this->service_name, targetnick, targetnewnick);

		return CmdResult::SUCCESS;
	}

	RouteDescriptor GetRouting(User* user, const Params& parameters) override
	{
		return ROUTE_OPT_UCAST(parameters[0]);
	}
};

class CommandSAPart final
	: public Command
{
private:
	SharedData& data;

public:
	CommandSAPart(const WeakModulePtr& mod, SharedData& sd)
		: Command(mod, "SAPART", 2, 3)
		, data(sd)
	{
		accepts_multiple_targets = true;
		access_needed = CmdAccess::OPERATOR;
		allow_empty_last_param = true;
		syntax = { "<nick> <channel>[,<channel>]+ [:<reason>]" };
		translation = { TranslateType::NICK, TranslateType::TEXT, TranslateType::TEXT };
	}

	CmdResult Handle(User* user, const Params& parameters) override
	{
		if (CommandParser::LoopCall(user, this, parameters, 1))
			return CmdResult::FAILURE;

		const auto& targetnick = parameters[0];
		auto* target = ServerInstance->Users.Find(targetnick, true);
		if (!target)
		{
			user->WriteRemoteNumeric(Numerics::NoSuchNick(targetnick));
			return CmdResult::FAILURE;
		}
		if (target->IsModeSet(data.servprotectmode))
		{
			user->WriteRemoteNumeric(Numerics::NoPrivileges("you can not use the {} command on a protected service", this->service_name));
			return CmdResult::FAILURE;
		}

		const auto& targetchan = parameters[0];
		auto* chan = ServerInstance->Channels.Find(targetchan);
		if (!chan)
		{
			user->WriteRemoteNumeric(Numerics::NoSuchChannel(targetchan));
			return CmdResult::FAILURE;
		}

		if (!target->IsLocal())
			return CmdResult::SUCCESS; // User will be handled by their own server.

		const auto& reason = parameters.size() > 2 ? parameters.back() : "";
		if (!chan->PartUser(target, data.sign ? FMT::format("{}{}({})", reason, reason.empty() ? "" : " ", user->nick) : reason))
		{
			user->WriteRemoteNumeric(ERR_USERNOTINCHANNEL, target->nick, chan->name, "They are not on that channel");
			return CmdResult::FAILURE;
		}

		ServerInstance->SNO.WriteGlobalSno('a', "{} ({}) used {} to part {} from {} ({})", user->nick,
			user->oper->GetName(), this->service_name, target->nick, chan->name, reason);

		return CmdResult::FAILURE;
	}

	RouteDescriptor GetRouting(User* user, const Params& parameters) override
	{
		return ROUTE_OPT_UCAST(parameters[0]);
	}
};

class CommandSAQuit final
	: public Command
{
private:
	SharedData& data;

public:
	CommandSAQuit(const WeakModulePtr& mod, SharedData& sd)
		: Command(mod, "SAQUIT", 2, 2)
		, data(sd)
	{
		access_needed = CmdAccess::OPERATOR;
		allow_empty_last_param = true;
		syntax = { "<nick> :<reason>" };
		translation = { TranslateType::NICK, TranslateType::TEXT };
	}

	CmdResult Handle(User* user, const Params& parameters) override
	{
		const auto& targetnick = parameters[0];
		auto* target = ServerInstance->Users.Find(targetnick, true);
		if (!target)
		{
			user->WriteRemoteNumeric(Numerics::NoSuchNick(targetnick));
			return CmdResult::FAILURE;
		}
		if (target->IsModeSet(data.servprotectmode))
		{
			user->WriteRemoteNumeric(Numerics::NoPrivileges("you can not use the {} command on a protected service", this->service_name));
			return CmdResult::FAILURE;
		}

		if (!target->IsLocal())
			return CmdResult::SUCCESS; // User will be handled by their own server.

		const auto& reason = parameters.size() > 2 ? parameters.back() : "";
		ServerInstance->Users.QuitUser(target, data.sign ? FMT::format("{}{}({})", reason, reason.empty() ? "" : " ", user->nick) : reason);

		ServerInstance->SNO.WriteGlobalSno('a', "{} ({}) used {} to quit {} ({})", user->nick,
			user->oper->GetName(), this->service_name, target->nick, reason);

		return CmdResult::FAILURE;
	}

	RouteDescriptor GetRouting(User* user, const Params& parameters) override
	{
		return ROUTE_OPT_UCAST(parameters[0]);
	}
};


class CommandSATopic final
	: public Command
{
public:
	CommandSATopic(const WeakModulePtr& mod)
		: Command(mod, "SATOPIC", 2, 2)
	{
		access_needed = CmdAccess::OPERATOR;
		allow_empty_last_param = true;
		syntax = { "<channel> :<topic>" };
	}

	CmdResult Handle(User* user, const Params& parameters) override
	{
		auto* target = ServerInstance->Channels.Find(parameters[0]);
		if (!target)
		{
			user->WriteRemoteNumeric(Numerics::NoSuchChannel(parameters[0]));
			return CmdResult::FAILURE;
		}

		const std::string topic(parameters[1], 0, ServerInstance->Config->Limits.MaxTopic);
		if (target->topic == topic)
		{
			user->WriteRemoteNotice("*** {}: The topic on {} is already what you are trying to change it to.",
				this->service_name, target->name);
			return CmdResult::SUCCESS;
		}

		target->SetTopic(user, topic, ServerInstance->Time(), nullptr);

		ServerInstance->SNO.WriteGlobalSno('a', "{} ({}) used {} to change the topic of {} ({})", user->nick,
			user->oper->GetName(), this->service_name, target->name, topic);

		return CmdResult::SUCCESS;
	}
};

class ModuleSACommands final
	: public Module
{
private:
	SharedData data;
	CommandSAJoin cmdsajoin;
	CommandSAKick cmdsakick;
	CommandSAMode cmdsamode;
	CommandSANick cmdsanick;
	CommandSAPart cmdsapart;
	CommandSAQuit cmdsaquit;
	CommandSATopic cmdsatopic;

public:
	ModuleSACommands()
		: Module(VF_VENDOR, "Adds various server operator-only versions of regular commands.")
		, data(weak_from_this())
		, cmdsajoin(weak_from_this(), data)
		, cmdsakick(weak_from_this(), data)
		, cmdsamode(weak_from_this(), data)
		, cmdsanick(weak_from_this(), data)
		, cmdsapart(weak_from_this(), data)
		, cmdsaquit(weak_from_this(), data)
		, cmdsatopic(weak_from_this())
	{
	}

	void ReadConfig(ConfigStatus& status) override
	{
		const auto& tag = ServerInstance->Config->ConfValue("sacommands");
		data.sign = tag->getBool("sign", true);
	}

	void Prioritize() override
	{
		ServerInstance->Modules.SetPriority(shared_from_this(), I_OnRawMode, PRIORITY_BEFORE, "disable");
		ServerInstance->Modules.SetPriority(shared_from_this(), I_OnPreMode, PRIORITY_BEFORE, "override");
	}

	ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) override
	{
		if (cmdsamode.active)
			return MOD_RES_ALLOW;
		return MOD_RES_PASSTHRU;
	}

	void OnMode(User* user, User* destuser, Channel* destchan, const Modes::ChangeList& modes, ModeParser::ModeProcessFlag processflags) override
	{
		if (!cmdsamode.active)
			return;


		auto modestr = ClientProtocol::Messages::Mode::ToModeLetters(modes);
		for (const auto& item : modes.getlist())
		{
			if (!item.param.empty())
				modestr.append(1, ' ').append(item.param);
		}

		cmdsamode.LogUsage(user, destuser ? destuser->nick : destchan->name, modestr);
	}
};

MODULE_INIT(ModuleSACommands)