]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_autoop.cpp
889c3c801aca12e21ff4149043ac3e8c588e0798
[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                 const ModeParser::PrefixModeList& pmlist = ServerInstance->Modes->GetPrefixModes();
41                 for (ModeParser::PrefixModeList::const_iterator i = pmlist.begin(); i != pmlist.end(); ++i)
42                 {
43                         PrefixMode* mh = *i;
44                         if (mh->name == mid)
45                                 return mh;
46                 }
47                 return NULL;
48         }
49
50         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding)
51         {
52                 std::string::size_type pos = parameter.find(':');
53                 if (pos == 0 || pos == std::string::npos)
54                         return adding ? MOD_RES_DENY : MOD_RES_PASSTHRU;
55                 unsigned int mylevel = channel->GetPrefixValue(source);
56                 std::string mid = parameter.substr(0, pos);
57                 PrefixMode* mh = FindMode(mid);
58
59                 if (adding && !mh)
60                 {
61                         source->WriteNumeric(415, "%s :Cannot find prefix mode '%s' for autoop",
62                                 mid.c_str(), mid.c_str());
63                         return MOD_RES_DENY;
64                 }
65                 else if (!mh)
66                         return MOD_RES_PASSTHRU;
67
68                 std::string dummy;
69                 if (mh->AccessCheck(source, channel, dummy, true) == MOD_RES_DENY)
70                         return MOD_RES_DENY;
71                 if (mh->GetLevelRequired() > mylevel)
72                 {
73                         source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You must be able to set mode '%s' to include it in an autoop",
74                                 channel->name.c_str(), mid.c_str());
75                         return MOD_RES_DENY;
76                 }
77                 return MOD_RES_PASSTHRU;
78         }
79 };
80
81 class ModuleAutoOp : public Module
82 {
83         AutoOpList mh;
84
85  public:
86         ModuleAutoOp() : mh(this)
87         {
88         }
89
90         void OnPostJoin(Membership *memb) CXX11_OVERRIDE
91         {
92                 if (!IS_LOCAL(memb->user))
93                         return;
94
95                 ListModeBase::ModeList* list = mh.GetList(memb->chan);
96                 if (list)
97                 {
98                         std::string modeline("+");
99                         std::vector<std::string> modechange;
100                         modechange.push_back(memb->chan->name);
101                         for (ListModeBase::ModeList::iterator it = list->begin(); it != list->end(); it++)
102                         {
103                                 std::string::size_type colon = it->mask.find(':');
104                                 if (colon == std::string::npos)
105                                         continue;
106                                 if (memb->chan->CheckBan(memb->user, it->mask.substr(colon+1)))
107                                 {
108                                         PrefixMode* given = mh.FindMode(it->mask.substr(0, colon));
109                                         if (given)
110                                                 modeline.push_back(given->GetModeChar());
111                                 }
112                         }
113                         modechange.push_back(modeline);
114                         for(std::string::size_type i = modeline.length(); i > 1; --i) // we use "i > 1" instead of "i" so we skip the +
115                                 modechange.push_back(memb->user->nick);
116                         if(modechange.size() >= 3)
117                                 ServerInstance->Modes->Process(modechange, ServerInstance->FakeClient);
118                 }
119         }
120
121         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
122         {
123                 mh.DoRehash();
124         }
125
126         Version GetVersion() CXX11_OVERRIDE
127         {
128                 return Version("Provides support for the +w channel mode", VF_VENDOR);
129         }
130 };
131
132 MODULE_INIT(ModuleAutoOp)