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/>.
28 /* Originally written by Om, January 2009
31 class BanRedirectEntry
34 std::string targetchan;
37 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
38 : targetchan(target), banmask(mask)
43 typedef std::vector<BanRedirectEntry> BanRedirectList;
45 class BanRedirect : public ModeWatcher
47 ChanModeReference ban;
49 SimpleExtItem<BanRedirectList> extItem;
50 BanRedirect(Module* parent)
51 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
53 , extItem("banredirect", ExtensionItem::EXT_CHANNEL, parent)
57 bool BeforeMode(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE
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) && !param.empty())
68 BanRedirectList* redirects;
71 enum { NICK, IDENT, HOST, CHAN } current = NICK;
72 std::string::iterator start_pos = param.begin();
74 if (param.length() >= 2 && param[1] == ':')
77 if (param.find('#') == std::string::npos)
80 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
81 unsigned int maxbans = banlm->GetLimit(channel);
82 ListModeBase::ModeList* list = banlm->GetList(channel);
83 if ((list) && (adding) && (maxbans <= list->size()))
85 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));
89 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
96 mask[current].assign(start_pos, curr);
101 if (current != IDENT)
103 mask[current].assign(start_pos, curr);
110 mask[current].assign(start_pos, curr);
117 if(mask[current].empty())
119 mask[current].assign(start_pos, param.end());
122 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
123 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
125 /* std::string::swap() is fast - it runs in constant time */
126 mask[NICK].swap(mask[IDENT]);
129 if (!mask[NICK].empty() && mask[IDENT].empty() && mask[HOST].empty())
131 if (mask[NICK].find('.') != std::string::npos || mask[NICK].find(':') != std::string::npos)
133 mask[NICK].swap(mask[HOST]);
137 for(int i = 0; i < 3; i++)
145 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
147 if(mask[CHAN].length())
149 if (adding && IS_LOCAL(source))
151 if (!ServerInstance->IsChannel(mask[CHAN]))
153 source->WriteNumeric(ERR_NOSUCHCHANNEL, channel->name, InspIRCd::Format("Invalid channel name in redirection (%s)", mask[CHAN].c_str()));
157 Channel *c = ServerInstance->FindChan(mask[CHAN]);
160 source->WriteNumeric(690, InspIRCd::Format("Target channel %s must exist to be set as a redirect.", mask[CHAN].c_str()));
163 else if (adding && c->GetPrefixValue(source) < OP_VALUE)
165 source->WriteNumeric(690, InspIRCd::Format("You must be opped on %s to set it as a redirect.", mask[CHAN].c_str()));
169 if (irc::equals(channel->name, mask[CHAN]))
171 source->WriteNumeric(690, channel->name, "You cannot set a ban redirection to the channel the ban is on");
178 /* It's a properly valid redirecting ban, and we're adding it */
179 redirects = extItem.get(channel);
182 redirects = new BanRedirectList;
183 extItem.set(channel, redirects);
187 for (BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); ++redir)
189 // Mimic the functionality used when removing the mode
190 if (irc::equals(redir->targetchan, mask[CHAN]) && irc::equals(redir->banmask, param))
192 // Make sure the +b handler will still set the right ban
193 param.append(mask[CHAN]);
194 // Silently ignore the duplicate and don't set metadata
195 // This still allows channel ops to set/unset a redirect ban to clear "ghost" redirects
201 /* Here 'param' doesn't have the channel on it yet */
202 redirects->push_back(BanRedirectEntry(mask[CHAN], param));
205 param.append(mask[CHAN]);
209 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
210 redirects = extItem.get(channel);
213 /* But there were, so we need to remove the matching one if there is one */
215 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
217 if ((irc::equals(redir->targetchan, mask[CHAN])) && (irc::equals(redir->banmask, param)))
219 redirects->erase(redir);
221 if(redirects->empty())
223 extItem.unset(channel);
231 /* Append the channel so the default +b handler can remove the entry too */
232 param.append(mask[CHAN]);
241 class ModuleBanRedirect : public Module
245 ChanModeReference limitmode;
246 ChanModeReference redirectmode;
252 , limitmode(this, "limit")
253 , redirectmode(this, "redirect")
257 void OnCleanup(ExtensionItem::ExtensibleType type, Extensible* item) CXX11_OVERRIDE
259 if (type == ExtensionItem::EXT_CHANNEL)
261 Channel* chan = static_cast<Channel*>(item);
262 BanRedirectList* redirects = re.extItem.get(chan);
266 ModeHandler* ban = ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL);
267 Modes::ChangeList changelist;
269 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
270 changelist.push_remove(ban, i->targetchan.insert(0, i->banmask));
272 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
273 changelist.push_add(ban, i->banmask);
275 ServerInstance->Modes->Process(ServerInstance->FakeClient, chan, NULL, changelist, ModeParser::MODE_LOCALONLY);
280 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
284 BanRedirectList* redirects = re.extItem.get(chan);
288 /* We actually had some ban redirects to check */
290 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
291 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
295 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
296 if (result == MOD_RES_ALLOW)
298 // they have a ban exception
299 return MOD_RES_PASSTHRU;
302 std::string ipmask(user->nick);
303 ipmask.append(1, '!').append(user->MakeHostIP());
305 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
307 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
309 /* This prevents recursion when a user sets multiple ban redirects in a chain
312 * If we're here and nofollow is true then we're already redirecting this user
313 * and there's a redirecting ban set on this channel that matches him, too.
319 /* tell them they're banned and are being transferred */
320 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
321 std::string destlimit;
324 destlimit = destchan->GetModeParameter(limitmode);
326 if(destchan && destchan->IsModeSet(redirectmode) && !destlimit.empty() && (destchan->GetUserCounter() >= ConvToNum<size_t>(destlimit)))
328 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (You are banned)");
333 user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (You are banned)");
334 user->WriteNumeric(470, chan->name, redir->targetchan, "You are banned from this channel, so you are automatically transferred to the redirected channel.");
336 Channel::JoinUser(user, redir->targetchan);
344 return MOD_RES_PASSTHRU;
347 Version GetVersion() CXX11_OVERRIDE
349 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
353 MODULE_INIT(ModuleBanRedirect)