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