2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class CommandList : public Command
28 ChanModeReference secretmode;
29 ChanModeReference privatemode;
31 /** Parses the creation time or topic set time out of a LIST parameter.
32 * @param value The parameter containing a minute count.
33 * @return The UNIX time at \p value minutes ago.
35 time_t ParseMinutes(const std::string& value)
37 time_t minutes = ConvToNum<time_t>(value.c_str() + 2);
40 return ServerInstance->Time() - (minutes * 60);
44 /** Constructor for list.
46 CommandList(Module* parent)
47 : Command(parent,"LIST", 0, 0)
48 , secretmode(creator, "secret")
49 , privatemode(creator, "private")
55 * @param parameters The parameters to the command
56 * @param user The user issuing the command
57 * @return A value from CmdResult to indicate command success or failure.
59 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
65 CmdResult CommandList::Handle(User* user, const Params& parameters)
67 // C: Searching based on creation time, via the "C<val" and "C>val" modifiers
68 // to search for a channel creation time that is lower or higher than val
70 time_t mincreationtime = 0;
71 time_t maxcreationtime = 0;
73 // M: Searching based on mask.
74 // N: Searching based on !mask.
75 bool match_name_topic = false;
76 bool match_inverted = false;
77 const char* match = NULL;
79 // T: Searching based on topic time, via the "T<val" and "T>val" modifiers to
80 // search for a topic time that is lower or higher than val respectively.
81 time_t mintopictime = 0;
82 time_t maxtopictime = 0;
84 // U: Searching based on user count within the channel, via the "<val" and
85 // ">val" modifiers to search for a channel that has less than or more than
86 // val users respectively.
90 if ((parameters.size() == 1) && (!parameters[0].empty()))
92 if (parameters[0][0] == '<')
94 maxusers = ConvToNum<size_t>(parameters[0].c_str() + 1);
96 else if (parameters[0][0] == '>')
98 minusers = ConvToNum<size_t>(parameters[0].c_str() + 1);
100 else if (!parameters[0].compare(0, 2, "C<", 2))
102 mincreationtime = ParseMinutes(parameters[0]);
104 else if (!parameters[0].compare(0, 2, "C>", 2))
106 maxcreationtime = ParseMinutes(parameters[0]);
108 else if (!parameters[0].compare(0, 2, "T<", 2))
110 mintopictime = ParseMinutes(parameters[0]);
112 else if (!parameters[0].compare(0, 2, "T>", 2))
114 maxtopictime = ParseMinutes(parameters[0]);
118 // If the glob is prefixed with ! it is inverted.
119 match = parameters[0].c_str();
122 match_inverted = true;
126 // Ensure that the user didn't just run "LIST !".
128 match_name_topic = true;
132 const bool has_privs = user->HasPrivPermission("channels/auspex");
134 user->WriteNumeric(RPL_LISTSTART, "Channel", "Users Name");
135 const chan_hash& chans = ServerInstance->GetChans();
136 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
138 Channel* const chan = i->second;
140 // Check the user count if a search has been specified.
141 const size_t users = chan->GetUserCounter();
142 if ((minusers && users <= minusers) || (maxusers && users >= maxusers))
145 // Check the creation ts if a search has been specified.
146 const time_t creationtime = chan->age;
147 if ((mincreationtime && creationtime <= mincreationtime) || (maxcreationtime && creationtime >= maxcreationtime))
150 // Check the topic ts if a search has been specified.
151 const time_t topictime = chan->topicset;
152 if ((mintopictime && (!topictime || topictime <= mintopictime)) || (maxtopictime && (!topictime || topictime >= maxtopictime)))
155 // Attempt to match a glob pattern.
156 if (match_name_topic)
158 bool matches = InspIRCd::Match(chan->name, match) || InspIRCd::Match(chan->topic, match);
160 // The user specified an match that we did not match.
161 if (!matches && !match_inverted)
164 // The user specified an inverted match that we did match.
165 if (matches && match_inverted)
169 // if the channel is not private/secret, OR the user is on the channel anyway
170 bool n = (has_privs || chan->HasUser(user));
172 // If we're not in the channel and +s is set on it, we want to ignore it
173 if ((n) || (!chan->IsModeSet(secretmode)))
175 if ((!n) && (chan->IsModeSet(privatemode)))
177 // Channel is private (+p) and user is outside/not privileged
178 user->WriteNumeric(RPL_LIST, '*', users, "");
182 /* User is in the channel/privileged, channel is not +s */
183 user->WriteNumeric(RPL_LIST, chan->name, users, InspIRCd::Format("[+%s] %s", chan->ChanModes(n), chan->topic.c_str()));
187 user->WriteNumeric(RPL_LISTEND, "End of channel list.");
192 class CoreModList : public Module
203 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
205 tokens["ELIST"] = "CMNTU";
209 Version GetVersion() CXX11_OVERRIDE
211 return Version("Provides the LIST command", VF_VENDOR|VF_CORE);
215 MODULE_INIT(CoreModList)