]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_autoop.cpp
Show the mode syntax in ERR_INVALIDMODEPARAM.
[user/henk/code/inspircd.git] / src / modules / m_autoop.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2011 Jackmcbarn <jackmcbarn@jackmcbarn.no-ip.org>
5  *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "listmode.h"
23
24 /** Handles +w channel mode
25  */
26 class AutoOpList : public ListModeBase
27 {
28  public:
29         AutoOpList(Module* Creator)
30                 : ListModeBase(Creator, "autoop", 'w', "End of Channel Access List", 910, 911, true)
31         {
32                 ranktoset = ranktounset = OP_VALUE;
33                 syntax = "<prefix>:<mask>";
34                 tidy = false;
35         }
36
37         PrefixMode* FindMode(const std::string& mid)
38         {
39                 if (mid.length() == 1)
40                         return ServerInstance->Modes->FindPrefixMode(mid[0]);
41
42                 ModeHandler* mh = ServerInstance->Modes->FindMode(mid, MODETYPE_CHANNEL);
43                 return mh ? mh->IsPrefixMode() : NULL;
44         }
45
46         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding) CXX11_OVERRIDE
47         {
48                 std::string::size_type pos = parameter.find(':');
49                 if (pos == 0 || pos == std::string::npos)
50                         return adding ? MOD_RES_DENY : MOD_RES_PASSTHRU;
51                 unsigned int mylevel = channel->GetPrefixValue(source);
52                 std::string mid(parameter, 0, pos);
53                 PrefixMode* mh = FindMode(mid);
54
55                 if (adding && !mh)
56                 {
57                         source->WriteNumeric(ERR_UNKNOWNMODE, mid, InspIRCd::Format("Cannot find prefix mode '%s' for autoop", mid.c_str()));
58                         return MOD_RES_DENY;
59                 }
60                 else if (!mh)
61                         return MOD_RES_PASSTHRU;
62
63                 std::string dummy;
64                 if (mh->AccessCheck(source, channel, dummy, true) == MOD_RES_DENY)
65                         return MOD_RES_DENY;
66                 if (mh->GetLevelRequired(adding) > mylevel)
67                 {
68                         source->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, InspIRCd::Format("You must be able to %s mode %c (%s) to %s an autoop containing it",
69                                 adding ? "set" : "unset", mh->GetModeChar(), mh->name.c_str(), adding ? "add" : "remove"));
70                         return MOD_RES_DENY;
71                 }
72                 return MOD_RES_PASSTHRU;
73         }
74 };
75
76 class ModuleAutoOp : public Module
77 {
78         AutoOpList mh;
79
80  public:
81         ModuleAutoOp() : mh(this)
82         {
83         }
84
85         void OnPostJoin(Membership *memb) CXX11_OVERRIDE
86         {
87                 if (!IS_LOCAL(memb->user))
88                         return;
89
90                 ListModeBase::ModeList* list = mh.GetList(memb->chan);
91                 if (list)
92                 {
93                         Modes::ChangeList changelist;
94                         for (ListModeBase::ModeList::iterator it = list->begin(); it != list->end(); it++)
95                         {
96                                 std::string::size_type colon = it->mask.find(':');
97                                 if (colon == std::string::npos)
98                                         continue;
99                                 if (memb->chan->CheckBan(memb->user, it->mask.substr(colon+1)))
100                                 {
101                                         PrefixMode* given = mh.FindMode(it->mask.substr(0, colon));
102                                         if (given)
103                                                 changelist.push_add(given, memb->user->nick);
104                                 }
105                         }
106                         ServerInstance->Modes->Process(ServerInstance->FakeClient, memb->chan, NULL, changelist);
107                 }
108         }
109
110         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
111         {
112                 mh.DoRehash();
113         }
114
115         Version GetVersion() CXX11_OVERRIDE
116         {
117                 return Version("Provides channel mode +w, basic channel access controls", VF_VENDOR);
118         }
119 };
120
121 MODULE_INIT(ModuleAutoOp)