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 #include "u_listmode.h"
21 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
23 /* Originally written by Om, January 2007
26 class BanRedirectEntry
29 std::string targetchan;
32 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
33 : targetchan(target), banmask(mask)
38 typedef std::vector<BanRedirectEntry> BanRedirectList;
39 typedef std::deque<std::string> StringDeque;
41 class BanRedirect : public ModeWatcher
46 BanRedirect(InspIRCd* Instance)
47 : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL), Srv(Instance)
51 bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string ¶m, bool adding, ModeType type)
53 /* nick!ident@host -> nick!ident@host
54 * nick!ident@host#chan -> nick!ident@host#chan
55 * nick@host#chan -> nick!*@host#chan
56 * nick!ident#chan -> nick!ident@*#chan
57 * nick#chan -> nick!*@*#chan
60 if(channel && (type == MODETYPE_CHANNEL) && param.length())
62 BanRedirectList* redirects;
65 enum { NICK, IDENT, HOST, CHAN } current = NICK;
66 std::string::iterator start_pos = param.begin();
67 long maxbans = channel->GetMaxBans();
69 if(channel->bans.size() > static_cast<unsigned>(maxbans))
71 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);
75 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
80 mask[current].assign(start_pos, curr);
85 mask[current].assign(start_pos, curr);
90 mask[current].assign(start_pos, curr);
97 if(mask[current].empty())
99 mask[current].assign(start_pos, param.end());
102 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
103 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
105 /* std::string::swap() is fast - it runs in constant time */
106 mask[NICK].swap(mask[IDENT]);
109 for(int i = 0; i < 3; i++)
117 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
119 if(mask[CHAN].length())
121 if(Srv->IsChannel(mask[CHAN].c_str()))
123 if(irc::string(channel->name) == irc::string(mask[CHAN].c_str()))
125 source->WriteServ("690 %s %s :You cannot set a ban redirection to the channel the ban is on", source->nick, channel->name);
132 /* It's a properly valid redirecting ban, and we're adding it */
133 if(!channel->GetExt("banredirects", redirects))
135 redirects = new BanRedirectList;
136 channel->Extend("banredirects", redirects);
139 /* Here 'param' doesn't have the channel on it yet */
140 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
143 param.append(mask[CHAN]);
147 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
148 if(channel->GetExt("banredirects", redirects))
150 /* But there were, so we need to remove the matching one if there is one */
152 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
155 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
157 redirects->erase(redir);
159 if(redirects->empty())
162 channel->Shrink("banredirects");
170 /* Append the channel so the default +b handler can remove the entry too */
171 param.append(mask[CHAN]);
177 source->WriteServ("403 %s %s :Invalid channel name in redirection (%s)", source->nick, channel->name, mask[CHAN].c_str());
187 class ModuleBanRedirect : public Module
191 Module* ExceptionModule;
194 ModuleBanRedirect(InspIRCd* Me)
197 re = new BanRedirect(Me);
200 if(!ServerInstance->AddModeWatcher(re))
201 throw ModuleException("Could not add mode watcher");
206 void Implements(char* List)
208 List[I_OnRehash] = List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
211 virtual void OnChannelDelete(chanrec* chan)
213 OnCleanup(TYPE_CHANNEL, chan);
216 virtual void OnCleanup(int target_type, void* item)
218 if(target_type == TYPE_CHANNEL)
220 chanrec* chan = static_cast<chanrec*>(item);
221 BanRedirectList* redirects;
223 if(chan->GetExt("banredirects", redirects))
225 irc::modestacker modestack(false);
226 StringDeque stackresult;
227 const char* mode_junk[MAXMODES+1];
228 userrec* myhorriblefakeuser = new userrec(ServerInstance);
229 myhorriblefakeuser->SetFd(FD_MAGIC_NUMBER);
231 mode_junk[0] = chan->name;
233 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
235 modestack.Push('b', i->targetchan.insert(0, i->banmask));
238 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
240 modestack.PushPlus();
241 modestack.Push('b', i->banmask);
244 while(modestack.GetStackedLine(stackresult))
246 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
248 mode_junk[i+1] = stackresult[i].c_str();
251 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, myhorriblefakeuser);
254 DELETE(myhorriblefakeuser);
256 chan->Shrink("banredirects");
261 virtual void OnRehash(userrec* user, const std::string ¶m)
263 ExceptionModule = ServerInstance->FindModule("m_banexception.so");
266 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
268 /* This prevents recursion when a user sets multiple ban redirects in a chain
274 /* Return 1 to prevent the join, 0 to allow it */
277 BanRedirectList* redirects;
279 if(chan->GetExt("banredirects", redirects))
281 /* We actually had some ban redirects to check */
283 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
284 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
289 ListModeRequest n(this, ExceptionModule, user, chan);
290 /* Users with ban exceptions are allowed to join without being redirected */
295 std::string ipmask(user->nick);
296 ipmask.append(1, '!').append(user->MakeHostIP());
298 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
300 if(ServerInstance->MatchText(user->GetFullRealHost(), redir->banmask) || ServerInstance->MatchText(user->GetFullHost(), redir->banmask) || ServerInstance->MatchText(ipmask, redir->banmask))
302 /* tell them they're banned and are being transferred */
303 chanrec* destchan = ServerInstance->FindChan(redir->targetchan);
305 if(destchan && ServerInstance->FindModule("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
307 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
312 user->WriteServ("470 %s :You are banned from %s. You are being automatically redirected to %s", user->nick, chan->name, redir->targetchan.c_str());
314 chanrec::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", ServerInstance->Time(true));
325 virtual ~ModuleBanRedirect()
327 ServerInstance->Modes->DelModeWatcher(re);
331 virtual Version GetVersion()
333 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
336 Priority Prioritize()
338 return (Priority)ServerInstance->PriorityBefore("m_banexception.so");
343 class ModuleBanRedirectFactory : public ModuleFactory
346 ModuleBanRedirectFactory()
350 ~ModuleBanRedirectFactory()
354 virtual Module * CreateModule(InspIRCd* Me)
356 return new ModuleBanRedirect(Me);
362 extern "C" DllExport void * init_module( void )
364 return new ModuleBanRedirectFactory;