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) : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL)
45 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type)
47 /* nick!ident@host -> nick!ident@host
48 * nick!ident@host#chan -> nick!ident@host#chan
49 * nick@host#chan -> nick!*@host#chan
50 * nick!ident#chan -> nick!ident@*#chan
51 * nick#chan -> nick!*@*#chan
54 if(channel && (type == MODETYPE_CHANNEL) && param.length())
56 BanRedirectList* redirects;
59 enum { NICK, IDENT, HOST, CHAN } current = NICK;
60 std::string::iterator start_pos = param.begin();
61 long maxbans = channel->GetMaxBans();
63 if (param.length() >= 2 && param[1] == ':')
66 if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
68 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);
72 for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
77 mask[current].assign(start_pos, curr);
82 mask[current].assign(start_pos, curr);
87 mask[current].assign(start_pos, curr);
94 if(mask[current].empty())
96 mask[current].assign(start_pos, param.end());
99 /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
100 if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
102 /* std::string::swap() is fast - it runs in constant time */
103 mask[NICK].swap(mask[IDENT]);
106 for(int i = 0; i < 3; i++)
114 param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
116 if(mask[CHAN].length())
118 if (IS_LOCAL(source))
120 if (!ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
122 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
126 Channel *c = ServerInstance->FindChan(mask[CHAN].c_str());
129 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
132 else if (c->GetStatus(source) < STATUS_OP)
134 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
138 if (assign(channel->name) == mask[CHAN])
140 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());
147 /* It's a properly valid redirecting ban, and we're adding it */
148 if(!channel->GetExt("banredirects", redirects))
150 redirects = new BanRedirectList;
151 channel->Extend("banredirects", redirects);
154 /* Here 'param' doesn't have the channel on it yet */
155 redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
158 param.append(mask[CHAN]);
162 /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
163 if(channel->GetExt("banredirects", redirects))
165 /* But there were, so we need to remove the matching one if there is one */
167 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
170 if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
172 redirects->erase(redir);
174 if(redirects->empty())
177 channel->Shrink("banredirects");
185 /* Append the channel so the default +b handler can remove the entry too */
186 param.append(mask[CHAN]);
195 class ModuleBanRedirect : public Module
199 Module* ExceptionModule;
202 ModuleBanRedirect(InspIRCd* Me)
207 if(!ServerInstance->Modes->AddModeWatcher(&re))
208 throw ModuleException("Could not add mode watcher");
212 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
213 Me->Modules->Attach(list, this, 4);
217 virtual void OnChannelDelete(Channel* chan)
219 OnCleanup(TYPE_CHANNEL, chan);
222 virtual void OnCleanup(int target_type, void* item)
224 if(target_type == TYPE_CHANNEL)
226 Channel* chan = static_cast<Channel*>(item);
227 BanRedirectList* redirects;
229 if(chan->GetExt("banredirects", redirects))
231 irc::modestacker modestack(ServerInstance, false);
232 StringDeque stackresult;
233 std::vector<std::string> mode_junk;
234 mode_junk.push_back(chan->name);
236 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
238 modestack.Push('b', i->targetchan.insert(0, i->banmask));
241 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
243 modestack.PushPlus();
244 modestack.Push('b', i->banmask);
247 while(modestack.GetStackedLine(stackresult))
249 mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
250 ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
251 mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
255 chan->Shrink("banredirects");
260 virtual void OnRehash(User* user)
262 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
265 virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
267 /* This prevents recursion when a user sets multiple ban redirects in a chain
271 return MOD_RES_PASSTHRU;
273 /* Return 1 to prevent the join, 0 to allow it */
276 BanRedirectList* redirects;
278 if(chan->GetExt("banredirects", redirects))
280 /* We actually had some ban redirects to check */
282 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
283 * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
288 ListModeRequest n(this, ExceptionModule, user, chan);
289 /* Users with ban exceptions are allowed to join without being redirected */
291 return MOD_RES_PASSTHRU;
294 std::string ipmask(user->nick);
295 ipmask.append(1, '!').append(user->MakeHostIP());
297 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
299 if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
301 /* tell them they're banned and are being transferred */
302 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
303 std::string destlimit;
306 destlimit = destchan->GetModeParameter('l');
308 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
310 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
311 return MOD_RES_ALLOW;
315 user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
316 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());
318 Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
320 return MOD_RES_ALLOW;
326 return MOD_RES_PASSTHRU;
329 virtual ~ModuleBanRedirect()
331 ServerInstance->Modes->DelModeWatcher(&re);
334 virtual Version GetVersion()
336 return Version("$Id$", VF_COMMON|VF_VENDOR, API_VERSION);
341 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
342 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
346 MODULE_INIT(ModuleBanRedirect)