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))
121 if (!ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
123 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
127 Channel *c = ServerInstance->FindChan(mask[CHAN].c_str());
130 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
133 else if (c->GetStatus(source) < STATUS_OP)
135 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
139 if (assign(channel->name) == mask[CHAN])
141 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());
148 /* It's a properly valid redirecting ban, and we're adding it */
149 if(!channel->GetExt("banredirects", redirects))
151 redirects = new BanRedirectList;
152 channel->Extend("banredirects", redirects);
155 /* Here 'param' doesn't have the channel on it yet */
156 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
159 param.append(mask[CHAN]);
163 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
164 if(channel->GetExt("banredirects", redirects))
166 /* But there were, so we need to remove the matching one if there is one */
168 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
171 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
173 redirects->erase(redir);
175 if(redirects->empty())
178 channel->Shrink("banredirects");
186 /* Append the channel so the default +b handler can remove the entry too */
187 param.append(mask[CHAN]);
196 class ModuleBanRedirect : public Module
200 Module* ExceptionModule;
203 ModuleBanRedirect(InspIRCd* Me)
206 re = new BanRedirect(Me);
209 if(!ServerInstance->Modes->AddModeWatcher(re))
212 throw ModuleException("Could not add mode watcher");
217 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
218 Me->Modules->Attach(list, this, 4);
222 virtual void OnChannelDelete(Channel* chan)
224 OnCleanup(TYPE_CHANNEL, chan);
227 virtual void OnCleanup(int target_type, void* item)
229 if(target_type == TYPE_CHANNEL)
231 Channel* chan = static_cast<Channel*>(item);
232 BanRedirectList* redirects;
234 if(chan->GetExt("banredirects", redirects))
236 irc::modestacker modestack(ServerInstance, false);
237 StringDeque stackresult;
238 std::vector<std::string> mode_junk;
239 mode_junk.push_back(chan->name);
241 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
243 modestack.Push('b', i->targetchan.insert(0, i->banmask));
246 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
248 modestack.PushPlus();
249 modestack.Push('b', i->banmask);
252 while(modestack.GetStackedLine(stackresult))
254 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
255 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
256 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
260 chan->Shrink("banredirects");
265 virtual void OnRehash(User* user)
267 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
270 virtual int 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
278 /* Return 1 to prevent the join, 0 to allow it */
281 BanRedirectList* redirects;
283 if(chan->GetExt("banredirects", redirects))
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?
293 ListModeRequest n(this, ExceptionModule, user, chan);
294 /* Users with ban exceptions are allowed to join without being redirected */
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(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
334 virtual ~ModuleBanRedirect()
336 ServerInstance->Modes->DelModeWatcher(re);
340 virtual Version GetVersion()
342 return Version("$Id$", VF_COMMON|VF_VENDOR, API_VERSION);
347 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
348 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
352 MODULE_INIT(ModuleBanRedirect)