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