]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_denychans.cpp
Modify the log message to contain the log type.
[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 class ModuleDenyChannels : public Module
26 {
27         ChanModeReference redirectmode;
28
29  public:
30         ModuleDenyChannels()
31                 : redirectmode(this, "redirect")
32         {
33         }
34
35         void init() CXX11_OVERRIDE
36         {
37                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnRehash };
38                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
39         }
40
41         void OnRehash(User* user) CXX11_OVERRIDE
42         {
43                 /* check for redirect validity and loops/chains */
44                 ConfigTagList tags = ServerInstance->Config->ConfTags("badchan");
45                 for (ConfigIter i = tags.first; i != tags.second; ++i)
46                 {
47                         std::string name = i->second->getString("name");
48                         std::string redirect = i->second->getString("redirect");
49
50                         if (!redirect.empty())
51                         {
52
53                                 if (!ServerInstance->IsChannel(redirect))
54                                 {
55                                         if (user)
56                                                 user->WriteNotice("Invalid badchan redirect '" + redirect + "'");
57                                         throw ModuleException("Invalid badchan redirect, not a channel");
58                                 }
59
60                                 for (ConfigIter j = tags.first; j != tags.second; ++j)
61                                 {
62                                         if (InspIRCd::Match(redirect, j->second->getString("name")))
63                                         {
64                                                 bool goodchan = false;
65                                                 ConfigTagList goodchans = ServerInstance->Config->ConfTags("badchan");
66                                                 for (ConfigIter k = goodchans.first; k != goodchans.second; ++k)
67                                                 {
68                                                         if (InspIRCd::Match(redirect, k->second->getString("name")))
69                                                                 goodchan = true;
70                                                 }
71
72                                                 if (!goodchan)
73                                                 {
74                                                         /* <badchan:redirect> is a badchan */
75                                                         if (user)
76                                                                 user->WriteNotice("Badchan " + name + " redirects to badchan " + redirect);
77                                                         throw ModuleException("Badchan redirect loop");
78                                                 }
79                                         }
80                                 }
81                         }
82                 }
83         }
84
85         Version GetVersion() CXX11_OVERRIDE
86         {
87                 return Version("Implements config tags which allow blocking of joins to channels", VF_VENDOR);
88         }
89
90
91         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
92         {
93                 ConfigTagList tags = ServerInstance->Config->ConfTags("badchan");
94                 for (ConfigIter j = tags.first; j != tags.second; ++j)
95                 {
96                         if (InspIRCd::Match(cname, j->second->getString("name")))
97                         {
98                                 if (user->IsOper() && j->second->getBool("allowopers"))
99                                 {
100                                         return MOD_RES_PASSTHRU;
101                                 }
102                                 else
103                                 {
104                                         std::string reason = j->second->getString("reason");
105                                         std::string redirect = j->second->getString("redirect");
106
107                                         ConfigTagList goodchans = ServerInstance->Config->ConfTags("goodchan");
108                                         for (ConfigIter i = goodchans.first; i != goodchans.second; ++i)
109                                         {
110                                                 if (InspIRCd::Match(cname, i->second->getString("name")))
111                                                 {
112                                                         return MOD_RES_PASSTHRU;
113                                                 }
114                                         }
115
116                                         if (ServerInstance->IsChannel(redirect))
117                                         {
118                                                 /* simple way to avoid potential loops: don't redirect to +L channels */
119                                                 Channel *newchan = ServerInstance->FindChan(redirect);
120                                                 if ((!newchan) || (!newchan->IsModeSet(redirectmode)))
121                                                 {
122                                                         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());
123                                                         Channel::JoinUser(user, redirect);
124                                                         return MOD_RES_DENY;
125                                                 }
126                                         }
127
128                                         user->WriteNumeric(926, "%s %s :Channel %s is forbidden: %s",user->nick.c_str(),cname.c_str(),cname.c_str(),reason.c_str());
129                                         return MOD_RES_DENY;
130                                 }
131                         }
132                 }
133                 return MOD_RES_PASSTHRU;
134         }
135 };
136
137 MODULE_INIT(ModuleDenyChannels)