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 */
19 /* Originally written by Om, January 2009
22 class BanRedirectEntry
25 std::string targetchan;
28 BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
29 : targetchan(target), banmask(mask)
34 typedef std::vector<BanRedirectEntry> BanRedirectList;
35 typedef std::deque<std::string> StringDeque;
37 class BanRedirect : public ModeWatcher
40 SimpleExtItem<BanRedirectList> extItem;
41 BanRedirect(Module* parent) : ModeWatcher(parent, 'b', MODETYPE_CHANNEL),
42 extItem("banredirect", parent)
46 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type)
48 /* nick!ident@host -> nick!ident@host
49 * nick!ident@host#chan -> nick!ident@host#chan
50 * nick@host#chan -> nick!*@host#chan
51 * nick!ident#chan -> nick!ident@*#chan
52 * nick#chan -> nick!*@*#chan
55 if(channel && (type == MODETYPE_CHANNEL) && param.length())
57 BanRedirectList* redirects;
60 enum { NICK, IDENT, HOST, CHAN } current = NICK;
61 std::string::iterator start_pos = param.begin();
62 long maxbans = channel->GetMaxBans();
64 if (param.length() >= 2 && param[1] == ':')
67 if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
69 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);
73 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
78 mask[current].assign(start_pos, curr);
83 mask[current].assign(start_pos, curr);
88 /* bug #921: don't barf when redirecting to ## channels */
91 mask[current].assign(start_pos, curr);
99 if(mask[current].empty())
101 mask[current].assign(start_pos, param.end());
104 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
105 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
107 /* std::string::swap() is fast - it runs in constant time */
108 mask[NICK].swap(mask[IDENT]);
111 for(int i = 0; i < 3; i++)
119 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
121 if(mask[CHAN].length())
123 if (IS_LOCAL(source))
125 if (!ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
127 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
131 Channel *c = ServerInstance->FindChan(mask[CHAN].c_str());
134 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
137 else if (adding && c->GetPrefixValue(source) < OP_VALUE)
139 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
143 if (assign(channel->name) == mask[CHAN])
145 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());
152 /* It's a properly valid redirecting ban, and we're adding it */
153 redirects = extItem.get(channel);
156 redirects = new BanRedirectList;
157 extItem.set(channel, redirects);
160 /* Here 'param' doesn't have the channel on it yet */
161 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
164 param.append(mask[CHAN]);
168 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
169 redirects = extItem.get(channel);
172 /* But there were, so we need to remove the matching one if there is one */
174 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
177 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
179 redirects->erase(redir);
181 if(redirects->empty())
183 extItem.unset(channel);
191 /* Append the channel so the default +b handler can remove the entry too */
192 param.append(mask[CHAN]);
201 class ModuleBanRedirect : public Module
216 if(!ServerInstance->Modes->AddModeWatcher(&re))
217 throw ModuleException("Could not add mode watcher");
221 ServerInstance->Extensions.Register(&re.extItem);
222 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete };
223 ServerInstance->Modules->Attach(list, this, 3);
226 virtual void OnChannelDelete(Channel* chan)
228 OnCleanup(TYPE_CHANNEL, chan);
231 virtual void OnCleanup(int target_type, void* item)
233 if(target_type == TYPE_CHANNEL)
235 Channel* chan = static_cast<Channel*>(item);
236 BanRedirectList* redirects = re.extItem.get(chan);
240 irc::modestacker modestack(false);
241 StringDeque stackresult;
242 std::vector<std::string> mode_junk;
243 mode_junk.push_back(chan->name);
245 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
247 modestack.Push('b', i->targetchan.insert(0, i->banmask));
250 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
252 modestack.PushPlus();
253 modestack.Push('b', i->banmask);
256 while(modestack.GetStackedLine(stackresult))
258 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
259 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
260 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
266 virtual void OnRehash(User* user)
270 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
272 /* This prevents recursion when a user sets multiple ban redirects in a chain
276 return MOD_RES_PASSTHRU;
278 /* Return 1 to prevent the join, 0 to allow it */
281 BanRedirectList* redirects = re.extItem.get(chan);
285 /* We actually had some ban redirects to check */
287 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
288 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
292 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
293 if (result == MOD_RES_ALLOW)
295 // they have a ban exception
296 return MOD_RES_PASSTHRU;
299 std::string ipmask(user->nick);
300 ipmask.append(1, '!').append(user->MakeHostIP());
302 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
304 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
306 /* tell them they're banned and are being transferred */
307 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
308 std::string destlimit;
311 destlimit = destchan->GetModeParameter('l');
313 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
315 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
320 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
321 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());
323 Channel::JoinUser(user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
331 return MOD_RES_PASSTHRU;
334 virtual ~ModuleBanRedirect()
336 /* XXX is this the best place to do this? */
337 if (!ServerInstance->Modes->DelModeWatcher(&re))
338 ServerInstance->Logs->Log("m_banredirect.so", DEBUG, "Failed to delete modewatcher!");
341 virtual Version GetVersion()
343 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
348 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
349 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
353 MODULE_INIT(ModuleBanRedirect)