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