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