1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 : public classbase
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 BanRedirect(InspIRCd* Instance)
42 : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL)
46 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type, bool)
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 mask[current].assign(start_pos, curr);
95 if(mask[current].empty())
97 mask[current].assign(start_pos, param.end());
100 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
101 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
103 /* std::string::swap() is fast - it runs in constant time */
104 mask[NICK].swap(mask[IDENT]);
107 for(int i = 0; i < 3; i++)
115 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
117 if(mask[CHAN].length())
119 if(!IS_LOCAL(source) || ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
121 if (assign(channel->name) == mask[CHAN])
123 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());
130 /* It's a properly valid redirecting ban, and we're adding it */
131 if(!channel->GetExt("banredirects", redirects))
133 redirects = new BanRedirectList;
134 channel->Extend("banredirects", redirects);
137 /* Here 'param' doesn't have the channel on it yet */
138 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
141 param.append(mask[CHAN]);
145 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
146 if(channel->GetExt("banredirects", redirects))
148 /* But there were, so we need to remove the matching one if there is one */
150 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
153 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
155 redirects->erase(redir);
157 if(redirects->empty())
160 channel->Shrink("banredirects");
168 /* Append the channel so the default +b handler can remove the entry too */
169 param.append(mask[CHAN]);
175 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
185 class ModuleBanRedirect : public Module
189 Module* ExceptionModule;
192 ModuleBanRedirect(InspIRCd* Me)
195 re = new BanRedirect(Me);
198 if(!ServerInstance->Modes->AddModeWatcher(re))
201 throw ModuleException("Could not add mode watcher");
206 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
207 Me->Modules->Attach(list, this, 4);
211 virtual void OnChannelDelete(Channel* chan)
213 OnCleanup(TYPE_CHANNEL, chan);
216 virtual void OnCleanup(int target_type, void* item)
218 if(target_type == TYPE_CHANNEL)
220 Channel* chan = static_cast<Channel*>(item);
221 BanRedirectList* redirects;
223 if(chan->GetExt("banredirects", redirects))
225 irc::modestacker modestack(ServerInstance, false);
226 StringDeque stackresult;
227 std::vector<std::string> mode_junk;
228 mode_junk.push_back(chan->name);
230 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
232 modestack.Push('b', i->targetchan.insert(0, i->banmask));
235 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
237 modestack.PushPlus();
238 modestack.Push('b', i->banmask);
241 while(modestack.GetStackedLine(stackresult))
243 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
245 mode_junk.push_back(stackresult[i]);
248 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
252 chan->Shrink("banredirects");
257 virtual void OnRehash(User* user, const std::string ¶m)
259 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
262 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
264 /* This prevents recursion when a user sets multiple ban redirects in a chain
270 /* Return 1 to prevent the join, 0 to allow it */
273 BanRedirectList* redirects;
275 if(chan->GetExt("banredirects", redirects))
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?
285 ListModeRequest n(this, ExceptionModule, user, chan);
286 /* Users with ban exceptions are allowed to join without being redirected */
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 /* tell them they're banned and are being transferred */
299 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
300 std::string destlimit;
303 destlimit = destchan->GetModeParameter('l');
305 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
307 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
312 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
313 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());
315 Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
326 virtual ~ModuleBanRedirect()
328 ServerInstance->Modes->DelModeWatcher(re);
332 virtual Version GetVersion()
334 return Version("$Id$", VF_COMMON|VF_VENDOR, API_VERSION);
339 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
340 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
344 MODULE_INIT(ModuleBanRedirect)