]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/core_channel.cpp
Merge pull request #1271 from SaberUK/master+exemption
[user/henk/code/inspircd.git] / src / coremods / core_channel / core_channel.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "core_channel.h"
22 #include "invite.h"
23 #include "listmode.h"
24
25 class CoreModChannel : public Module, public CheckExemption::EventListener
26 {
27         Invite::APIImpl invapi;
28         CommandInvite cmdinvite;
29         CommandJoin cmdjoin;
30         CommandKick cmdkick;
31         CommandNames cmdnames;
32         CommandTopic cmdtopic;
33         insp::flat_map<std::string, char> exemptions;
34
35         ModResult IsInvited(User* user, Channel* chan)
36         {
37                 LocalUser* localuser = IS_LOCAL(user);
38                 if ((localuser) && (invapi.IsInvited(localuser, chan)))
39                         return MOD_RES_ALLOW;
40                 return MOD_RES_PASSTHRU;
41         }
42
43  public:
44         CoreModChannel()
45                 : CheckExemption::EventListener(this)
46                 , invapi(this)
47                 , cmdinvite(this, invapi)
48                 , cmdjoin(this)
49                 , cmdkick(this)
50                 , cmdnames(this)
51                 , cmdtopic(this)
52         {
53         }
54
55         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
56         {
57                 ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");
58                 Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
59                 if (optionstag->getBool("invitebypassmodes", true))
60                         ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
61                 else
62                 {
63                         for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
64                                 ServerInstance->Modules.Detach(events[i], this);
65                 }
66
67                 std::string current;
68                 irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
69                 insp::flat_map<std::string, char> exempts;
70                 while (defaultstream.GetToken(current))
71                 {
72                         std::string::size_type pos = current.find(':');
73                         if (pos == std::string::npos || (pos + 2) > current.size())
74                                 throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());
75
76                         const std::string restriction = current.substr(0, pos);
77                         const char prefix = current[pos + 1];
78
79                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
80                         exempts[restriction] = prefix;
81                 }
82                 exemptions.swap(exempts);
83         }
84
85         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
86         {
87                 // Build a map of limits to their mode character.
88                 insp::flat_map<int, std::string> limits;
89                 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
90                 for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
91                 {
92                         const unsigned int limit = (*iter)->GetLowerLimit();
93                         limits[limit].push_back((*iter)->GetModeChar());
94                 }
95
96                 // Generate the MAXLIST token from the limits map.
97                 std::string& buffer = tokens["MAXLIST"];
98                 for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
99                 {
100                         if (!buffer.empty())
101                                 buffer.push_back(',');
102
103                         buffer.append(iter->second);
104                         buffer.push_back(':');
105                         buffer.append(ConvToStr(iter->first));
106                 }
107         }
108
109         void OnPostJoin(Membership* memb) CXX11_OVERRIDE
110         {
111                 Channel* const chan = memb->chan;
112                 LocalUser* const localuser = IS_LOCAL(memb->user);
113                 if (localuser)
114                 {
115                         // Remove existing invite, if any
116                         invapi.Remove(localuser, chan);
117
118                         if (chan->topicset)
119                                 Topic::ShowTopic(localuser, chan);
120
121                         // Show all members of the channel, including invisible (+i) users
122                         cmdnames.SendNames(localuser, chan, true);
123                 }
124         }
125
126         ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
127         {
128                 // Hook only runs when being invited bypasses +bkl
129                 return IsInvited(user, chan);
130         }
131
132         ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
133         {
134                 // Hook only runs when being invited bypasses +bkl
135                 return IsInvited(user, chan);
136         }
137
138         ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
139         {
140                 // Hook only runs when being invited bypasses +bkl
141                 return IsInvited(user, chan);
142         }
143
144         ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
145         {
146                 // Hook always runs
147                 return IsInvited(user, chan);
148         }
149
150         void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
151         {
152                 invapi.RemoveAll(user);
153         }
154
155         void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
156         {
157                 // Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
158                 invapi.RemoveAll(chan);
159         }
160
161         ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
162         {
163                 if (!exemptions.count(restriction))
164                         return MOD_RES_PASSTHRU;
165
166                 unsigned int mypfx = chan->GetPrefixValue(user);
167                 char minmode = exemptions[restriction];
168
169                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
170                 if (mh && mypfx >= mh->GetPrefixRank())
171                         return MOD_RES_ALLOW;
172                 if (mh || minmode == '*')
173                         return MOD_RES_DENY;
174                 return MOD_RES_PASSTHRU;
175         }
176
177         void Prioritize() CXX11_OVERRIDE
178         {
179                 ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
180         }
181
182         Version GetVersion() CXX11_OVERRIDE
183         {
184                 return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
185         }
186 };
187
188 MODULE_INIT(CoreModChannel)