]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_list.cpp
600ec47c2e3b31772aaa92de9e2cdcf990577fdd
[user/henk/code/inspircd.git] / src / coremods / core_list.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22
23 /** Handle /LIST.
24  */
25 class CommandList : public Command
26 {
27  private:
28         ChanModeReference secretmode;
29         ChanModeReference privatemode;
30
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.
34          */
35         time_t ParseMinutes(const std::string& value)
36         {
37                 time_t minutes = ConvToNum<time_t>(value.c_str() + 2);
38                 if (!minutes)
39                         return 0;
40                 return ServerInstance->Time() - (minutes * 60);
41         }
42
43  public:
44         /** Constructor for list.
45          */
46         CommandList(Module* parent)
47                 : Command(parent,"LIST", 0, 0)
48                 , secretmode(creator, "secret")
49                 , privatemode(creator, "private")
50         {
51                 Penalty = 5;
52         }
53
54         /** Handle command.
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.
58          */
59         CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE;
60 };
61
62
63 /** Handle /LIST
64  */
65 CmdResult CommandList::Handle (const std::vector<std::string>& parameters, User *user)
66 {
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
69         // respectively.
70         time_t mincreationtime = 0;
71         time_t maxcreationtime = 0;
72
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;
78
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;
83
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.
87         size_t minusers = 0;
88         size_t maxusers = 0;
89
90         if ((parameters.size() == 1) && (!parameters[0].empty()))
91         {
92                 if (parameters[0][0] == '<')
93                 {
94                         maxusers = ConvToNum<size_t>(parameters[0].c_str() + 1);
95                 }
96                 else if (parameters[0][0] == '>')
97                 {
98                         minusers = ConvToNum<size_t>(parameters[0].c_str() + 1);
99                 }
100                 else if (!parameters[0].compare(0, 2, "C<", 2))
101                 {
102                         mincreationtime = ParseMinutes(parameters[0]);
103                 }
104                 else if (!parameters[0].compare(0, 2, "C>", 2))
105                 {
106                         maxcreationtime = ParseMinutes(parameters[0]);
107                 }
108                 else if (!parameters[0].compare(0, 2, "T<", 2))
109                 {
110                         mintopictime = ParseMinutes(parameters[0]);
111                 }
112                 else if (!parameters[0].compare(0, 2, "T>", 2))
113                 {
114                         maxtopictime = ParseMinutes(parameters[0]);
115                 }
116                 else
117                 {
118                         // If the glob is prefixed with ! it is inverted.
119                         match = parameters[0].c_str();
120                         if (match[0] == '!')
121                         {
122                                 match_inverted = true;
123                                 match += 1;
124                         }
125
126                         // Ensure that the user didn't just run "LIST !".
127                         if (match[0])
128                                 match_name_topic = true;
129                 }
130         }
131
132         const bool has_privs = user->HasPrivPermission("channels/auspex");
133
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)
137         {
138                 Channel* const chan = i->second;
139
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))
143                         continue;
144
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))
148                         continue;
149
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)))
153                         continue;
154
155                 // Attempt to match a glob pattern.
156                 if (match_name_topic)
157                 {
158                         bool matches = InspIRCd::Match(chan->name, match) || InspIRCd::Match(chan->topic, match);
159
160                         // The user specified an match that we did not match.
161                         if (!matches && !match_inverted)
162                                 continue;
163
164                         // The user specified an inverted match that we did match.
165                         if (matches && match_inverted)
166                                 continue;
167                 }
168
169                 // if the channel is not private/secret, OR the user is on the channel anyway
170                 bool n = (has_privs || chan->HasUser(user));
171
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)))
174                 {
175                         if ((!n) && (chan->IsModeSet(privatemode)))
176                         {
177                                 // Channel is private (+p) and user is outside/not privileged
178                                 user->WriteNumeric(RPL_LIST, '*', users, "");
179                         }
180                         else
181                         {
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()));
184                         }
185                 }
186         }
187         user->WriteNumeric(RPL_LISTEND, "End of channel list.");
188
189         return CMD_SUCCESS;
190 }
191
192 class CoreModList : public Module
193 {
194  private:
195         CommandList cmd;
196
197  public:
198         CoreModList()
199                 : cmd(this)
200         {
201         }
202
203         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
204         {
205                 tokens["ELIST"] = "CMNTU";
206                 tokens["SAFELIST"];
207         }
208
209         Version GetVersion() CXX11_OVERRIDE
210         {
211                 return Version("Provides the LIST command", VF_VENDOR|VF_CORE);
212         }
213 };
214
215 MODULE_INIT(CoreModList)