]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
73d8270d1b098452caa55dc9fc1c886bba7825bd
[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", 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                         ListModeBase* banlm = static_cast<ListModeBase*>(*ban);
78                         unsigned int maxbans = banlm->GetLimit(channel);
79                         ListModeBase::ModeList* list = banlm->GetList(channel);
80                         if ((list) && (adding) && (maxbans <= list->size()))
81                         {
82                                 source->WriteNumeric(478, "%s %s :Channel ban list for %s is full (maximum entries for this channel is %u)", source->nick.c_str(), channel->name.c_str(), channel->name.c_str(), maxbans);
83                                 return false;
84                         }
85
86                         for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
87                         {
88                                 switch(*curr)
89                                 {
90                                         case '!':
91                                                 if (current != NICK)
92                                                         break;
93                                                 mask[current].assign(start_pos, curr);
94                                                 current = IDENT;
95                                                 start_pos = curr+1;
96                                                 break;
97                                         case '@':
98                                                 if (current != IDENT)
99                                                         break;
100                                                 mask[current].assign(start_pos, curr);
101                                                 current = HOST;
102                                                 start_pos = curr+1;
103                                                 break;
104                                         case '#':
105                                                 if (current == CHAN)
106                                                         break;
107                                                 mask[current].assign(start_pos, curr);
108                                                 current = CHAN;
109                                                 start_pos = curr;
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                         for(int i = 0; i < 3; i++)
127                         {
128                                 if(mask[i].empty())
129                                 {
130                                         mask[i].assign("*");
131                                 }
132                         }
133
134                         param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
135
136                         if(mask[CHAN].length())
137                         {
138                                 if (adding && IS_LOCAL(source))
139                                 {
140                                         if (!ServerInstance->IsChannel(mask[CHAN]))
141                                         {
142                                                 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
143                                                 return false;
144                                         }
145
146                                         Channel *c = ServerInstance->FindChan(mask[CHAN]);
147                                         if (!c)
148                                         {
149                                                 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
150                                                 return false;
151                                         }
152                                         else if (adding && c->GetPrefixValue(source) < OP_VALUE)
153                                         {
154                                                 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
155                                                 return false;
156                                         }
157
158                                         if (assign(channel->name) == mask[CHAN])
159                                         {
160                                                 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());
161                                                 return false;
162                                         }
163                                 }
164
165                                 if(adding)
166                                 {
167                                         /* It's a properly valid redirecting ban, and we're adding it */
168                                         redirects = extItem.get(channel);
169                                         if (!redirects)
170                                         {
171                                                 redirects = new BanRedirectList;
172                                                 extItem.set(channel, redirects);
173                                         }
174
175                                         /* Here 'param' doesn't have the channel on it yet */
176                                         redirects->push_back(BanRedirectEntry(mask[CHAN], param));
177
178                                         /* Now it does */
179                                         param.append(mask[CHAN]);
180                                 }
181                                 else
182                                 {
183                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
184                                         redirects = extItem.get(channel);
185                                         if (redirects)
186                                         {
187                                                 /* But there were, so we need to remove the matching one if there is one */
188
189                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
190                                                 {
191                                                         /* Ugly as fuck */
192                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
193                                                         {
194                                                                 redirects->erase(redir);
195
196                                                                 if(redirects->empty())
197                                                                 {
198                                                                         extItem.unset(channel);
199                                                                 }
200
201                                                                 break;
202                                                         }
203                                                 }
204                                         }
205
206                                         /* Append the channel so the default +b handler can remove the entry too */
207                                         param.append(mask[CHAN]);
208                                 }
209                         }
210                 }
211
212                 return true;
213         }
214 };
215
216 class ModuleBanRedirect : public Module
217 {
218         BanRedirect re;
219         bool nofollow;
220         ChanModeReference limitmode;
221         ChanModeReference redirectmode;
222
223  public:
224         ModuleBanRedirect()
225                 : re(this)
226                 , nofollow(false)
227                 , limitmode(this, "limit")
228                 , redirectmode(this, "redirect")
229         {
230         }
231
232         void init() CXX11_OVERRIDE
233         {
234                 ServerInstance->Modes->AddModeWatcher(&re);
235                 ServerInstance->Modules->AddService(re.extItem);
236         }
237
238         void OnCleanup(int target_type, void* item) CXX11_OVERRIDE
239         {
240                 if(target_type == TYPE_CHANNEL)
241                 {
242                         Channel* chan = static_cast<Channel*>(item);
243                         BanRedirectList* redirects = re.extItem.get(chan);
244
245                         if(redirects)
246                         {
247                                 irc::modestacker modestack(false);
248
249                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
250                                 {
251                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
252                                 }
253
254                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
255                                 {
256                                         modestack.PushPlus();
257                                         modestack.Push('b', i->banmask);
258                                 }
259
260                                 std::vector<std::string> stackresult;
261                                 stackresult.push_back(chan->name);
262                                 while (modestack.GetStackedLine(stackresult))
263                                 {
264                                         ServerInstance->Modes->Process(stackresult, ServerInstance->FakeClient, ModeParser::MODE_LOCALONLY);
265                                         stackresult.erase(stackresult.begin() + 1, stackresult.end());
266                                 }
267                         }
268                 }
269         }
270
271         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
272         {
273                 if (chan)
274                 {
275                         BanRedirectList* redirects = re.extItem.get(chan);
276
277                         if (redirects)
278                         {
279                                 /* We actually had some ban redirects to check */
280
281                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
282                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
283                                  */
284
285                                 ModResult result;
286                                 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
287                                 if (result == MOD_RES_ALLOW)
288                                 {
289                                         // they have a ban exception
290                                         return MOD_RES_PASSTHRU;
291                                 }
292
293                                 std::string ipmask(user->nick);
294                                 ipmask.append(1, '!').append(user->MakeHostIP());
295
296                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
297                                 {
298                                         if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
299                                         {
300                                                 /* This prevents recursion when a user sets multiple ban redirects in a chain
301                                                  * (thanks Potter)
302                                                  *
303                                                  * If we're here and nofollow is true then we're already redirecting this user
304                                                  * and there's a redirecting ban set on this channel that matches him, too.
305                                                  * Deny both joins.
306                                                  */
307                                                 if (nofollow)
308                                                         return MOD_RES_DENY;
309
310                                                 /* tell them they're banned and are being transferred */
311                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
312                                                 std::string destlimit;
313
314                                                 if (destchan)
315                                                         destlimit = destchan->GetModeParameter(limitmode);
316
317                                                 if(destchan && destchan->IsModeSet(redirectmode) && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
318                                                 {
319                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
320                                                         return MOD_RES_DENY;
321                                                 }
322                                                 else
323                                                 {
324                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
325                                                         user->WriteNumeric(470, "%s %s %s :You are banned from this channel, so you are automatically transfered to the redirected channel.", user->nick.c_str(), chan->name.c_str(), redir->targetchan.c_str());
326                                                         nofollow = true;
327                                                         Channel::JoinUser(user, redir->targetchan);
328                                                         nofollow = false;
329                                                         return MOD_RES_DENY;
330                                                 }
331                                         }
332                                 }
333                         }
334                 }
335                 return MOD_RES_PASSTHRU;
336         }
337
338         ~ModuleBanRedirect()
339         {
340                 /* XXX is this the best place to do this? */
341                 if (!ServerInstance->Modes->DelModeWatcher(&re))
342                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Failed to delete modewatcher!");
343         }
344
345         Version GetVersion() CXX11_OVERRIDE
346         {
347                 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
348         }
349 };
350
351 MODULE_INIT(ModuleBanRedirect)