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