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