]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_exemptchanops.cpp
Fix the cloaking module on C++98 compilers.
[user/henk/code/inspircd.git] / src / modules / m_exemptchanops.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25 #include "listmode.h"
26 #include "modules/exemption.h"
27
28 enum
29 {
30         RPL_ENDOFEXEMPTIONLIST = 953,
31         RPL_EXEMPTIONLIST = 954
32 };
33
34 class ExemptChanOps : public ListModeBase
35 {
36  public:
37         ExemptChanOps(Module* Creator)
38                 : ListModeBase(Creator, "exemptchanops", 'X', "End of channel exemptchanops list", RPL_EXEMPTIONLIST, RPL_ENDOFEXEMPTIONLIST, false)
39         {
40                 syntax = "<restriction>:<prefix>";
41         }
42
43         static PrefixMode* FindMode(const std::string& mode)
44         {
45                 if (mode.length() == 1)
46                         return ServerInstance->Modes->FindPrefixMode(mode[0]);
47
48                 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_CHANNEL);
49                 return mh ? mh->IsPrefixMode() : NULL;
50         }
51
52         static bool ParseEntry(const std::string& entry, std::string& restriction, std::string& prefix)
53         {
54                 // The entry must be in the format <restriction>:<prefix>.
55                 std::string::size_type colon = entry.find(':');
56                 if (colon == std::string::npos || colon == entry.length()-1)
57                         return false;
58
59                 restriction.assign(entry, 0, colon);
60                 prefix.assign(entry, colon + 1, std::string::npos);
61                 return true;
62         }
63
64         ModResult AccessCheck(User* source, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
65         {
66                 std::string restriction;
67                 std::string prefix;
68                 if (!ParseEntry(parameter, restriction, prefix))
69                         return MOD_RES_PASSTHRU;
70
71                 PrefixMode* pm = FindMode(prefix);
72                 if (!pm)
73                         return MOD_RES_PASSTHRU;
74
75                 if (channel->GetPrefixValue(source) >= pm->GetLevelRequired(adding))
76                         return MOD_RES_PASSTHRU;
77
78                 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, InspIRCd::Format("You must be able to %s mode %c (%s) to %s a restriction containing it",
79                         adding ? "set" : "unset", pm->GetModeChar(), pm->name.c_str(), adding ? "add" : "remove"));
80                 return MOD_RES_DENY;
81         }
82
83         bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
84         {
85                 std::string restriction;
86                 std::string prefix;
87                 if (!ParseEntry(word, restriction, prefix))
88                 {
89                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word));
90                         return false;
91                 }
92
93                 // If there is a '-' in the restriction string ignore it and everything after it
94                 // to support "auditorium-vis" and "auditorium-see" in m_auditorium
95                 std::string::size_type dash = restriction.find('-');
96                 if (dash != std::string::npos)
97                         restriction.erase(dash);
98
99                 if (!ServerInstance->Modes->FindMode(restriction, MODETYPE_CHANNEL))
100                 {
101                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown restriction."));
102                         return false;
103                 }
104
105                 if (prefix != "*" && !FindMode(prefix))
106                 {
107                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown prefix mode."));
108                         return false;
109                 }
110
111                 return true;
112         }
113 };
114
115 class ExemptHandler : public CheckExemption::EventListener
116 {
117  public:
118         ExemptChanOps ec;
119         ExemptHandler(Module* me)
120                 : CheckExemption::EventListener(me)
121                 , ec(me)
122         {
123         }
124
125         ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
126         {
127                 unsigned int mypfx = chan->GetPrefixValue(user);
128                 std::string minmode;
129
130                 ListModeBase::ModeList* list = ec.GetList(chan);
131
132                 if (list)
133                 {
134                         for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
135                         {
136                                 std::string::size_type pos = (*i).mask.find(':');
137                                 if (pos == std::string::npos)
138                                         continue;
139                                 if (!i->mask.compare(0, pos, restriction))
140                                         minmode.assign(i->mask, pos + 1, std::string::npos);
141                         }
142                 }
143
144                 PrefixMode* mh = ExemptChanOps::FindMode(minmode);
145                 if (mh && mypfx >= mh->GetPrefixRank())
146                         return MOD_RES_ALLOW;
147                 if (mh || minmode == "*")
148                         return MOD_RES_DENY;
149
150                 return MOD_RES_PASSTHRU;
151         }
152 };
153
154 class ModuleExemptChanOps : public Module
155 {
156         ExemptHandler eh;
157
158  public:
159         ModuleExemptChanOps() : eh(this)
160         {
161         }
162
163         Version GetVersion() CXX11_OVERRIDE
164         {
165                 return Version("Adds channel mode X (exemptchanops) which allows channel operators to grant exemptions to various channel-level restrictions.", VF_VENDOR);
166         }
167
168         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
169         {
170                 eh.ec.DoRehash();
171         }
172 };
173
174 MODULE_INIT(ModuleExemptChanOps)