]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Merge pull request #1458 from linuxdaemon/insp20+duplicate-redir-meta
[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 :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);
84                                 return false;
85                         }
86
87                         for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
88                         {
89                                 switch(*curr)
90                                 {
91                                         case '!':
92                                                 mask[current].assign(start_pos, curr);
93                                                 current = IDENT;
94                                                 start_pos = curr+1;
95                                                 break;
96                                         case '@':
97                                                 mask[current].assign(start_pos, curr);
98                                                 current = HOST;
99                                                 start_pos = curr+1;
100                                                 break;
101                                         case '#':
102                                                 /* bug #921: don't barf when redirecting to ## channels */
103                                                 if (current != CHAN)
104                                                 {
105                                                         mask[current].assign(start_pos, curr);
106                                                         current = CHAN;
107                                                         start_pos = curr;
108                                                 }
109                                                 break;
110                                 }
111                         }
112
113                         if(mask[current].empty())
114                         {
115                                 mask[current].assign(start_pos, param.end());
116                         }
117
118                         /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
119                         if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
120                         {
121                                 /* std::string::swap() is fast - it runs in constant time */
122                                 mask[NICK].swap(mask[IDENT]);
123                         }
124
125                         if (!mask[NICK].empty() && mask[IDENT].empty() && mask[HOST].empty())
126                         {
127                                 if (mask[NICK].find('.') != std::string::npos || mask[NICK].find(':') != std::string::npos)
128                                 {
129                                         mask[NICK].swap(mask[HOST]);
130                                 }
131                         }
132
133                         for(int i = 0; i < 3; i++)
134                         {
135                                 if(mask[i].empty())
136                                 {
137                                         mask[i].assign("*");
138                                 }
139                         }
140
141                         param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
142
143                         if(mask[CHAN].length())
144                         {
145                                 if (adding && IS_LOCAL(source))
146                                 {
147                                         if (!ServerInstance->IsChannel(mask[CHAN].c_str(),  ServerInstance->Config->Limits.ChanMax))
148                                         {
149                                                 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
150                                                 return false;
151                                         }
152
153                                         Channel *c = ServerInstance->FindChan(mask[CHAN]);
154                                         if (!c)
155                                         {
156                                                 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
157                                                 return false;
158                                         }
159                                         else if (adding && c->GetPrefixValue(source) < OP_VALUE)
160                                         {
161                                                 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
162                                                 return false;
163                                         }
164
165                                         if (assign(channel->name) == mask[CHAN])
166                                         {
167                                                 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());
168                                                 return false;
169                                         }
170                                 }
171
172                                 if(adding)
173                                 {
174                                         /* It's a properly valid redirecting ban, and we're adding it */
175                                         redirects = extItem.get(channel);
176                                         if (!redirects)
177                                         {
178                                                 redirects = new BanRedirectList;
179                                                 extItem.set(channel, redirects);
180                                         }
181                                         else
182                                         {
183                                                 for (BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); ++redir)
184                                                 {
185                                                         // Mimic the functionality used when removing the mode
186                                                         if ((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
187                                                         {
188                                                                 // Make sure the +b handler will still set the right ban
189                                                                 param.append(mask[CHAN]);
190                                                                 // Silently ignore the duplicate and don't set metadata
191                                                                 // This still allows channel ops to set/unset a redirect ban to clear "ghost" redirects
192                                                                 return true;
193                                                         }
194                                                 }
195                                         }
196
197                                         /* Here 'param' doesn't have the channel on it yet */
198                                         redirects->push_back(BanRedirectEntry(mask[CHAN], param));
199
200                                         /* Now it does */
201                                         param.append(mask[CHAN]);
202                                 }
203                                 else
204                                 {
205                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
206                                         redirects = extItem.get(channel);
207                                         if (redirects)
208                                         {
209                                                 /* But there were, so we need to remove the matching one if there is one */
210
211                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
212                                                 {
213                                                         /* Ugly as fuck */
214                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
215                                                         {
216                                                                 redirects->erase(redir);
217
218                                                                 if(redirects->empty())
219                                                                 {
220                                                                         extItem.unset(channel);
221                                                                 }
222
223                                                                 break;
224                                                         }
225                                                 }
226                                         }
227
228                                         /* Append the channel so the default +b handler can remove the entry too */
229                                         param.append(mask[CHAN]);
230                                 }
231                         }
232                 }
233
234                 return true;
235         }
236 };
237
238 class ModuleBanRedirect : public Module
239 {
240         BanRedirect re;
241         bool nofollow;
242
243  public:
244         ModuleBanRedirect()
245         : re(this)
246         {
247                 nofollow = false;
248         }
249
250
251         void init()
252         {
253                 if(!ServerInstance->Modes->AddModeWatcher(&re))
254                         throw ModuleException("Could not add mode watcher");
255
256                 ServerInstance->Modules->AddService(re.extItem);
257                 Implementation list[] = { I_OnUserPreJoin };
258                 ServerInstance->Modules->Attach(list, this, sizeof(list)/sizeof(Implementation));
259         }
260
261         virtual void OnCleanup(int target_type, void* item)
262         {
263                 if(target_type == TYPE_CHANNEL)
264                 {
265                         Channel* chan = static_cast<Channel*>(item);
266                         BanRedirectList* redirects = re.extItem.get(chan);
267
268                         if(redirects)
269                         {
270                                 irc::modestacker modestack(false);
271                                 StringDeque stackresult;
272                                 std::vector<std::string> mode_junk;
273                                 mode_junk.push_back(chan->name);
274
275                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
276                                 {
277                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
278                                 }
279
280                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
281                                 {
282                                         modestack.PushPlus();
283                                         modestack.Push('b', i->banmask);
284                                 }
285
286                                 while(modestack.GetStackedLine(stackresult))
287                                 {
288                                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
289                                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
290                                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
291                                 }
292                         }
293                 }
294         }
295
296         virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
297         {
298                 if (chan)
299                 {
300                         BanRedirectList* redirects = re.extItem.get(chan);
301
302                         if (redirects)
303                         {
304                                 /* We actually had some ban redirects to check */
305
306                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
307                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
308                                  */
309
310                                 ModResult result;
311                                 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
312                                 if (result == MOD_RES_ALLOW)
313                                 {
314                                         // they have a ban exception
315                                         return MOD_RES_PASSTHRU;
316                                 }
317
318                                 std::string ipmask(user->nick);
319                                 ipmask.append(1, '!').append(user->MakeHostIP());
320
321                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
322                                 {
323                                         if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
324                                         {
325                                                 /* This prevents recursion when a user sets multiple ban redirects in a chain
326                                                  * (thanks Potter)
327                                                  *
328                                                  * If we're here and nofollow is true then we're already redirecting this user
329                                                  * and there's a redirecting ban set on this channel that matches him, too.
330                                                  * Deny both joins.
331                                                  */
332                                                 if (nofollow)
333                                                         return MOD_RES_DENY;
334
335                                                 /* tell them they're banned and are being transferred */
336                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
337                                                 std::string destlimit;
338
339                                                 if (destchan)
340                                                         destlimit = destchan->GetModeParameter('l');
341
342                                                 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
343                                                 {
344                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
345                                                         return MOD_RES_DENY;
346                                                 }
347                                                 else
348                                                 {
349                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
350                                                         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());
351                                                         nofollow = true;
352                                                         Channel::JoinUser(user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
353                                                         nofollow = false;
354                                                         return MOD_RES_DENY;
355                                                 }
356                                         }
357                                 }
358                         }
359                 }
360                 return MOD_RES_PASSTHRU;
361         }
362
363         virtual ~ModuleBanRedirect()
364         {
365                 /* XXX is this the best place to do this? */
366                 if (!ServerInstance->Modes->DelModeWatcher(&re))
367                         ServerInstance->Logs->Log("m_banredirect.so", DEBUG, "Failed to delete modewatcher!");
368         }
369
370         virtual Version GetVersion()
371         {
372                 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
373         }
374 };
375
376 MODULE_INIT(ModuleBanRedirect)