]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_list.cpp
Use WriteNumeric() everywhere we send numerics and include the user's nick automatically
[user/henk/code/inspircd.git] / src / commands / cmd_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. These command handlers can be reloaded by the core,
24  * and handle basic RFC1459 commands. Commands within modules work
25  * the same way, however, they can be fully unloaded, where these
26  * may not.
27  */
28 class CommandList : public Command
29 {
30         ChanModeReference secretmode;
31         ChanModeReference privatemode;
32
33  public:
34         /** Constructor for list.
35          */
36         CommandList(Module* parent)
37                 : Command(parent,"LIST", 0, 0)
38                 , secretmode(creator, "secret")
39                 , privatemode(creator, "private")
40         {
41                 Penalty = 5;
42         }
43
44         /** Handle command.
45          * @param parameters The parameters to the comamnd
46          * @param pcnt The number of parameters passed to teh command
47          * @param user The user issuing the command
48          * @return A value from CmdResult to indicate command success or failure.
49          */
50         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
51 };
52
53
54 /** Handle /LIST
55  */
56 CmdResult CommandList::Handle (const std::vector<std::string>& parameters, User *user)
57 {
58         int minusers = 0, maxusers = 0;
59
60         user->WriteNumeric(RPL_LISTSTART, "Channel :Users Name");
61
62         /* Work around mIRC suckyness. YOU SUCK, KHALED! */
63         if (parameters.size() == 1)
64         {
65                 if (parameters[0][0] == '<')
66                 {
67                         maxusers = atoi((parameters[0].c_str())+1);
68                 }
69                 else if (parameters[0][0] == '>')
70                 {
71                         minusers = atoi((parameters[0].c_str())+1);
72                 }
73         }
74
75         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
76         {
77                 // attempt to match a glob pattern
78                 long users = i->second->GetUserCounter();
79
80                 bool too_few = (minusers && (users <= minusers));
81                 bool too_many = (maxusers && (users >= maxusers));
82
83                 if (too_many || too_few)
84                         continue;
85
86                 if (parameters.size() && !parameters[0].empty() && (parameters[0][0] != '<' && parameters[0][0] != '>'))
87                 {
88                         if (!InspIRCd::Match(i->second->name, parameters[0]) && !InspIRCd::Match(i->second->topic, parameters[0]))
89                                 continue;
90                 }
91
92                 // if the channel is not private/secret, OR the user is on the channel anyway
93                 bool n = (i->second->HasUser(user) || user->HasPrivPermission("channels/auspex"));
94
95                 if (!n && i->second->IsModeSet(privatemode))
96                 {
97                         /* Channel is +p and user is outside/not privileged */
98                         user->WriteNumeric(RPL_LIST, "* %ld :", users);
99                 }
100                 else
101                 {
102                         if (n || !i->second->IsModeSet(secretmode))
103                         {
104                                 /* User is in the channel/privileged, channel is not +s */
105                                 user->WriteNumeric(RPL_LIST, "%s %ld :[+%s] %s",i->second->name.c_str(),users,i->second->ChanModes(n),i->second->topic.c_str());
106                         }
107                 }
108         }
109         user->WriteNumeric(RPL_LISTEND, ":End of channel list.");
110
111         return CMD_SUCCESS;
112 }
113
114
115 COMMAND_INIT(CommandList)