summaryrefslogtreecommitdiff
path: root/src/coremods/core_list.cpp
blob: d925e7c8249de607009974cfb5f83d9e45ed1e7d (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
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
 *   Copyright (C) 2015 Daniel Vassdal <shutter@canternet.org>
 *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.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) 2007 Dennis Friis <peavey@inspircd.org>
 *   Copyright (C) 2006-2007, 2010 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"

/** Handle /LIST.
 */
class CommandList : public Command
{
 private:
	ChanModeReference secretmode;
	ChanModeReference privatemode;

	/** Parses the creation time or topic set time out of a LIST parameter.
	 * @param value The parameter containing a minute count.
	 * @return The UNIX time at \p value minutes ago.
	 */
	time_t ParseMinutes(const std::string& value)
	{
		time_t minutes = ConvToNum<time_t>(value.c_str() + 2);
		if (!minutes)
			return 0;
		return ServerInstance->Time() - (minutes * 60);
	}

 public:
	/** Constructor for list.
	 */
	CommandList(Module* parent)
		: Command(parent,"LIST", 0, 0)
		, secretmode(creator, "secret")
		, privatemode(creator, "private")
	{
		allow_empty_last_param = false;
		Penalty = 5;
	}

	/** Handle command.
	 * @param parameters The parameters to the command
	 * @param user The user issuing the command
	 * @return A value from CmdResult to indicate command success or failure.
	 */
	CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
};


/** Handle /LIST
 */
CmdResult CommandList::Handle(User* user, const Params& parameters)
{
	// C: Searching based on creation time, via the "C<val" and "C>val" modifiers
	// to search for a channel creation time that is lower or higher than val
	// respectively.
	time_t mincreationtime = 0;
	time_t maxcreationtime = 0;

	// M: Searching based on mask.
	// N: Searching based on !mask.
	bool match_name_topic = false;
	bool match_inverted = false;
	const char* match = NULL;

	// T: Searching based on topic time, via the "T<val" and "T>val" modifiers to
	// search for a topic time that is lower or higher than val respectively.
	time_t mintopictime = 0;
	time_t maxtopictime = 0;

	// U: Searching based on user count within the channel, via the "<val" and
	// ">val" modifiers to search for a channel that has less than or more than
	// val users respectively.
	size_t minusers = 0;
	size_t maxusers = 0;

	for (Params::const_iterator iter = parameters.begin(); iter != parameters.end(); ++iter)
	{
		const std::string& constraint = *iter;
		if (constraint[0] == '<')
		{
			maxusers = ConvToNum<size_t>(constraint.c_str() + 1);
		}
		else if (constraint[0] == '>')
		{
			minusers = ConvToNum<size_t>(constraint.c_str() + 1);
		}
		else if (!constraint.compare(0, 2, "C<", 2) || !constraint.compare(0, 2, "c<", 2))
		{
			mincreationtime = ParseMinutes(constraint);
		}
		else if (!constraint.compare(0, 2, "C>", 2) || !constraint.compare(0, 2, "c>", 2))
		{
			maxcreationtime = ParseMinutes(constraint);
		}
		else if (!constraint.compare(0, 2, "T<", 2) || !constraint.compare(0, 2, "t<", 2))
		{
			mintopictime = ParseMinutes(constraint);
		}
		else if (!constraint.compare(0, 2, "T>", 2) || !constraint.compare(0, 2, "t>", 2))
		{
			maxtopictime = ParseMinutes(constraint);
		}
		else
		{
			// If the glob is prefixed with ! it is inverted.
			match = constraint.c_str();
			if (match[0] == '!')
			{
				match_inverted = true;
				match += 1;
			}

			// Ensure that the user didn't just run "LIST !".
			if (match[0])
				match_name_topic = true;
		}
	}

	const bool has_privs = user->HasPrivPermission("channels/auspex");

	user->WriteNumeric(RPL_LISTSTART, "Channel", "Users Name");
	const chan_hash& chans = ServerInstance->GetChans();
	for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
	{
		Channel* const chan = i->second;

		// Check the user count if a search has been specified.
		const size_t users = chan->GetUserCounter();
		if ((minusers && users <= minusers) || (maxusers && users >= maxusers))
			continue;

		// Check the creation ts if a search has been specified.
		const time_t creationtime = chan->age;
		if ((mincreationtime && creationtime <= mincreationtime) || (maxcreationtime && creationtime >= maxcreationtime))
			continue;

		// Check the topic ts if a search has been specified.
		const time_t topictime = chan->topicset;
		if ((mintopictime && (!topictime || topictime <= mintopictime)) || (maxtopictime && (!topictime || topictime >= maxtopictime)))
			continue;

		// Attempt to match a glob pattern.
		if (match_name_topic)
		{
			bool matches = InspIRCd::Match(chan->name, match) || InspIRCd::Match(chan->topic, match);

			// The user specified an match that we did not match.
			if (!matches && !match_inverted)
				continue;

			// The user specified an inverted match that we did match.
			if (matches && match_inverted)
				continue;
		}

		// if the channel is not private/secret, OR the user is on the channel anyway
		bool n = (has_privs || chan->HasUser(user));

		// If we're not in the channel and +s is set on it, we want to ignore it
		if ((n) || (!chan->IsModeSet(secretmode)))
		{
			if ((!n) && (chan->IsModeSet(privatemode)))
			{
				// Channel is private (+p) and user is outside/not privileged
				user->WriteNumeric(RPL_LIST, '*', users, "");
			}
			else
			{
				/* User is in the channel/privileged, channel is not +s */
				user->WriteNumeric(RPL_LIST, chan->name, users, InspIRCd::Format("[+%s] %s", chan->ChanModes(n), chan->topic.c_str()));
			}
		}
	}
	user->WriteNumeric(RPL_LISTEND, "End of channel list.");

	return CMD_SUCCESS;
}

class CoreModList : public Module
{
 private:
	CommandList cmd;

 public:
	CoreModList()
		: cmd(this)
	{
	}

	void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
	{
		tokens["ELIST"] = "CMNTU";
		tokens["SAFELIST"];
	}

	Version GetVersion() CXX11_OVERRIDE
	{
		return Version("Provides the LIST command", VF_VENDOR|VF_CORE);
	}
};

MODULE_INIT(CoreModList)