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;
47 class BanRedirect : public ModeWatcher
50 SimpleExtItem<BanRedirectList> extItem;
51 BanRedirect(Module* parent) : ModeWatcher(parent, 'b', MODETYPE_CHANNEL),
52 extItem("banredirect", parent)
56 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type)
58 /* nick!ident@host -> nick!ident@host
59 * nick!ident@host#chan -> nick!ident@host#chan
60 * nick@host#chan -> nick!*@host#chan
61 * nick!ident#chan -> nick!ident@*#chan
62 * nick#chan -> nick!*@*#chan
65 if(channel && (type == MODETYPE_CHANNEL) && param.length())
67 BanRedirectList* redirects;
70 enum { NICK, IDENT, HOST, CHAN } current = NICK;
71 std::string::iterator start_pos = param.begin();
72 long maxbans = channel->GetMaxBans();
74 if (param.length() >= 2 && param[1] == ':')
77 if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
79 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);
83 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
88 mask[current].assign(start_pos, curr);
93 mask[current].assign(start_pos, curr);
98 /* bug #921: don't barf when redirecting to ## channels */
101 mask[current].assign(start_pos, curr);
109 if(mask[current].empty())
111 mask[current].assign(start_pos, param.end());
114 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
115 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
117 /* std::string::swap() is fast - it runs in constant time */
118 mask[NICK].swap(mask[IDENT]);
121 for(int i = 0; i < 3; i++)
129 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
131 if(mask[CHAN].length())
133 if (adding && IS_LOCAL(source))
135 if (!ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
137 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
141 Channel *c = ServerInstance->FindChan(mask[CHAN]);
144 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
147 else if (adding && c->GetPrefixValue(source) < OP_VALUE)
149 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
153 if (assign(channel->name) == mask[CHAN])
155 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());
162 /* It's a properly valid redirecting ban, and we're adding it */
163 redirects = extItem.get(channel);
166 redirects = new BanRedirectList;
167 extItem.set(channel, redirects);
170 /* Here 'param' doesn't have the channel on it yet */
171 redirects->push_back(BanRedirectEntry(mask[CHAN], param));
174 param.append(mask[CHAN]);
178 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
179 redirects = extItem.get(channel);
182 /* But there were, so we need to remove the matching one if there is one */
184 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
187 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
189 redirects->erase(redir);
191 if(redirects->empty())
193 extItem.unset(channel);
201 /* Append the channel so the default +b handler can remove the entry too */
202 param.append(mask[CHAN]);
211 class ModuleBanRedirect : public Module
226 if(!ServerInstance->Modes->AddModeWatcher(&re))
227 throw ModuleException("Could not add mode watcher");
229 ServerInstance->Modules->AddService(re.extItem);
230 Implementation list[] = { I_OnUserPreJoin };
231 ServerInstance->Modules->Attach(list, this, sizeof(list)/sizeof(Implementation));
234 virtual void OnCleanup(int target_type, void* item)
236 if(target_type == TYPE_CHANNEL)
238 Channel* chan = static_cast<Channel*>(item);
239 BanRedirectList* redirects = re.extItem.get(chan);
243 irc::modestacker modestack(false);
244 std::vector<std::string> stackresult;
245 std::vector<std::string> mode_junk;
246 mode_junk.push_back(chan->name);
248 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
250 modestack.Push('b', i->targetchan.insert(0, i->banmask));
253 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
255 modestack.PushPlus();
256 modestack.Push('b', i->banmask);
259 while(modestack.GetStackedLine(stackresult))
261 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
262 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
263 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
269 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const std::string& cname, std::string &privs, const std::string &keygiven)
273 BanRedirectList* redirects = re.extItem.get(chan);
277 /* We actually had some ban redirects to check */
279 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
280 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
284 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
285 if (result == MOD_RES_ALLOW)
287 // they have a ban exception
288 return MOD_RES_PASSTHRU;
291 std::string ipmask(user->nick);
292 ipmask.append(1, '!').append(user->MakeHostIP());
294 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
296 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
298 /* This prevents recursion when a user sets multiple ban redirects in a chain
301 * If we're here and nofollow is true then we're already redirecting this user
302 * and there's a redirecting ban set on this channel that matches him, too.
308 /* tell them they're banned and are being transferred */
309 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
310 std::string destlimit;
313 destlimit = destchan->GetModeParameter('l');
315 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
317 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
322 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
323 user->WriteNumeric(470, "%s %s %s :You are banned from this channel, so you are automatically transfered to the redirected channel.", user->nick.c_str(), chan->name.c_str(), redir->targetchan.c_str());
325 Channel::JoinUser(user, redir->targetchan, false, "", false, ServerInstance->Time());
333 return MOD_RES_PASSTHRU;
336 virtual ~ModuleBanRedirect()
338 /* XXX is this the best place to do this? */
339 if (!ServerInstance->Modes->DelModeWatcher(&re))
340 ServerInstance->Logs->Log("m_banredirect.so", DEBUG, "Failed to delete modewatcher!");
343 virtual Version GetVersion()
345 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
349 MODULE_INIT(ModuleBanRedirect)