2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2015 Daniel Vassdal <shutter@canternet.org>
6 * Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
10 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11 * Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
13 * This file is part of InspIRCd. InspIRCd is free software: you can
14 * redistribute it and/or modify it under the terms of the GNU General Public
15 * License as published by the Free Software Foundation, version 2.
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class CommandList : public Command
34 ChanModeReference secretmode;
35 ChanModeReference privatemode;
37 /** Parses the creation time or topic set time out of a LIST parameter.
38 * @param value The parameter containing a minute count.
39 * @return The UNIX time at \p value minutes ago.
41 time_t ParseMinutes(const std::string& value)
43 time_t minutes = ConvToNum<time_t>(value.c_str() + 2);
46 return ServerInstance->Time() - (minutes * 60);
50 // Whether to show modes in the LIST response.
53 CommandList(Module* parent)
54 : Command(parent,"LIST", 0, 0)
55 , secretmode(creator, "secret")
56 , privatemode(creator, "private")
58 allow_empty_last_param = false;
63 * @param parameters The parameters to the command
64 * @param user The user issuing the command
65 * @return A value from CmdResult to indicate command success or failure.
67 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
73 CmdResult CommandList::Handle(User* user, const Params& parameters)
75 // C: Searching based on creation time, via the "C<val" and "C>val" modifiers
76 // to search for a channel creation time that is lower or higher than val
78 time_t mincreationtime = 0;
79 time_t maxcreationtime = 0;
81 // M: Searching based on mask.
82 // N: Searching based on !mask.
83 bool match_name_topic = false;
84 bool match_inverted = false;
85 const char* match = NULL;
87 // T: Searching based on topic time, via the "T<val" and "T>val" modifiers to
88 // search for a topic time that is lower or higher than val respectively.
89 time_t mintopictime = 0;
90 time_t maxtopictime = 0;
92 // U: Searching based on user count within the channel, via the "<val" and
93 // ">val" modifiers to search for a channel that has less than or more than
94 // val users respectively.
98 for (Params::const_iterator iter = parameters.begin(); iter != parameters.end(); ++iter)
100 const std::string& constraint = *iter;
101 if (constraint[0] == '<')
103 maxusers = ConvToNum<size_t>(constraint.c_str() + 1);
105 else if (constraint[0] == '>')
107 minusers = ConvToNum<size_t>(constraint.c_str() + 1);
109 else if (!constraint.compare(0, 2, "C<", 2) || !constraint.compare(0, 2, "c<", 2))
111 mincreationtime = ParseMinutes(constraint);
113 else if (!constraint.compare(0, 2, "C>", 2) || !constraint.compare(0, 2, "c>", 2))
115 maxcreationtime = ParseMinutes(constraint);
117 else if (!constraint.compare(0, 2, "T<", 2) || !constraint.compare(0, 2, "t<", 2))
119 mintopictime = ParseMinutes(constraint);
121 else if (!constraint.compare(0, 2, "T>", 2) || !constraint.compare(0, 2, "t>", 2))
123 maxtopictime = ParseMinutes(constraint);
127 // If the glob is prefixed with ! it is inverted.
128 match = constraint.c_str();
131 match_inverted = true;
135 // Ensure that the user didn't just run "LIST !".
137 match_name_topic = true;
141 const bool has_privs = user->HasPrivPermission("channels/auspex");
143 user->WriteNumeric(RPL_LISTSTART, "Channel", "Users Name");
144 const chan_hash& chans = ServerInstance->GetChans();
145 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
147 Channel* const chan = i->second;
149 // Check the user count if a search has been specified.
150 const size_t users = chan->GetUserCounter();
151 if ((minusers && users <= minusers) || (maxusers && users >= maxusers))
154 // Check the creation ts if a search has been specified.
155 const time_t creationtime = chan->age;
156 if ((mincreationtime && creationtime <= mincreationtime) || (maxcreationtime && creationtime >= maxcreationtime))
159 // Check the topic ts if a search has been specified.
160 const time_t topictime = chan->topicset;
161 if ((mintopictime && (!topictime || topictime <= mintopictime)) || (maxtopictime && (!topictime || topictime >= maxtopictime)))
164 // Attempt to match a glob pattern.
165 if (match_name_topic)
167 bool matches = InspIRCd::Match(chan->name, match) || InspIRCd::Match(chan->topic, match);
169 // The user specified an match that we did not match.
170 if (!matches && !match_inverted)
173 // The user specified an inverted match that we did match.
174 if (matches && match_inverted)
178 // if the channel is not private/secret, OR the user is on the channel anyway
179 bool n = (has_privs || chan->HasUser(user));
181 // If we're not in the channel and +s is set on it, we want to ignore it
182 if ((n) || (!chan->IsModeSet(secretmode)))
184 if ((!n) && (chan->IsModeSet(privatemode)))
186 // Channel is private (+p) and user is outside/not privileged
187 user->WriteNumeric(RPL_LIST, '*', users, "");
191 // Show the list response with the modes and topic.
192 user->WriteNumeric(RPL_LIST, chan->name, users, InspIRCd::Format("[+%s] %s", chan->ChanModes(n), chan->topic.c_str()));
196 // Show the list response with just the modes.
197 user->WriteNumeric(RPL_LIST, chan->name, users, chan->topic);
201 user->WriteNumeric(RPL_LISTEND, "End of channel list.");
206 class CoreModList : public Module
217 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
219 ConfigTag* tag = ServerInstance->Config->ConfValue("options");
220 cmd.showmodes = tag->getBool("modesinlist", true);
223 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
225 tokens["ELIST"] = "CMNTU";
229 Version GetVersion() CXX11_OVERRIDE
231 return Version("Provides the LIST command", VF_VENDOR|VF_CORE);
235 MODULE_INIT(CoreModList)