]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[user/henk/code/inspircd.git] / src / modules / m_banredirect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 "mode.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "inspircd.h"
19
20 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
21
22 /* Originally written by Om, January 2007
23  */
24
25 class BanRedirectEntry
26 {
27  public:
28         std::string targetchan;
29         std::string banmask;
30
31         BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
32         : targetchan(target), banmask(mask)
33         {
34         }
35 };
36
37 typedef std::vector<BanRedirectEntry> BanRedirectList;
38 typedef std::deque<std::string> StringDeque;
39
40 class BanRedirect : public ModeWatcher
41 {
42  private:
43         InspIRCd* Srv;
44  public:
45         BanRedirect(InspIRCd* Instance)
46         : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL), Srv(Instance)
47         {
48         }
49
50         bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &param, bool adding, ModeType type)
51         {
52                 /* nick!ident@host -> nick!ident@host
53                  * nick!ident@host#chan -> nick!ident@host#chan
54                  * nick@host#chan -> nick!*@host#chan
55                  * nick!ident#chan -> nick!ident@*#chan
56                  * nick#chan -> nick!*@*#chan
57                  */
58                 
59                 if(channel && (type == MODETYPE_CHANNEL) && param.length())
60                 {
61                         BanRedirectList* redirects;
62                         
63                         std::string mask[4];
64                         enum { NICK, IDENT, HOST, CHAN } current = NICK;
65                         std::string::iterator start_pos = param.begin();
66                         long maxbans = channel->GetMaxBans();
67                 
68                         if(channel->bans.size() > static_cast<unsigned>(maxbans))
69                         {
70                                 source->WriteServ("478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)", source->nick, channel->name, channel->name, 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                                                 mask[current].assign(start_pos, curr);
90                                                 current = CHAN;
91                                                 start_pos = curr;
92                                                 break;
93                                 }
94                         }
95                                 
96                         if(mask[current].empty())
97                         {
98                                 mask[current].assign(start_pos, param.end());   
99                         }
100                         
101                         /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
102                         if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
103                         {
104                                 /* std::string::swap() is fast - it runs in constant time */
105                                 mask[NICK].swap(mask[IDENT]);
106                         }
107                                 
108                         for(int i = 0; i < 3; i++)
109                         {
110                                 if(mask[i].empty())
111                                 {
112                                         mask[i].assign("*");
113                                 }
114                         }
115                                 
116                         param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
117         
118                         if(mask[CHAN].length())
119                         {
120                                 if(Srv->IsChannel(mask[CHAN].c_str()))
121                                 {
122                                         if(irc::string(channel->name) == irc::string(mask[CHAN].c_str()))
123                                         {
124                                                 source->WriteServ("690 %s %s :You cannot set a ban redirection to the channel the ban is on", source->nick, channel->name);
125                                                 return false;
126                                         }
127                                         else
128                                         {
129                                                 if(adding)
130                                                 {
131                                                         /* It's a properly valid redirecting ban, and we're adding it */
132                                                         if(!channel->GetExt("banredirects", redirects))
133                                                         {
134                                                                 redirects = new BanRedirectList;
135                                                                 channel->Extend("banredirects", redirects);
136                                                         }
137                                 
138                                                         /* Here 'param' doesn't have the channel on it yet */
139                                                         redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
140                                                         
141                                                         /* Now it does */
142                                                         param.append(mask[CHAN]);
143                                                 }
144                                                 else
145                                                 {
146                                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
147                                                         if(channel->GetExt("banredirects", redirects))
148                                                         {
149                                                                 /* But there were, so we need to remove the matching one if there is one */
150                                         
151                                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
152                                                                 {
153                                                                         /* Ugly as fuck */
154                                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
155                                                                         {
156                                                                                 redirects->erase(redir);
157                                                                                 
158                                                                                 if(redirects->empty())
159                                                                                 {
160                                                                                         DELETE(redirects);
161                                                                                         channel->Shrink("banredirects");
162                                                                                 }
163                                                                                 
164                                                                                 break;
165                                                                         }
166                                                                 }
167                                                         }
168                                                         
169                                                         /* Append the channel so the default +b handler can remove the entry too */
170                                                         param.append(mask[CHAN]);
171                                                 }
172                                         }
173                                 }
174                                 else
175                                 {
176                                         source->WriteServ("403 %s %s :Invalid channel name in redirection (%s)", source->nick, channel->name, mask[CHAN].c_str());
177                                         return false;
178                                 }
179                         }
180                 }
181                 
182                 return true;
183         }
184 };
185
186 class ModuleBanRedirect : public Module
187 {
188         BanRedirect* re;
189         InspIRCd* Srv;
190         bool nofollow;
191         
192  public:
193         ModuleBanRedirect(InspIRCd* Me)
194         : Module(Me), Srv(Me)
195         {
196                 re = new BanRedirect(Me);
197                 nofollow = false;
198                 
199                 if(!Srv->AddModeWatcher(re))
200                         throw ModuleException("Could not add mode watcher");
201         }
202         
203         void Implements(char* List)
204         {
205                 List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
206         }
207         
208         virtual void OnChannelDelete(chanrec* chan)
209         {
210                 OnCleanup(TYPE_CHANNEL, chan);
211         }
212         
213         virtual void OnCleanup(int target_type, void* item)
214         {
215                 if(target_type == TYPE_CHANNEL)
216                 {
217                         chanrec* chan = static_cast<chanrec*>(item);
218                         BanRedirectList* redirects;
219                         
220                         if(chan->GetExt("banredirects", redirects))
221                         {
222                                 irc::modestacker modestack(false);
223                                 StringDeque stackresult;
224                                 const char* mode_junk[MAXMODES+1];
225                                 userrec* myhorriblefakeuser = new userrec(Srv);
226                                 myhorriblefakeuser->SetFd(FD_MAGIC_NUMBER);
227                                 
228                                 mode_junk[0] = chan->name;
229                                 
230                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
231                                 {
232                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
233                                 }
234                                 
235                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
236                                 {
237                                         modestack.PushPlus();
238                                         modestack.Push('b', i->banmask);
239                                 }
240                                 
241                                 while(modestack.GetStackedLine(stackresult))
242                                 {
243                                         for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
244                                         {
245                                                 mode_junk[i+1] = stackresult[i].c_str();
246                                         }
247                                         
248                                         Srv->SendMode(mode_junk, stackresult.size() + 1, myhorriblefakeuser);
249                                 }
250                                 
251                                 DELETE(myhorriblefakeuser);
252                                 DELETE(redirects);
253                                 chan->Shrink("banredirects");
254                         }
255                 }
256         }
257
258         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
259         {
260                 /* This prevents recursion when a user sets multiple ban redirects in a chain
261                  * (thanks Potter)
262                  */
263                 if (nofollow)
264                         return 0;
265
266                 /* Return 1 to prevent the join, 0 to allow it */
267                 if (chan)
268                 {
269                         BanRedirectList* redirects;
270                         
271                         if(chan->GetExt("banredirects", redirects))
272                         {
273                                 /* We actually had some ban redirects to check */
274                                 
275                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
276                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
277                                  */
278                                 std::string ipmask(user->nick);
279                                 ipmask.append(1, '!').append(user->MakeHostIP());
280                                 
281                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
282                                 {
283                                         if(Srv->MatchText(user->GetFullRealHost(), redir->banmask) || Srv->MatchText(user->GetFullHost(), redir->banmask) || Srv->MatchText(ipmask, redir->banmask))
284                                         {
285                                                 /* tell them they're banned and are being transferred */
286                                                 chanrec* destchan = Srv->FindChan(redir->targetchan);
287                                                 
288                                                 if(destchan && Srv->FindModule("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
289                                                 {
290                                                         user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
291                                                         return 1;
292                                                 }
293                                                 else
294                                                 {
295                                                         user->WriteServ("470 %s :You are banned from %s. You are being automatically redirected to %s", user->nick, chan->name, redir->targetchan.c_str());
296                                                         nofollow = true;
297                                                         chanrec::JoinUser(Srv, user, redir->targetchan.c_str(), false, "", ServerInstance->Time(true));
298                                                         nofollow = false;
299                                                         return 1;
300                                                 }
301                                         }
302                                 }
303                         }
304                 }
305                 return 0;
306         }
307
308         virtual ~ModuleBanRedirect()
309         {
310                 Srv->Modes->DelModeWatcher(re);
311                 DELETE(re);
312         }
313         
314         virtual Version GetVersion()
315         {
316                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
317         }
318 };
319
320
321 class ModuleBanRedirectFactory : public ModuleFactory
322 {
323  public:
324         ModuleBanRedirectFactory()
325         {
326         }
327         
328         ~ModuleBanRedirectFactory()
329         {
330         }
331         
332         virtual Module * CreateModule(InspIRCd* Me)
333         {
334                 return new ModuleBanRedirect(Me);
335         }
336         
337 };
338
339
340 extern "C" DllExport void * init_module( void )
341 {
342         return new ModuleBanRedirectFactory;
343 }