]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_banredirect.cpp
Make classbase and refcountbase uncopyable; expand comments on their indended uses
[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
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         SimpleExtItem<BanRedirectList> extItem;
42         BanRedirect(Module* parent) : ModeWatcher(parent, 'b', MODETYPE_CHANNEL),
43                 extItem("banredirect", parent)
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 (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))
121                                 {
122                                         if (!ServerInstance->IsChannel(mask[CHAN].c_str(),  ServerInstance->Config->Limits.ChanMax))
123                                         {
124                                                 source->WriteNumeric(403, "%s %s :Invalid channel name in redirection (%s)", source->nick.c_str(), channel->name.c_str(), mask[CHAN].c_str());
125                                                 return false;
126                                         }
127
128                                         Channel *c = ServerInstance->FindChan(mask[CHAN].c_str());
129                                         if (!c)
130                                         {
131                                                 source->WriteNumeric(690, "%s :Target channel %s must exist to be set as a redirect.",source->nick.c_str(),mask[CHAN].c_str());
132                                                 return false;
133                                         }
134                                         else if (c->GetPrefixValue(source) < OP_VALUE)
135                                         {
136                                                 source->WriteNumeric(690, "%s :You must be opped on %s to set it as a redirect.",source->nick.c_str(), mask[CHAN].c_str());
137                                                 return false;
138                                         }
139
140                                         if (assign(channel->name) == mask[CHAN])
141                                         {
142                                                 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());
143                                                 return false;
144                                         }
145                                 }
146
147                                 if(adding)
148                                 {
149                                         /* It's a properly valid redirecting ban, and we're adding it */
150                                         redirects = extItem.get(channel);
151                                         if (!redirects)
152                                         {
153                                                 redirects = new BanRedirectList;
154                                                 extItem.set(channel, redirects);
155                                         }
156
157                                         /* Here 'param' doesn't have the channel on it yet */
158                                         redirects->push_back(BanRedirectEntry(mask[CHAN].c_str(), param.c_str()));
159
160                                         /* Now it does */
161                                         param.append(mask[CHAN]);
162                                 }
163                                 else
164                                 {
165                                         /* Removing a ban, if there's no extensible there are no redirecting bans and we're fine. */
166                                         redirects = extItem.get(channel);
167                                         if (redirects)
168                                         {
169                                                 /* But there were, so we need to remove the matching one if there is one */
170
171                                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
172                                                 {
173                                                         /* Ugly as fuck */
174                                                         if((irc::string(redir->targetchan.c_str()) == irc::string(mask[CHAN].c_str())) && (irc::string(redir->banmask.c_str()) == irc::string(param.c_str())))
175                                                         {
176                                                                 redirects->erase(redir);
177
178                                                                 if(redirects->empty())
179                                                                 {
180                                                                         extItem.unset(channel);
181                                                                 }
182
183                                                                 break;
184                                                         }
185                                                 }
186                                         }
187
188                                         /* Append the channel so the default +b handler can remove the entry too */
189                                         param.append(mask[CHAN]);
190                                 }
191                         }
192                 }
193
194                 return true;
195         }
196 };
197
198 class ModuleBanRedirect : public Module
199 {
200         BanRedirect re;
201         bool nofollow;
202         Module* ExceptionModule;
203
204  public:
205         ModuleBanRedirect()
206         : re(this)
207         {
208                 nofollow = false;
209
210                 if(!ServerInstance->Modes->AddModeWatcher(&re))
211                         throw ModuleException("Could not add mode watcher");
212
213                 OnRehash(NULL);
214
215                 ServerInstance->Extensions.Register(&re.extItem);
216                 Implementation list[] = { I_OnRehash, I_OnUserPreJoin, I_OnChannelDelete };
217                 ServerInstance->Modules->Attach(list, this, 3);
218         }
219
220         virtual void OnChannelDelete(Channel* chan)
221         {
222                 OnCleanup(TYPE_CHANNEL, chan);
223         }
224
225         virtual void OnCleanup(int target_type, void* item)
226         {
227                 if(target_type == TYPE_CHANNEL)
228                 {
229                         Channel* chan = static_cast<Channel*>(item);
230                         BanRedirectList* redirects = re.extItem.get(chan);
231
232                         if(redirects)
233                         {
234                                 irc::modestacker modestack(false);
235                                 StringDeque stackresult;
236                                 std::vector<std::string> mode_junk;
237                                 mode_junk.push_back(chan->name);
238
239                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
240                                 {
241                                         modestack.Push('b', i->targetchan.insert(0, i->banmask));
242                                 }
243
244                                 for(BanRedirectList::iterator i = redirects->begin(); i != redirects->end(); i++)
245                                 {
246                                         modestack.PushPlus();
247                                         modestack.Push('b', i->banmask);
248                                 }
249
250                                 while(modestack.GetStackedLine(stackresult))
251                                 {
252                                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
253                                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
254                                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
255                                 }
256                         }
257                 }
258         }
259
260         virtual void OnRehash(User* user)
261         {
262                 ExceptionModule = ServerInstance->Modules->Find("m_banexception.so");
263         }
264
265         virtual ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
266         {
267                 /* This prevents recursion when a user sets multiple ban redirects in a chain
268                  * (thanks Potter)
269                  */
270                 if (nofollow)
271                         return MOD_RES_PASSTHRU;
272
273                 /* Return 1 to prevent the join, 0 to allow it */
274                 if (chan)
275                 {
276                         BanRedirectList* redirects = re.extItem.get(chan);
277
278                         if (redirects)
279                         {
280                                 /* We actually had some ban redirects to check */
281
282                                 /* This was replaced with user->MakeHostIP() when I had a snprintf(), but MakeHostIP() doesn't seem to add the nick.
283                                  * Maybe we should have a GetFullIPHost() or something to match GetFullHost() and GetFullRealHost?
284                                  */
285
286                                 ModResult result;
287                                 FIRST_MOD_RESULT(OnCheckChannelBan, result, (user, chan));
288                                 if (result == MOD_RES_ALLOW)
289                                 {
290                                         // they have a ban exception
291                                         return MOD_RES_PASSTHRU;
292                                 }
293
294                                 std::string ipmask(user->nick);
295                                 ipmask.append(1, '!').append(user->MakeHostIP());
296
297                                 for(BanRedirectList::iterator redir = redirects->begin(); redir != redirects->end(); redir++)
298                                 {
299                                         if(InspIRCd::Match(user->GetFullRealHost(), redir->banmask) || InspIRCd::Match(user->GetFullHost(), redir->banmask) || InspIRCd::MatchCIDR(ipmask, redir->banmask))
300                                         {
301                                                 /* tell them they're banned and are being transferred */
302                                                 Channel* destchan = ServerInstance->FindChan(redir->targetchan);
303                                                 std::string destlimit;
304
305                                                 if (destchan)
306                                                         destlimit = destchan->GetModeParameter('l');
307
308                                                 if(destchan && ServerInstance->Modules->Find("m_redirect.so") && destchan->IsModeSet('L') && !destlimit.empty() && (destchan->GetUserCounter() >= atoi(destlimit.c_str())))
309                                                 {
310                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
311                                                         return MOD_RES_ALLOW;
312                                                 }
313                                                 else
314                                                 {
315                                                         user->WriteNumeric(474, "%s %s :Cannot join channel (You are banned)", user->nick.c_str(), chan->name.c_str());
316                                                         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());
317                                                         nofollow = true;
318                                                         Channel::JoinUser(user, redir->targetchan.c_str(), false, "", false, ServerInstance->Time());
319                                                         nofollow = false;
320                                                         return MOD_RES_ALLOW;
321                                                 }
322                                         }
323                                 }
324                         }
325                 }
326                 return MOD_RES_PASSTHRU;
327         }
328
329         virtual ~ModuleBanRedirect()
330         {
331         }
332
333         virtual Version GetVersion()
334         {
335                 return Version("Allows an extended ban (+b) syntax redirecting banned users to another channel", VF_COMMON|VF_VENDOR);
336         }
337
338         void Prioritize()
339         {
340                 Module* banex = ServerInstance->Modules->Find("m_banexception.so");
341                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &banex);
342         }
343 };
344
345 MODULE_INIT(ModuleBanRedirect)