]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Fix BanCache entries existing after X-line expiry.
[user/henk/code/inspircd.git] / src / modules / m_banredirect.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007, 2009 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
9  *   Copyright (C) 2007 Oliver Lupton <oliverlupton@gmail.com>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26 #include "u_listmode.h"
27
28 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
29
30 /* Originally written by Om, January 2009
31  */
32
33 class BanRedirectEntry
34 {
35  public:
36         std::string targetchan;
37         std::string banmask;
38
39         BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
40         : targetchan(target), banmask(mask)
41         {
42         }
43 };
44
45 typedef std::vector<BanRedirectEntry> BanRedirectList;
46 typedef std::deque<std::string> StringDeque;
47
48 class BanRedirect : public ModeWatcher
49 {
50  public:
51         SimpleExtItem<BanRedirectList> extItem;
52         BanRedirect(Module* parent) : ModeWatcher(parent, 'b', MODETYPE_CHANNEL),
53                 extItem("banredirect", parent)
54         {
55         }
56
57         bool BeforeMode(User* source, User* dest, Channel* channel, std::string &param, bool adding, ModeType type)
58         {
59                 /* nick!ident@host -> nick!ident@host
60                  * nick!ident@host#chan -> nick!ident@host#chan
61                  * nick@host#chan -> nick!*@host#chan
62                  * nick!ident#chan -> nick!ident@*#chan
63                  * nick#chan -> nick!*@*#chan
64                  */
65
66                 if(channel && (type == MODETYPE_CHANNEL) && param.length())
67                 {
68                         BanRedirectList* redirects;
69
70                         std::string mask[4];
71                         enum { NICK, IDENT, HOST, CHAN } current = NICK;
72                         std::string::iterator start_pos = param.begin();
73                         long maxbans = channel->GetMaxBans();
74
75                         if (param.length() >= 2 && param[1] == ':')
76                                 return true;
77
78                         if (param.find('#') == std::string::npos)
79                                 return true;
80
81                         if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
82                         {
83                                 source->WriteNumeric(478, "%s %s %c :Channel ban list for %s is full (maximum entries for this channel is %ld)",
84                                         source->nick.c_str(), channel->name.c_str(), mode, channel->name.c_str(), maxbans);
85                                 return false;
86                         }
87
88                         for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
89                         {
90                                 switch(*curr)
91                                 {
92                                         case '!':
93                                                 mask[current].assign(start_pos, curr);
94                                                 current = IDENT;
95                                                 start_pos = curr+1;
96                                                 break;
97                                         case '@':
98                                                 mask[current].assign(start_pos, curr);
99                                                 current = HOST;
100                                                 start_pos = curr+1;
101                                                 break;
102                                         case '#':
103                                                 /* bug #921: don't barf when redirecting to ## channels */
104                                                 if (current != CHAN)
105                                                 {
106                                                         mask[current].assign(start_pos, curr);
107                                                         current = CHAN;
108                                                         start_pos = curr;
109                                                 }
110                                                 break;
111                                 }
112                         }
113
114                         if(mask[current].empty())
115                         {
116                                 mask[current].assign(start_pos, param.end());
117                         }
118
119                         /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
120                         if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
121                         {
122                                 /* std::string::swap() is fast - it runs in constant time */
123                                 mask[NICK].swap(mask[IDENT]);
124                         }
125
126                         if (!mask[NICK].empty() && mask[IDENT].empty() && mask[HOST].empty())
127                         {
128                                 if (mask[NICK].find('.') != std::string::npos || mask[NICK].find(':') != std::string::npos)
129                                 {
130                                         mask[NICK].swap(mask[HOST]);
131                                 }
132                         }
133
134                         for(int i = 0; i < 3; i++)
135                         {
136                                 if(mask[i].empty())
137                                 {
138                                         mask[i].assign("*");
139                                 }
140                         }
141
142                         param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
143
144                         if(mask[CHAN].length())
145                         {
146                                 if (adding && IS_LOCAL(source))
147                                 {
148                                         if (!ServerInstance->IsChannel(mask[CHAN].c_str(),  ServerInstance->Config->Limits.ChanMax))
149                                         {
150                                                 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
151                                                 return false;
152                                         }
153
154                                         Channel *c = ServerInstance->FindChan(mask[CHAN]);
155                                         if (!c)
156                                         {
157                                                 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
158                                                 return false;
159                                         }
160                                         else if (adding && c->GetPrefixValue(source) < OP_VALUE)
161                                         {
162                                                 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
163                                                 return false;
164                                         }
165
166                                         if (assign(channel->name) == mask[CHAN])
167                                         {
168                                                 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());
169                                                 return false;
170                                         }
171                                 }
172
173                                 if(adding)
174                                 {
175                                         /* It's a properly valid redirecting ban, and we're adding it */
176                                         redirects = extItem.get(channel);
177                                         if (!redirects)
178                                         {
179                                                 redirects = new BanRedirectList;
180                                                 extItem.set(channel, redirects);
181                                         }
182                                         else
183                                         {
184                                                 for (BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); ++redir)
185                                                 {
186                                                         // Mimic the functionality used when removing the mode
187                                                         if ((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
188                                                         {
189                                                                 // Make sure the +b handler will still set the right ban
190                                                                 param.append(mask[CHAN]);
191                                                                 // Silently ignore the duplicate and don't set metadata
192                                                                 // This still allows channel ops to set/unset a redirect ban to clear "ghost" redirects
193                                                                 return true;
194                                                         }
195                                                 }
196                                         }
197
198                                         /* Here 'param' doesn't have the channel on it yet */
199                                         redirects->push_back(BanRedirectEntry(mask[CHAN], param));
200
201                                         /* Now it does */
202                                         param.append(mask[CHAN]);
203                                 }
204                                 else
205                                 {
206                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
207                                         redirects = extItem.get(channel);
208                                         if (redirects)
209                                         {
210                                                 /* But there were, so we need to remove the matching one if there is one */
211
212                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
213                                                 {
214                                                         /* Ugly as fuck */
215                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
216                                                         {
217                                                                 redirects->erase(redir);
218
219                                                                 if(redirects->empty())
220                                                                 {
221                                                                         extItem.unset(channel);
222                                                                 }
223
224                                                                 break;
225                                                         }
226                                                 }
227                                         }
228
229                                         /* Append the channel so the default +b handler can remove the entry too */
230                                         param.append(mask[CHAN]);
231                                 }
232                         }
233                 }
234
235                 return true;
236         }
237 };
238
239 class ModuleBanRedirect : public Module
240 {
241         BanRedirect re;
242         bool nofollow;
243
244  public:
245         ModuleBanRedirect()
246         : re(this)
247         {
248                 nofollow = false;
249         }
250
251
252         void init()
253         {
254                 if(!ServerInstance->Modes->AddModeWatcher(&re))
255                         throw ModuleException("Could not add mode watcher");
256
257                 ServerInstance->Modules->AddService(re.extItem);
258                 Implementation list[] = { I_OnUserPreJoin };
259                 ServerInstance->Modules->Attach(list, this, sizeof(list)/sizeof(Implementation));
260         }
261
262         virtual void OnCleanup(int target_type, void* item)
263         {
264                 if(target_type == TYPE_CHANNEL)
265                 {
266                         Channel* chan = static_cast<Channel*>(item);
267                         BanRedirectList* redirects = re.extItem.get(chan);
268
269                         if(redirects)
270                         {
271                                 irc::modestacker modestack(false);
272                                 StringDeque stackresult;
273                                 std::vector<std::string> mode_junk;
274                                 mode_junk.push_back(chan->name);
275
276                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
277                                 {
278                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
279                                 }
280
281                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
282                                 {
283                                         modestack.PushPlus();
284                                         modestack.Push('b', i->banmask);
285                                 }
286
287                                 while(modestack.GetStackedLine(stackresult))
288                                 {
289                                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
290                                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
291                                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
292                                 }
293                         }
294                 }
295         }
296
297         virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
298         {
299                 if (chan)
300                 {
301                         BanRedirectList* redirects = re.extItem.get(chan);
302
303                         if (redirects)
304                         {
305                                 /* We actually had some ban redirects to check */
306
307                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
308                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
309                                  */
310
311                                 ModResult result;
312                                 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
313                                 if (result == MOD_RES_ALLOW)
314                                 {
315                                         // they have a ban exception
316                                         return MOD_RES_PASSTHRU;
317                                 }
318
319                                 std::string ipmask(user->nick);
320                                 ipmask.append(1, '!').append(user->MakeHostIP());
321
322                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
323                                 {
324                                         if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
325                                         {
326                                                 /* This prevents recursion when a user sets multiple ban redirects in a chain
327                                                  * (thanks Potter)
328                                                  *
329                                                  * If we're here and nofollow is true then we're already redirecting this user
330                                                  * and there's a redirecting ban set on this channel that matches him, too.
331                                                  * Deny both joins.
332                                                  */
333                                                 if (nofollow)
334                                                         return MOD_RES_DENY;
335
336                                                 /* tell them they're banned and are being transferred */
337                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
338                                                 std::string destlimit;
339
340                                                 if (destchan)
341                                                         destlimit = destchan->GetModeParameter('l');
342
343                                                 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
344                                                 {
345                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
346                                                         return MOD_RES_DENY;
347                                                 }
348                                                 else
349                                                 {
350                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
351                                                         user->WriteNumeric(470, "%s %s %s :You are banned from this channel, so you are automatically transferred to the redirected channel.", user->nick.c_str(), chan->name.c_str(), redir->targetchan.c_str());
352                                                         nofollow = true;
353                                                         Channel::JoinUser(user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
354                                                         nofollow = false;
355                                                         return MOD_RES_DENY;
356                                                 }
357                                         }
358                                 }
359                         }
360                 }
361                 return MOD_RES_PASSTHRU;
362         }
363
364         virtual ~ModuleBanRedirect()
365         {
366                 /* XXX is this the best place to do this? */
367                 if (!ServerInstance->Modes->DelModeWatcher(&re))
368                         ServerInstance->Logs->Log("m_banredirect.so", DEBUG, "Failed to delete modewatcher!");
369         }
370
371         virtual Version GetVersion()
372         {
373                 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
374         }
375 };
376
377 MODULE_INIT(ModuleBanRedirect)