]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Merge branch 'insp20' into insp3.
[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 "listmode.h"
27
28 /* Originally written by Om, January 2009
29  */
30
31 class BanRedirectEntry
32 {
33  public:
34         std::string targetchan;
35         std::string banmask;
36
37         BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
38         : targetchan(target), banmask(mask)
39         {
40         }
41 };
42
43 typedef std::vector<BanRedirectEntry> BanRedirectList;
44
45 class BanRedirect : public ModeWatcher
46 {
47         ChanModeReference ban;
48  public:
49         SimpleExtItem<BanRedirectList> extItem;
50         BanRedirect(Module* parent)
51                 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
52                 , ban(parent, "ban")
53                 , extItem("banredirect", ExtensionItem::EXT_CHANNEL, parent)
54         {
55         }
56
57         bool BeforeMode(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE
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) && !param.empty())
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
74                         if (param.length() >= 2 && param[1] == ':')
75                                 return true;
76
77                         if (param.find('#') == std::string::npos)
78                                 return true;
79
80                         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
81                         unsigned int maxbans = banlm->GetLimit(channel);
82                         ListModeBase::ModeList* list = banlm->GetList(channel);
83                         if ((list) && (adding) && (maxbans <= list->size()))
84                         {
85                                 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));
86                                 return false;
87                         }
88
89                         for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
90                         {
91                                 switch(*curr)
92                                 {
93                                         case '!':
94                                                 if (current != NICK)
95                                                         break;
96                                                 mask[current].assign(start_pos, curr);
97                                                 current = IDENT;
98                                                 start_pos = curr+1;
99                                                 break;
100                                         case '@':
101                                                 if (current != IDENT)
102                                                         break;
103                                                 mask[current].assign(start_pos, curr);
104                                                 current = HOST;
105                                                 start_pos = curr+1;
106                                                 break;
107                                         case '#':
108                                                 if (current == CHAN)
109                                                         break;
110                                                 mask[current].assign(start_pos, curr);
111                                                 current = CHAN;
112                                                 start_pos = curr;
113                                                 break;
114                                 }
115                         }
116
117                         if(mask[current].empty())
118                         {
119                                 mask[current].assign(start_pos, param.end());
120                         }
121
122                         /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
123                         if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
124                         {
125                                 /* std::string::swap() is fast - it runs in constant time */
126                                 mask[NICK].swap(mask[IDENT]);
127                         }
128
129                         if (!mask[NICK].empty() && mask[IDENT].empty() && mask[HOST].empty())
130                         {
131                                 if (mask[NICK].find('.') != std::string::npos || mask[NICK].find(':') != std::string::npos)
132                                 {
133                                         mask[NICK].swap(mask[HOST]);
134                                 }
135                         }
136
137                         for(int i = 0; i < 3; i++)
138                         {
139                                 if(mask[i].empty())
140                                 {
141                                         mask[i].assign("*");
142                                 }
143                         }
144
145                         param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
146
147                         if(mask[CHAN].length())
148                         {
149                                 if (adding && IS_LOCAL(source))
150                                 {
151                                         if (!ServerInstance->IsChannel(mask[CHAN]))
152                                         {
153                                                 source->WriteNumeric(ERR_NOSUCHCHANNEL, channel->name, InspIRCd::Format("Invalid channel name in redirection (%s)", mask[CHAN].c_str()));
154                                                 return false;
155                                         }
156
157                                         Channel *c = ServerInstance->FindChan(mask[CHAN]);
158                                         if (!c)
159                                         {
160                                                 source->WriteNumeric(690, InspIRCd::Format("Target channel %s must exist to be set as a redirect.", mask[CHAN].c_str()));
161                                                 return false;
162                                         }
163                                         else if (adding && c->GetPrefixValue(source) < OP_VALUE)
164                                         {
165                                                 source->WriteNumeric(690, InspIRCd::Format("You must be opped on %s to set it as a redirect.", mask[CHAN].c_str()));
166                                                 return false;
167                                         }
168
169                                         if (irc::equals(channel->name, mask[CHAN]))
170                                         {
171                                                 source->WriteNumeric(690, channel->name, "You cannot set a ban redirection to the channel the ban is on");
172                                                 return false;
173                                         }
174                                 }
175
176                                 if(adding)
177                                 {
178                                         /* It's a properly valid redirecting ban, and we're adding it */
179                                         redirects = extItem.get(channel);
180                                         if (!redirects)
181                                         {
182                                                 redirects = new BanRedirectList;
183                                                 extItem.set(channel, redirects);
184                                         }
185                                         else
186                                         {
187                                                 for (BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); ++redir)
188                                                 {
189                                                         // Mimic the functionality used when removing the mode
190                                                         if (irc::equals(redir->targetchan, mask[CHAN]) && irc::equals(redir->banmask, param))
191                                                         {
192                                                                 // Make sure the +b handler will still set the right ban
193                                                                 param.append(mask[CHAN]);
194                                                                 // Silently ignore the duplicate and don't set metadata
195                                                                 // This still allows channel ops to set/unset a redirect ban to clear "ghost" redirects
196                                                                 return true;
197                                                         }
198                                                 }
199                                         }
200
201                                         /* Here 'param' doesn't have the channel on it yet */
202                                         redirects->push_back(BanRedirectEntry(mask[CHAN], param));
203
204                                         /* Now it does */
205                                         param.append(mask[CHAN]);
206                                 }
207                                 else
208                                 {
209                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
210                                         redirects = extItem.get(channel);
211                                         if (redirects)
212                                         {
213                                                 /* But there were, so we need to remove the matching one if there is one */
214
215                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
216                                                 {
217                                                         if ((irc::equals(redir->targetchan, mask[CHAN])) && (irc::equals(redir->banmask, param)))
218                                                         {
219                                                                 redirects->erase(redir);
220
221                                                                 if(redirects->empty())
222                                                                 {
223                                                                         extItem.unset(channel);
224                                                                 }
225
226                                                                 break;
227                                                         }
228                                                 }
229                                         }
230
231                                         /* Append the channel so the default +b handler can remove the entry too */
232                                         param.append(mask[CHAN]);
233                                 }
234                         }
235                 }
236
237                 return true;
238         }
239 };
240
241 class ModuleBanRedirect : public Module
242 {
243         BanRedirect re;
244         bool nofollow;
245         ChanModeReference limitmode;
246         ChanModeReference redirectmode;
247
248  public:
249         ModuleBanRedirect()
250                 : re(this)
251                 , nofollow(false)
252                 , limitmode(this, "limit")
253                 , redirectmode(this, "redirect")
254         {
255         }
256
257         void OnCleanup(ExtensionItem::ExtensibleType type, Extensible* item) CXX11_OVERRIDE
258         {
259                 if (type == ExtensionItem::EXT_CHANNEL)
260                 {
261                         Channel* chan = static_cast<Channel*>(item);
262                         BanRedirectList* redirects = re.extItem.get(chan);
263
264                         if(redirects)
265                         {
266                                 ModeHandler* ban = ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL);
267                                 Modes::ChangeList changelist;
268
269                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
270                                         changelist.push_remove(ban, i->targetchan.insert(0, i->banmask));
271
272                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
273                                         changelist.push_add(ban, i->banmask);
274
275                                 ServerInstance->Modes->Process(ServerInstance->FakeClient, chan, NULL, changelist, ModeParser::MODE_LOCALONLY);
276                         }
277                 }
278         }
279
280         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
281         {
282                 if (chan)
283                 {
284                         BanRedirectList* redirects = re.extItem.get(chan);
285
286                         if (redirects)
287                         {
288                                 /* We actually had some ban redirects to check */
289
290                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
291                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
292                                  */
293
294                                 ModResult result;
295                                 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
296                                 if (result == MOD_RES_ALLOW)
297                                 {
298                                         // they have a ban exception
299                                         return MOD_RES_PASSTHRU;
300                                 }
301
302                                 std::string ipmask(user->nick);
303                                 ipmask.append(1, '!').append(user->MakeHostIP());
304
305                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
306                                 {
307                                         if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
308                                         {
309                                                 /* This prevents recursion when a user sets multiple ban redirects in a chain
310                                                  * (thanks Potter)
311                                                  *
312                                                  * If we're here and nofollow is true then we're already redirecting this user
313                                                  * and there's a redirecting ban set on this channel that matches him, too.
314                                                  * Deny both joins.
315                                                  */
316                                                 if (nofollow)
317                                                         return MOD_RES_DENY;
318
319                                                 /* tell them they're banned and are being transferred */
320                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
321                                                 std::string destlimit;
322
323                                                 if (destchan)
324                                                         destlimit = destchan->GetModeParameter(limitmode);
325
326                                                 if(destchan && destchan->IsModeSet(redirectmode) && !destlimit.empty() && (destchan->GetUserCounter() >= ConvToNum<size_t>(destlimit)))
327                                                 {
328                                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
329                                                         return MOD_RES_DENY;
330                                                 }
331                                                 else
332                                                 {
333                                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (you're banned)");
334                                                         user->WriteNumeric(470, chan->name, redir->targetchan, "You are banned from this channel, so you are automatically being transferred to the redirected channel.");
335                                                         nofollow = true;
336                                                         Channel::JoinUser(user, redir->targetchan);
337                                                         nofollow = false;
338                                                         return MOD_RES_DENY;
339                                                 }
340                                         }
341                                 }
342                         }
343                 }
344                 return MOD_RES_PASSTHRU;
345         }
346
347         Version GetVersion() CXX11_OVERRIDE
348         {
349                 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
350         }
351 };
352
353 MODULE_INIT(ModuleBanRedirect)