]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_denychans.cpp
63f6f51b9da2ac893c72a9374afc3cc6073c06b4
[user/henk/code/inspircd.git] / src / modules / m_denychans.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2005 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 /* $ModDesc: Implements config tags which allow blocking of joins to channels */
26
27 class ModuleDenyChannels : public Module
28 {
29  public:
30         void init() CXX11_OVERRIDE
31         {
32                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnRehash };
33                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
34         }
35
36         void OnRehash(User* user) CXX11_OVERRIDE
37         {
38                 /* check for redirect validity and loops/chains */
39                 ConfigTagList tags = ServerInstance->Config->ConfTags("badchan");
40                 for (ConfigIter i = tags.first; i != tags.second; ++i)
41                 {
42                         std::string name = i->second->getString("name");
43                         std::string redirect = i->second->getString("redirect");
44
45                         if (!redirect.empty())
46                         {
47
48                                 if (!ServerInstance->IsChannel(redirect, ServerInstance->Config->Limits.ChanMax))
49                                 {
50                                         if (user)
51                                                 user->WriteNotice("Invalid badchan redirect '" + redirect + "'");
52                                         throw ModuleException("Invalid badchan redirect, not a channel");
53                                 }
54
55                                 for (ConfigIter j = tags.first; j != tags.second; ++j)
56                                 {
57                                         if (InspIRCd::Match(redirect, j->second->getString("name")))
58                                         {
59                                                 bool goodchan = false;
60                                                 ConfigTagList goodchans = ServerInstance->Config->ConfTags("badchan");
61                                                 for (ConfigIter k = goodchans.first; k != goodchans.second; ++k)
62                                                 {
63                                                         if (InspIRCd::Match(redirect, k->second->getString("name")))
64                                                                 goodchan = true;
65                                                 }
66
67                                                 if (!goodchan)
68                                                 {
69                                                         /* <badchan:redirect> is a badchan */
70                                                         if (user)
71                                                                 user->WriteNotice("Badchan " + name + " redirects to badchan " + redirect);
72                                                         throw ModuleException("Badchan redirect loop");
73                                                 }
74                                         }
75                                 }
76                         }
77                 }
78         }
79
80         Version GetVersion() CXX11_OVERRIDE
81         {
82                 return Version("Implements config tags which allow blocking of joins to channels", VF_VENDOR);
83         }
84
85
86         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
87         {
88                 ConfigTagList tags = ServerInstance->Config->ConfTags("badchan");
89                 for (ConfigIter j = tags.first; j != tags.second; ++j)
90                 {
91                         if (InspIRCd::Match(cname, j->second->getString("name")))
92                         {
93                                 if (user->IsOper() && j->second->getBool("allowopers"))
94                                 {
95                                         return MOD_RES_PASSTHRU;
96                                 }
97                                 else
98                                 {
99                                         std::string reason = j->second->getString("reason");
100                                         std::string redirect = j->second->getString("redirect");
101
102                                         ConfigTagList goodchans = ServerInstance->Config->ConfTags("goodchan");
103                                         for (ConfigIter i = goodchans.first; i != goodchans.second; ++i)
104                                         {
105                                                 if (InspIRCd::Match(cname, i->second->getString("name")))
106                                                 {
107                                                         return MOD_RES_PASSTHRU;
108                                                 }
109                                         }
110
111                                         if (ServerInstance->IsChannel(redirect.c_str(), ServerInstance->Config->Limits.ChanMax))
112                                         {
113                                                 /* simple way to avoid potential loops: don't redirect to +L channels */
114                                                 Channel *newchan = ServerInstance->FindChan(redirect);
115                                                 if ((!newchan) || (!(newchan->IsModeSet('L'))))
116                                                 {
117                                                         user->WriteNumeric(926, "%s %s :Channel %s is forbidden, redirecting to %s: %s",user->nick.c_str(),cname.c_str(),cname.c_str(),redirect.c_str(), reason.c_str());
118                                                         Channel::JoinUser(user, redirect);
119                                                         return MOD_RES_DENY;
120                                                 }
121                                         }
122
123                                         user->WriteNumeric(926, "%s %s :Channel %s is forbidden: %s",user->nick.c_str(),cname.c_str(),cname.c_str(),reason.c_str());
124                                         return MOD_RES_DENY;
125                                 }
126                         }
127                 }
128                 return MOD_RES_PASSTHRU;
129         }
130 };
131
132 MODULE_INIT(ModuleDenyChannels)