]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_denychans.cpp
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / src / modules / m_denychans.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2013, 2018 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012-2013 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2005, 2007, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "inspircd.h"
29
30 enum
31 {
32         // InspIRCd-specific.
33         ERR_BADCHANNEL = 926
34 };
35
36 struct BadChannel
37 {
38         bool allowopers;
39         std::string name;
40         std::string reason;
41         std::string redirect;
42
43         BadChannel(const std::string& Name, const std::string& Redirect, const std::string& Reason, bool AllowOpers)
44                 : allowopers(AllowOpers)
45                 , name(Name)
46                 , reason(Reason)
47                 , redirect(Redirect)
48         {
49         }
50 };
51
52 typedef std::vector<BadChannel> BadChannels;
53 typedef std::vector<std::string> GoodChannels;
54
55 class ModuleDenyChannels : public Module
56 {
57  private:
58         BadChannels badchannels;
59         GoodChannels goodchannels;
60         UserModeReference antiredirectmode;
61         ChanModeReference redirectmode;
62
63  public:
64         ModuleDenyChannels()
65                 : antiredirectmode(this, "antiredirect")
66                 , redirectmode(this, "redirect")
67         {
68         }
69
70         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
71         {
72                 GoodChannels goodchans;
73                 ConfigTagList tags = ServerInstance->Config->ConfTags("goodchan");
74                 for (ConfigIter iter = tags.first; iter != tags.second; ++iter)
75                 {
76                         ConfigTag* tag = iter->second;
77
78                         // Ensure that we have the <goodchan:name> parameter.
79                         const std::string name = tag->getString("name");
80                         if (name.empty())
81                                 throw ModuleException("<goodchan:name> is a mandatory field, at " + tag->getTagLocation());
82
83                         goodchans.push_back(name);
84                 }
85
86                 BadChannels badchans;
87                 tags = ServerInstance->Config->ConfTags("badchan");
88                 for (ConfigIter i = tags.first; i != tags.second; ++i)
89                 {
90                         ConfigTag* tag = i->second;
91
92                         // Ensure that we have the <badchan:name> parameter.
93                         const std::string name = tag->getString("name");
94                         if (name.empty())
95                                 throw ModuleException("<badchan:name> is a mandatory field, at " + tag->getTagLocation());
96
97                         // Ensure that we have the <badchan:reason> parameter.
98                         const std::string reason = tag->getString("reason");
99                         if (reason.empty())
100                                 throw ModuleException("<badchan:reason> is a mandatory field, at " + tag->getTagLocation());
101
102                         const std::string redirect = tag->getString("redirect");
103                         if (!redirect.empty())
104                         {
105                                 // Ensure that <badchan:redirect> contains a channel name.
106                                 if (!ServerInstance->IsChannel(redirect))
107                                         throw ModuleException("<badchan:redirect> is not a valid channel name, at " + tag->getTagLocation());
108
109                                 // We defer the rest of the validation of the redirect channel until we have
110                                 // finished parsing all of the badchans.
111                         }
112
113                         badchans.push_back(BadChannel(name, redirect, reason, tag->getBool("allowopers")));
114                 }
115
116                 // Now we have all of the badchan information recorded we can check that all redirect
117                 // channels can actually be redirected to.
118                 for (BadChannels::const_iterator i = badchans.begin(); i != badchans.end(); ++i)
119                 {
120                         const BadChannel& badchan = *i;
121
122                         // If there is no redirect channel we have nothing to do.
123                         if (badchan.redirect.empty())
124                                 continue;
125
126                         // If the redirect channel is whitelisted then it is okay.
127                         bool whitelisted = false;
128                         for (GoodChannels::const_iterator j = goodchans.begin(); j != goodchans.end(); ++j)
129                         {
130                                 if (InspIRCd::Match(badchan.redirect, *j))
131                                 {
132                                         whitelisted = true;
133                                         break;
134                                 }
135                         }
136
137                         if (whitelisted)
138                                 continue;
139
140                         // If the redirect channel is not blacklisted then it is okay.
141                         for (BadChannels::const_iterator j = badchans.begin(); j != badchans.end(); ++j)
142                                 if (InspIRCd::Match(badchan.redirect, j->name))
143                                         throw ModuleException("<badchan:redirect> cannot be a blacklisted channel name");
144                 }
145
146                 // The config file contained no errors so we can apply the new configuration.
147                 badchannels.swap(badchans);
148                 goodchannels.swap(goodchans);
149         }
150
151         Version GetVersion() CXX11_OVERRIDE
152         {
153                 return Version("Allows the server administrator to prevent users from joining channels matching a glob.", VF_VENDOR);
154         }
155
156
157         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
158         {
159                 for (BadChannels::const_iterator j = badchannels.begin(); j != badchannels.end(); ++j)
160                 {
161                         const BadChannel& badchan = *j;
162
163                         // If the channel does not match the current entry we have nothing else to do.
164                         if (!InspIRCd::Match(cname, badchan.name))
165                                 continue;
166
167                         // If the user is an oper and opers are allowed to enter this blacklisted channel
168                         // then allow the join.
169                         if (user->IsOper() && badchan.allowopers)
170                                 return MOD_RES_PASSTHRU;
171
172                         // If the channel matches a whitelist then allow the join.
173                         for (GoodChannels::const_iterator i = goodchannels.begin(); i != goodchannels.end(); ++i)
174                                 if (InspIRCd::Match(cname, *i))
175                                         return MOD_RES_PASSTHRU;
176
177                         // If there is no redirect chan, the user has enabled the antiredirect mode, or
178                         // the target channel redirects elsewhere we just tell the user and deny the join.
179                         Channel* target = NULL;
180                         if (badchan.redirect.empty() || user->IsModeSet(antiredirectmode)
181                                 || ((target = ServerInstance->FindChan(badchan.redirect)) && target->IsModeSet(redirectmode)))
182                         {
183                                 user->WriteNumeric(ERR_BADCHANNEL, cname, InspIRCd::Format("Channel %s is forbidden: %s",
184                                         cname.c_str(), badchan.reason.c_str()));
185                                 return MOD_RES_DENY;
186                         }
187
188                         // Redirect the user to the target channel.
189                         user->WriteNumeric(ERR_BADCHANNEL, cname, InspIRCd::Format("Channel %s is forbidden, redirecting to %s: %s",
190                                 cname.c_str(), badchan.redirect.c_str(), badchan.reason.c_str()));
191                         Channel::JoinUser(user, badchan.redirect);
192                         return MOD_RES_DENY;
193                 }
194                 return MOD_RES_PASSTHRU;
195         }
196 };
197
198 MODULE_INIT(ModuleDenyChannels)