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