]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_redirect.cpp
Move to new api
[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 class Redirect : public ModeHandler
28 {
29         Server* Srv;
30  public:
31         Redirect(Server* s) : ModeHandler('L', 1, 0, false, MODETYPE_CHANNEL, false), Srv(s) { }
32
33         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
34         {
35                 if (adding)
36                 {
37                         chanrec* c = NULL;
38
39                         if (!IsValidChannelName(parameter.c_str()))
40                         {
41                                 WriteServ(source->fd,"403 %s %s :Invalid channel name",source->nick, parameter.c_str());
42                                 parameter = "";
43                                 return MODEACTION_DENY;
44                         }
45
46                         c = Srv->FindChannel(parameter);
47                         if (c)
48                         {
49                                 /* Fix by brain: Dont let a channel be linked to *itself* either */
50                                 if ((c == channel) || (c->IsModeSet('L')))
51                                 {
52                                         WriteServ(source->fd,"690 %s :Circular redirection, mode +L to %s not allowed.",source->nick,parameter.c_str());
53                                         parameter = "";
54                                         return MODEACTION_DENY;
55                                 }
56                         }
57
58                         channel->SetMode('L', true);
59                         channel->SetModeParam('L', parameter.c_str(), true);
60                         return MODEACTION_ALLOW;
61                 }
62                 else
63                 {
64                         if (channel->IsModeSet('L'))
65                         {
66                                 channel->SetMode('L', false);
67                                 return MODEACTION_ALLOW;
68                         }
69                 }
70
71                 return MODEACTION_DENY;
72                 
73         }
74 };
75
76 class ModuleRedirect : public Module
77 {
78         Server *Srv;
79         Redirect* re;
80         
81  public:
82  
83         ModuleRedirect(Server* Me)
84                 : Module::Module(Me)
85         {
86                 Srv = Me;
87                 re = new Redirect(Me);
88                 Srv->AddMode(re, 'L');
89         }
90         
91         void Implements(char* List)
92         {
93                 List[I_On005Numeric] = List[I_OnUserPreJoin] = 1;
94         }
95
96         virtual void On005Numeric(std::string &output)
97         {
98                 InsertMode(output, "L", 3);
99         }
100         
101         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
102         {
103                 if (chan)
104                 {
105                         if (chan->IsModeSet('L'))
106                         {
107                                 if (Srv->CountUsers(chan) >= chan->limit)
108                                 {
109                                         std::string channel = chan->GetModeParameter('L');
110                                         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());
111                                         Srv->JoinUserToChannel(user,channel.c_str(),"");
112                                         return 1;
113                                 }
114                         }
115                 }
116                 return 0;
117         }
118
119         virtual ~ModuleRedirect()
120         {
121                 DELETE(re);
122         }
123         
124         virtual Version GetVersion()
125         {
126                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
127         }
128 };
129
130
131 class ModuleRedirectFactory : public ModuleFactory
132 {
133  public:
134         ModuleRedirectFactory()
135         {
136         }
137         
138         ~ModuleRedirectFactory()
139         {
140         }
141         
142         virtual Module * CreateModule(Server* Me)
143         {
144                 return new ModuleRedirect(Me);
145         }
146         
147 };
148
149
150 extern "C" void * init_module( void )
151 {
152         return new ModuleRedirectFactory;
153 }
154