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