2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007, 2009 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8 * Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
9 * Copyright (C) 2007 Oliver Lupton <oliverlupton@gmail.com>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "u_listmode.h"
28 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
30 /* Originally written by Om, January 2009
33 class BanRedirectEntry
36 std::string targetchan;
39 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
40 : targetchan(target), banmask(mask)
45 typedef std::vector<BanRedirectEntry> BanRedirectList;
46 typedef std::deque<std::string> StringDeque;
48 class BanRedirect : public ModeWatcher
51 SimpleExtItem<BanRedirectList> extItem;
52 BanRedirect(Module* parent) : ModeWatcher(parent, 'b', MODETYPE_CHANNEL),
53 extItem("banredirect", parent)
57 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type)
59 /* nick!ident@host -> nick!ident@host
60 * nick!ident@host#chan -> nick!ident@host#chan
61 * nick@host#chan -> nick!*@host#chan
62 * nick!ident#chan -> nick!ident@*#chan
63 * nick#chan -> nick!*@*#chan
66 if(channel && (type == MODETYPE_CHANNEL) && param.length())
68 BanRedirectList* redirects;
71 enum { NICK, IDENT, HOST, CHAN } current = NICK;
72 std::string::iterator start_pos = param.begin();
73 long maxbans = channel->GetMaxBans();
75 if (param.length() >= 2 && param[1] == ':')
78 if (param.find('#') == std::string::npos)
81 if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
83 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);
87 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
92 mask[current].assign(start_pos, curr);
97 mask[current].assign(start_pos, curr);
102 /* bug #921: don't barf when redirecting to ## channels */
105 mask[current].assign(start_pos, curr);
113 if(mask[current].empty())
115 mask[current].assign(start_pos, param.end());
118 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
119 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
121 /* std::string::swap() is fast - it runs in constant time */
122 mask[NICK].swap(mask[IDENT]);
125 if (!mask[NICK].empty() && mask[IDENT].empty() && mask[HOST].empty())
127 if (mask[NICK].find('.') != std::string::npos || mask[NICK].find(':') != std::string::npos)
129 mask[NICK].swap(mask[HOST]);
133 for(int i = 0; i < 3; i++)
141 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
143 if(mask[CHAN].length())
145 if (adding && IS_LOCAL(source))
147 if (!ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
149 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
153 Channel *c = ServerInstance->FindChan(mask[CHAN]);
156 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
159 else if (adding && c->GetPrefixValue(source) < OP_VALUE)
161 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
165 if (assign(channel->name) == mask[CHAN])
167 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());
174 /* It's a properly valid redirecting ban, and we're adding it */
175 redirects = extItem.get(channel);
178 redirects = new BanRedirectList;
179 extItem.set(channel, redirects);
182 /* Here 'param' doesn't have the channel on it yet */
183 redirects->push_back(BanRedirectEntry(mask[CHAN], param));
186 param.append(mask[CHAN]);
190 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
191 redirects = extItem.get(channel);
194 /* But there were, so we need to remove the matching one if there is one */
196 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
199 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
201 redirects->erase(redir);
203 if(redirects->empty())
205 extItem.unset(channel);
213 /* Append the channel so the default +b handler can remove the entry too */
214 param.append(mask[CHAN]);
223 class ModuleBanRedirect : public Module
238 if(!ServerInstance->Modes->AddModeWatcher(&re))
239 throw ModuleException("Could not add mode watcher");
241 ServerInstance->Modules->AddService(re.extItem);
242 Implementation list[] = { I_OnUserPreJoin };
243 ServerInstance->Modules->Attach(list, this, sizeof(list)/sizeof(Implementation));
246 virtual void OnCleanup(int target_type, void* item)
248 if(target_type == TYPE_CHANNEL)
250 Channel* chan = static_cast<Channel*>(item);
251 BanRedirectList* redirects = re.extItem.get(chan);
255 irc::modestacker modestack(false);
256 StringDeque stackresult;
257 std::vector<std::string> mode_junk;
258 mode_junk.push_back(chan->name);
260 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
262 modestack.Push('b', i->targetchan.insert(0, i->banmask));
265 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
267 modestack.PushPlus();
268 modestack.Push('b', i->banmask);
271 while(modestack.GetStackedLine(stackresult))
273 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
274 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
275 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
281 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
285 BanRedirectList* redirects = re.extItem.get(chan);
289 /* We actually had some ban redirects to check */
291 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
292 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
296 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
297 if (result == MOD_RES_ALLOW)
299 // they have a ban exception
300 return MOD_RES_PASSTHRU;
303 std::string ipmask(user->nick);
304 ipmask.append(1, '!').append(user->MakeHostIP());
306 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
308 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
310 /* This prevents recursion when a user sets multiple ban redirects in a chain
313 * If we're here and nofollow is true then we're already redirecting this user
314 * and there's a redirecting ban set on this channel that matches him, too.
320 /* tell them they're banned and are being transferred */
321 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
322 std::string destlimit;
325 destlimit = destchan->GetModeParameter('l');
327 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
329 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
334 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
335 user->WriteNumeric(470, "%s %s %s :You are banned from this channel, so you are automatically transferred to the redirected channel.", user->nick.c_str(), chan->name.c_str(), redir->targetchan.c_str());
337 Channel::JoinUser(user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
345 return MOD_RES_PASSTHRU;
348 virtual ~ModuleBanRedirect()
350 /* XXX is this the best place to do this? */
351 if (!ServerInstance->Modes->DelModeWatcher(&re))
352 ServerInstance->Logs->Log("m_banredirect.so", DEBUG, "Failed to delete modewatcher!");
355 virtual Version GetVersion()
357 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
361 MODULE_INIT(ModuleBanRedirect)