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