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