1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 2007
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
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)
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(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
67 source->WriteServ("478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)", source->nick, channel->name, channel->name, maxbans);
71 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
76 mask[current].assign(start_pos, curr);
81 mask[current].assign(start_pos, curr);
86 mask[current].assign(start_pos, curr);
93 if(mask[current].empty())
95 mask[current].assign(start_pos, param.end());
98 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
99 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
101 /* std::string::swap() is fast - it runs in constant time */
102 mask[NICK].swap(mask[IDENT]);
105 for(int i = 0; i < 3; i++)
113 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
115 if(mask[CHAN].length())
117 if(Srv->IsChannel(mask[CHAN].c_str()))
119 if(irc::string(channel->name) == irc::string(mask[CHAN].c_str()))
121 source->WriteServ("690 %s %s :You cannot set a ban redirection to the channel the ban is on", source->nick, channel->name);
128 /* It's a properly valid redirecting ban, and we're adding it */
129 if(!channel->GetExt("banredirects", redirects))
131 redirects = new BanRedirectList;
132 channel->Extend("banredirects", redirects);
135 /* Here 'param' doesn't have the channel on it yet */
136 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
139 param.append(mask[CHAN]);
143 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
144 if(channel->GetExt("banredirects", redirects))
146 /* But there were, so we need to remove the matching one if there is one */
148 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
151 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
153 redirects->erase(redir);
155 if(redirects->empty())
158 channel->Shrink("banredirects");
166 /* Append the channel so the default +b handler can remove the entry too */
167 param.append(mask[CHAN]);
173 source->WriteServ("403 %s %s :Invalid channel name in redirection (%s)", source->nick, channel->name, mask[CHAN].c_str());
183 class ModuleBanRedirect : public Module
187 Module* ExceptionModule;
190 ModuleBanRedirect(InspIRCd* Me)
193 re = new BanRedirect(Me);
196 if(!ServerInstance->AddModeWatcher(re))
199 throw ModuleException("Could not add mode watcher");
204 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
205 Me->Modules->Attach(list, this, sizeof(*list));
209 virtual void OnChannelDelete(Channel* chan)
211 OnCleanup(TYPE_CHANNEL, chan);
214 virtual void OnCleanup(int target_type, void* item)
216 if(target_type == TYPE_CHANNEL)
218 Channel* chan = static_cast<Channel*>(item);
219 BanRedirectList* redirects;
221 if(chan->GetExt("banredirects", redirects))
223 irc::modestacker modestack(false);
224 StringDeque stackresult;
225 const char* mode_junk[MAXMODES+2];
227 mode_junk[0] = chan->name;
229 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
231 modestack.Push('b', i->targetchan.insert(0, i->banmask));
234 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
236 modestack.PushPlus();
237 modestack.Push('b', i->banmask);
240 while(modestack.GetStackedLine(stackresult))
242 for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
244 mode_junk[i+1] = stackresult[i].c_str();
247 ServerInstance->SendMode(mode_junk, stackresult.size() + 1, ServerInstance->FakeClient);
251 chan->Shrink("banredirects");
256 virtual void OnRehash(User* user, const std::string ¶m)
258 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
261 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, bool synching = false)
263 /* This prevents recursion when a user sets multiple ban redirects in a chain
269 /* Return 1 to prevent the join, 0 to allow it */
272 BanRedirectList* redirects;
274 if(chan->GetExt("banredirects", redirects))
276 /* We actually had some ban redirects to check */
278 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
279 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
284 ListModeRequest n(this, ExceptionModule, user, chan);
285 /* Users with ban exceptions are allowed to join without being redirected */
290 std::string ipmask(user->nick);
291 ipmask.append(1, '!').append(user->MakeHostIP());
293 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
295 if(ServerInstance->MatchText(user->GetFullRealHost(), redir->banmask) || ServerInstance->MatchText(user->GetFullHost(), redir->banmask) || ServerInstance->MatchText(ipmask, redir->banmask))
297 /* tell them they're banned and are being transferred */
298 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
300 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
302 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
307 user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
308 user->WriteServ("470 %s :You are being automatically redirected to %s", user->nick, redir->targetchan.c_str());
310 Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time(true));
321 virtual ~ModuleBanRedirect()
323 ServerInstance->Modes->DelModeWatcher(re);
327 virtual Version GetVersion()
329 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
334 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
335 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIO_BEFORE, &banex);
339 MODULE_INIT(ModuleBanRedirect)