2 * InspIRCd -- Internet Relay Chat Daemon
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, 2010 Craig Edwards <brain@inspircd.org>
12 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
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.
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
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/>.
43 BadChannel(const std::string& Name, const std::string& Redirect, const std::string& Reason, bool AllowOpers)
44 : allowopers(AllowOpers)
52 typedef std::vector<BadChannel> BadChannels;
53 typedef std::vector<std::string> GoodChannels;
55 class ModuleDenyChannels : public Module
58 BadChannels badchannels;
59 GoodChannels goodchannels;
60 UserModeReference antiredirectmode;
61 ChanModeReference redirectmode;
65 : antiredirectmode(this, "antiredirect")
66 , redirectmode(this, "redirect")
70 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
72 GoodChannels goodchans;
73 ConfigTagList tags = ServerInstance->Config->ConfTags("goodchan");
74 for (ConfigIter iter = tags.first; iter != tags.second; ++iter)
76 ConfigTag* tag = iter->second;
78 // Ensure that we have the <goodchan:name> parameter.
79 const std::string name = tag->getString("name");
81 throw ModuleException("<goodchan:name> is a mandatory field, at " + tag->getTagLocation());
83 goodchans.push_back(name);
87 tags = ServerInstance->Config->ConfTags("badchan");
88 for (ConfigIter i = tags.first; i != tags.second; ++i)
90 ConfigTag* tag = i->second;
92 // Ensure that we have the <badchan:name> parameter.
93 const std::string name = tag->getString("name");
95 throw ModuleException("<badchan:name> is a mandatory field, at " + tag->getTagLocation());
97 // Ensure that we have the <badchan:reason> parameter.
98 const std::string reason = tag->getString("reason");
100 throw ModuleException("<badchan:reason> is a mandatory field, at " + tag->getTagLocation());
102 const std::string redirect = tag->getString("redirect");
103 if (!redirect.empty())
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());
109 // We defer the rest of the validation of the redirect channel until we have
110 // finished parsing all of the badchans.
113 badchans.push_back(BadChannel(name, redirect, reason, tag->getBool("allowopers")));
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)
120 const BadChannel& badchan = *i;
122 // If there is no redirect channel we have nothing to do.
123 if (badchan.redirect.empty())
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)
130 if (InspIRCd::Match(badchan.redirect, *j))
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");
146 // The config file contained no errors so we can apply the new configuration.
147 badchannels.swap(badchans);
148 goodchannels.swap(goodchans);
151 Version GetVersion() CXX11_OVERRIDE
153 return Version("Allows the server administrator to prevent users from joining channels matching a glob.", VF_VENDOR);
157 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
159 for (BadChannels::const_iterator j = badchannels.begin(); j != badchannels.end(); ++j)
161 const BadChannel& badchan = *j;
163 // If the channel does not match the current entry we have nothing else to do.
164 if (!InspIRCd::Match(cname, badchan.name))
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;
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;
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)))
183 user->WriteNumeric(ERR_BADCHANNEL, cname, InspIRCd::Format("Channel %s is forbidden: %s",
184 cname.c_str(), badchan.reason.c_str()));
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);
194 return MOD_RES_PASSTHRU;
198 MODULE_INIT(ModuleDenyChannels)