]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_denychans.cpp
fixed some indentation and spacing in modules
[user/henk/code/inspircd.git] / src / modules / m_denychans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 #include "wildcard.h"
16
17 /* $ModDesc: Implements config tags which allow blocking of joins to channels */
18
19 class ModuleDenyChannels : public Module
20 {
21  private:
22
23
24         ConfigReader *Conf;
25
26  public:
27         ModuleDenyChannels(InspIRCd* Me) : Module(Me)
28         {
29
30                 Conf = new ConfigReader(ServerInstance);
31                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnRehash };
32                 ServerInstance->Modules->Attach(eventlist, this, 2);
33         }
34
35         virtual void OnRehash(User* user, const std::string &param)
36         {
37                 delete Conf;
38                 Conf = new ConfigReader(ServerInstance);
39                 /* check for redirect validity and loops/chains */
40                 for (int i =0; i < Conf->Enumerate("badchan"); i++)
41                 {
42                         std::string name = Conf->ReadValue("badchan","name",i);
43                         std::string redirect = Conf->ReadValue("badchan","redirect",i);
44
45                         if (!redirect.empty())
46                         {
47
48                                 if (!ServerInstance->IsChannel(redirect.c_str(), ServerInstance->Config->Limits.ChanMax))
49                                 {
50                                         if (user)
51                                                 user->WriteServ("Notice %s :Invalid badchan redirect '%s'", user->nick.c_str(), redirect.c_str());
52                                         throw ModuleException("Invalid badchan redirect, not a channel");
53                                 }
54
55                                 for (int j =0; j < Conf->Enumerate("badchan"); j++)
56                                 {
57                                         if (match(redirect, Conf->ReadValue("badchan","name",j)))
58                                         {
59                                                 bool goodchan = false;
60                                                 for (int k =0; k < Conf->Enumerate("goodchan"); k++)
61                                                 {
62                                                         if (match(redirect, Conf->ReadValue("goodchan","name",k)))
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                 delete Conf;
82         }
83
84         virtual Version GetVersion()
85         {
86                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
87         }
88
89
90         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
91         {
92                 for (int j =0; j < Conf->Enumerate("badchan"); j++)
93                 {
94                         if (match(cname, Conf->ReadValue("badchan","name",j)))
95                         {
96                                 if (IS_OPER(user) && Conf->ReadFlag("badchan","allowopers",j))
97                                 {
98                                         return 0;
99                                 }
100                                 else
101                                 {
102                                         std::string reason = Conf->ReadValue("badchan","reason",j);
103                                         std::string redirect = Conf->ReadValue("badchan","redirect",j);
104
105                                         for (int i = 0; i < Conf->Enumerate("goodchan"); i++)
106                                         {
107                                                 if (match(cname, Conf->ReadValue("goodchan", "name", i)))
108                                                 {
109                                                         return 0;
110                                                 }
111                                         }
112
113                                         if (ServerInstance->IsChannel(redirect.c_str(), ServerInstance->Config->Limits.ChanMax))
114                                         {
115                                                 /* simple way to avoid potential loops: don't redirect to +L channels */
116                                                 Channel *newchan = ServerInstance->FindChan(redirect);
117                                                 if ((!newchan) || (!(newchan->IsModeSet('L'))))
118                                                 {
119                                                         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());
120                                                         Channel::JoinUser(ServerInstance,user,redirect.c_str(),false,"",false,ServerInstance->Time());
121                                                         return 1;
122                                                 }
123                                         }
124
125                                         user->WriteNumeric(926, "%s %s :Channel %s is forbidden: %s",user->nick.c_str(),cname,cname,reason.c_str());
126                                         return 1;
127                                 }
128                         }
129                 }
130                 return 0;
131         }
132 };
133
134 MODULE_INIT(ModuleDenyChannels)