1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "u_listmode.h"
17 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
18 /* $ModDep: ../../include/u_listmode.h */
20 /* Originally written by Om, January 2009
23 class BanRedirectEntry
26 std::string targetchan;
29 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
30 : targetchan(target), banmask(mask)
35 typedef std::vector<BanRedirectEntry> BanRedirectList;
36 typedef std::deque<std::string> StringDeque;
38 class BanRedirect : public ModeWatcher
41 SimpleExtItem<BanRedirectList> extItem;
42 BanRedirect(Module* parent) : ModeWatcher(parent, 'b', MODETYPE_CHANNEL),
43 extItem("banredirect", parent)
47 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type)
49 /* nick!ident@host -> nick!ident@host
50 * nick!ident@host#chan -> nick!ident@host#chan
51 * nick@host#chan -> nick!*@host#chan
52 * nick!ident#chan -> nick!ident@*#chan
53 * nick#chan -> nick!*@*#chan
56 if(channel && (type == MODETYPE_CHANNEL) && param.length())
58 BanRedirectList* redirects;
61 enum { NICK, IDENT, HOST, CHAN } current = NICK;
62 std::string::iterator start_pos = param.begin();
63 long maxbans = channel->GetMaxBans();
65 if (param.length() >= 2 && param[1] == ':')
68 if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
70 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);
74 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
79 mask[current].assign(start_pos, curr);
84 mask[current].assign(start_pos, curr);
89 /* bug #921: don't barf when redirecting to ## channels */
92 mask[current].assign(start_pos, curr);
100 if(mask[current].empty())
102 mask[current].assign(start_pos, param.end());
105 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
106 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
108 /* std::string::swap() is fast - it runs in constant time */
109 mask[NICK].swap(mask[IDENT]);
112 for(int i = 0; i < 3; i++)
120 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
122 if(mask[CHAN].length())
124 if (IS_LOCAL(source))
126 if (!ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
128 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
132 Channel *c = ServerInstance->FindChan(mask[CHAN].c_str());
135 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
138 else if (c->GetPrefixValue(source) < OP_VALUE)
140 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
144 if (assign(channel->name) == mask[CHAN])
146 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());
153 /* It's a properly valid redirecting ban, and we're adding it */
154 redirects = extItem.get(channel);
157 redirects = new BanRedirectList;
158 extItem.set(channel, redirects);
161 /* Here 'param' doesn't have the channel on it yet */
162 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
165 param.append(mask[CHAN]);
169 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
170 redirects = extItem.get(channel);
173 /* But there were, so we need to remove the matching one if there is one */
175 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
178 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
180 redirects->erase(redir);
182 if(redirects->empty())
184 extItem.unset(channel);
192 /* Append the channel so the default +b handler can remove the entry too */
193 param.append(mask[CHAN]);
202 class ModuleBanRedirect : public Module
206 Module* ExceptionModule;
214 if(!ServerInstance->Modes->AddModeWatcher(&re))
215 throw ModuleException("Could not add mode watcher");
219 ServerInstance->Extensions.Register(&re.extItem);
220 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete };
221 ServerInstance->Modules->Attach(list, this, 3);
224 virtual void OnChannelDelete(Channel* chan)
226 OnCleanup(TYPE_CHANNEL, chan);
229 virtual void OnCleanup(int target_type, void* item)
231 if(target_type == TYPE_CHANNEL)
233 Channel* chan = static_cast<Channel*>(item);
234 BanRedirectList* redirects = re.extItem.get(chan);
238 irc::modestacker modestack(false);
239 StringDeque stackresult;
240 std::vector<std::string> mode_junk;
241 mode_junk.push_back(chan->name);
243 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
245 modestack.Push('b', i->targetchan.insert(0, i->banmask));
248 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
250 modestack.PushPlus();
251 modestack.Push('b', i->banmask);
254 while(modestack.GetStackedLine(stackresult))
256 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
257 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
258 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
264 virtual void OnRehash(User* user)
266 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
269 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
271 /* This prevents recursion when a user sets multiple ban redirects in a chain
275 return MOD_RES_PASSTHRU;
277 /* Return 1 to prevent the join, 0 to allow it */
280 BanRedirectList* redirects = re.extItem.get(chan);
284 /* We actually had some ban redirects to check */
286 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
287 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
291 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
292 if (result == MOD_RES_ALLOW)
294 // they have a ban exception
295 return MOD_RES_PASSTHRU;
298 std::string ipmask(user->nick);
299 ipmask.append(1, '!').append(user->MakeHostIP());
301 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
303 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
305 /* tell them they're banned and are being transferred */
306 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
307 std::string destlimit;
310 destlimit = destchan->GetModeParameter('l');
312 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
314 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
319 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
320 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());
322 Channel::JoinUser(user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
330 return MOD_RES_PASSTHRU;
333 virtual ~ModuleBanRedirect()
335 /* XXX is this the best place to do this? */
336 if (!ServerInstance->Modes->DelModeWatcher(&re))
337 ServerInstance->Logs->Log("m_banredirect.so", DEBUG, "Failed to delete modewatcher!");
340 virtual Version GetVersion()
342 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
347 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
348 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
352 MODULE_INIT(ModuleBanRedirect)