]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_redirect.cpp
e09c9b33b184294abcafa94854e7c4b5b737ffb6
[user/henk/code/inspircd.git] / src / modules / m_redirect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18
19 /* $ModDesc: Provides channel mode +L (limit redirection) */
20
21 /** Handle channel mode +L
22  */
23 class Redirect : public ModeHandler
24 {
25  public:
26         Redirect(InspIRCd* Instance) : ModeHandler(Instance, 'L', 1, 0, false, MODETYPE_CHANNEL, false) { }
27
28         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
29         {
30                 if (channel->IsModeSet('L'))
31                         return std::make_pair(true, channel->GetModeParameter('L'));
32                 else
33                         return std::make_pair(false, parameter);
34         }
35
36         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
37         {
38                 /* When TS is equal, the alphabetically later one wins */
39                 return (their_param < our_param);
40         }
41         
42         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
43         {
44                 if (adding)
45                 {
46                         chanrec* c = NULL;
47
48                         if (!ServerInstance->IsChannel(parameter.c_str()))
49                         {
50                                 source->WriteServ("403 %s %s :Invalid channel name",source->nick, parameter.c_str());
51                                 parameter = "";
52                                 return MODEACTION_DENY;
53                         }
54
55                         c = ServerInstance->FindChan(parameter);
56                         if (c)
57                         {
58                                 /* Fix by brain: Dont let a channel be linked to *itself* either */
59                                 if (IS_LOCAL(source))
60                                 {
61                                         if ((c == channel) || (c->IsModeSet('L')))
62                                         {
63                                                 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());
64                                                 parameter = "";
65                                                 return MODEACTION_DENY;
66                                         }
67                                         else
68                                         {
69                                                 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
70                                                 {
71                                                         if ((i->second != channel) && (i->second->IsModeSet('L')) && (irc::string(i->second->GetModeParameter('L').c_str()) == irc::string(channel->name)))
72                                                         {
73                                                                 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);
74                                                                 return MODEACTION_DENY;
75                                                         }
76                                                 }
77                                         }
78                                 }
79                         }
80
81                         channel->SetMode('L', true);
82                         channel->SetModeParam('L', parameter.c_str(), true);
83                         return MODEACTION_ALLOW;
84                 }
85                 else
86                 {
87                         if (channel->IsModeSet('L'))
88                         {
89                                 channel->SetMode('L', false);
90                                 return MODEACTION_ALLOW;
91                         }
92                 }
93
94                 return MODEACTION_DENY;
95                 
96         }
97 };
98
99 class ModuleRedirect : public Module
100 {
101         
102         Redirect* re;
103         
104  public:
105  
106         ModuleRedirect(InspIRCd* Me)
107                 : Module::Module(Me)
108         {
109                 
110                 re = new Redirect(ServerInstance);
111                 if (!ServerInstance->AddMode(re, 'L'))
112                         throw ModuleException("Could not add new modes!");
113         }
114         
115         void Implements(char* List)
116         {
117                 List[I_OnUserPreJoin] = 1;
118         }
119
120         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
121         {
122                 if (chan)
123                 {
124                         if (chan->IsModeSet('L') && chan->limit)
125                         {
126                                 if (chan->GetUserCounter() >= chan->limit)
127                                 {
128                                         std::string channel = chan->GetModeParameter('L');
129
130                                         /* sometimes broken ulines can make circular or chained +L, avoid this */
131                                         chanrec* destchan = NULL;
132                                         destchan = ServerInstance->FindChan(channel);
133                                         if (destchan && destchan->IsModeSet('L'))
134                                         {
135                                                 user->WriteServ("470 %s :%s is full, but has a circular redirect (+L), not following redirection to %s", user->nick, cname, channel.c_str());
136                                                 return 1;
137                                         }
138
139                                         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());
140                                         chanrec::JoinUser(ServerInstance, user, channel.c_str(), false, "", ServerInstance->Time(true));
141                                         return 1;
142                                 }
143                         }
144                 }
145                 return 0;
146         }
147
148         virtual ~ModuleRedirect()
149         {
150                 ServerInstance->Modes->DelMode(re);
151                 DELETE(re);
152         }
153         
154         virtual Version GetVersion()
155         {
156                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
157         }
158 };
159
160
161 class ModuleRedirectFactory : public ModuleFactory
162 {
163  public:
164         ModuleRedirectFactory()
165         {
166         }
167         
168         ~ModuleRedirectFactory()
169         {
170         }
171         
172         virtual Module * CreateModule(InspIRCd* Me)
173         {
174                 return new ModuleRedirect(Me);
175         }
176         
177 };
178
179
180 extern "C" void * init_module( void )
181 {
182         return new ModuleRedirectFactory;
183 }
184