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