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