]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_denychans.cpp
f47d414bd92ec5b14250fe95678a80643ca52217
[user/henk/code/inspircd.git] / src / modules / m_denychans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Implements config tags which allow blocking of joins to channels */
17
18 class ModuleDenyChannels : public Module
19 {
20  private:
21
22
23         ConfigReader *Conf;
24
25  public:
26         ModuleDenyChannels(InspIRCd* Me) : Module(Me)
27         {
28
29                 Conf = new ConfigReader(ServerInstance);
30                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnRehash };
31                 ServerInstance->Modules->Attach(eventlist, this, 2);
32         }
33
34         virtual void OnRehash(User* user)
35         {
36                 delete Conf;
37                 Conf = new ConfigReader(ServerInstance);
38                 /* check for redirect validity and loops/chains */
39                 for (int i =0; i < Conf->Enumerate("badchan"); i++)
40                 {
41                         std::string name = Conf->ReadValue("badchan","name",i);
42                         std::string redirect = Conf->ReadValue("badchan","redirect",i);
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 (int j =0; j < Conf->Enumerate("badchan"); j++)
55                                 {
56                                         if (InspIRCd::Match(redirect, Conf->ReadValue("badchan","name",j)))
57                                         {
58                                                 bool goodchan = false;
59                                                 for (int k =0; k < Conf->Enumerate("goodchan"); k++)
60                                                 {
61                                                         if (InspIRCd::Match(redirect, Conf->ReadValue("goodchan","name",k)))
62                                                                 goodchan = true;
63                                                 }
64
65                                                 if (!goodchan)
66                                                 {
67                                                         /* <badchan:redirect> is a badchan */
68                                                         if (user)
69                                                                 user->WriteServ("NOTICE %s :Badchan %s redirects to badchan %s", user->nick.c_str(), name.c_str(), redirect.c_str());
70                                                         throw ModuleException("Badchan redirect loop");
71                                                 }
72                                         }
73                                 }
74                         }
75                 }
76         }
77
78         virtual ~ModuleDenyChannels()
79         {
80                 delete Conf;
81         }
82
83         virtual Version GetVersion()
84         {
85                 return Version("Implements config tags which allow blocking of joins to channels", VF_VENDOR,API_VERSION);
86         }
87
88
89         virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
90         {
91                 for (int j =0; j < Conf->Enumerate("badchan"); j++)
92                 {
93                         if (InspIRCd::Match(cname, Conf->ReadValue("badchan","name",j)))
94                         {
95                                 if (IS_OPER(user) && Conf->ReadFlag("badchan","allowopers",j))
96                                 {
97                                         return MOD_RES_PASSTHRU;
98                                 }
99                                 else
100                                 {
101                                         std::string reason = Conf->ReadValue("badchan","reason",j);
102                                         std::string redirect = Conf->ReadValue("badchan","redirect",j);
103
104                                         for (int i = 0; i < Conf->Enumerate("goodchan"); i++)
105                                         {
106                                                 if (InspIRCd::Match(cname, Conf->ReadValue("goodchan", "name", i)))
107                                                 {
108                                                         return MOD_RES_PASSTHRU;
109                                                 }
110                                         }
111
112                                         if (ServerInstance->IsChannel(redirect.c_str(), ServerInstance->Config->Limits.ChanMax))
113                                         {
114                                                 /* simple way to avoid potential loops: don't redirect to +L channels */
115                                                 Channel *newchan = ServerInstance->FindChan(redirect);
116                                                 if ((!newchan) || (!(newchan->IsModeSet('L'))))
117                                                 {
118                                                         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());
119                                                         Channel::JoinUser(ServerInstance,user,redirect.c_str(),false,"",false,ServerInstance->Time());
120                                                         return MOD_RES_DENY;
121                                                 }
122                                         }
123
124                                         user->WriteNumeric(926, "%s %s :Channel %s is forbidden: %s",user->nick.c_str(),cname,cname,reason.c_str());
125                                         return MOD_RES_DENY;
126                                 }
127                         }
128                 }
129                 return MOD_RES_PASSTHRU;
130         }
131 };
132
133 MODULE_INIT(ModuleDenyChannels)