]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_denychans.cpp
Use CommandBase::Params instead of std::vector<std::string>.
[user/henk/code/inspircd.git] / src / modules / m_denychans.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
9  *
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.
13  *
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
17  * details.
18  *
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/>.
21  */
22
23
24 #include "inspircd.h"
25
26 enum
27 {
28         // InspIRCd-specific.
29         ERR_BADCHANNEL = 926
30 };
31
32 struct BadChannel
33 {
34         bool allowopers;
35         std::string name;
36         std::string reason;
37         std::string redirect;
38
39         BadChannel(const std::string& Name, const std::string& Redirect, const std::string& Reason, bool AllowOpers)
40                 : allowopers(AllowOpers)
41                 , name(Name)
42                 , reason(Reason)
43                 , redirect(Redirect)
44         {
45         }
46 };
47
48 typedef std::vector<BadChannel> BadChannels;
49 typedef std::vector<std::string> GoodChannels;
50
51 class ModuleDenyChannels : public Module
52 {
53  private:
54         BadChannels badchannels;
55         GoodChannels goodchannels;
56         UserModeReference antiredirectmode;
57         ChanModeReference redirectmode;
58
59  public:
60         ModuleDenyChannels()
61                 : antiredirectmode(this, "antiredirect")
62                 , redirectmode(this, "redirect")
63         {
64         }
65
66         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
67         {
68                 GoodChannels goodchans;
69                 ConfigTagList tags = ServerInstance->Config->ConfTags("goodchan");
70                 for (ConfigIter iter = tags.first; iter != tags.second; ++iter)
71                 {
72                         ConfigTag* tag = iter->second;
73
74                         // Ensure that we have the <goodchan:name> parameter.
75                         const std::string name = tag->getString("name");
76                         if (name.empty())
77                                 throw ModuleException("<goodchan:name> is a mandatory field, at " + tag->getTagLocation());
78
79                         goodchans.push_back(name);
80                 }
81
82                 BadChannels badchans;
83                 tags = ServerInstance->Config->ConfTags("badchan");
84                 for (ConfigIter i = tags.first; i != tags.second; ++i)
85                 {
86                         ConfigTag* tag = i->second;
87
88                         // Ensure that we have the <badchan:name> parameter.
89                         const std::string name = tag->getString("name");
90                         if (name.empty())
91                                 throw ModuleException("<badchan:name> is a mandatory field, at " + tag->getTagLocation());
92
93                         // Ensure that we have the <badchan:reason> parameter.
94                         const std::string reason = tag->getString("reason");
95                         if (reason.empty())
96                                 throw ModuleException("<badchan:reason> is a mandatory field, at " + tag->getTagLocation());
97
98                         const std::string redirect = tag->getString("redirect");
99                         if (!redirect.empty())
100                         {
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());
104
105                                 // We defer the rest of the validation of the redirect channel until we have
106                                 // finished parsing all of the badchans.
107                         }
108
109                         badchans.push_back(BadChannel(name, redirect, reason, tag->getBool("allowopers")));
110                 }
111
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)
115                 {
116                         const BadChannel& badchan = *i;
117
118                         // If there is no redirect channel we have nothing to do.
119                         if (badchan.redirect.empty())
120                                 continue;
121
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))
125                                         continue;
126
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");
131                 }
132
133                 // The config file contained no errors so we can apply the new configuration.
134                 badchannels.swap(badchans);
135                 goodchannels.swap(goodchans);
136         }
137
138         Version GetVersion() CXX11_OVERRIDE
139         {
140                 return Version("Implements config tags which allow blocking of joins to channels", VF_VENDOR);
141         }
142
143
144         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
145         {
146                 for (BadChannels::const_iterator j = badchannels.begin(); j != badchannels.end(); ++j)
147                 {
148                         const BadChannel& badchan = *j;
149
150                         // If the channel does not match the current entry we have nothing else to do.
151                         if (!InspIRCd::Match(cname, badchan.name))
152                                 continue;
153
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;
158
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;
163
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)))
169                         {
170                                 user->WriteNumeric(ERR_BADCHANNEL, cname, InspIRCd::Format("Channel %s is forbidden: %s",
171                                         cname.c_str(), badchan.reason.c_str()));
172                                 return MOD_RES_DENY;
173                         }
174
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);
179                         return MOD_RES_DENY;
180                 }
181                 return MOD_RES_PASSTHRU;
182         }
183 };
184
185 MODULE_INIT(ModuleDenyChannels)