summaryrefslogtreecommitdiff
path: root/src/coremods/core_channel/core_channel.cpp
blob: b989f8778e2ab4a168a5b6b32ffc66745bf62647 (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
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
 *
 * 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 "core_channel.h"
#include "invite.h"
#include "listmode.h"

namespace
{
/** Hook that sends a MODE after a JOIN if the user in the JOIN has some modes prefix set.
 * This happens e.g. when modules such as operprefix explicitly set prefix modes on the joining
 * user, or when a member with prefix modes does a host cycle.
 */
class JoinHook : public ClientProtocol::EventHook
{
	ClientProtocol::Messages::Mode modemsg;
	Modes::ChangeList modechangelist;
	const User* joininguser;

 public:
	/** If true, MODE changes after JOIN will be sourced from the user, rather than the server
	 */
	bool modefromuser;

	JoinHook(Module* mod)
		: ClientProtocol::EventHook(mod, "JOIN")
	{
	}

	void OnEventInit(const ClientProtocol::Event& ev) CXX11_OVERRIDE
	{
		const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
		const Membership& memb = *join.GetMember();

		modechangelist.clear();
		for (std::string::const_iterator i = memb.modes.begin(); i != memb.modes.end(); ++i)
		{
			PrefixMode* const pm = ServerInstance->Modes.FindPrefixMode(*i);
			if (!pm)
				continue; // Shouldn't happen
			modechangelist.push_add(pm, memb.user->nick);
		}

		if (modechangelist.empty())
		{
			// Member got no modes on join
			joininguser = NULL;
			return;
		}

		joininguser = memb.user;

		// Prepare a mode protocol event that we can append to the message list in OnPreEventSend()
		modemsg.SetParams(memb.chan, NULL, modechangelist);
		if (modefromuser)
			modemsg.SetSource(join);
		else
			modemsg.SetSourceUser(ServerInstance->FakeClient);
	}

	ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
	{
		// If joininguser is NULL then they didn't get any modes on join, skip.
		// Also don't show their own modes to them, they get that in the NAMES list not via MODE.
		if ((joininguser) && (user != joininguser))
			messagelist.push_back(&modemsg);
		return MOD_RES_PASSTHRU;
	}
};

}

class CoreModChannel : public Module, public CheckExemption::EventListener
{
	Invite::APIImpl invapi;
	CommandInvite cmdinvite;
	CommandJoin cmdjoin;
	CommandKick cmdkick;
	CommandNames cmdnames;
	CommandTopic cmdtopic;
	Events::ModuleEventProvider evprov;
	JoinHook joinhook;

	ModeChannelBan banmode;
	SimpleChannelModeHandler inviteonlymode;
	ModeChannelKey keymode;
	ModeChannelLimit limitmode;
	SimpleChannelModeHandler moderatedmode;
	SimpleChannelModeHandler noextmsgmode;
	ModeChannelOp opmode;
	SimpleChannelModeHandler privatemode;
	SimpleChannelModeHandler secretmode;
	SimpleChannelModeHandler topiclockmode;
	ModeChannelVoice voicemode;

	insp::flat_map<std::string, char> exemptions;

	ModResult IsInvited(User* user, Channel* chan)
	{
		LocalUser* localuser = IS_LOCAL(user);
		if ((localuser) && (invapi.IsInvited(localuser, chan)))
			return MOD_RES_ALLOW;
		return MOD_RES_PASSTHRU;
	}

 public:
	CoreModChannel()
		: CheckExemption::EventListener(this, UINT_MAX)
		, invapi(this)
		, cmdinvite(this, invapi)
		, cmdjoin(this)
		, cmdkick(this)
		, cmdnames(this)
		, cmdtopic(this)
		, evprov(this, "event/channel")
		, joinhook(this)
		, banmode(this)
		, inviteonlymode(this, "inviteonly", 'i')
		, keymode(this)
		, limitmode(this)
		, moderatedmode(this, "moderated", 'm')
		, noextmsgmode(this, "noextmsg", 'n')
		, opmode(this)
		, privatemode(this, "private", 'p')
		, secretmode(this, "secret", 's')
		, topiclockmode(this, "topiclock", 't')
		, voicemode(this)
	{
	}

	void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
	{
		ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");

		std::string current;
		irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
		insp::flat_map<std::string, char> exempts;
		while (defaultstream.GetToken(current))
		{
			std::string::size_type pos = current.find(':');
			if (pos == std::string::npos || (pos + 2) > current.size())
				throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());

			const std::string restriction = current.substr(0, pos);
			const char prefix = current[pos + 1];

			ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
			exempts[restriction] = prefix;
		}

		ConfigTag* securitytag = ServerInstance->Config->ConfValue("security");
		const std::string announceinvites = securitytag->getString("announceinvites", "dynamic");
		Invite::AnnounceState newannouncestate;
		if (stdalgo::string::equalsci(announceinvites, "none"))
			newannouncestate = Invite::ANNOUNCE_NONE;
		else if (stdalgo::string::equalsci(announceinvites, "all"))
			newannouncestate = Invite::ANNOUNCE_ALL;
		else if (stdalgo::string::equalsci(announceinvites, "ops"))
			newannouncestate = Invite::ANNOUNCE_OPS;
		else if (stdalgo::string::equalsci(announceinvites, "dynamic"))
			newannouncestate = Invite::ANNOUNCE_DYNAMIC;
		else
			throw ModuleException(announceinvites + " is an invalid <security:announceinvites> value, at " + securitytag->getTagLocation());

		// Config is valid, apply it

		// Validates and applies <maxlist> tags, so do it first
		banmode.DoRehash();

		exemptions.swap(exempts);
		// In 2.0 we allowed limits of 0 to be set. This is non-standard behaviour
		// and will be removed in the next major release.
		limitmode.minlimit = optionstag->getBool("allowzerolimit", true) ? 0 : 1;;
		cmdinvite.announceinvites = newannouncestate;
		joinhook.modefromuser = optionstag->getBool("cyclehostsfromuser");

		Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
		if (optionstag->getBool("invitebypassmodes", true))
			ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
		else
		{
			for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
				ServerInstance->Modules.Detach(events[i], this);
		}
	}

	void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
	{
		tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen);

		insp::flat_map<int, std::string> limits;
		std::string vlist;
		const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
		for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
		{
			ListModeBase* lm = *iter;

			const unsigned int limit = lm->GetLowerLimit();
			limits[limit].push_back(lm->GetModeChar());

			if (lm->HasVariableLength())
				vlist.push_back(lm->GetModeChar());
		}

		std::string& buffer = tokens["MAXLIST"];
		for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
		{
			if (!buffer.empty())
				buffer.push_back(',');

			std::string modes(iter->second);
			std::sort(modes.begin(), modes.end());

			buffer.append(modes);
			buffer.push_back(':');
			buffer.append(ConvToStr(iter->first));
		}

		if (!vlist.empty())
		{
			tokens["VBANLIST"]; // deprecated
			tokens["VLIST"] = vlist;
		}
	}

	ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string&, std::string&, const std::string& keygiven) CXX11_OVERRIDE
	{
		if (!chan)
			return MOD_RES_PASSTHRU;

		// Check whether the channel key is correct.
		const std::string ckey = chan->GetModeParameter(&keymode);
		if (!ckey.empty())
		{
			ModResult MOD_RESULT;
			FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, keygiven));
			if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, keygiven)))
			{
				// If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
				user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (incorrect channel key)");
				return MOD_RES_DENY;
			}
		}

		// Check whether the invite only mode is set.
		if (chan->IsModeSet(inviteonlymode))
		{
			ModResult MOD_RESULT;
			FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
			if (MOD_RESULT != MOD_RES_ALLOW)
			{
				user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (invite only)");
				return MOD_RES_DENY;
			}
		}

		// Check whether the limit would be exceeded by this user joining.
		if (chan->IsModeSet(limitmode))
		{
			ModResult MOD_RESULT;
			FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
			if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast<size_t>(limitmode.ext.get(chan))))
			{
				user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (channel is full)");
				return MOD_RES_DENY;
			}
		}

		// Everything looks okay.
		return MOD_RES_PASSTHRU;
	}

	void OnPostJoin(Membership* memb) CXX11_OVERRIDE
	{
		Channel* const chan = memb->chan;
		LocalUser* const localuser = IS_LOCAL(memb->user);
		if (localuser)
		{
			// Remove existing invite, if any
			invapi.Remove(localuser, chan);

			if (chan->topic.length())
				Topic::ShowTopic(localuser, chan);

			// Show all members of the channel, including invisible (+i) users
			cmdnames.SendNames(localuser, chan, true);
		}
	}

	ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
	{
		// Hook only runs when being invited bypasses +bkl
		return IsInvited(user, chan);
	}

	ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
	{
		// Hook only runs when being invited bypasses +bkl
		return IsInvited(user, chan);
	}

	ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
	{
		// Hook only runs when being invited bypasses +bkl
		return IsInvited(user, chan);
	}

	ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
	{
		// Hook always runs
		return IsInvited(user, chan);
	}

	void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
	{
		invapi.RemoveAll(user);
	}

	void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
	{
		// Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
		invapi.RemoveAll(chan);
	}

	ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
	{
		if (!exemptions.count(restriction))
			return MOD_RES_PASSTHRU;

		unsigned int mypfx = chan->GetPrefixValue(user);
		char minmode = exemptions[restriction];

		PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
		if (mh && mypfx >= mh->GetPrefixRank())
			return MOD_RES_ALLOW;
		if (mh || minmode == '*')
			return MOD_RES_DENY;
		return MOD_RES_PASSTHRU;
	}

	void Prioritize() CXX11_OVERRIDE
	{
		ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
		ServerInstance->Modules.SetPriority(this, I_OnUserPreJoin, PRIORITY_LAST);
	}

	Version GetVersion() CXX11_OVERRIDE
	{
		return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
	}
};

MODULE_INIT(CoreModChannel)