]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
In the grand tradition of huge fucking commits:
[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 "u_listmode.h"
16
17 /* $ModDesc: Allows an extended ban (+b) syntax redirecting banned users to another channel */
18
19 /* Originally written by Om, January 2007
20  */
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(User* source, User* dest, Channel* 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(adding && (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         bool nofollow;
187         Module* ExceptionModule;
188
189  public:
190         ModuleBanRedirect(InspIRCd* Me)
191         : Module(Me)
192         {
193                 re = new BanRedirect(Me);
194                 nofollow = false;
195                 
196                 if(!ServerInstance->AddModeWatcher(re))
197                         throw ModuleException("Could not add mode watcher");
198
199                 OnRehash(NULL, "");
200         }
201         
202         void Implements(char* List)
203         {
204                 List[I_OnRehash] = List[I_OnUserPreJoin] = List[I_OnChannelDelete] = List[I_OnCleanup] = 1;
205         }
206         
207         virtual void OnChannelDelete(Channel* chan)
208         {
209                 OnCleanup(TYPE_CHANNEL, chan);
210         }
211         
212         virtual void OnCleanup(int target_type, void* item)
213         {
214                 if(target_type == TYPE_CHANNEL)
215                 {
216                         Channel* chan = static_cast<Channel*>(item);
217                         BanRedirectList* redirects;
218                         
219                         if(chan->GetExt("banredirects", redirects))
220                         {
221                                 irc::modestacker modestack(false);
222                                 StringDeque stackresult;
223                                 const char* mode_junk[MAXMODES+2];
224                                 
225                                 mode_junk[0] = chan->name;
226                                 
227                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
228                                 {
229                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
230                                 }
231                                 
232                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
233                                 {
234                                         modestack.PushPlus();
235                                         modestack.Push('b', i->banmask);
236                                 }
237                                 
238                                 while(modestack.GetStackedLine(stackresult))
239                                 {
240                                         for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
241                                         {
242                                                 mode_junk[i+1] = stackresult[i].c_str();
243                                         }
244                                         
245                                         ServerInstance->SendMode(mode_junk, stackresult.size() + 1, ServerInstance->FakeClient);
246                                 }
247                                 
248                                 DELETE(redirects);
249                                 chan->Shrink("banredirects");
250                         }
251                 }
252         }
253
254         virtual void OnRehash(User* user, const std::string &param)
255         {
256                 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
257         }
258
259         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
260         {
261                 /* This prevents recursion when a user sets multiple ban redirects in a chain
262                  * (thanks Potter)
263                  */
264                 if (nofollow)
265                         return 0;
266
267                 /* Return 1 to prevent the join, 0 to allow it */
268                 if (chan)
269                 {
270                         BanRedirectList* redirects;
271                         
272                         if(chan->GetExt("banredirects", redirects))
273                         {
274                                 /* We actually had some ban redirects to check */
275                                 
276                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
277                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
278                                  */
279
280                                 if (ExceptionModule)
281                                 {
282                                         ListModeRequest n(this, ExceptionModule, user, chan);
283                                         /* Users with ban exceptions are allowed to join without being redirected */
284                                         if (n.Send())
285                                                 return 0;
286                                 }
287
288                                 std::string ipmask(user->nick);
289                                 ipmask.append(1, '!').append(user->MakeHostIP());
290                                 
291                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
292                                 {
293                                         if(ServerInstance->MatchText(user->GetFullRealHost(), redir->banmask) || ServerInstance->MatchText(user->GetFullHost(), redir->banmask) || ServerInstance->MatchText(ipmask, redir->banmask))
294                                         {
295                                                 /* tell them they're banned and are being transferred */
296                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
297                                                 
298                                                 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && destchan->limit && (destchan->GetUserCounter() >= destchan->limit))
299                                                 {
300                                                         user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
301                                                         return 1;
302                                                 }
303                                                 else
304                                                 {
305                                                         user->WriteServ("474 %s %s :Cannot join channel (You are banned)", user->nick, chan->name);
306                                                         user->WriteServ("470 %s :You are being automatically redirected to %s", user->nick, redir->targetchan.c_str());
307                                                         nofollow = true;
308                                                         Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", ServerInstance->Time(true));
309                                                         nofollow = false;
310                                                         return 1;
311                                                 }
312                                         }
313                                 }
314                         }
315                 }
316                 return 0;
317         }
318
319         virtual ~ModuleBanRedirect()
320         {
321                 ServerInstance->Modes->DelModeWatcher(re);
322                 DELETE(re);
323         }
324         
325         virtual Version GetVersion()
326         {
327                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
328         }
329         
330         Priority Prioritize()
331         {
332                 return (Priority)ServerInstance->Modules->PriorityBefore("m_banexception.so");
333         }
334 };
335
336
337 MODULE_INIT(ModuleBanRedirect)