]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_autoop.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[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) : ListModeBase(Creator, "autoop", 'w', "End of Channel Access List", 910, 911, true)
30         {
31                 levelrequired = OP_VALUE;
32                 tidy = false;
33         }
34
35         PrefixMode* FindMode(const std::string& mid)
36         {
37                 if (mid.length() == 1)
38                         return ServerInstance->Modes->FindPrefixMode(mid[0]);
39
40                 ModeHandler* mh = ServerInstance->Modes->FindMode(mid, MODETYPE_CHANNEL);
41                 return mh ? mh->IsPrefixMode() : NULL;
42         }
43
44         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding)
45         {
46                 std::string::size_type pos = parameter.find(':');
47                 if (pos == 0 || pos == std::string::npos)
48                         return adding ? MOD_RES_DENY : MOD_RES_PASSTHRU;
49                 unsigned int mylevel = channel->GetPrefixValue(source);
50                 std::string mid(parameter, 0, pos);
51                 PrefixMode* mh = FindMode(mid);
52
53                 if (adding && !mh)
54                 {
55                         source->WriteNumeric(415, mid, InspIRCd::Format("Cannot find prefix mode '%s' for autoop", mid.c_str()));
56                         return MOD_RES_DENY;
57                 }
58                 else if (!mh)
59                         return MOD_RES_PASSTHRU;
60
61                 std::string dummy;
62                 if (mh->AccessCheck(source, channel, dummy, true) == MOD_RES_DENY)
63                         return MOD_RES_DENY;
64                 if (mh->GetLevelRequired() > mylevel)
65                 {
66                         source->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, InspIRCd::Format("You must be able to set mode '%s' to include it in an autoop", mid.c_str()));
67                         return MOD_RES_DENY;
68                 }
69                 return MOD_RES_PASSTHRU;
70         }
71 };
72
73 class ModuleAutoOp : public Module
74 {
75         AutoOpList mh;
76
77  public:
78         ModuleAutoOp() : mh(this)
79         {
80         }
81
82         void OnPostJoin(Membership *memb) CXX11_OVERRIDE
83         {
84                 if (!IS_LOCAL(memb->user))
85                         return;
86
87                 ListModeBase::ModeList* list = mh.GetList(memb->chan);
88                 if (list)
89                 {
90                         Modes::ChangeList changelist;
91                         for (ListModeBase::ModeList::iterator it = list->begin(); it != list->end(); it++)
92                         {
93                                 std::string::size_type colon = it->mask.find(':');
94                                 if (colon == std::string::npos)
95                                         continue;
96                                 if (memb->chan->CheckBan(memb->user, it->mask.substr(colon+1)))
97                                 {
98                                         PrefixMode* given = mh.FindMode(it->mask.substr(0, colon));
99                                         if (given)
100                                                 changelist.push_add(given, memb->user->nick);
101                                 }
102                         }
103                         ServerInstance->Modes->Process(ServerInstance->FakeClient, memb->chan, NULL, changelist);
104                 }
105         }
106
107         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
108         {
109                 mh.DoRehash();
110         }
111
112         Version GetVersion() CXX11_OVERRIDE
113         {
114                 return Version("Provides support for the +w channel mode", VF_VENDOR);
115         }
116 };
117
118 MODULE_INIT(ModuleAutoOp)