]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Convert more modules
[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 "inspircd.h"
15 #include "mode.h"
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "u_listmode.h"
20
21 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
22
23 /* Originally written by Om, January 2007
24  */
25
26 class BanRedirectEntry
27 {
28  public:
29         std::string targetchan;
30         std::string banmask;
31
32         BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
33         : targetchan(target), banmask(mask)
34         {
35         }
36 };
37
38 typedef std::vector<BanRedirectEntry> BanRedirectList;
39 typedef std::deque<std::string> StringDeque;
40
41 class BanRedirect : public ModeWatcher
42 {
43  private:
44         InspIRCd* Srv;
45  public:
46         BanRedirect(InspIRCd* Instance)
47         : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL), Srv(Instance)
48         {
49         }
50
51         bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &param, bool adding, ModeType type)
52         {
53                 /* nick!ident@host -> nick!ident@host
54                  * nick!ident@host#chan -> nick!ident@host#chan
55                  * nick@host#chan -> nick!*@host#chan
56                  * nick!ident#chan -> nick!ident@*#chan
57                  * nick#chan -> nick!*@*#chan
58                  */
59                 
60                 if(channel && (type == MODETYPE_CHANNEL) && param.length())
61                 {
62                         BanRedirectList* redirects;
63                         
64                         std::string mask[4];
65                         enum { NICK, IDENT, HOST, CHAN } current = NICK;
66                         std::string::iterator start_pos = param.begin();
67                         long maxbans = channel->GetMaxBans();
68                 
69                         if(channel->bans.size() > static_cast<unsigned>(maxbans))
70                         {
71                                 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);
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(Srv->IsChannel(mask[CHAN].c_str()))
122                                 {
123                                         if(irc::string(channel->name) == irc::string(mask[CHAN].c_str()))
124                                         {
125                                                 source->WriteServ("690 %s %s :You cannot set a ban redirection to the channel the ban is on", source->nick, channel->name);
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->WriteServ("403 %s %s :Invalid channel name in redirection (%s)", source->nick, channel->name, 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->AddModeWatcher(re))
201                         throw ModuleException("Could not add mode watcher");
202
203                 OnRehash(NULL, "");
204         }
205         
206         void Implements(char* List)
207         {
208                 List[I_OnRehash] = List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
209         }
210         
211         virtual void OnChannelDelete(chanrec* chan)
212         {
213                 OnCleanup(TYPE_CHANNEL, chan);
214         }
215         
216         virtual void OnCleanup(int target_type, void* item)
217         {
218                 if(target_type == TYPE_CHANNEL)
219                 {
220                         chanrec* chan = static_cast<chanrec*>(item);
221                         BanRedirectList* redirects;
222                         
223                         if(chan->GetExt("banredirects", redirects))
224                         {
225                                 irc::modestacker modestack(false);
226                                 StringDeque stackresult;
227                                 const char* mode_junk[MAXMODES+2];
228                                 userrec* myhorriblefakeuser = new userrec(ServerInstance);
229                                 myhorriblefakeuser->SetFd(FD_MAGIC_NUMBER);
230                                 
231                                 mode_junk[0] = chan->name;
232                                 
233                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
234                                 {
235                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
236                                 }
237                                 
238                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
239                                 {
240                                         modestack.PushPlus();
241                                         modestack.Push('b', i->banmask);
242                                 }
243                                 
244                                 while(modestack.GetStackedLine(stackresult))
245                                 {
246                                         for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
247                                         {
248                                                 mode_junk[i+1] = stackresult[i].c_str();
249                                         }
250                                         
251                                         ServerInstance->SendMode(mode_junk, stackresult.size() + 1, myhorriblefakeuser);
252                                 }
253                                 
254                                 DELETE(myhorriblefakeuser);
255                                 DELETE(redirects);
256                                 chan->Shrink("banredirects");
257                         }
258                 }
259         }
260
261         virtual void OnRehash(userrec* user, const std::string &param)
262         {
263                 ExceptionModule = ServerInstance->FindModule("m_banexception.so");
264         }
265
266         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
267         {
268                 /* This prevents recursion when a user sets multiple ban redirects in a chain
269                  * (thanks Potter)
270                  */
271                 if (nofollow)
272                         return 0;
273
274                 /* Return 1 to prevent the join, 0 to allow it */
275                 if (chan)
276                 {
277                         BanRedirectList* redirects;
278                         
279                         if(chan->GetExt("banredirects", redirects))
280                         {
281                                 /* We actually had some ban redirects to check */
282                                 
283                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
284                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
285                                  */
286
287                                 if (ExceptionModule)
288                                 {
289                                         ListModeRequest n(this, ExceptionModule, user, chan);
290                                         /* Users with ban exceptions are allowed to join without being redirected */
291                                         if (n.Send())
292                                                 return 0;
293                                 }
294
295                                 std::string ipmask(user->nick);
296                                 ipmask.append(1, '!').append(user->MakeHostIP());
297                                 
298                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
299                                 {
300                                         if(ServerInstance->MatchText(user->GetFullRealHost(), redir->banmask) || ServerInstance->MatchText(user->GetFullHost(), redir->banmask) || ServerInstance->MatchText(ipmask, redir->banmask))
301                                         {
302                                                 /* tell them they're banned and are being transferred */
303                                                 chanrec* destchan = ServerInstance->FindChan(redir->targetchan);
304                                                 
305                                                 if(destchan && ServerInstance->FindModule("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
306                                                 {
307                                                         user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
308                                                         return 1;
309                                                 }
310                                                 else
311                                                 {
312                                                         user->WriteServ("470 %s :You are banned from %s. You are being automatically redirected to %s", user->nick, chan->name, redir->targetchan.c_str());
313                                                         nofollow = true;
314                                                         chanrec::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", ServerInstance->Time(true));
315                                                         nofollow = false;
316                                                         return 1;
317                                                 }
318                                         }
319                                 }
320                         }
321                 }
322                 return 0;
323         }
324
325         virtual ~ModuleBanRedirect()
326         {
327                 ServerInstance->Modes->DelModeWatcher(re);
328                 DELETE(re);
329         }
330         
331         virtual Version GetVersion()
332         {
333                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
334         }
335         
336         Priority Prioritize()
337         {
338                 return (Priority)ServerInstance->PriorityBefore("m_banexception.so");
339         }
340 };
341
342
343 MODULE_INIT(ModuleBanRedirect);