]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_exemptchanops.cpp
Merge pull request #574 from SaberUK/master+build-comment-cleanup
[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                 // TODO actually make sure there's a prop for this
33                 if ((word.length() > 35) || (word.empty()))
34                 {
35                         user->WriteNumeric(955, "%s %s %s :word is too %s for exemptchanops list",user->nick.c_str(), chan->name.c_str(), word.c_str(), (word.empty() ? "short" : "long"));
36                         return false;
37                 }
38
39                 return true;
40         }
41
42         void TellListTooLong(User* user, Channel* chan, std::string &word)
43         {
44                 user->WriteNumeric(959, "%s %s %s :Channel exemptchanops list is full", user->nick.c_str(), chan->name.c_str(), word.c_str());
45         }
46
47         void TellAlreadyOnList(User* user, Channel* chan, std::string &word)
48         {
49                 user->WriteNumeric(957, "%s %s :The word %s is already on the exemptchanops list",user->nick.c_str(), chan->name.c_str(), word.c_str());
50         }
51
52         void TellNotSet(User* user, Channel* chan, std::string &word)
53         {
54                 user->WriteNumeric(958, "%s %s :No such exemptchanops word is set",user->nick.c_str(), chan->name.c_str());
55         }
56 };
57
58 class ExemptHandler : public HandlerBase3<ModResult, User*, Channel*, const std::string&>
59 {
60  public:
61         ExemptChanOps ec;
62         ExemptHandler(Module* me) : ec(me) {}
63
64         ModeHandler* FindMode(const std::string& mid)
65         {
66                 if (mid.length() == 1)
67                         return ServerInstance->Modes->FindMode(mid[0], MODETYPE_CHANNEL);
68                 for(char c='A'; c < 'z'; c++)
69                 {
70                         ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
71                         if (mh && mh->name == mid)
72                                 return mh;
73                 }
74                 return NULL;
75         }
76
77         ModResult Call(User* user, Channel* chan, const std::string& restriction)
78         {
79                 unsigned int mypfx = chan->GetPrefixValue(user);
80                 std::string minmode;
81
82                 ListModeBase::ModeList* list = ec.GetList(chan);
83
84                 if (list)
85                 {
86                         for (ListModeBase::ModeList::iterator i = list->begin(); i != list->end(); ++i)
87                         {
88                                 std::string::size_type pos = (*i).mask.find(':');
89                                 if (pos == std::string::npos)
90                                         continue;
91                                 if ((*i).mask.substr(0,pos) == restriction)
92                                         minmode = (*i).mask.substr(pos + 1);
93                         }
94                 }
95
96                 ModeHandler* mh = FindMode(minmode);
97                 if (mh && mypfx >= mh->GetPrefixRank())
98                         return MOD_RES_ALLOW;
99                 if (mh || minmode == "*")
100                         return MOD_RES_DENY;
101
102                 return ServerInstance->HandleOnCheckExemption.Call(user, chan, restriction);
103         }
104 };
105
106 class ModuleExemptChanOps : public Module
107 {
108         std::string defaults;
109         ExemptHandler eh;
110
111  public:
112         ModuleExemptChanOps() : eh(this)
113         {
114         }
115
116         void init() CXX11_OVERRIDE
117         {
118                 ServerInstance->Modules->AddService(eh.ec);
119                 Implementation eventlist[] = { I_OnRehash, I_OnSyncChannel };
120                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
121                 ServerInstance->OnCheckExemption = &eh;
122
123                 OnRehash(NULL);
124         }
125
126         ~ModuleExemptChanOps()
127         {
128                 ServerInstance->OnCheckExemption = &ServerInstance->HandleOnCheckExemption;
129         }
130
131         Version GetVersion() CXX11_OVERRIDE
132         {
133                 return Version("Provides the ability to allow channel operators to be exempt from certain modes.",VF_VENDOR);
134         }
135
136         void OnRehash(User* user) CXX11_OVERRIDE
137         {
138                 eh.ec.DoRehash();
139         }
140
141         void OnSyncChannel(Channel* chan, Module* proto, void* opaque) CXX11_OVERRIDE
142         {
143                 eh.ec.DoSyncChannel(chan, proto, opaque);
144         }
145 };
146
147 MODULE_INIT(ModuleExemptChanOps)