1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 */
18 /* $ModDep: ../../include/u_listmode.h */
20 /* Originally written by Om, January 2008
23 class BanRedirectEntry : public classbase
26 std::string targetchan;
29 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
30 : targetchan(target), banmask(mask)
35 typedef std::vector<BanRedirectEntry> BanRedirectList;
36 typedef std::deque<std::string> StringDeque;
38 class BanRedirect : public ModeWatcher
43 BanRedirect(InspIRCd* Instance)
44 : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL), Srv(Instance)
48 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type, bool)
50 /* nick!ident@host -> nick!ident@host
51 * nick!ident@host#chan -> nick!ident@host#chan
52 * nick@host#chan -> nick!*@host#chan
53 * nick!ident#chan -> nick!ident@*#chan
54 * nick#chan -> nick!*@*#chan
57 if(channel && (type == MODETYPE_CHANNEL) && param.length())
59 BanRedirectList* redirects;
62 enum { NICK, IDENT, HOST, CHAN } current = NICK;
63 std::string::iterator start_pos = param.begin();
64 long maxbans = channel->GetMaxBans();
66 if (param.length() >= 2 && param[1] == ':')
69 if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
71 source->WriteNumeric(478, "%s %s :Channel ban list for %s is full (maximum entries for this channel is %ld)", source->nick.c_str(), channel->name.c_str(), channel->name.c_str(), 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(!IS_LOCAL(source) || Srv->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
123 if (assign(channel->name) == mask[CHAN])
125 source->WriteNumeric(690, "%s %s :You cannot set a ban redirection to the channel the ban is on", source->nick.c_str(), channel->name.c_str());
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->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
187 class ModuleBanRedirect : public Module
191 Module* ExceptionModule;
194 ModuleBanRedirect(InspIRCd* Me)
197 re = new BanRedirect(Me);
200 if(!ServerInstance->Modes->AddModeWatcher(re))
203 throw ModuleException("Could not add mode watcher");
208 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
209 Me->Modules->Attach(list, this, 4);
213 virtual void OnChannelDelete(Channel* chan)
215 OnCleanup(TYPE_CHANNEL, chan);
218 virtual void OnCleanup(int target_type, void* item)
220 if(target_type == TYPE_CHANNEL)
222 Channel* chan = static_cast<Channel*>(item);
223 BanRedirectList* redirects;
225 if(chan->GetExt("banredirects", redirects))
227 irc::modestacker modestack(ServerInstance, false);
228 StringDeque stackresult;
229 std::vector<std::string> mode_junk;
230 mode_junk.push_back(chan->name);
232 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
234 modestack.Push('b', i->targetchan.insert(0, i->banmask));
237 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
239 modestack.PushPlus();
240 modestack.Push('b', i->banmask);
243 while(modestack.GetStackedLine(stackresult))
245 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
247 mode_junk.push_back(stackresult[i]);
250 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
254 chan->Shrink("banredirects");
259 virtual void OnRehash(User* user, const std::string ¶m)
261 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
264 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
266 /* This prevents recursion when a user sets multiple ban redirects in a chain
272 /* Return 1 to prevent the join, 0 to allow it */
275 BanRedirectList* redirects;
277 if(chan->GetExt("banredirects", redirects))
279 /* We actually had some ban redirects to check */
281 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
282 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
287 ListModeRequest n(this, ExceptionModule, user, chan);
288 /* Users with ban exceptions are allowed to join without being redirected */
293 std::string ipmask(user->nick);
294 ipmask.append(1, '!').append(user->MakeHostIP());
296 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
298 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
300 /* tell them they're banned and are being transferred */
301 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
302 std::string destlimit;
305 destlimit = destchan->GetModeParameter('l');
307 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
309 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
314 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
315 user->WriteNumeric(470, "%s :You are being automatically redirected to %s", user->nick.c_str(), redir->targetchan.c_str());
317 Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
328 virtual ~ModuleBanRedirect()
330 ServerInstance->Modes->DelModeWatcher(re);
334 virtual Version GetVersion()
336 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
341 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
342 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIO_BEFORE, &banex);
346 MODULE_INIT(ModuleBanRedirect)