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