2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2014 Adam <Adam@anope.org>
6 * Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2013, 2017-2018 Sadie Powell <sadie@witchery.services>
8 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
9 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 * Copyright (C) 2009 Matt Smith <dz@inspircd.org>
12 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
13 * Copyright (C) 2008 Uli Schlachter <psychon@inspircd.org>
14 * Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
15 * Copyright (C) 2007 Oliver Lupton <om@inspircd.org>
16 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
18 * This file is part of InspIRCd. InspIRCd is free software: you can
19 * redistribute it and/or modify it under the terms of the GNU General Public
20 * License as published by the Free Software Foundation, version 2.
22 * This program is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 /* Originally written by Om, January 2009
38 class BanRedirectEntry
41 std::string targetchan;
44 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
45 : targetchan(target), banmask(mask)
50 typedef std::vector<BanRedirectEntry> BanRedirectList;
52 class BanRedirect : public ModeWatcher
54 ChanModeReference ban;
56 SimpleExtItem<BanRedirectList> extItem;
57 BanRedirect(Module* parent)
58 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
60 , extItem("banredirect", ExtensionItem::EXT_CHANNEL, parent)
64 bool BeforeMode(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE
66 /* nick!ident@host -> nick!ident@host
67 * nick!ident@host#chan -> nick!ident@host#chan
68 * nick@host#chan -> nick!*@host#chan
69 * nick!ident#chan -> nick!ident@*#chan
70 * nick#chan -> nick!*@*#chan
73 if ((channel) && !param.empty())
75 BanRedirectList* redirects;
78 enum { NICK, IDENT, HOST, CHAN } current = NICK;
79 std::string::iterator start_pos = param.begin();
81 if (param.length() >= 2 && param[1] == ':')
84 if (param.find('#') == std::string::npos)
87 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
88 unsigned int maxbans = banlm->GetLimit(channel);
89 ListModeBase::ModeList* list = banlm->GetList(channel);
90 if ((list) && (adding) && (maxbans <= list->size()))
92 source->WriteNumeric(ERR_BANLISTFULL, channel->name, banlm->GetModeChar(), InspIRCd::Format("Channel ban list for %s is full (maximum entries for this channel is %u)", channel->name.c_str(), maxbans));
96 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
103 mask[current].assign(start_pos, curr);
108 if (current != IDENT)
110 mask[current].assign(start_pos, curr);
117 mask[current].assign(start_pos, curr);
124 if(mask[current].empty())
126 mask[current].assign(start_pos, param.end());
129 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
130 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
132 /* std::string::swap() is fast - it runs in constant time */
133 mask[NICK].swap(mask[IDENT]);
136 if (!mask[NICK].empty() && mask[IDENT].empty() && mask[HOST].empty())
138 if (mask[NICK].find('.') != std::string::npos || mask[NICK].find(':') != std::string::npos)
140 mask[NICK].swap(mask[HOST]);
144 for(int i = 0; i < 3; i++)
152 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
154 if(mask[CHAN].length())
156 if (adding && IS_LOCAL(source))
158 if (!ServerInstance->IsChannel(mask[CHAN]))
160 source->WriteNumeric(ERR_NOSUCHCHANNEL, channel->name, InspIRCd::Format("Invalid channel name in redirection (%s)", mask[CHAN].c_str()));
164 Channel *c = ServerInstance->FindChan(mask[CHAN]);
167 source->WriteNumeric(690, InspIRCd::Format("Target channel %s must exist to be set as a redirect.", mask[CHAN].c_str()));
170 else if (adding && c->GetPrefixValue(source) < OP_VALUE)
172 source->WriteNumeric(690, InspIRCd::Format("You must be opped on %s to set it as a redirect.", mask[CHAN].c_str()));
176 if (irc::equals(channel->name, mask[CHAN]))
178 source->WriteNumeric(690, channel->name, "You cannot set a ban redirection to the channel the ban is on");
185 /* It's a properly valid redirecting ban, and we're adding it */
186 redirects = extItem.get(channel);
189 redirects = new BanRedirectList;
190 extItem.set(channel, redirects);
194 for (BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); ++redir)
196 // Mimic the functionality used when removing the mode
197 if (irc::equals(redir->targetchan, mask[CHAN]) && irc::equals(redir->banmask, param))
199 // Make sure the +b handler will still set the right ban
200 param.append(mask[CHAN]);
201 // Silently ignore the duplicate and don't set metadata
202 // This still allows channel ops to set/unset a redirect ban to clear "ghost" redirects
208 /* Here 'param' doesn't have the channel on it yet */
209 redirects->push_back(BanRedirectEntry(mask[CHAN], param));
212 param.append(mask[CHAN]);
216 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
217 redirects = extItem.get(channel);
220 /* But there were, so we need to remove the matching one if there is one */
222 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
224 if ((irc::equals(redir->targetchan, mask[CHAN])) && (irc::equals(redir->banmask, param)))
226 redirects->erase(redir);
228 if(redirects->empty())
230 extItem.unset(channel);
238 /* Append the channel so the default +b handler can remove the entry too */
239 param.append(mask[CHAN]);
248 class ModuleBanRedirect : public Module
252 ChanModeReference limitmode;
253 ChanModeReference redirectmode;
259 , limitmode(this, "limit")
260 , redirectmode(this, "redirect")
264 void OnCleanup(ExtensionItem::ExtensibleType type, Extensible* item) CXX11_OVERRIDE
266 if (type == ExtensionItem::EXT_CHANNEL)
268 Channel* chan = static_cast<Channel*>(item);
269 BanRedirectList* redirects = re.extItem.get(chan);
273 ModeHandler* ban = ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL);
274 Modes::ChangeList changelist;
276 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
277 changelist.push_remove(ban, i->targetchan.insert(0, i->banmask));
279 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
280 changelist.push_add(ban, i->banmask);
282 ServerInstance->Modes->Process(ServerInstance->FakeClient, chan, NULL, changelist, ModeParser::MODE_LOCALONLY);
287 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
291 BanRedirectList* redirects = re.extItem.get(chan);
295 /* We actually had some ban redirects to check */
297 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
298 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
302 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
303 if (result == MOD_RES_ALLOW)
305 // they have a ban exception
306 return MOD_RES_PASSTHRU;
309 std::string ipmask(user->nick);
310 ipmask.append(1, '!').append(user->MakeHostIP());
312 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
314 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
316 /* This prevents recursion when a user sets multiple ban redirects in a chain
319 * If we're here and nofollow is true then we're already redirecting this user
320 * and there's a redirecting ban set on this channel that matches him, too.
326 /* tell them they're banned and are being transferred */
327 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
328 std::string destlimit;
331 destlimit = destchan->GetModeParameter(limitmode);
333 if(destchan && destchan->IsModeSet(redirectmode) && !destlimit.empty() && (destchan->GetUserCounter() >= ConvToNum<size_t>(destlimit)))
335 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
340 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
341 user->WriteNumeric(470, chan->name, redir->targetchan, "You are banned from this channel, so you are automatically being transferred to the redirected channel.");
343 Channel::JoinUser(user, redir->targetchan);
351 return MOD_RES_PASSTHRU;
354 Version GetVersion() CXX11_OVERRIDE
356 return Version("Allows specifying a channel to redirect a banned user to in the ban mask.", VF_COMMON|VF_VENDOR);
360 MODULE_INIT(ModuleBanRedirect)