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