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 * ---------------------------------------------------
20 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
22 class BanRedirectEntry
25 std::string targetchan;
28 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
29 : targetchan(target), banmask(mask)
34 typedef std::vector<BanRedirectEntry> BanRedirectList;
35 typedef std::deque<std::string> StringDeque;
37 class BanRedirect : public ModeWatcher
42 BanRedirect(InspIRCd* Instance)
43 : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL), Srv(Instance)
47 bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶m, bool adding, ModeType type)
49 /* nick!ident@host -> nick!ident@host
50 * nick!ident@host#chan -> nick!ident@host#chan
51 * nick@host#chan -> nick!*@host#chan
52 * nick!ident#chan -> nick!ident@*#chan
53 * nick#chan -> nick!*@*#chan
56 if(channel && (type == MODETYPE_CHANNEL) && param.length())
58 BanRedirectList* redirects;
61 enum { NICK, IDENT, HOST, CHAN } current = NICK;
62 std::string::iterator start_pos = param.begin();
63 long maxbans = channel->GetMaxBans();
65 if(channel->bans.size() > static_cast<unsigned>(maxbans))
67 source->WriteServ("478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)", source->nick, channel->name, channel->name, maxbans);
71 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
76 mask[current].assign(start_pos, curr);
81 mask[current].assign(start_pos, curr);
86 mask[current].assign(start_pos, curr);
93 if(mask[current].empty())
95 mask[current].assign(start_pos, param.end());
98 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
99 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
101 /* std::string::swap() is fast - it runs in constant time */
102 mask[NICK].swap(mask[IDENT]);
105 for(int i = 0; i < 3; i++)
113 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
115 if(mask[CHAN].length())
117 if(Srv->IsChannel(mask[CHAN].c_str()))
119 if(irc::string(channel->name) == irc::string(mask[CHAN].c_str()))
121 source->WriteServ("690 %s %s :You cannot set a ban redirection to the channel the ban is on", source->nick, channel->name);
128 /* It's a properly valid redirecting ban, and we're adding it */
129 if(!channel->GetExt("banredirects", redirects))
131 redirects = new BanRedirectList;
132 channel->Extend("banredirects", redirects);
135 /* Here 'param' doesn't have the channel on it yet */
136 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
139 param.append(mask[CHAN]);
143 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
144 if(channel->GetExt("banredirects", redirects))
146 /* But there were, so we need to remove the matching one if there is one */
148 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
151 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
153 redirects->erase(redir);
155 if(redirects->empty())
158 channel->Shrink("banredirects");
166 /* Append the channel so the default +b handler can remove the entry too */
167 param.append(mask[CHAN]);
173 source->WriteServ("403 %s %s :Invalid channel name in redirection (%s)", source->nick, channel->name, mask[CHAN].c_str());
183 class ModuleBanRedirect : public Module
189 ModuleBanRedirect(InspIRCd* Me)
190 : Module::Module(Me), Srv(Me)
192 re = new BanRedirect(Me);
194 if(!Srv->AddModeWatcher(re))
195 throw ModuleException("Could not add mode watcher");
198 void Implements(char* List)
200 List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
203 virtual void OnChannelDelete(chanrec* chan)
205 OnCleanup(TYPE_CHANNEL, chan);
208 virtual void OnCleanup(int target_type, void* item)
210 if(target_type == TYPE_CHANNEL)
212 chanrec* chan = static_cast<chanrec*>(item);
213 BanRedirectList* redirects;
215 if(chan->GetExt("banredirects", redirects))
217 irc::modestacker modestack(false);
218 StringDeque stackresult;
219 const char* mode_junk[MAXMODES+1];
220 userrec* myhorriblefakeuser = new userrec(Srv);
221 myhorriblefakeuser->SetFd(FD_MAGIC_NUMBER);
223 mode_junk[0] = chan->name;
225 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
227 modestack.Push('b', i->targetchan.insert(0, i->banmask));
230 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
232 modestack.PushPlus();
233 modestack.Push('b', i->banmask);
236 while(modestack.GetStackedLine(stackresult))
238 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
240 mode_junk[i+1] = stackresult[i].c_str();
243 Srv->SendMode(mode_junk, stackresult.size() + 1, myhorriblefakeuser);
246 DELETE(myhorriblefakeuser);
248 chan->Shrink("banredirects");
253 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
255 /* Return 1 to prevent the join, 0 to allow it */
258 BanRedirectList* redirects;
260 if(chan->GetExt("banredirects", redirects))
262 /* We actually had some ban redirects to check */
264 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
265 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
267 std::string ipmask(user->nick);
268 ipmask.append(1, '!').append(user->MakeHostIP());
270 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
272 if(Srv->MatchText(user->GetFullRealHost(), redir->banmask) || Srv->MatchText(user->GetFullHost(), redir->banmask) || Srv->MatchText(ipmask, redir->banmask))
274 /* tell them they're banned and are being transferred */
275 chanrec* destchan = Srv->FindChan(redir->targetchan);
277 if(destchan && Srv->FindModule("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
279 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
284 user->WriteServ("470 %s :You are banned from %s. You are being automatically redirected to %s", user->nick, chan->name, redir->targetchan.c_str());
285 chanrec::JoinUser(Srv, user, redir->targetchan.c_str(), false);
295 virtual ~ModuleBanRedirect()
297 Srv->Modes->DelModeWatcher(re);
301 virtual Version GetVersion()
303 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
308 class ModuleBanRedirectFactory : public ModuleFactory
311 ModuleBanRedirectFactory()
315 ~ModuleBanRedirectFactory()
319 virtual Module * CreateModule(InspIRCd* Me)
321 return new ModuleBanRedirect(Me);
327 extern "C" void * init_module( void )
329 return new ModuleBanRedirectFactory;