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