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