]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[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)
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, 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 (assign(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
186                                         /* Here 'param' doesn't have the channel on it yet */
187                                         redirects->push_back(BanRedirectEntry(mask[CHAN], param));
188
189                                         /* Now it does */
190                                         param.append(mask[CHAN]);
191                                 }
192                                 else
193                                 {
194                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
195                                         redirects = extItem.get(channel);
196                                         if (redirects)
197                                         {
198                                                 /* But there were, so we need to remove the matching one if there is one */
199
200                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
201                                                 {
202                                                         /* Ugly as fuck */
203                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
204                                                         {
205                                                                 redirects->erase(redir);
206
207                                                                 if(redirects->empty())
208                                                                 {
209                                                                         extItem.unset(channel);
210                                                                 }
211
212                                                                 break;
213                                                         }
214                                                 }
215                                         }
216
217                                         /* Append the channel so the default +b handler can remove the entry too */
218                                         param.append(mask[CHAN]);
219                                 }
220                         }
221                 }
222
223                 return true;
224         }
225 };
226
227 class ModuleBanRedirect : public Module
228 {
229         BanRedirect re;
230         bool nofollow;
231         ChanModeReference limitmode;
232         ChanModeReference redirectmode;
233
234  public:
235         ModuleBanRedirect()
236                 : re(this)
237                 , nofollow(false)
238                 , limitmode(this, "limit")
239                 , redirectmode(this, "redirect")
240         {
241         }
242
243         void OnCleanup(int target_type, void* item) CXX11_OVERRIDE
244         {
245                 if(target_type == TYPE_CHANNEL)
246                 {
247                         Channel* chan = static_cast<Channel*>(item);
248                         BanRedirectList* redirects = re.extItem.get(chan);
249
250                         if(redirects)
251                         {
252                                 ModeHandler* ban = ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL);
253                                 Modes::ChangeList changelist;
254
255                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
256                                         changelist.push_remove(ban, i->targetchan.insert(0, i->banmask));
257
258                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
259                                         changelist.push_add(ban, i->banmask);
260
261                                 ServerInstance->Modes->Process(ServerInstance->FakeClient, chan, NULL, changelist, ModeParser::MODE_LOCALONLY);
262                         }
263                 }
264         }
265
266         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
267         {
268                 if (chan)
269                 {
270                         BanRedirectList* redirects = re.extItem.get(chan);
271
272                         if (redirects)
273                         {
274                                 /* We actually had some ban redirects to check */
275
276                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
277                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
278                                  */
279
280                                 ModResult result;
281                                 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
282                                 if (result == MOD_RES_ALLOW)
283                                 {
284                                         // they have a ban exception
285                                         return MOD_RES_PASSTHRU;
286                                 }
287
288                                 std::string ipmask(user->nick);
289                                 ipmask.append(1, '!').append(user->MakeHostIP());
290
291                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
292                                 {
293                                         if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
294                                         {
295                                                 /* This prevents recursion when a user sets multiple ban redirects in a chain
296                                                  * (thanks Potter)
297                                                  *
298                                                  * If we're here and nofollow is true then we're already redirecting this user
299                                                  * and there's a redirecting ban set on this channel that matches him, too.
300                                                  * Deny both joins.
301                                                  */
302                                                 if (nofollow)
303                                                         return MOD_RES_DENY;
304
305                                                 /* tell them they're banned and are being transferred */
306                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
307                                                 std::string destlimit;
308
309                                                 if (destchan)
310                                                         destlimit = destchan->GetModeParameter(limitmode);
311
312                                                 if(destchan && destchan->IsModeSet(redirectmode) && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
313                                                 {
314                                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (You are banned)");
315                                                         return MOD_RES_DENY;
316                                                 }
317                                                 else
318                                                 {
319                                                         user->WriteNumeric(ERR_BANNEDFROMCHAN, chan->name, "Cannot join channel (You are banned)");
320                                                         user->WriteNumeric(470, chan->name, redir->targetchan, "You are banned from this channel, so you are automatically transferred to the redirected channel.");
321                                                         nofollow = true;
322                                                         Channel::JoinUser(user, redir->targetchan);
323                                                         nofollow = false;
324                                                         return MOD_RES_DENY;
325                                                 }
326                                         }
327                                 }
328                         }
329                 }
330                 return MOD_RES_PASSTHRU;
331         }
332
333         Version GetVersion() CXX11_OVERRIDE
334         {
335                 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
336         }
337 };
338
339 MODULE_INIT(ModuleBanRedirect)