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 /* Originally written by Om, January 2007
25 class BanRedirectEntry
28 std::string targetchan;
31 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
32 : targetchan(target), banmask(mask)
37 typedef std::vector<BanRedirectEntry> BanRedirectList;
38 typedef std::deque<std::string> StringDeque;
40 class BanRedirect : public ModeWatcher
45 BanRedirect(InspIRCd* Instance)
46 : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL), Srv(Instance)
50 bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶m, bool adding, ModeType type)
52 /* nick!ident@host -> nick!ident@host
53 * nick!ident@host#chan -> nick!ident@host#chan
54 * nick@host#chan -> nick!*@host#chan
55 * nick!ident#chan -> nick!ident@*#chan
56 * nick#chan -> nick!*@*#chan
59 if(channel && (type == MODETYPE_CHANNEL) && param.length())
61 BanRedirectList* redirects;
64 enum { NICK, IDENT, HOST, CHAN } current = NICK;
65 std::string::iterator start_pos = param.begin();
66 long maxbans = channel->GetMaxBans();
68 if(channel->bans.size() > static_cast<unsigned>(maxbans))
70 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);
74 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
79 mask[current].assign(start_pos, curr);
84 mask[current].assign(start_pos, curr);
89 mask[current].assign(start_pos, curr);
96 if(mask[current].empty())
98 mask[current].assign(start_pos, param.end());
101 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
102 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
104 /* std::string::swap() is fast - it runs in constant time */
105 mask[NICK].swap(mask[IDENT]);
108 for(int i = 0; i < 3; i++)
116 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
118 if(mask[CHAN].length())
120 if(Srv->IsChannel(mask[CHAN].c_str()))
122 if(irc::string(channel->name) == irc::string(mask[CHAN].c_str()))
124 source->WriteServ("690 %s %s :You cannot set a ban redirection to the channel the ban is on", source->nick, channel->name);
131 /* It's a properly valid redirecting ban, and we're adding it */
132 if(!channel->GetExt("banredirects", redirects))
134 redirects = new BanRedirectList;
135 channel->Extend("banredirects", redirects);
138 /* Here 'param' doesn't have the channel on it yet */
139 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
142 param.append(mask[CHAN]);
146 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
147 if(channel->GetExt("banredirects", redirects))
149 /* But there were, so we need to remove the matching one if there is one */
151 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
154 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
156 redirects->erase(redir);
158 if(redirects->empty())
161 channel->Shrink("banredirects");
169 /* Append the channel so the default +b handler can remove the entry too */
170 param.append(mask[CHAN]);
176 source->WriteServ("403 %s %s :Invalid channel name in redirection (%s)", source->nick, channel->name, mask[CHAN].c_str());
186 class ModuleBanRedirect : public Module
193 ModuleBanRedirect(InspIRCd* Me)
194 : Module::Module(Me), Srv(Me)
196 re = new BanRedirect(Me);
199 if(!Srv->AddModeWatcher(re))
200 throw ModuleException("Could not add mode watcher");
203 void Implements(char* List)
205 List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
208 virtual void OnChannelDelete(chanrec* chan)
210 OnCleanup(TYPE_CHANNEL, chan);
213 virtual void OnCleanup(int target_type, void* item)
215 if(target_type == TYPE_CHANNEL)
217 chanrec* chan = static_cast<chanrec*>(item);
218 BanRedirectList* redirects;
220 if(chan->GetExt("banredirects", redirects))
222 irc::modestacker modestack(false);
223 StringDeque stackresult;
224 const char* mode_junk[MAXMODES+1];
225 userrec* myhorriblefakeuser = new userrec(Srv);
226 myhorriblefakeuser->SetFd(FD_MAGIC_NUMBER);
228 mode_junk[0] = chan->name;
230 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
232 modestack.Push('b', i->targetchan.insert(0, i->banmask));
235 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
237 modestack.PushPlus();
238 modestack.Push('b', i->banmask);
241 while(modestack.GetStackedLine(stackresult))
243 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
245 mode_junk[i+1] = stackresult[i].c_str();
248 Srv->SendMode(mode_junk, stackresult.size() + 1, myhorriblefakeuser);
251 DELETE(myhorriblefakeuser);
253 chan->Shrink("banredirects");
258 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
260 /* This prevents recursion when a user sets multiple ban redirects in a chain
266 /* Return 1 to prevent the join, 0 to allow it */
269 BanRedirectList* redirects;
271 if(chan->GetExt("banredirects", redirects))
273 /* We actually had some ban redirects to check */
275 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
276 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
278 std::string ipmask(user->nick);
279 ipmask.append(1, '!').append(user->MakeHostIP());
281 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
283 if(Srv->MatchText(user->GetFullRealHost(), redir->banmask) || Srv->MatchText(user->GetFullHost(), redir->banmask) || Srv->MatchText(ipmask, redir->banmask))
285 /* tell them they're banned and are being transferred */
286 chanrec* destchan = Srv->FindChan(redir->targetchan);
288 if(destchan && Srv->FindModule("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
290 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
295 user->WriteServ("470 %s :You are banned from %s. You are being automatically redirected to %s", user->nick, chan->name, redir->targetchan.c_str());
297 chanrec::JoinUser(Srv, user, redir->targetchan.c_str(), false, "");
308 virtual ~ModuleBanRedirect()
310 Srv->Modes->DelModeWatcher(re);
314 virtual Version GetVersion()
316 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
321 class ModuleBanRedirectFactory : public ModuleFactory
324 ModuleBanRedirectFactory()
328 ~ModuleBanRedirectFactory()
332 virtual Module * CreateModule(InspIRCd* Me)
334 return new ModuleBanRedirect(Me);
340 extern "C" void * init_module( void )
342 return new ModuleBanRedirectFactory;