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