]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Match conversion stuff.
[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 : public classbase
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, bool)
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 (param.length() >= 2 && param[1] == ':')
66                                 return true;
67
68                         if(adding && (channel->bans.size() > static_cast<unsigned>(maxbans)))
69                         {
70                                 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);
71                                 return false;
72                         }
73
74                         for(std::string::iterator curr = start_pos; curr != param.end(); curr++)
75                         {
76                                 switch(*curr)
77                                 {
78                                         case '!':
79                                                 mask[current].assign(start_pos, curr);
80                                                 current = IDENT;
81                                                 start_pos = curr+1;
82                                                 break;
83                                         case '@':
84                                                 mask[current].assign(start_pos, curr);
85                                                 current = HOST;
86                                                 start_pos = curr+1;
87                                                 break;
88                                         case '#':
89                                                 mask[current].assign(start_pos, curr);
90                                                 current = CHAN;
91                                                 start_pos = curr;
92                                                 break;
93                                 }
94                         }
95
96                         if(mask[current].empty())
97                         {
98                                 mask[current].assign(start_pos, param.end());
99                         }
100
101                         /* nick@host wants to be changed to *!nick@host rather than nick!*@host... */
102                         if(mask[NICK].length() && mask[HOST].length() && mask[IDENT].empty())
103                         {
104                                 /* std::string::swap() is fast - it runs in constant time */
105                                 mask[NICK].swap(mask[IDENT]);
106                         }
107
108                         for(int i = 0; i < 3; i++)
109                         {
110                                 if(mask[i].empty())
111                                 {
112                                         mask[i].assign("*");
113                                 }
114                         }
115
116                         param.assign(mask[NICK]).append(1, '!').append(mask[IDENT]).append(1, '@').append(mask[HOST]);
117
118                         if(mask[CHAN].length())
119                         {
120                                 if(!IS_LOCAL(source) || Srv->IsChannel(mask[CHAN].c_str(), ServerInstance->Config->Limits.ChanMax))
121                                 {
122                                         if (assign(channel->name) == mask[CHAN])
123                                         {
124                                                 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());
125                                                 return false;
126                                         }
127                                         else
128                                         {
129                                                 if(adding)
130                                                 {
131                                                         /* It's a properly valid redirecting ban, and we're adding it */
132                                                         if(!channel->GetExt("banredirects", redirects))
133                                                         {
134                                                                 redirects = new BanRedirectList;
135                                                                 channel->Extend("banredirects", redirects);
136                                                         }
137
138                                                         /* Here 'param' doesn't have the channel on it yet */
139                                                         redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
140
141                                                         /* Now it does */
142                                                         param.append(mask[CHAN]);
143                                                 }
144                                                 else
145                                                 {
146                                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
147                                                         if(channel->GetExt("banredirects", redirects))
148                                                         {
149                                                                 /* But there were, so we need to remove the matching one if there is one */
150
151                                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
152                                                                 {
153                                                                         /* Ugly as fuck */
154                                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
155                                                                         {
156                                                                                 redirects->erase(redir);
157
158                                                                                 if(redirects->empty())
159                                                                                 {
160                                                                                         delete redirects;
161                                                                                         channel->Shrink("banredirects");
162                                                                                 }
163
164                                                                                 break;
165                                                                         }
166                                                                 }
167                                                         }
168
169                                                         /* Append the channel so the default +b handler can remove the entry too */
170                                                         param.append(mask[CHAN]);
171                                                 }
172                                         }
173                                 }
174                                 else
175                                 {
176                                         source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
177                                         return false;
178                                 }
179                         }
180                 }
181
182                 return true;
183         }
184 };
185
186 class ModuleBanRedirect : public Module
187 {
188         BanRedirect* re;
189         bool nofollow;
190         Module* ExceptionModule;
191
192  public:
193         ModuleBanRedirect(InspIRCd* Me)
194         : Module(Me)
195         {
196                 re = new BanRedirect(Me);
197                 nofollow = false;
198
199                 if(!ServerInstance->Modes->AddModeWatcher(re))
200                 {
201                         delete re;
202                         throw ModuleException("Could not add mode watcher");
203                 }
204
205                 OnRehash(NULL, "");
206
207                 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
208                 Me->Modules->Attach(list, this, 4);
209
210         }
211
212         virtual void OnChannelDelete(Channel* chan)
213         {
214                 OnCleanup(TYPE_CHANNEL, chan);
215         }
216
217         virtual void OnCleanup(int target_type, void* item)
218         {
219                 if(target_type == TYPE_CHANNEL)
220                 {
221                         Channel* chan = static_cast<Channel*>(item);
222                         BanRedirectList* redirects;
223
224                         if(chan->GetExt("banredirects", redirects))
225                         {
226                                 irc::modestacker modestack(ServerInstance, false);
227                                 StringDeque stackresult;
228                                 std::vector<std::string> mode_junk;
229                                 mode_junk.push_back(chan->name);
230
231                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
232                                 {
233                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
234                                 }
235
236                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
237                                 {
238                                         modestack.PushPlus();
239                                         modestack.Push('b', i->banmask);
240                                 }
241
242                                 while(modestack.GetStackedLine(stackresult))
243                                 {
244                                         for(StringDeque::size_type i = 0; i < stackresult.size(); i++)
245                                         {
246                                                 mode_junk.push_back(stackresult[i]);
247                                         }
248
249                                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
250                                 }
251
252                                 delete redirects;
253                                 chan->Shrink("banredirects");
254                         }
255                 }
256         }
257
258         virtual void OnRehash(User* user, const std::string &param)
259         {
260                 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
261         }
262
263         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
264         {
265                 /* This prevents recursion when a user sets multiple ban redirects in a chain
266                  * (thanks Potter)
267                  */
268                 if (nofollow)
269                         return 0;
270
271                 /* Return 1 to prevent the join, 0 to allow it */
272                 if (chan)
273                 {
274                         BanRedirectList* redirects;
275
276                         if(chan->GetExt("banredirects", redirects))
277                         {
278                                 /* We actually had some ban redirects to check */
279
280                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
281                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
282                                  */
283
284                                 if (ExceptionModule)
285                                 {
286                                         ListModeRequest n(this, ExceptionModule, user, chan);
287                                         /* Users with ban exceptions are allowed to join without being redirected */
288                                         if (n.Send())
289                                                 return 0;
290                                 }
291
292                                 std::string ipmask(user->nick);
293                                 ipmask.append(1, '!').append(user->MakeHostIP());
294
295                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
296                                 {
297                                         if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
298                                         {
299                                                 /* tell them they're banned and are being transferred */
300                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
301                                                 std::string destlimit;
302
303                                                 if (destchan)
304                                                         destlimit = destchan->GetModeParameter('l');
305
306                                                 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
307                                                 {
308                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
309                                                         return 1;
310                                                 }
311                                                 else
312                                                 {
313                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
314                                                         user->WriteNumeric(470, "%s :You are being automatically redirected to %s", user->nick.c_str(), redir->targetchan.c_str());
315                                                         nofollow = true;
316                                                         Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
317                                                         nofollow = false;
318                                                         return 1;
319                                                 }
320                                         }
321                                 }
322                         }
323                 }
324                 return 0;
325         }
326
327         virtual ~ModuleBanRedirect()
328         {
329                 ServerInstance->Modes->DelModeWatcher(re);
330                 delete re;
331         }
332
333         virtual Version GetVersion()
334         {
335                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
336         }
337
338         void Prioritize()
339         {
340                 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
341                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIO_BEFORE, &banex);
342         }
343 };
344
345 MODULE_INIT(ModuleBanRedirect)