2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
22 #include "modules/exemption.h"
24 /** Handles channel mode +X
26 class ExemptChanOps : public ListModeBase
29 ExemptChanOps(Module* Creator)
30 : ListModeBase(Creator, "exemptchanops", 'X', "End of channel exemptchanops list", 954, 953, false)
32 syntax = "<restriction>:<prefix>";
35 static PrefixMode* FindMode(const std::string& mode)
37 if (mode.length() == 1)
38 return ServerInstance->Modes->FindPrefixMode(mode[0]);
40 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_CHANNEL);
41 return mh ? mh->IsPrefixMode() : NULL;
44 static bool ParseEntry(const std::string& entry, std::string& restriction, std::string& prefix)
46 // The entry must be in the format <restriction>:<prefix>.
47 std::string::size_type colon = entry.find(':');
48 if (colon == std::string::npos || colon == entry.length()-1)
51 restriction.assign(entry, 0, colon);
52 prefix.assign(entry, colon + 1, std::string::npos);
56 ModResult AccessCheck(User* source, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
58 std::string restriction;
60 if (!ParseEntry(parameter, restriction, prefix))
61 return MOD_RES_PASSTHRU;
63 PrefixMode* pm = FindMode(prefix);
65 return MOD_RES_PASSTHRU;
67 if (channel->GetPrefixValue(source) >= pm->GetLevelRequired(adding))
68 return MOD_RES_PASSTHRU;
70 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, InspIRCd::Format("You must be able to %s mode %c (%s) to %s a restriction containing it",
71 adding ? "set" : "unset", pm->GetModeChar(), pm->name.c_str(), adding ? "add" : "remove"));
75 bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
77 std::string restriction;
79 if (!ParseEntry(word, restriction, prefix))
81 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word));
85 // If there is a '-' in the restriction string ignore it and everything after it
86 // to support "auditorium-vis" and "auditorium-see" in m_auditorium
87 std::string::size_type dash = restriction.find('-');
88 if (dash != std::string::npos)
89 restriction.erase(dash);
91 if (!ServerInstance->Modes->FindMode(restriction, MODETYPE_CHANNEL))
93 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown restriction."));
97 if (prefix != "*" && !FindMode(prefix))
99 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown prefix mode."));
107 class ExemptHandler : public CheckExemption::EventListener
111 ExemptHandler(Module* me)
112 : CheckExemption::EventListener(me)
117 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
119 unsigned int mypfx = chan->GetPrefixValue(user);
122 ListModeBase::ModeList* list = ec.GetList(chan);
126 for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
128 std::string::size_type pos = (*i).mask.find(':');
129 if (pos == std::string::npos)
131 if (!i->mask.compare(0, pos, restriction))
132 minmode.assign(i->mask, pos + 1, std::string::npos);
136 PrefixMode* mh = ExemptChanOps::FindMode(minmode);
137 if (mh && mypfx >= mh->GetPrefixRank())
138 return MOD_RES_ALLOW;
139 if (mh || minmode == "*")
142 return MOD_RES_PASSTHRU;
146 class ModuleExemptChanOps : public Module
151 ModuleExemptChanOps() : eh(this)
155 Version GetVersion() CXX11_OVERRIDE
157 return Version("Provides the ability to allow channel operators to be exempt from certain modes", VF_VENDOR);
160 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
166 MODULE_INIT(ModuleExemptChanOps)