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