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