]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
MetaData rework
[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))
120                                 {
121                                         if (!ServerInstance->IsChannel(mask[CHAN].c_str(),  ServerInstance->Config->Limits.ChanMax))
122                                         {
123                                                 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
124                                                 return false;
125                                         }
126
127                                         Channel *c = ServerInstance->FindChan(mask[CHAN].c_str());
128                                         if (!c)
129                                         {
130                                                 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
131                                                 return false;
132                                         }
133                                         else if (c->GetStatus(source) < STATUS_OP)
134                                         {
135                                                 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
136                                                 return false;
137                                         }
138
139                                         if (assign(channel->name) == mask[CHAN])
140                                         {
141                                                 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());
142                                                 return false;
143                                         }
144                                 }
145
146                                 if(adding)
147                                 {
148                                         /* It's a properly valid redirecting ban, and we're adding it */
149                                         if(!channel->GetExt("banredirects", redirects))
150                                         {
151                                                 redirects = new BanRedirectList;
152                                                 channel->Extend("banredirects", redirects);
153                                         }
154
155                                         /* Here 'param' doesn't have the channel on it yet */
156                                         redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
157
158                                         /* Now it does */
159                                         param.append(mask[CHAN]);
160                                 }
161                                 else
162                                 {
163                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
164                                         if(channel->GetExt("banredirects", redirects))
165                                         {
166                                                 /* But there were, so we need to remove the matching one if there is one */
167
168                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
169                                                 {
170                                                         /* Ugly as fuck */
171                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
172                                                         {
173                                                                 redirects->erase(redir);
174
175                                                                 if(redirects->empty())
176                                                                 {
177                                                                         delete redirects;
178                                                                         channel->Shrink("banredirects");
179                                                                 }
180
181                                                                 break;
182                                                         }
183                                                 }
184                                         }
185
186                                         /* Append the channel so the default +b handler can remove the entry too */
187                                         param.append(mask[CHAN]);
188                                 }
189                         }
190                 }
191
192                 return true;
193         }
194 };
195
196 class ModuleBanRedirect : public Module
197 {
198         BanRedirect re;
199         bool nofollow;
200         Module* ExceptionModule;
201
202  public:
203         ModuleBanRedirect(InspIRCd* Me)
204         : Module(Me), re(Me)
205         {
206                 nofollow = false;
207
208                 if(!ServerInstance->Modes->AddModeWatcher(&re))
209                         throw ModuleException("Could not add mode watcher");
210
211                 OnRehash(NULL);
212
213                 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete, I_OnCleanup };
214                 Me->Modules->Attach(list, this, 4);
215
216         }
217
218         virtual void OnChannelDelete(Channel* chan)
219         {
220                 OnCleanup(TYPE_CHANNEL, chan);
221         }
222
223         virtual void OnCleanup(int target_type, void* item)
224         {
225                 if(target_type == TYPE_CHANNEL)
226                 {
227                         Channel* chan = static_cast<Channel*>(item);
228                         BanRedirectList* redirects;
229
230                         if(chan->GetExt("banredirects", redirects))
231                         {
232                                 irc::modestacker modestack(ServerInstance, false);
233                                 StringDeque stackresult;
234                                 std::vector<std::string> mode_junk;
235                                 mode_junk.push_back(chan->name);
236
237                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
238                                 {
239                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
240                                 }
241
242                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
243                                 {
244                                         modestack.PushPlus();
245                                         modestack.Push('b', i->banmask);
246                                 }
247
248                                 while(modestack.GetStackedLine(stackresult))
249                                 {
250                                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
251                                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
252                                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
253                                 }
254
255                                 delete redirects;
256                                 chan->Shrink("banredirects");
257                         }
258                 }
259         }
260
261         virtual void OnRehash(User* user)
262         {
263                 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
264         }
265
266         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
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(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
301                                         {
302                                                 /* tell them they're banned and are being transferred */
303                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
304                                                 std::string destlimit;
305
306                                                 if (destchan)
307                                                         destlimit = destchan->GetModeParameter('l');
308
309                                                 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
310                                                 {
311                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
312                                                         return 1;
313                                                 }
314                                                 else
315                                                 {
316                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
317                                                         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());
318                                                         nofollow = true;
319                                                         Channel::JoinUser(ServerInstance, user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
320                                                         nofollow = false;
321                                                         return 1;
322                                                 }
323                                         }
324                                 }
325                         }
326                 }
327                 return 0;
328         }
329
330         virtual ~ModuleBanRedirect()
331         {
332                 ServerInstance->Modes->DelModeWatcher(&re);
333         }
334
335         virtual Version GetVersion()
336         {
337                 return Version("$Id$", VF_COMMON|VF_VENDOR, API_VERSION);
338         }
339
340         void Prioritize()
341         {
342                 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
343                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
344         }
345 };
346
347 MODULE_INIT(ModuleBanRedirect)