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