]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rmode.cpp
Move prefix mode specific fields and getters into PrefixMode
[user/henk/code/inspircd.git] / src / modules / m_rmode.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "listmode.h"
22
23 /** Handle /RMODE
24  */
25 class CommandRMode : public Command
26 {
27  public:
28         CommandRMode(Module* Creator) : Command(Creator,"RMODE", 2, 3)
29         {
30                 allow_empty_last_param = false;
31                 syntax = "<channel> <mode> [pattern]";
32         }
33
34         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
35         {
36                 ModeHandler* mh;
37                 Channel* chan = ServerInstance->FindChan(parameters[0]);
38                 char modeletter = parameters[1][0];
39
40                 if (chan == NULL)
41                 {
42                         user->WriteNotice("The channel " + parameters[0] + " does not exist.");
43                         return CMD_FAILURE;
44                 }
45
46                 mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
47                 if (mh == NULL || parameters[1].size() > 1)
48                 {
49                         user->WriteNotice(parameters[1] + " is not a valid channel mode.");
50                         return CMD_FAILURE;
51                 }
52
53                 if (chan->GetPrefixValue(user) < mh->GetLevelRequired())
54                 {
55                         user->WriteNotice("You do not have access to unset " + ConvToStr(modeletter) + " on " +  chan->name + ".");
56                         return CMD_FAILURE;
57                 }
58
59                 std::string pattern = parameters.size() > 2 ? parameters[2] : "*";
60                 PrefixMode* pm;
61                 ListModeBase* lm;
62                 ListModeBase::ModeList* ml;
63                 irc::modestacker modestack(false);
64
65                 if (!mh->IsListMode())
66                 {
67                         if (chan->IsModeSet(mh))
68                                 modestack.Push(modeletter);
69                 }
70                 else if ((pm = mh->IsPrefixMode()))
71                 {
72                         // As user prefix modes don't have a GetList() method, let's iterate through the channel's users.
73                         for (UserMembIter it = chan->userlist.begin(); it != chan->userlist.end(); ++it)
74                         {
75                                 if (!InspIRCd::Match(it->first->nick, pattern))
76                                         continue;
77                                 if (it->second->hasMode(modeletter) && !((it->first == user) && (pm->GetPrefixRank() > VOICE_VALUE)))
78                                         modestack.Push(modeletter, it->first->nick);
79                         }
80                 }
81                 else if (((lm = dynamic_cast<ListModeBase*>(mh)) != NULL) && ((ml = lm->GetList(chan)) != NULL))
82                 {
83                         for (ListModeBase::ModeList::iterator it = ml->begin(); it != ml->end(); ++it)
84                         {
85                                 if (!InspIRCd::Match(it->mask, pattern))
86                                         continue;
87                                 modestack.Push(modeletter, it->mask);
88                         }
89                 }
90                 else
91                 {
92                         user->WriteNotice("Could not remove channel mode " + ConvToStr(modeletter));
93                         return CMD_FAILURE;
94                 }
95
96                 parameterlist stackresult;
97                 stackresult.push_back(chan->name);
98                 while (modestack.GetStackedLine(stackresult))
99                 {
100                         ServerInstance->Modes->Process(stackresult, user);
101                         stackresult.erase(stackresult.begin() + 1, stackresult.end());
102                 }
103
104                 return CMD_SUCCESS;
105         }
106 };
107
108 class ModuleRMode : public Module
109 {
110         CommandRMode cmd;
111
112  public:
113         ModuleRMode() : cmd(this) { }
114
115         Version GetVersion() CXX11_OVERRIDE
116         {
117                 return Version("Allows glob-based removal of list modes", VF_VENDOR);
118         }
119 };
120
121 MODULE_INIT(ModuleRMode)