]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rmode.cpp
Tidy up keywords on module methods.
[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 /* $ModDesc: Provides support for the RMODE command - Makes mass removal of chan listmodes by glob pattern possible */
24
25 /** Handle /RMODE
26  */
27 class CommandRMode : public Command
28 {
29  public:
30         CommandRMode(Module* Creator) : Command(Creator,"RMODE", 2, 3)
31         {
32                 allow_empty_last_param = false;
33                 syntax = "<channel> <mode> [pattern]";
34         }
35
36         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
37         {
38                 ModeHandler* mh;
39                 Channel* chan = ServerInstance->FindChan(parameters[0]);
40                 char modeletter = parameters[1][0];
41
42                 if (chan == NULL)
43                 {
44                         user->WriteNotice("The channel " + parameters[0] + " does not exist.");
45                         return CMD_FAILURE;
46                 }
47
48                 mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
49                 if (mh == NULL || parameters[1].size() > 1)
50                 {
51                         user->WriteNotice(parameters[1] + " is not a valid channel mode.");
52                         return CMD_FAILURE;
53                 }
54
55                 if (chan->GetPrefixValue(user) < mh->GetLevelRequired())
56                 {
57                         user->WriteNotice("You do not have access to unset " + ConvToStr(modeletter) + " on " +  chan->name + ".");
58                         return CMD_FAILURE;
59                 }
60
61                 unsigned int prefixrank;
62                 char prefixchar;
63                 std::string pattern = parameters.size() > 2 ? parameters[2] : "*";
64                 ListModeBase* lm;
65                 ListModeBase::ModeList* ml;
66                 irc::modestacker modestack(false);
67
68                 if (!mh->IsListMode())
69                 {
70                         if (chan->IsModeSet(modeletter))
71                                 modestack.Push(modeletter);
72                 }
73                 else if (((prefixrank = mh->GetPrefixRank()) && (prefixchar = mh->GetPrefix())))
74                 {
75                         // As user prefix modes don't have a GetList() method, let's iterate through the channel's users.
76                         for (UserMembIter it = chan->userlist.begin(); it != chan->userlist.end(); ++it)
77                         {
78                                 if (!InspIRCd::Match(it->first->nick, pattern))
79                                         continue;
80                                 if (((strchr(chan->GetAllPrefixChars(user), prefixchar)) != NULL) && !(it->first == user && prefixrank > VOICE_VALUE))
81                                         modestack.Push(modeletter, it->first->nick);
82                         }
83                 }
84                 else if (((lm = dynamic_cast<ListModeBase*>(mh)) != NULL) && ((ml = lm->GetList(chan)) != NULL))
85                 {
86                         for (ListModeBase::ModeList::iterator it = ml->begin(); it != ml->end(); ++it)
87                         {
88                                 if (!InspIRCd::Match(it->mask, pattern))
89                                         continue;
90                                 modestack.Push(modeletter, it->mask);
91                         }
92                 }
93                 else
94                 {
95                         user->WriteNotice("Could not remove channel mode " + ConvToStr(modeletter));
96                         return CMD_FAILURE;
97                 }
98
99                 parameterlist stackresult;
100                 stackresult.push_back(chan->name);
101                 while (modestack.GetStackedLine(stackresult))
102                 {
103                         ServerInstance->SendMode(stackresult, user);
104                         stackresult.erase(stackresult.begin() + 1, stackresult.end());
105                 }
106
107                 return CMD_SUCCESS;
108         }
109 };
110
111 class ModuleRMode : public Module
112 {
113         CommandRMode cmd;
114
115  public:
116         ModuleRMode() : cmd(this) { }
117
118         void init() CXX11_OVERRIDE
119         {
120                 ServerInstance->Modules->AddService(cmd);
121         }
122
123         Version GetVersion() CXX11_OVERRIDE
124         {
125                 return Version("Allows glob-based removal of list modes", VF_VENDOR);
126         }
127 };
128
129 MODULE_INIT(ModuleRMode)