]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_redirect.cpp
Update $ModDep lines so that these properly depend on their headers in the makefile
[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 "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "inspircd.h"
23
24 /* $ModDesc: Provides channel mode +L (limit redirection) */
25
26 /** Handle channel mode +L
27  */
28 class Redirect : public ModeHandler
29 {
30  public:
31         Redirect(InspIRCd* Instance) : ModeHandler(Instance, 'L', 1, 0, false, MODETYPE_CHANNEL, false) { }
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 (!ServerInstance->IsChannel(parameter.c_str()))
54                         {
55                                 source->WriteServ("403 %s %s :Invalid channel name",source->nick, parameter.c_str());
56                                 parameter = "";
57                                 return MODEACTION_DENY;
58                         }
59
60                         c = ServerInstance->FindChan(parameter);
61                         if (c)
62                         {
63                                 /* Fix by brain: Dont let a channel be linked to *itself* either */
64                                 if (IS_LOCAL(source))
65                                 {
66                                         if ((c == channel) || (c->IsModeSet('L')))
67                                         {
68                                                 source->WriteServ("690 %s :Circular or chained +L to %s not allowed (Channel already has +L). Pack of wild dogs has been unleashed.",source->nick,parameter.c_str());
69                                                 parameter = "";
70                                                 return MODEACTION_DENY;
71                                         }
72                                         else
73                                         {
74                                                 for (chan_hash::const_iterator i = ServerInstance->chanlist.begin(); i != ServerInstance->chanlist.end(); i++)
75                                                 {
76                                                         if ((i->second != channel) && (i->second->IsModeSet('L')) && (irc::string(i->second->GetModeParameter('L').c_str()) == irc::string(channel->name)))
77                                                         {
78                                                                 source->WriteServ("690 %s :Circular or chained +L to %s not allowed (Already forwarded here from %s). Angry monkeys dispatched.",source->nick,parameter.c_str(),i->second->name);
79                                                                 return MODEACTION_DENY;
80                                                         }
81                                                 }
82                                         }
83                                 }
84                         }
85
86                         channel->SetMode('L', true);
87                         channel->SetModeParam('L', parameter.c_str(), true);
88                         return MODEACTION_ALLOW;
89                 }
90                 else
91                 {
92                         if (channel->IsModeSet('L'))
93                         {
94                                 channel->SetMode('L', false);
95                                 return MODEACTION_ALLOW;
96                         }
97                 }
98
99                 return MODEACTION_DENY;
100                 
101         }
102 };
103
104 class ModuleRedirect : public Module
105 {
106         
107         Redirect* re;
108         
109  public:
110  
111         ModuleRedirect(InspIRCd* Me)
112                 : Module::Module(Me)
113         {
114                 
115                 re = new Redirect(ServerInstance);
116                 ServerInstance->AddMode(re, 'L');
117         }
118         
119         void Implements(char* List)
120         {
121                 List[I_OnUserPreJoin] = 1;
122         }
123
124         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
125         {
126                 if (chan)
127                 {
128                         if (chan->IsModeSet('L') && chan->limit)
129                         {
130                                 if (chan->GetUserCounter() >= chan->limit)
131                                 {
132                                         std::string channel = chan->GetModeParameter('L');
133                                         user->WriteServ("470 %s :%s has become full, so you are automatically being transferred to the linked channel %s",user->nick,cname,channel.c_str());
134                                         chanrec::JoinUser(ServerInstance, user, channel.c_str(), false);
135                                         return 1;
136                                 }
137                         }
138                 }
139                 return 0;
140         }
141
142         virtual ~ModuleRedirect()
143         {
144                 ServerInstance->Modes->DelMode(re);
145                 DELETE(re);
146         }
147         
148         virtual Version GetVersion()
149         {
150                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
151         }
152 };
153
154
155 class ModuleRedirectFactory : public ModuleFactory
156 {
157  public:
158         ModuleRedirectFactory()
159         {
160         }
161         
162         ~ModuleRedirectFactory()
163         {
164         }
165         
166         virtual Module * CreateModule(InspIRCd* Me)
167         {
168                 return new ModuleRedirect(Me);
169         }
170         
171 };
172
173
174 extern "C" void * init_module( void )
175 {
176         return new ModuleRedirectFactory;
177 }
178