]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_autoop.cpp
Merge pull request #574 from SaberUK/master+build-comment-cleanup
[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         ModeHandler* FindMode(const std::string& mid)
36         {
37                 if (mid.length() == 1)
38                         return ServerInstance->Modes->FindMode(mid[0], MODETYPE_CHANNEL);
39                 for(char c='A'; c < 'z'; c++)
40                 {
41                         ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
42                         if (mh && mh->name == mid)
43                                 return mh;
44                 }
45                 return NULL;
46         }
47
48         ModResult AccessCheck(User* source, Channel* channel, std::string &parameter, bool adding)
49         {
50                 std::string::size_type pos = parameter.find(':');
51                 if (pos == 0 || pos == std::string::npos)
52                         return adding ? MOD_RES_DENY : MOD_RES_PASSTHRU;
53                 unsigned int mylevel = channel->GetPrefixValue(source);
54                 std::string mid = parameter.substr(0, pos);
55                 ModeHandler* mh = FindMode(mid);
56
57                 if (adding && (!mh || !mh->GetPrefixRank()))
58                 {
59                         source->WriteNumeric(415, "%s %s :Cannot find prefix mode '%s' for autoop",
60                                 source->nick.c_str(), mid.c_str(), 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() > mylevel)
70                 {
71                         source->WriteNumeric(482, "%s %s :You must be able to set mode '%s' to include it in an autoop",
72                                 source->nick.c_str(), channel->name.c_str(), mid.c_str());
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 init() CXX11_OVERRIDE
89         {
90                 ServerInstance->Modules->AddService(mh);
91                 mh.DoImplements(this);
92
93                 Implementation list[] = { I_OnPostJoin, };
94                 ServerInstance->Modules->Attach(list, this, sizeof(list)/sizeof(Implementation));
95         }
96
97         void OnPostJoin(Membership *memb) CXX11_OVERRIDE
98         {
99                 if (!IS_LOCAL(memb->user))
100                         return;
101
102                 ListModeBase::ModeList* list = mh.GetList(memb->chan);
103                 if (list)
104                 {
105                         std::string modeline("+");
106                         std::vector<std::string> modechange;
107                         modechange.push_back(memb->chan->name);
108                         for (ListModeBase::ModeList::iterator it = list->begin(); it != list->end(); it++)
109                         {
110                                 std::string::size_type colon = it->mask.find(':');
111                                 if (colon == std::string::npos)
112                                         continue;
113                                 if (memb->chan->CheckBan(memb->user, it->mask.substr(colon+1)))
114                                 {
115                                         ModeHandler* given = mh.FindMode(it->mask.substr(0, colon));
116                                         if (given && given->GetPrefixRank())
117                                                 modeline.push_back(given->GetModeChar());
118                                 }
119                         }
120                         modechange.push_back(modeline);
121                         for(std::string::size_type i = modeline.length(); i > 1; --i) // we use "i > 1" instead of "i" so we skip the +
122                                 modechange.push_back(memb->user->nick);
123                         if(modechange.size() >= 3)
124                                 ServerInstance->Modes->Process(modechange, ServerInstance->FakeClient);
125                 }
126         }
127
128         void OnSyncChannel(Channel* chan, Module* proto, void* opaque) CXX11_OVERRIDE
129         {
130                 mh.DoSyncChannel(chan, proto, opaque);
131         }
132
133         void OnRehash(User* user) CXX11_OVERRIDE
134         {
135                 mh.DoRehash();
136         }
137
138         Version GetVersion() CXX11_OVERRIDE
139         {
140                 return Version("Provides support for the +w channel mode", VF_VENDOR);
141         }
142 };
143
144 MODULE_INIT(ModuleAutoOp)