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