]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_redirect.cpp
Remove unnecessary header traffic
[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 "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(userrec* source, userrec* dest, chanrec* 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, chanrec* channel)
34         {
35                 /* When TS is equal, the alphabetically later one wins */
36                 return (their_param < our_param);
37         }
38         
39         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
40         {
41                 if (adding)
42                 {
43                         chanrec* 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, 'L'))
109                         throw ModuleException("Could not add new modes!");
110         }
111         
112         void Implements(char* List)
113         {
114                 List[I_OnUserPreJoin] = 1;
115         }
116
117         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
118         {
119                 if (chan)
120                 {
121                         if (chan->IsModeSet('L') && chan->limit)
122                         {
123                                 if (chan->GetUserCounter() >= chan->limit)
124                                 {
125                                         std::string channel = chan->GetModeParameter('L');
126
127                                         /* sometimes broken ulines can make circular or chained +L, avoid this */
128                                         chanrec* destchan = NULL;
129                                         destchan = ServerInstance->FindChan(channel);
130                                         if (destchan && destchan->IsModeSet('L'))
131                                         {
132                                                 user->WriteServ("470 %s :%s is full, but has a circular redirect (+L), not following redirection to %s", user->nick, cname, channel.c_str());
133                                                 return 1;
134                                         }
135
136                                         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());
137                                         chanrec::JoinUser(ServerInstance, user, channel.c_str(), false, "", ServerInstance->Time(true));
138                                         return 1;
139                                 }
140                         }
141                 }
142                 return 0;
143         }
144
145         virtual ~ModuleRedirect()
146         {
147                 ServerInstance->Modes->DelMode(re);
148                 DELETE(re);
149         }
150         
151         virtual Version GetVersion()
152         {
153                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
154         }
155 };
156
157 MODULE_INIT(ModuleRedirect)