]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_exemptchanops.cpp
a7f86cdb5440c25c6769b37a3688823f9d977eea
[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         }
33
34         bool ValidateParam(User* user, Channel* chan, std::string& word) CXX11_OVERRIDE
35         {
36                 std::string::size_type p = word.find(':');
37                 if (p == std::string::npos)
38                 {
39                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Invalid exemptchanops entry, format is <restriction>:<prefix>"));
40                         return false;
41                 }
42
43                 std::string restriction(word, 0, p);
44                 // If there is a '-' in the restriction string ignore it and everything after it
45                 // to support "auditorium-vis" and "auditorium-see" in m_auditorium
46                 p = restriction.find('-');
47                 if (p != std::string::npos)
48                         restriction.erase(p);
49
50                 if (!ServerInstance->Modes->FindMode(restriction, MODETYPE_CHANNEL))
51                 {
52                         user->WriteNumeric(Numerics::InvalidModeParameter(chan, this, word, "Unknown restriction"));
53                         return false;
54                 }
55
56                 return true;
57         }
58 };
59
60 class ExemptHandler : public CheckExemption::EventListener
61 {
62  public:
63         ExemptChanOps ec;
64         ExemptHandler(Module* me)
65                 : CheckExemption::EventListener(me)
66                 , ec(me)
67         {
68         }
69
70         PrefixMode* FindMode(const std::string& mid)
71         {
72                 if (mid.length() == 1)
73                         return ServerInstance->Modes->FindPrefixMode(mid[0]);
74
75                 ModeHandler* mh = ServerInstance->Modes->FindMode(mid, MODETYPE_CHANNEL);
76                 return mh ? mh->IsPrefixMode() : NULL;
77         }
78
79         ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
80         {
81                 unsigned int mypfx = chan->GetPrefixValue(user);
82                 std::string minmode;
83
84                 ListModeBase::ModeList* list = ec.GetList(chan);
85
86                 if (list)
87                 {
88                         for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
89                         {
90                                 std::string::size_type pos = (*i).mask.find(':');
91                                 if (pos == std::string::npos)
92                                         continue;
93                                 if (!i->mask.compare(0, pos, restriction))
94                                         minmode.assign(i->mask, pos + 1, std::string::npos);
95                         }
96                 }
97
98                 PrefixMode* mh = FindMode(minmode);
99                 if (mh && mypfx >= mh->GetPrefixRank())
100                         return MOD_RES_ALLOW;
101                 if (mh || minmode == "*")
102                         return MOD_RES_DENY;
103
104                 return MOD_RES_PASSTHRU;
105         }
106 };
107
108 class ModuleExemptChanOps : public Module
109 {
110         ExemptHandler eh;
111
112  public:
113         ModuleExemptChanOps() : eh(this)
114         {
115         }
116
117         Version GetVersion() CXX11_OVERRIDE
118         {
119                 return Version("Provides the ability to allow channel operators to be exempt from certain modes.",VF_VENDOR);
120         }
121
122         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
123         {
124                 eh.ec.DoRehash();
125         }
126 };
127
128 MODULE_INIT(ModuleExemptChanOps)