1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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 2008
22 class BanRedirectEntry : public classbase
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
42 BanRedirect(InspIRCd* Instance)
43 : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL), Srv(Instance)
47 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type, bool)
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 mask[current].assign(start_pos, curr);
96 if(mask[current].empty())
98 mask[current].assign(start_pos, param.end());
101 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
102 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
104 /* std::string::swap() is fast - it runs in constant time */
105 mask[NICK].swap(mask[IDENT]);
108 for(int i = 0; i < 3; i++)
116 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
118 if(mask[CHAN].length())
120 if(!IS_LOCAL(source) || Srv->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
122 if (assign(channel->name) == mask[CHAN])
124 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());
131 /* It's a properly valid redirecting ban, and we're adding it */
132 if(!channel->GetExt("banredirects", redirects))
134 redirects = new BanRedirectList;
135 channel->Extend("banredirects", redirects);
138 /* Here 'param' doesn't have the channel on it yet */
139 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
142 param.append(mask[CHAN]);
146 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
147 if(channel->GetExt("banredirects", redirects))
149 /* But there were, so we need to remove the matching one if there is one */
151 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
154 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
156 redirects->erase(redir);
158 if(redirects->empty())
161 channel->Shrink("banredirects");
169 /* Append the channel so the default +b handler can remove the entry too */
170 param.append(mask[CHAN]);
176 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
186 class ModuleBanRedirect : public Module
190 Module* ExceptionModule;
193 ModuleBanRedirect(InspIRCd* Me)
196 re = new BanRedirect(Me);
199 if(!ServerInstance->Modes->AddModeWatcher(re))
202 throw ModuleException("Could not add mode watcher");
207 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
208 Me->Modules->Attach(list, this, 4);
212 virtual void OnChannelDelete(Channel* chan)
214 OnCleanup(TYPE_CHANNEL, chan);
217 virtual void OnCleanup(int target_type, void* item)
219 if(target_type == TYPE_CHANNEL)
221 Channel* chan = static_cast<Channel*>(item);
222 BanRedirectList* redirects;
224 if(chan->GetExt("banredirects", redirects))
226 irc::modestacker modestack(ServerInstance, false);
227 StringDeque stackresult;
228 std::vector<std::string> mode_junk;
229 mode_junk.push_back(chan->name);
231 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
233 modestack.Push('b', i->targetchan.insert(0, i->banmask));
236 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
238 modestack.PushPlus();
239 modestack.Push('b', i->banmask);
242 while(modestack.GetStackedLine(stackresult))
244 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
246 mode_junk.push_back(stackresult[i]);
249 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
253 chan->Shrink("banredirects");
258 virtual void OnRehash(User* user, const std::string ¶m)
260 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
263 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
265 /* This prevents recursion when a user sets multiple ban redirects in a chain
271 /* Return 1 to prevent the join, 0 to allow it */
274 BanRedirectList* redirects;
276 if(chan->GetExt("banredirects", redirects))
278 /* We actually had some ban redirects to check */
280 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
281 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
286 ListModeRequest n(this, ExceptionModule, user, chan);
287 /* Users with ban exceptions are allowed to join without being redirected */
292 std::string ipmask(user->nick);
293 ipmask.append(1, '!').append(user->MakeHostIP());
295 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
297 if(ServerInstance->MatchText(user->GetFullRealHost(), redir->banmask) || ServerInstance->MatchText(user->GetFullHost(), redir->banmask) || ServerInstance->MatchText(ipmask, redir->banmask))
299 /* tell them they're banned and are being transferred */
300 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
301 std::string destlimit;
304 destlimit = destchan->GetModeParameter('l');
306 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
308 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
313 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
314 user->WriteNumeric(470, "%s :You are being automatically redirected to %s", user->nick.c_str(), redir->targetchan.c_str());
316 Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
327 virtual ~ModuleBanRedirect()
329 ServerInstance->Modes->DelModeWatcher(re);
333 virtual Version GetVersion()
335 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
340 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
341 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIO_BEFORE, &banex);
345 MODULE_INIT(ModuleBanRedirect)