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 /* $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
51 SimpleExtItem<BanRedirectList> extItem;
52 BanRedirect(Module* parent)
53 : ModeWatcher(parent, 'b', MODETYPE_CHANNEL)
55 , extItem("banredirect", parent)
59 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type)
61 /* nick!ident@host -> nick!ident@host
62 * nick!ident@host#chan -> nick!ident@host#chan
63 * nick@host#chan -> nick!*@host#chan
64 * nick!ident#chan -> nick!ident@*#chan
65 * nick#chan -> nick!*@*#chan
68 if(channel && (type == MODETYPE_CHANNEL) && param.length())
70 BanRedirectList* redirects;
73 enum { NICK, IDENT, HOST, CHAN } current = NICK;
74 std::string::iterator start_pos = param.begin();
76 if (param.length() >= 2 && param[1] == ':')
79 ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
80 unsigned int maxbans = banlm->GetLimit(channel);
81 ListModeBase::ModeList* list = banlm->GetList(channel);
82 if ((list) && (adding) && (maxbans <= list->size()))
84 source->WriteNumeric(478, "%s %s :Channel ban list for %s is full (maximum entries for this channel is %u)", source->nick.c_str(), channel->name.c_str(), channel->name.c_str(), maxbans);
88 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
93 mask[current].assign(start_pos, curr);
98 mask[current].assign(start_pos, curr);
103 /* bug #921: don't barf when redirecting to ## channels */
106 mask[current].assign(start_pos, curr);
114 if(mask[current].empty())
116 mask[current].assign(start_pos, param.end());
119 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
120 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
122 /* std::string::swap() is fast - it runs in constant time */
123 mask[NICK].swap(mask[IDENT]);
126 for(int i = 0; i < 3; i++)
134 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
136 if(mask[CHAN].length())
138 if (adding && IS_LOCAL(source))
140 if (!ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
142 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
146 Channel *c = ServerInstance->FindChan(mask[CHAN]);
149 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
152 else if (adding && c->GetPrefixValue(source) < OP_VALUE)
154 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
158 if (assign(channel->name) == mask[CHAN])
160 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());
167 /* It's a properly valid redirecting ban, and we're adding it */
168 redirects = extItem.get(channel);
171 redirects = new BanRedirectList;
172 extItem.set(channel, redirects);
175 /* Here 'param' doesn't have the channel on it yet */
176 redirects->push_back(BanRedirectEntry(mask[CHAN], param));
179 param.append(mask[CHAN]);
183 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
184 redirects = extItem.get(channel);
187 /* But there were, so we need to remove the matching one if there is one */
189 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
192 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
194 redirects->erase(redir);
196 if(redirects->empty())
198 extItem.unset(channel);
206 /* Append the channel so the default +b handler can remove the entry too */
207 param.append(mask[CHAN]);
216 class ModuleBanRedirect : public Module
231 if(!ServerInstance->Modes->AddModeWatcher(&re))
232 throw ModuleException("Could not add mode watcher");
234 ServerInstance->Modules->AddService(re.extItem);
235 Implementation list[] = { I_OnUserPreJoin };
236 ServerInstance->Modules->Attach(list, this, sizeof(list)/sizeof(Implementation));
239 virtual void OnCleanup(int target_type, void* item)
241 if(target_type == TYPE_CHANNEL)
243 Channel* chan = static_cast<Channel*>(item);
244 BanRedirectList* redirects = re.extItem.get(chan);
248 irc::modestacker modestack(false);
249 std::vector<std::string> stackresult;
250 std::vector<std::string> mode_junk;
251 mode_junk.push_back(chan->name);
253 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
255 modestack.Push('b', i->targetchan.insert(0, i->banmask));
258 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
260 modestack.PushPlus();
261 modestack.Push('b', i->banmask);
264 while(modestack.GetStackedLine(stackresult))
266 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
267 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
268 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
274 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const std::string& cname, std::string &privs, const std::string &keygiven)
278 BanRedirectList* redirects = re.extItem.get(chan);
282 /* We actually had some ban redirects to check */
284 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
285 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
289 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
290 if (result == MOD_RES_ALLOW)
292 // they have a ban exception
293 return MOD_RES_PASSTHRU;
296 std::string ipmask(user->nick);
297 ipmask.append(1, '!').append(user->MakeHostIP());
299 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
301 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
303 /* This prevents recursion when a user sets multiple ban redirects in a chain
306 * If we're here and nofollow is true then we're already redirecting this user
307 * and there's a redirecting ban set on this channel that matches him, too.
313 /* tell them they're banned and are being transferred */
314 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
315 std::string destlimit;
318 destlimit = destchan->GetModeParameter('l');
320 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
322 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
327 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
328 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());
330 Channel::JoinUser(user, redir->targetchan, false, "", false, ServerInstance->Time());
338 return MOD_RES_PASSTHRU;
341 virtual ~ModuleBanRedirect()
343 /* XXX is this the best place to do this? */
344 if (!ServerInstance->Modes->DelModeWatcher(&re))
345 ServerInstance->Logs->Log("m_banredirect.so", LOG_DEBUG, "Failed to delete modewatcher!");
348 virtual Version GetVersion()
350 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
354 MODULE_INIT(ModuleBanRedirect)