]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_autoop.cpp
Introduce ModeProcessFlags, can be passed to ModeParser::Process() to indicate local...
[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 /* $ModDesc: Provides support for the +w channel mode, autoop list */
25
26 /** Handles +w channel mode
27  */
28 class AutoOpList : public ListModeBase
29 {
30  public:
31         AutoOpList(Module* Creator) : ListModeBase(Creator, "autoop", 'w', "End of Channel Access List", 910, 911, true)
32         {
33                 levelrequired = OP_VALUE;
34                 tidy = false;
35         }
36
37         ModeHandler* FindMode(const std::string& mid)
38         {
39                 if (mid.length() == 1)
40                         return ServerInstance->Modes->FindMode(mid[0], MODETYPE_CHANNEL);
41                 for(char c='A'; c < 'z'; c++)
42                 {
43                         ModeHandler* mh = ServerInstance->Modes->FindMode(c, MODETYPE_CHANNEL);
44                         if (mh && 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                 ModeHandler* mh = FindMode(mid);
58
59                 if (adding && (!mh || !mh->GetPrefixRank()))
60                 {
61                         source->WriteNumeric(415, "%s %s :Cannot find prefix mode '%s' for autoop",
62                                 source->nick.c_str(), 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(482, "%s %s :You must be able to set mode '%s' to include it in an autoop",
74                                 source->nick.c_str(), 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 init() CXX11_OVERRIDE
91         {
92                 ServerInstance->Modules->AddService(mh);
93                 mh.DoImplements(this);
94
95                 Implementation list[] = { I_OnPostJoin, };
96                 ServerInstance->Modules->Attach(list, this, sizeof(list)/sizeof(Implementation));
97         }
98
99         void OnPostJoin(Membership *memb) CXX11_OVERRIDE
100         {
101                 if (!IS_LOCAL(memb->user))
102                         return;
103
104                 ListModeBase::ModeList* list = mh.GetList(memb->chan);
105                 if (list)
106                 {
107                         std::string modeline("+");
108                         std::vector<std::string> modechange;
109                         modechange.push_back(memb->chan->name);
110                         for (ListModeBase::ModeList::iterator it = list->begin(); it != list->end(); it++)
111                         {
112                                 std::string::size_type colon = it->mask.find(':');
113                                 if (colon == std::string::npos)
114                                         continue;
115                                 if (memb->chan->CheckBan(memb->user, it->mask.substr(colon+1)))
116                                 {
117                                         ModeHandler* given = mh.FindMode(it->mask.substr(0, colon));
118                                         if (given && given->GetPrefixRank())
119                                                 modeline.push_back(given->GetModeChar());
120                                 }
121                         }
122                         modechange.push_back(modeline);
123                         for(std::string::size_type i = modeline.length(); i > 1; --i) // we use "i > 1" instead of "i" so we skip the +
124                                 modechange.push_back(memb->user->nick);
125                         if(modechange.size() >= 3)
126                                 ServerInstance->Modes->Process(modechange, ServerInstance->FakeClient);
127                 }
128         }
129
130         void OnSyncChannel(Channel* chan, Module* proto, void* opaque) CXX11_OVERRIDE
131         {
132                 mh.DoSyncChannel(chan, proto, opaque);
133         }
134
135         void OnRehash(User* user) CXX11_OVERRIDE
136         {
137                 mh.DoRehash();
138         }
139
140         Version GetVersion() CXX11_OVERRIDE
141         {
142                 return Version("Provides support for the +w channel mode", VF_VENDOR);
143         }
144 };
145
146 MODULE_INIT(ModuleAutoOp)