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