]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_redirect.cpp
Fix to m_redirect to prevent circular link to self
[user/henk/code/inspircd.git] / src / modules / m_redirect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include "helperfuncs.h"
24
25 /* $ModDesc: Provides channel mode +L (limit redirection) */
26
27
28 class ModuleRedirect : public Module
29 {
30         Server *Srv;
31         
32  public:
33  
34         ModuleRedirect(Server* Me)
35                 : Module::Module(Me)
36         {
37                 Srv = Me;
38                 Srv->AddExtendedMode('L',MT_CHANNEL,false,1,0);
39         }
40         
41         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
42         {
43                 if ((modechar == 'L') && (type == MT_CHANNEL))
44                 {
45                         if (mode_on)
46                         {
47                                 std::string ChanToJoin = params[0];
48                                 chanrec* c = Srv->FindChannel(ChanToJoin);
49                                 if (c)
50                                 {
51                                         /* Fix by brain: Dont let a channel be linked to *itself* either */
52                                         if ((c == target) || (c->IsCustomModeSet('L')))
53                                         {
54                                                 WriteServ(user->fd,"690 %s :Circular redirection, mode +L to %s not allowed.",user->nick,params[0].c_str());
55                                                 return 0;
56                                         }
57                                 }
58                         }
59                         return 1;
60                 }
61                 return 0;
62         }
63
64         void Implements(char* List)
65         {
66                 List[I_On005Numeric] = List[I_OnUserPreJoin] = List[I_OnExtendedMode] = 1;
67         }
68
69         virtual void On005Numeric(std::string &output)
70         {
71                 std::stringstream line(output);
72                 std::string temp1, temp2;
73                 while (!line.eof())
74                 {
75                         line >> temp1;
76                         if (temp1.substr(0,10) == "CHANMODES=")
77                         {
78                                 // By doing this we're *assuming* no other module has fucked up the CHANMODES=
79                                 // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory,
80                                 // with a honking great EXCEPTION :)
81                                 temp1.insert(temp1.find(",")+1,"L");
82                         }
83                         temp2 = temp2 + temp1 + " ";
84                 }
85                 if (temp2.length())
86                         output = temp2.substr(0,temp2.length()-1);
87         }
88         
89         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
90         {
91                 if (chan)
92                 {
93                         if (chan->IsCustomModeSet('L'))
94                         {
95                                 if (Srv->CountUsers(chan) >= chan->limit)
96                                 {
97                                         std::string channel = chan->GetModeParameter('L');
98                                         WriteServ(user->fd,"470 %s :%s has become full, so you are automatically being transferred to the linked channel %s",user->nick,cname,channel.c_str());
99                                         Srv->JoinUserToChannel(user,channel.c_str(),"");
100                                         return 1;
101                                 }
102                         }
103                 }
104                 return 0;
105         }
106
107         virtual ~ModuleRedirect()
108         {
109         }
110         
111         virtual Version GetVersion()
112         {
113                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
114         }
115 };
116
117
118 class ModuleRedirectFactory : public ModuleFactory
119 {
120  public:
121         ModuleRedirectFactory()
122         {
123         }
124         
125         ~ModuleRedirectFactory()
126         {
127         }
128         
129         virtual Module * CreateModule(Server* Me)
130         {
131                 return new ModuleRedirect(Me);
132         }
133         
134 };
135
136
137 extern "C" void * init_module( void )
138 {
139         return new ModuleRedirectFactory;
140 }
141