2 * InspIRCd -- Internet Relay Chat Daemon
4 *. Copyright (C) 2018 Peter Powell <petpow@saberuk.com>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8 * Copyright (C) 2005 Craig Edwards <craigedwards@brainbox.cc>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 BadChannel(const std::string& Name, const std::string& Redirect, const std::string& Reason, bool AllowOpers)
40 : allowopers(AllowOpers)
48 typedef std::vector<BadChannel> BadChannels;
49 typedef std::vector<std::string> GoodChannels;
51 class ModuleDenyChannels : public Module
54 BadChannels badchannels;
55 GoodChannels goodchannels;
56 UserModeReference antiredirectmode;
57 ChanModeReference redirectmode;
61 : antiredirectmode(this, "antiredirect")
62 , redirectmode(this, "redirect")
66 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
68 GoodChannels goodchans;
69 ConfigTagList tags = ServerInstance->Config->ConfTags("goodchan");
70 for (ConfigIter iter = tags.first; iter != tags.second; ++iter)
72 ConfigTag* tag = iter->second;
74 // Ensure that we have the <goodchan:name> parameter.
75 const std::string name = tag->getString("name");
77 throw ModuleException("<goodchan:name> is a mandatory field, at " + tag->getTagLocation());
79 goodchans.push_back(name);
83 tags = ServerInstance->Config->ConfTags("badchan");
84 for (ConfigIter i = tags.first; i != tags.second; ++i)
86 ConfigTag* tag = i->second;
88 // Ensure that we have the <badchan:name> parameter.
89 const std::string name = tag->getString("name");
91 throw ModuleException("<badchan:name> is a mandatory field, at " + tag->getTagLocation());
93 // Ensure that we have the <badchan:reason> parameter.
94 const std::string reason = tag->getString("reason");
96 throw ModuleException("<badchan:reason> is a mandatory field, at " + tag->getTagLocation());
98 const std::string redirect = tag->getString("redirect");
99 if (!redirect.empty())
101 // Ensure that <badchan:redirect> contains a channel name.
102 if (!ServerInstance->IsChannel(redirect))
103 throw ModuleException("<badchan:redirect> is not a valid channel name, at " + tag->getTagLocation());
105 // We defer the rest of the validation of the redirect channel until we have
106 // finished parsing all of the badchans.
109 badchans.push_back(BadChannel(name, redirect, reason, tag->getBool("allowopers")));
112 // Now we have all of the badchan information recorded we can check that all redirect
113 // channels can actually be redirected to.
114 for (BadChannels::const_iterator i = badchans.begin(); i != badchans.end(); ++i)
116 const BadChannel& badchan = *i;
118 // If there is no redirect channel we have nothing to do.
119 if (badchan.redirect.empty())
122 // If the redirect channel is whitelisted then it is okay.
123 for (GoodChannels::const_iterator j = goodchans.begin(); j != goodchans.end(); ++j)
124 if (InspIRCd::Match(badchan.redirect, *j))
127 // If the redirect channel is not blacklisted then it is okay.
128 for (BadChannels::const_iterator j = badchans.begin(); j != badchans.end(); ++j)
129 if (InspIRCd::Match(badchan.redirect, j->name))
130 throw ModuleException("<badchan:redirect> cannot be a blacklisted channel name");
133 // The config file contained no errors so we can apply the new configuration.
134 badchannels.swap(badchans);
135 goodchannels.swap(goodchans);
138 Version GetVersion() CXX11_OVERRIDE
140 return Version("Implements config tags which allow blocking of joins to channels", VF_VENDOR);
144 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
146 for (BadChannels::const_iterator j = badchannels.begin(); j != badchannels.end(); ++j)
148 const BadChannel& badchan = *j;
150 // If the channel does not match the current entry we have nothing else to do.
151 if (!InspIRCd::Match(cname, badchan.name))
154 // If the user is an oper and opers are allowed to enter this blacklisted channel
155 // then allow the join.
156 if (user->IsOper() && badchan.allowopers)
157 return MOD_RES_PASSTHRU;
159 // If the channel matches a whitelist then allow the join.
160 for (GoodChannels::const_iterator i = goodchannels.begin(); i != goodchannels.end(); ++i)
161 if (InspIRCd::Match(cname, *i))
162 return MOD_RES_PASSTHRU;
164 // If there is no redirect chan, the user has enabled the antiredirect mode, or
165 // the target channel redirects elsewhere we just tell the user and deny the join.
166 Channel* target = NULL;
167 if (badchan.redirect.empty() || user->IsModeSet(antiredirectmode)
168 || ((target = ServerInstance->FindChan(badchan.redirect)) && target->IsModeSet(redirectmode)))
170 user->WriteNumeric(ERR_BADCHANNEL, cname, InspIRCd::Format("Channel %s is forbidden: %s",
171 cname.c_str(), badchan.reason.c_str()));
175 // Redirect the user to the target channel.
176 user->WriteNumeric(ERR_BADCHANNEL, cname, InspIRCd::Format("Channel %s is forbidden, redirecting to %s: %s",
177 cname.c_str(), badchan.redirect.c_str(), badchan.reason.c_str()));
178 Channel::JoinUser(user, badchan.redirect);
181 return MOD_RES_PASSTHRU;
185 MODULE_INIT(ModuleDenyChannels)