]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Actually use irc::modestacker correctly
[user/henk/code/inspircd.git] / src / modules / m_banredirect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 /* $ModDep: ../../include/u_listmode.h */
19
20 /* Originally written by Om, January 2009
21  */
22
23 class BanRedirectEntry : public classbase
24 {
25  public:
26         std::string targetchan;
27         std::string banmask;
28
29         BanRedirectEntry(const std::string &target = "", const std::string &mask = "")
30         : targetchan(target), banmask(mask)
31         {
32         }
33 };
34
35 typedef std::vector<BanRedirectEntry> BanRedirectList;
36 typedef std::deque<std::string> StringDeque;
37
38 class BanRedirect : public ModeWatcher
39 {
40  public:
41         BanRedirect(InspIRCd* Instance)
42         : ModeWatcher(Instance, 'b', MODETYPE_CHANNEL)
43         {
44         }
45
46         bool BeforeMode(User* source, User* dest, Channel* channel, std::string &param, bool adding, ModeType type, bool)
47         {
48                 /* nick!ident@host -> nick!ident@host
49                  * nick!ident@host#chan -> nick!ident@host#chan
50                  * nick@host#chan -> nick!*@host#chan
51                  * nick!ident#chan -> nick!ident@*#chan
52                  * nick#chan -> nick!*@*#chan
53                  */
54
55                 if(channel && (type == MODETYPE_CHANNEL) && param.length())
56                 {
57                         BanRedirectList* redirects;
58
59                         std::string mask[4];
60                         enum { NICK, IDENT, HOST, CHAN } current = NICK;
61                         std::string::iterator start_pos = param.begin();
62                         long maxbans = channel->GetMaxBans();
63
64                         if (param.length() >= 2 && param[1] == ':')
65                                 return true;
66
67                         if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
68                         {
69                                 source->WriteNumeric(478, "%s %s :Channel ban list for %s is full (maximum entries for this channel is %ld)", source->nick.c_str(), channel->name.c_str(), channel->name.c_str(), 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                         /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
101                         if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
102                         {
103                                 /* std::string::swap() is fast - it runs in constant time */
104                                 mask[NICK].swap(mask[IDENT]);
105                         }
106
107                         for(int i = 0; i < 3; i++)
108                         {
109                                 if(mask[i].empty())
110                                 {
111                                         mask[i].assign("*");
112                                 }
113                         }
114
115                         param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
116
117                         if(mask[CHAN].length())
118                         {
119                                 if(!IS_LOCAL(source) || ServerInstance->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
120                                 {
121                                         if (assign(channel->name) == mask[CHAN])
122                                         {
123                                                 source->WriteNumeric(690, "%s %s :You cannot set a ban redirection to the channel the ban is on", source->nick.c_str(), channel->name.c_str());
124                                                 return false;
125                                         }
126                                         else
127                                         {
128                                                 if(adding)
129                                                 {
130                                                         /* It's a properly valid redirecting ban, and we're adding it */
131                                                         if(!channel->GetExt("banredirects", redirects))
132                                                         {
133                                                                 redirects = new BanRedirectList;
134                                                                 channel->Extend("banredirects", redirects);
135                                                         }
136
137                                                         /* Here 'param' doesn't have the channel on it yet */
138                                                         redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
139
140                                                         /* Now it does */
141                                                         param.append(mask[CHAN]);
142                                                 }
143                                                 else
144                                                 {
145                                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
146                                                         if(channel->GetExt("banredirects", redirects))
147                                                         {
148                                                                 /* But there were, so we need to remove the matching one if there is one */
149
150                                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
151                                                                 {
152                                                                         /* Ugly as fuck */
153                                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
154                                                                         {
155                                                                                 redirects->erase(redir);
156
157                                                                                 if(redirects->empty())
158                                                                                 {
159                                                                                         delete redirects;
160                                                                                         channel->Shrink("banredirects");
161                                                                                 }
162
163                                                                                 break;
164                                                                         }
165                                                                 }
166                                                         }
167
168                                                         /* Append the channel so the default +b handler can remove the entry too */
169                                                         param.append(mask[CHAN]);
170                                                 }
171                                         }
172                                 }
173                                 else
174                                 {
175                                         source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
176                                         return false;
177                                 }
178                         }
179                 }
180
181                 return true;
182         }
183 };
184
185 class ModuleBanRedirect : public Module
186 {
187         BanRedirect* re;
188         bool nofollow;
189         Module* ExceptionModule;
190
191  public:
192         ModuleBanRedirect(InspIRCd* Me)
193         : Module(Me)
194         {
195                 re = new BanRedirect(Me);
196                 nofollow = false;
197
198                 if(!ServerInstance->Modes->AddModeWatcher(re))
199                 {
200                         delete re;
201                         throw ModuleException("Could not add mode watcher");
202                 }
203
204                 OnRehash(NULL, "");
205
206                 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
207                 Me->Modules->Attach(list, this, 4);
208
209         }
210
211         virtual void OnChannelDelete(Channel* 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                         Channel* chan = static_cast<Channel*>(item);
221                         BanRedirectList* redirects;
222
223                         if(chan->GetExt("banredirects", redirects))
224                         {
225                                 irc::modestacker modestack(ServerInstance, false);
226                                 StringDeque stackresult;
227                                 std::vector<std::string> mode_junk;
228                                 mode_junk.push_back(chan->name);
229
230                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
231                                 {
232                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
233                                 }
234
235                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
236                                 {
237                                         modestack.PushPlus();
238                                         modestack.Push('b', i->banmask);
239                                 }
240
241                                 while(modestack.GetStackedLine(stackresult))
242                                 {
243                                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
244                                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
245                                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
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, const std::string &keygiven)
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(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
294                                         {
295                                                 /* tell them they're banned and are being transferred */
296                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
297                                                 std::string destlimit;
298
299                                                 if (destchan)
300                                                         destlimit = destchan->GetModeParameter('l');
301
302                                                 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
303                                                 {
304                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
305                                                         return 1;
306                                                 }
307                                                 else
308                                                 {
309                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
310                                                         user->WriteNumeric(470, "%s %s %s :You are banned from this channel, so you are automatically transfered to the redirected channel.", user->nick.c_str(), chan->name.c_str(), redir->targetchan.c_str());
311                                                         nofollow = true;
312                                                         Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
313                                                         nofollow = false;
314                                                         return 1;
315                                                 }
316                                         }
317                                 }
318                         }
319                 }
320                 return 0;
321         }
322
323         virtual ~ModuleBanRedirect()
324         {
325                 ServerInstance->Modes->DelModeWatcher(re);
326                 delete re;
327         }
328
329         virtual Version GetVersion()
330         {
331                 return Version("$Id$", VF_COMMON|VF_VENDOR, API_VERSION);
332         }
333
334         void Prioritize()
335         {
336                 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
337                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
338         }
339 };
340
341 MODULE_INIT(ModuleBanRedirect)