1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Provides channel mode +L (limit redirection) */
21 /** Handle channel mode +L
23 class Redirect : public ModeHandler
26 Redirect(InspIRCd* Instance) : ModeHandler(Instance, 'L', 1, 0, false, MODETYPE_CHANNEL, false) { }
28 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
30 if (channel->IsModeSet('L'))
31 return std::make_pair(true, channel->GetModeParameter('L'));
33 return std::make_pair(false, parameter);
36 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
38 /* When TS is equal, the alphabetically later one wins */
39 return (their_param < our_param);
42 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
48 if (!ServerInstance->IsChannel(parameter.c_str()))
50 source->WriteServ("403 %s %s :Invalid channel name",source->nick, parameter.c_str());
52 return MODEACTION_DENY;
55 c = ServerInstance->FindChan(parameter);
58 /* Fix by brain: Dont let a channel be linked to *itself* either */
61 if ((c == channel) || (c->IsModeSet('L')))
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());
65 return MODEACTION_DENY;
69 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
71 if ((i->second != channel) && (i->second->IsModeSet('L')) && (irc::string(i->second->GetModeParameter('L').c_str()) == irc::string(channel->name)))
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;
81 channel->SetMode('L', true);
82 channel->SetModeParam('L', parameter.c_str(), true);
83 return MODEACTION_ALLOW;
87 if (channel->IsModeSet('L'))
89 channel->SetMode('L', false);
90 return MODEACTION_ALLOW;
94 return MODEACTION_DENY;
99 class ModuleRedirect : public Module
106 ModuleRedirect(InspIRCd* Me)
110 re = new Redirect(ServerInstance);
111 if (!ServerInstance->AddMode(re, 'L'))
112 throw ModuleException("Could not add new modes!");
115 void Implements(char* List)
117 List[I_OnUserPreJoin] = 1;
120 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
124 if (chan->IsModeSet('L') && chan->limit)
126 if (chan->GetUserCounter() >= chan->limit)
128 std::string channel = chan->GetModeParameter('L');
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'))
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());
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);
148 virtual ~ModuleRedirect()
150 ServerInstance->Modes->DelMode(re);
154 virtual Version GetVersion()
156 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
161 class ModuleRedirectFactory : public ModuleFactory
164 ModuleRedirectFactory()
168 ~ModuleRedirectFactory()
172 virtual Module * CreateModule(InspIRCd* Me)
174 return new ModuleRedirect(Me);
180 extern "C" void * init_module( void )
182 return new ModuleRedirectFactory;