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)
34 static PrefixMode* FindMode(const std::string& mode)
36 if (mode.length() == 1)
37 return ServerInstance->Modes->FindPrefixMode(mode[0]);
39 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_CHANNEL);
40 return mh ? mh->IsPrefixMode() : NULL;
43 static bool ParseEntry(const std::string& entry, std::string& restriction, std::string& prefix)
45 // The entry must be in the format <restriction>:<prefix>.
46 std::string::size_type colon = entry.find(':');
47 if (colon == std::string::npos || colon == entry.length()-1)
50 restriction.assign(entry, 0, colon);
51 prefix.assign(entry, colon + 1, std::string::npos);
55 ModResult AccessCheck(User* source, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
57 std::string restriction;
59 if (!ParseEntry(parameter, restriction, prefix))
60 return MOD_RES_PASSTHRU;
62 PrefixMode* pm = FindMode(prefix);
64 return MOD_RES_PASSTHRU;
66 if (channel->GetPrefixValue(source) >= pm->GetLevelRequired(adding))
67 return MOD_RES_PASSTHRU;
69 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, InspIRCd::Format("You must be able to %s mode %c (%s) to %s a restriction containing it",
70 adding ? "set" : "unset", pm->GetModeChar(), pm->name.c_str(), adding ? "add" : "remove"));
74 bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
76 std::string restriction;
78 if (!ParseEntry(word, restriction, prefix))
80 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Invalid exemptchanops entry, format is <restriction>:<prefix>"));
84 // If there is a '-' in the restriction string ignore it and everything after it
85 // to support "auditorium-vis" and "auditorium-see" in m_auditorium
86 std::string::size_type dash = restriction.find('-');
87 if (dash != std::string::npos)
88 restriction.erase(dash);
90 if (!ServerInstance->Modes->FindMode(restriction, MODETYPE_CHANNEL))
92 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown restriction"));
96 if (prefix != "*" && !FindMode(prefix))
98 user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown prefix mode"));
106 class ExemptHandler : public CheckExemption::EventListener
110 ExemptHandler(Module* me)
111 : CheckExemption::EventListener(me)
116 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
118 unsigned int mypfx = chan->GetPrefixValue(user);
121 ListModeBase::ModeList* list = ec.GetList(chan);
125 for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
127 std::string::size_type pos = (*i).mask.find(':');
128 if (pos == std::string::npos)
130 if (!i->mask.compare(0, pos, restriction))
131 minmode.assign(i->mask, pos + 1, std::string::npos);
135 PrefixMode* mh = ExemptChanOps::FindMode(minmode);
136 if (mh && mypfx >= mh->GetPrefixRank())
137 return MOD_RES_ALLOW;
138 if (mh || minmode == "*")
141 return MOD_RES_PASSTHRU;
145 class ModuleExemptChanOps : public Module
150 ModuleExemptChanOps() : eh(this)
154 Version GetVersion() CXX11_OVERRIDE
156 return Version("Provides the ability to allow channel operators to be exempt from certain modes", VF_VENDOR);
159 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
165 MODULE_INIT(ModuleExemptChanOps)