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 * ---------------------------------------------------
15 #include "u_listmode.h"
17 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
19 /* Originally written by Om, January 2007
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(User* source, User* dest, Channel* 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(adding && (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
187 Module* ExceptionModule;
190 ModuleBanRedirect(InspIRCd* Me)
193 re = new BanRedirect(Me);
196 if(!ServerInstance->AddModeWatcher(re))
197 throw ModuleException("Could not add mode watcher");
202 void Implements(char* List)
204 List[I_OnRehash] = List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
207 virtual void OnChannelDelete(Channel* chan)
209 OnCleanup(TYPE_CHANNEL, chan);
212 virtual void OnCleanup(int target_type, void* item)
214 if(target_type == TYPE_CHANNEL)
216 Channel* chan = static_cast<Channel*>(item);
217 BanRedirectList* redirects;
219 if(chan->GetExt("banredirects", redirects))
221 irc::modestacker modestack(false);
222 StringDeque stackresult;
223 const char* mode_junk[MAXMODES+2];
225 mode_junk[0] = chan->name;
227 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
229 modestack.Push('b', i->targetchan.insert(0, i->banmask));
232 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
234 modestack.PushPlus();
235 modestack.Push('b', i->banmask);
238 while(modestack.GetStackedLine(stackresult))
240 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
242 mode_junk[i+1] = stackresult[i].c_str();
245 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, ServerInstance->FakeClient);
249 chan->Shrink("banredirects");
254 virtual void OnRehash(User* user, const std::string ¶m)
256 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
259 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
261 /* This prevents recursion when a user sets multiple ban redirects in a chain
267 /* Return 1 to prevent the join, 0 to allow it */
270 BanRedirectList* redirects;
272 if(chan->GetExt("banredirects", redirects))
274 /* We actually had some ban redirects to check */
276 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
277 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
282 ListModeRequest n(this, ExceptionModule, user, chan);
283 /* Users with ban exceptions are allowed to join without being redirected */
288 std::string ipmask(user->nick);
289 ipmask.append(1, '!').append(user->MakeHostIP());
291 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
293 if(ServerInstance->MatchText(user->GetFullRealHost(), redir->banmask) || ServerInstance->MatchText(user->GetFullHost(), redir->banmask) || ServerInstance->MatchText(ipmask, redir->banmask))
295 /* tell them they're banned and are being transferred */
296 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
298 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
300 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
305 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
306 user->WriteServ("470 %s :You are being automatically redirected to %s", user->nick, redir->targetchan.c_str());
308 Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", ServerInstance->Time(true));
319 virtual ~ModuleBanRedirect()
321 ServerInstance->Modes->DelModeWatcher(re);
325 virtual Version GetVersion()
327 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
330 Priority Prioritize()
332 return (Priority)ServerInstance->Modules->PriorityBefore("m_banexception.so");
337 MODULE_INIT(ModuleBanRedirect)