aboutsummaryrefslogtreecommitdiff
path: root/modules/redirect.cpp
blob: 0797957b3bc76a0ef5044b607559d70c226688dd (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
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2019-2023, 2026 Sadie Powell <sadie@witchery.services>
 *   Copyright (C) 2017 Dylan Frank <b00mx0r@aureus.pw>
 *   Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
 *   Copyright (C) 2012, 2014 Shawn Smith <ShawnSmith0828@gmail.com>
 *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
 *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
 *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
 *   Copyright (C) 2006 Craig Edwards <brain@inspircd.org>
 *
 * 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 "extension.h"
#include "modules/extban.h"
#include "numerichelper.h"

enum
{
	// From UnrealIRCd?
	ERR_LINKCHANNEL = 470,

	// InspIRCd-specific.
	ERR_REDIRECT = 690,
};

namespace
{
	Channel* CheckRedirect(User* source, Channel* channel, const std::string& parameter)
	{
		if (!ServerInstance->Channels.IsChannel(parameter))
		{
			source->WriteNumeric(Numerics::NoSuchChannel(parameter));
			return nullptr;
		}

		auto* c = ServerInstance->Channels.Find(parameter);
		if (!source->IsOper())
		{
			if (!c)
			{
				source->WriteNumeric(ERR_REDIRECT, parameter, FMT::format("Target channel {} must exist to be set as a redirect.", parameter));
				return nullptr;
			}
			else if (c->GetPrefixValue(source) < OP_VALUE)
			{
				source->WriteNumeric(ERR_REDIRECT, c->name, FMT::format("You must be opped on {} to set it as a redirect.", c->name));
				return nullptr;
			}
		}
		return c;
	}
}

class RedirectExtBan final
	: public ExtBan::Acting
{
private:
	bool SplitBan(const std::string& text, std::string& chan, std::string& mask)
	{
		auto sep = text.find(':');
		if (sep == std::string::npos || sep == 0 || sep+1 >= text.length())
			return false; // Malformed.

		chan.assign(text, 0, sep);
		mask.assign(text, sep + 1);
		return true;
	}

public:
	std::string matchchan;

	RedirectExtBan(const WeakModulePtr& mod)
		: ExtBan::Acting(mod, "redirect", 'd')
	{
	}

	bool IsMatch(ListModeBase* lm, User* user, Channel* channel, const std::string& text, const ExtBan::MatchConfig& config) override
	{
		std::string target, mask;
		if (!SplitBan(text, target, mask))
			return false; // Malformed.

		matchchan = target;
		return ExtBan::Acting::IsMatch(lm, user, channel, mask, config);
	}

	bool Validate(ListModeBase* lm, LocalUser* user, Channel* channel, std::string& text) override
	{
		std::string target, mask;
		if (!SplitBan(text, target, mask))
		{
			user->WriteNumeric(ERR_REDIRECT, text, "Redirect extban must be in the format <chan>:<mask>.");
			return false; // Malformed.
		}

		auto* targetchan = CheckRedirect(user, channel, target);
		if (!targetchan)
			return false; // Bad target.

		Canonicalize(mask);
		text = FMT::format("{}:{}", targetchan->name, mask);
		return true;
	}
};

class RedirectMode final
	: public ParamMode<RedirectMode, StringExtItem>
{
public:
	RedirectMode(const WeakModulePtr& mod)
		: ParamMode<RedirectMode, StringExtItem>(mod, "redirect", 'L')
	{
		syntax = "<target>";
	}

	bool OnSet(User* source, Channel* channel, std::string& parameter) override
	{
		if (source->IsLocal())
		{
			auto* targetchan = CheckRedirect(source, channel, parameter);
			if (!targetchan)
				return false;

			parameter = targetchan->name;
		}

		ext.Set(channel, parameter);
		return true;
	}

	void SerializeParam(Channel* chan, const std::string* str, std::string& out)
	{
		out += *str;
	}
};

class BanRedirect final
	: public ModeWatcher
{
private:
	RedirectExtBan& redirectextban;

public:
	BanRedirect(const WeakModulePtr& mod, RedirectExtBan& extban)
		: ModeWatcher(mod, "ban", MODETYPE_CHANNEL)
		, redirectextban(extban)
	{
	}

	bool BeforeMode(User* source, User* dest, Channel* channel, Modes::Change& change) override
	{
		if (!change.adding || change.param.empty() || !source->IsLocal())
			return true; // Nothing to do.

		bool xbinverted; // Unused
		std::string xbname, xbvalue; // Unused
		if (ExtBan::Parse(change.param, xbname, xbvalue, xbinverted))
			return true; // Don't touch extbans

		const auto pos_of_pling = change.param.find_first_of('!');
		const auto pos_of_at = change.param.find_first_of('@', pos_of_pling == std::string::npos ? 0 : pos_of_pling);
		const auto pos_of_hash = change.param.find_first_of('#', pos_of_at == std::string::npos ? 0 : pos_of_at);
		if (pos_of_hash != std::string::npos)
		{
			// Rewrite old banredirect entries from foo!bar@baz#chan to redirect:#chan:foo!bar@baz
			change.param = FMT::format("{}:{}:{}", redirectextban.service_name,
				change.param.substr(pos_of_hash), change.param.substr(0, pos_of_hash));
		}
		return true;
	}
};

class ModuleRedirect final
	: public Module
{
private:
	bool action_ban;
	bool action_inviteonly;
	bool action_key;
	bool action_limit;
	std::string activechan;
	SimpleUserMode antiredirectmode;
	ChanModeReference banmode;
	ChanModeReference inviteonlymode;
	ChanModeReference keymode;
	ChanModeReference limitmode;
	RedirectExtBan redirectextban;
	RedirectMode redirectmode;
	BanRedirect banredirect;

	ModResult HandleRedirect(LocalUser* user, Channel* chan, const std::string& reason, bool param = true)
	{
		const auto* channel = param ? redirectmode.ext.Get(chan) : &redirectextban.matchchan;
		if (!channel)
			return MOD_RES_PASSTHRU; // Should never happen.

		// Sometimes broken services can make circular or chained +L, avoid this.
		if (!activechan.empty())
		{
			user->WriteNumeric(ERR_LINKCHANNEL, activechan, channel, "You may not join this channel. A redirect is set, but you cannot be redirected as it is a circular loop.");
			return MOD_RES_PASSTHRU;
		}

		if (user->IsModeSet(antiredirectmode))
		{
			user->WriteNumeric(ERR_LINKCHANNEL, chan->name, *channel,
				FMT::format("You cannot join {} ({}) and you cannot be redirected to {} as you have user mode +{} ({}) set.",
					chan->name, reason, *channel, antiredirectmode.GetModeChar(), antiredirectmode.service_name));
			return MOD_RES_DENY;
		}

		user->WriteNumeric(ERR_LINKCHANNEL, chan->name, *channel,
			FMT::format("You cannot join {} ({}) so you are being automatically transferred to {}.",
				chan->name, reason, *channel));

		activechan = chan->name;
		Channel::JoinUser(user, *channel);
		activechan.clear();

		return MOD_RES_DENY;
	}

public:
	ModuleRedirect()
		: Module(VF_VENDOR, "Allows users to be redirected to another channel.")
		, antiredirectmode(weak_from_this(), "antiredirect", 'L')
		, banmode(weak_from_this(), "ban")
		, inviteonlymode(weak_from_this(), "inviteonly")
		, keymode(weak_from_this(), "key")
		, limitmode(weak_from_this(), "limit")
		, redirectextban(weak_from_this())
		, redirectmode(weak_from_this())
		, banredirect(weak_from_this(), redirectextban)
	{
	}

	void ReadConfig(ConfigStatus&) override
	{
		const auto& tag = ServerInstance->Config->ConfValue("redirect");
		action_ban = tag->getBool("ban", false);
		action_inviteonly = tag->getBool("inviteonly", true);
		action_key = tag->getBool("key", true);
		action_limit = tag->getBool("limit", true);
	}

	ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, PrefixMode::Set& privs, const std::string& keygiven, bool override) override
	{
		if (override || !chan)
			return MOD_RES_PASSTHRU; // No redirect possible.

		auto modres = redirectextban.GetStatus(user, chan);
		if (modres == MOD_RES_DENY)
			return HandleRedirect(user, chan, "you're extbanned", false);

		if (!chan->IsModeSet(redirectmode))
			return MOD_RES_PASSTHRU; // All others require the mode.

		if (action_ban && chan->CheckList(*banmode, user))
			return HandleRedirect(user, chan, "you're banned");

		if (action_inviteonly && chan->IsModeSet(inviteonlymode))
		{
			FIRST_MOD_RESULT(OnCheckInvite, modres, (user, chan));
			if (modres != MOD_RES_ALLOW)
				return HandleRedirect(user, chan, "invite only");
		}

		if (action_key)
		{
			const auto key = chan->GetModeParameter(keymode);
			if (!key.empty())
			{
				FIRST_MOD_RESULT(OnCheckKey, modres, (user, chan, keygiven));
				if (!modres.check(InspIRCd::TimingSafeCompare(key, keygiven)))
					return HandleRedirect(user, chan, "incorrect channel key");
			}
		}

		if (action_limit)
		{
			const auto limit = chan->GetModeParameter(limitmode);
			if (!limit.empty())
			{
				FIRST_MOD_RESULT(OnCheckLimit, modres, (user, chan));
				if (!modres.check(chan->GetUsers().size() < ConvToNum<size_t>(limit)))
					return HandleRedirect(user, chan, "limit reached");
			}
		}

		return MOD_RES_PASSTHRU;
	}
};

MODULE_INIT(ModuleRedirect)