]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_exemptchanops.cpp
794b06f6abc77e4d91502ee53729b22f99f98901
[user/henk/code/inspircd.git] / src / modules / m_exemptchanops.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "listmode.h"
22 #include "modules/exemption.h"
23
24 /** Handles channel mode +X
25  */
26 class ExemptChanOps : public ListModeBase
27 {
28  public:
29         ExemptChanOps(Module* Creator)
30                 : ListModeBase(Creator, "exemptchanops", 'X', "End of channel exemptchanops list", 954, 953, false)
31         {
32                 syntax = "<restriction>:<prefix>";
33         }
34
35         static PrefixMode* FindMode(const std::string& mode)
36         {
37                 if (mode.length() == 1)
38                         return ServerInstance->Modes->FindPrefixMode(mode[0]);
39
40                 ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_CHANNEL);
41                 return mh ? mh->IsPrefixMode() : NULL;
42         }
43
44         static bool ParseEntry(const std::string& entry, std::string& restriction, std::string& prefix)
45         {
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)
49                         return false;
50
51                 restriction.assign(entry, 0, colon);
52                 prefix.assign(entry, colon + 1, std::string::npos);
53                 return true;
54         }
55
56         ModResult AccessCheck(User* source, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
57         {
58                 std::string restriction;
59                 std::string prefix;
60                 if (!ParseEntry(parameter, restriction, prefix))
61                         return MOD_RES_PASSTHRU;
62
63                 PrefixMode* pm = FindMode(prefix);
64                 if (!pm)
65                         return MOD_RES_PASSTHRU;
66
67                 if (channel->GetPrefixValue(source) >= pm->GetLevelRequired(adding))
68                         return MOD_RES_PASSTHRU;
69
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"));
72                 return MOD_RES_DENY;
73         }
74
75         bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
76         {
77                 std::string restriction;
78                 std::string prefix;
79                 if (!ParseEntry(word, restriction, prefix))
80                 {
81                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word));
82                         return false;
83                 }
84
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);
90
91                 if (!ServerInstance->Modes->FindMode(restriction, MODETYPE_CHANNEL))
92                 {
93                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown restriction."));
94                         return false;
95                 }
96
97                 if (prefix != "*" && !FindMode(prefix))
98                 {
99                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown prefix mode."));
100                         return false;
101                 }
102
103                 return true;
104         }
105 };
106
107 class ExemptHandler : public CheckExemption::EventListener
108 {
109  public:
110         ExemptChanOps ec;
111         ExemptHandler(Module* me)
112                 : CheckExemption::EventListener(me)
113                 , ec(me)
114         {
115         }
116
117         ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
118         {
119                 unsigned int mypfx = chan->GetPrefixValue(user);
120                 std::string minmode;
121
122                 ListModeBase::ModeList* list = ec.GetList(chan);
123
124                 if (list)
125                 {
126                         for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
127                         {
128                                 std::string::size_type pos = (*i).mask.find(':');
129                                 if (pos == std::string::npos)
130                                         continue;
131                                 if (!i->mask.compare(0, pos, restriction))
132                                         minmode.assign(i->mask, pos + 1, std::string::npos);
133                         }
134                 }
135
136                 PrefixMode* mh = ExemptChanOps::FindMode(minmode);
137                 if (mh && mypfx >= mh->GetPrefixRank())
138                         return MOD_RES_ALLOW;
139                 if (mh || minmode == "*")
140                         return MOD_RES_DENY;
141
142                 return MOD_RES_PASSTHRU;
143         }
144 };
145
146 class ModuleExemptChanOps : public Module
147 {
148         ExemptHandler eh;
149
150  public:
151         ModuleExemptChanOps() : eh(this)
152         {
153         }
154
155         Version GetVersion() CXX11_OVERRIDE
156         {
157                 return Version("Provides the ability to allow channel operators to be exempt from certain modes", VF_VENDOR);
158         }
159
160         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
161         {
162                 eh.ec.DoRehash();
163         }
164 };
165
166 MODULE_INIT(ModuleExemptChanOps)