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 // Srv->Log(DEBUG, "BeforeMode(%s, %s, %s, %s, %s, %s)", ((source && source->nick) ? source->nick : "NULL"), ((dest && dest->nick) ? dest->nick : "NULL"), ((channel && channel->name) ? channel->name : "NULL"), param.c_str(), (adding ? "true" : "false"), ((type == MODETYPE_CHANNEL) ? "MODETYPE_CHANNEL" : "MODETYPE_USER"));
51 /* nick!ident@host -> nick!ident@host
52 * nick!ident@host#chan -> nick!ident@host#chan
53 * nick@host#chan -> nick!*@host#chan
54 * nick!ident#chan -> nick!ident@*#chan
55 * nick#chan -> nick!*@*#chan
58 if(channel && (type == MODETYPE_CHANNEL) && param.length())
60 BanRedirectList* redirects;
63 enum { NICK, IDENT, HOST, CHAN } current = NICK;
64 std::string::iterator start_pos = param.begin();
65 long maxbans = channel->GetMaxBans();
67 if(channel->bans.size() > static_cast<unsigned>(maxbans))
69 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);
73 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
78 mask[current].assign(start_pos, curr);
83 mask[current].assign(start_pos, curr);
88 mask[current].assign(start_pos, curr);
95 if(mask[current].empty())
97 mask[current].assign(start_pos, param.end());
100 for(int i = 0; i < 3; i++)
108 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
110 Srv->Log(DEBUG, "mask[NICK] = '%s', mask[IDENT] = '%s', mask[HOST] = '%s', mask[CHAN] = '%s'", mask[NICK].c_str(), mask[IDENT].c_str(), mask[HOST].c_str(), mask[CHAN].c_str());
112 if(mask[CHAN].length())
114 if(Srv->IsChannel(mask[CHAN].c_str()))
116 if(irc::string(channel->name) == irc::string(mask[CHAN].c_str()))
118 source->WriteServ("690 %s %s :You cannot set a ban redirection to the channel the ban is on", source->nick, channel->name);
125 /* It's a properly valid redirecting ban, and we're adding it */
126 if(!channel->GetExt("banredirects", redirects))
128 redirects = new BanRedirectList;
129 channel->Extend("banredirects", redirects);
132 /* Here 'param' doesn't have the channel on it yet */
133 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
136 param.append(mask[CHAN]);
140 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
141 if(channel->GetExt("banredirects", redirects))
143 /* But there were, so we need to remove the matching one if there is one */
145 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
148 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
150 redirects->erase(redir);
152 if(redirects->empty())
155 channel->Shrink("banredirects");
163 /* Append the channel so the default +b handler can remove the entry too */
164 param.append(mask[CHAN]);
170 source->WriteServ("403 %s %s :Invalid channel name in redirection (%s)", source->nick, channel->name, mask[CHAN].c_str());
175 Srv->Log(DEBUG, "Changed param to: %s", param.c_str());
182 class ModuleBanRedirect : public Module
188 ModuleBanRedirect(InspIRCd* Me)
189 : Module::Module(Me), Srv(Me)
191 re = new BanRedirect(Me);
193 if(!Srv->AddModeWatcher(re))
194 throw ModuleException("Could not add mode watcher");
197 void Implements(char* List)
199 List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
202 virtual void OnChannelDelete(chanrec* chan)
204 OnCleanup(TYPE_CHANNEL, chan);
207 virtual void OnCleanup(int target_type, void* item)
209 if(target_type == TYPE_CHANNEL)
211 chanrec* chan = static_cast<chanrec*>(item);
212 BanRedirectList* redirects;
214 if(chan->GetExt("banredirects", redirects))
216 irc::modestacker modestack(false);
217 StringDeque stackresult;
218 const char* mode_junk[MAXMODES+1];
219 userrec* myhorriblefakeuser = new userrec(Srv);
220 myhorriblefakeuser->SetFd(FD_MAGIC_NUMBER);
222 mode_junk[0] = chan->name;
224 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
226 modestack.Push('b', i->targetchan.insert(0, i->banmask));
229 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
231 modestack.PushPlus();
232 modestack.Push('b', i->banmask);
235 while(modestack.GetStackedLine(stackresult))
237 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
239 mode_junk[i+1] = stackresult[i].c_str();
242 Srv->SendMode(mode_junk, stackresult.size() + 1, myhorriblefakeuser);
245 delete myhorriblefakeuser;
247 chan->Shrink("banredirects");
252 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
254 /* Return 1 to prevent the join, 0 to allow it */
257 BanRedirectList* redirects;
259 if(chan->GetExt("banredirects", redirects))
261 /* We actually had some ban redirects to check */
263 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
264 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
266 std::string ipmask(user->nick);
267 ipmask.append(1, '!').append(user->MakeHostIP());
268 Srv->Log(DEBUG, "Matching against %s, %s and %s", user->GetFullRealHost(), user->GetFullHost(), ipmask.c_str());
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 Srv->Log(DEBUG, "%s matches ban on %s -- might transferred to %s", user->nick, chan->name, redir->targetchan.c_str());
277 chanrec* destchan = Srv->FindChan(redir->targetchan);
279 if(destchan && Srv->FindModule("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
281 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
286 user->WriteServ("470 %s :You are banned from %s. You are being automatically redirected to %s", user->nick, chan->name, redir->targetchan.c_str());
287 chanrec::JoinUser(Srv, user, redir->targetchan.c_str(), false);
297 virtual ~ModuleBanRedirect()
299 Srv->Modes->DelModeWatcher(re);
303 virtual Version GetVersion()
305 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
310 class ModuleBanRedirectFactory : public ModuleFactory
313 ModuleBanRedirectFactory()
317 ~ModuleBanRedirectFactory()
321 virtual Module * CreateModule(InspIRCd* Me)
323 return new ModuleBanRedirect(Me);
329 extern "C" void * init_module( void )
331 return new ModuleBanRedirectFactory;