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")
51 allow_empty_last_param = false;
56 * @param parameters The parameters to the command
57 * @param user The user issuing the command
58 * @return A value from CmdResult to indicate command success or failure.
60 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
66 CmdResult CommandList::Handle(User* user, const Params& parameters)
68 // C: Searching based on creation time, via the "C<val" and "C>val" modifiers
69 // to search for a channel creation time that is lower or higher than val
71 time_t mincreationtime = 0;
72 time_t maxcreationtime = 0;
74 // M: Searching based on mask.
75 // N: Searching based on !mask.
76 bool match_name_topic = false;
77 bool match_inverted = false;
78 const char* match = NULL;
80 // T: Searching based on topic time, via the "T<val" and "T>val" modifiers to
81 // search for a topic time that is lower or higher than val respectively.
82 time_t mintopictime = 0;
83 time_t maxtopictime = 0;
85 // U: Searching based on user count within the channel, via the "<val" and
86 // ">val" modifiers to search for a channel that has less than or more than
87 // val users respectively.
91 for (Params::const_iterator iter = parameters.begin(); iter != parameters.end(); ++iter)
93 const std::string& constraint = *iter;
94 if (constraint[0] == '<')
96 maxusers = ConvToNum<size_t>(constraint.c_str() + 1);
98 else if (constraint[0] == '>')
100 minusers = ConvToNum<size_t>(constraint.c_str() + 1);
102 else if (!constraint.compare(0, 2, "C<", 2) || !constraint.compare(0, 2, "c<", 2))
104 mincreationtime = ParseMinutes(constraint);
106 else if (!constraint.compare(0, 2, "C>", 2) || !constraint.compare(0, 2, "c>", 2))
108 maxcreationtime = ParseMinutes(constraint);
110 else if (!constraint.compare(0, 2, "T<", 2) || !constraint.compare(0, 2, "t<", 2))
112 mintopictime = ParseMinutes(constraint);
114 else if (!constraint.compare(0, 2, "T>", 2) || !constraint.compare(0, 2, "t>", 2))
116 maxtopictime = ParseMinutes(constraint);
120 // If the glob is prefixed with ! it is inverted.
121 match = constraint.c_str();
124 match_inverted = true;
128 // Ensure that the user didn't just run "LIST !".
130 match_name_topic = true;
134 const bool has_privs = user->HasPrivPermission("channels/auspex");
136 user->WriteNumeric(RPL_LISTSTART, "Channel", "Users Name");
137 const chan_hash& chans = ServerInstance->GetChans();
138 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
140 Channel* const chan = i->second;
142 // Check the user count if a search has been specified.
143 const size_t users = chan->GetUserCounter();
144 if ((minusers && users <= minusers) || (maxusers && users >= maxusers))
147 // Check the creation ts if a search has been specified.
148 const time_t creationtime = chan->age;
149 if ((mincreationtime && creationtime <= mincreationtime) || (maxcreationtime && creationtime >= maxcreationtime))
152 // Check the topic ts if a search has been specified.
153 const time_t topictime = chan->topicset;
154 if ((mintopictime && (!topictime || topictime <= mintopictime)) || (maxtopictime && (!topictime || topictime >= maxtopictime)))
157 // Attempt to match a glob pattern.
158 if (match_name_topic)
160 bool matches = InspIRCd::Match(chan->name, match) || InspIRCd::Match(chan->topic, match);
162 // The user specified an match that we did not match.
163 if (!matches && !match_inverted)
166 // The user specified an inverted match that we did match.
167 if (matches && match_inverted)
171 // if the channel is not private/secret, OR the user is on the channel anyway
172 bool n = (has_privs || chan->HasUser(user));
174 // If we're not in the channel and +s is set on it, we want to ignore it
175 if ((n) || (!chan->IsModeSet(secretmode)))
177 if ((!n) && (chan->IsModeSet(privatemode)))
179 // Channel is private (+p) and user is outside/not privileged
180 user->WriteNumeric(RPL_LIST, '*', users, "");
184 /* User is in the channel/privileged, channel is not +s */
185 user->WriteNumeric(RPL_LIST, chan->name, users, InspIRCd::Format("[+%s] %s", chan->ChanModes(n), chan->topic.c_str()));
189 user->WriteNumeric(RPL_LISTEND, "End of channel list.");
194 class CoreModList : public Module
205 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
207 tokens["ELIST"] = "CMNTU";
211 Version GetVersion() CXX11_OVERRIDE
213 return Version("Provides the LIST command", VF_VENDOR|VF_CORE);
217 MODULE_INIT(CoreModList)