2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
26 #include "modules/exemption.h"
30 RPL_ENDOFEXEMPTIONLIST = 953,
31 RPL_EXEMPTIONLIST = 954
34 class ExemptChanOps : public ListModeBase
37 ExemptChanOps(Module* Creator)
38 : ListModeBase(Creator, "exemptchanops", 'X', "End of channel exemptchanops list", RPL_EXEMPTIONLIST, RPL_ENDOFEXEMPTIONLIST, false)
40 syntax = "<restriction>:<prefix>";
43 static PrefixMode* FindMode(const std::string& mode)
45 if (mode.length() == 1)
46 return ServerInstance->Modes->FindPrefixMode(mode[0]);
48 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_CHANNEL);
49 return mh ? mh->IsPrefixMode() : NULL;
52 static bool ParseEntry(const std::string& entry, std::string& restriction, std::string& prefix)
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)
59 restriction.assign(entry, 0, colon);
60 prefix.assign(entry, colon + 1, std::string::npos);
64 ModResult AccessCheck(User* source, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
66 std::string restriction;
68 if (!ParseEntry(parameter, restriction, prefix))
69 return MOD_RES_PASSTHRU;
71 PrefixMode* pm = FindMode(prefix);
73 return MOD_RES_PASSTHRU;
75 if (channel->GetPrefixValue(source) >= pm->GetLevelRequired(adding))
76 return MOD_RES_PASSTHRU;
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"));
83 bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
85 std::string restriction;
87 if (!ParseEntry(word, restriction, prefix))
89 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word));
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);
99 if (!ServerInstance->Modes->FindMode(restriction, MODETYPE_CHANNEL))
101 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown restriction."));
105 if (prefix != "*" && !FindMode(prefix))
107 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown prefix mode."));
115 class ExemptHandler : public CheckExemption::EventListener
119 ExemptHandler(Module* me)
120 : CheckExemption::EventListener(me)
125 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
127 unsigned int mypfx = chan->GetPrefixValue(user);
130 ListModeBase::ModeList* list = ec.GetList(chan);
134 for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
136 std::string::size_type pos = (*i).mask.find(':');
137 if (pos == std::string::npos)
139 if (!i->mask.compare(0, pos, restriction))
140 minmode.assign(i->mask, pos + 1, std::string::npos);
144 PrefixMode* mh = ExemptChanOps::FindMode(minmode);
145 if (mh && mypfx >= mh->GetPrefixRank())
146 return MOD_RES_ALLOW;
147 if (mh || minmode == "*")
150 return MOD_RES_PASSTHRU;
154 class ModuleExemptChanOps : public Module
159 ModuleExemptChanOps() : eh(this)
163 Version GetVersion() CXX11_OVERRIDE
165 return Version("Adds channel mode X (exemptchanops) which allows channel operators to grant exemptions to various channel-level restrictions.", VF_VENDOR);
168 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
174 MODULE_INIT(ModuleExemptChanOps)