2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2013, 2017-2019 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"
28 /** Handles channel mode +X
30 class ExemptChanOps : public ListModeBase
33 ExemptChanOps(Module* Creator)
34 : ListModeBase(Creator, "exemptchanops", 'X', "End of channel exemptchanops list", 954, 953, false)
36 syntax = "<restriction>:<prefix>";
39 static PrefixMode* FindMode(const std::string& mode)
41 if (mode.length() == 1)
42 return ServerInstance->Modes->FindPrefixMode(mode[0]);
44 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_CHANNEL);
45 return mh ? mh->IsPrefixMode() : NULL;
48 static bool ParseEntry(const std::string& entry, std::string& restriction, std::string& prefix)
50 // The entry must be in the format <restriction>:<prefix>.
51 std::string::size_type colon = entry.find(':');
52 if (colon == std::string::npos || colon == entry.length()-1)
55 restriction.assign(entry, 0, colon);
56 prefix.assign(entry, colon + 1, std::string::npos);
60 ModResult AccessCheck(User* source, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
62 std::string restriction;
64 if (!ParseEntry(parameter, restriction, prefix))
65 return MOD_RES_PASSTHRU;
67 PrefixMode* pm = FindMode(prefix);
69 return MOD_RES_PASSTHRU;
71 if (channel->GetPrefixValue(source) >= pm->GetLevelRequired(adding))
72 return MOD_RES_PASSTHRU;
74 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, InspIRCd::Format("You must be able to %s mode %c (%s) to %s a restriction containing it",
75 adding ? "set" : "unset", pm->GetModeChar(), pm->name.c_str(), adding ? "add" : "remove"));
79 bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
81 std::string restriction;
83 if (!ParseEntry(word, restriction, prefix))
85 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word));
89 // If there is a '-' in the restriction string ignore it and everything after it
90 // to support "auditorium-vis" and "auditorium-see" in m_auditorium
91 std::string::size_type dash = restriction.find('-');
92 if (dash != std::string::npos)
93 restriction.erase(dash);
95 if (!ServerInstance->Modes->FindMode(restriction, MODETYPE_CHANNEL))
97 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown restriction."));
101 if (prefix != "*" && !FindMode(prefix))
103 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown prefix mode."));
111 class ExemptHandler : public CheckExemption::EventListener
115 ExemptHandler(Module* me)
116 : CheckExemption::EventListener(me)
121 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
123 unsigned int mypfx = chan->GetPrefixValue(user);
126 ListModeBase::ModeList* list = ec.GetList(chan);
130 for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
132 std::string::size_type pos = (*i).mask.find(':');
133 if (pos == std::string::npos)
135 if (!i->mask.compare(0, pos, restriction))
136 minmode.assign(i->mask, pos + 1, std::string::npos);
140 PrefixMode* mh = ExemptChanOps::FindMode(minmode);
141 if (mh && mypfx >= mh->GetPrefixRank())
142 return MOD_RES_ALLOW;
143 if (mh || minmode == "*")
146 return MOD_RES_PASSTHRU;
150 class ModuleExemptChanOps : public Module
155 ModuleExemptChanOps() : eh(this)
159 Version GetVersion() CXX11_OVERRIDE
161 return Version("Provides the ability to allow channel operators to be exempt from certain modes", VF_VENDOR);
164 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
170 MODULE_INIT(ModuleExemptChanOps)