]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Don't sync filters defined in the config and expire them on rehash.
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2010 Jens Voss <DukePyrolator@anope.org>
5  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 class DelayJoinMode : public ModeHandler
26 {
27  public:
28         DelayJoinMode(Module* Parent) : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
29         {
30                 ranktoset = ranktounset = OP_VALUE;
31         }
32
33         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
34 };
35
36
37 namespace
38 {
39
40 /** Hook handler for join client protocol events.
41  * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
42  * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
43  * such as hostcycle generates a join.
44  */
45 class JoinHook : public ClientProtocol::EventHook
46 {
47         const LocalIntExt& unjoined;
48
49  public:
50         JoinHook(Module* mod, const LocalIntExt& unjoinedref)
51                 : ClientProtocol::EventHook(mod, "JOIN", 10)
52                 , unjoined(unjoinedref)
53         {
54         }
55
56         ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
57         {
58                 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
59                 const User* const u = join.GetMember()->user;
60                 if ((unjoined.get(u)) && (u != user))
61                         return MOD_RES_DENY;
62                 return MOD_RES_PASSTHRU;
63         }
64 };
65
66 }
67
68 class ModuleDelayJoin : public Module
69 {
70         DelayJoinMode djm;
71         void RevealUser(User* user, Channel* chan);
72  public:
73         LocalIntExt unjoined;
74         JoinHook joinhook;
75
76         ModuleDelayJoin()
77                 : djm(this)
78                 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
79                 , joinhook(this, unjoined)
80         {
81         }
82
83         Version GetVersion() CXX11_OVERRIDE;
84         ModResult OnNamesListItem(User* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
85         void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
86         void CleanUser(User* user);
87         void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
88         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
89         void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
90         void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
91         ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
92 };
93
94 // TODO: make RevealUser accessible from DelayJoinMode and get rid of this.
95 class DummyMessageDetails : public MessageDetails
96 {
97 public:
98         DummyMessageDetails() : MessageDetails(MSG_PRIVMSG, "", ClientProtocol::TagMap()) { }
99         bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE { return false; }
100         bool IsCTCP(std::string&) const CXX11_OVERRIDE { return false; }
101         bool IsCTCP() const CXX11_OVERRIDE { return false; }
102 };
103
104 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
105 {
106         /* no change */
107         if (channel->IsModeSet(this) == adding)
108                 return MODEACTION_DENY;
109
110         if (!adding)
111         {
112                 /*
113                  * Make all users visible, as +D is being removed. If we don't do this,
114                  * they remain permanently invisible on this channel!
115                  */
116                 MessageTarget msgtarget(channel, 0);
117                 DummyMessageDetails msgdetails;
118                 const Channel::MemberMap& users = channel->GetUsers();
119                 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
120                 {
121                         creator->OnUserMessage(n->first, msgtarget, msgdetails);
122                 }
123         }
124         channel->SetMode(this, adding);
125         return MODEACTION_ALLOW;
126 }
127
128 Version ModuleDelayJoin::GetVersion()
129 {
130         return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
131 }
132
133 ModResult ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string& prefixes, std::string& nick)
134 {
135         /* don't prevent the user from seeing themself */
136         if (issuer == memb->user)
137                 return MOD_RES_PASSTHRU;
138
139         /* If the user is hidden by delayed join, hide them from the NAMES list */
140         if (unjoined.get(memb))
141                 return MOD_RES_DENY;
142
143         return MOD_RES_PASSTHRU;
144 }
145
146 static void populate(CUList& except, Membership* memb)
147 {
148         const Channel::MemberMap& users = memb->chan->GetUsers();
149         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
150         {
151                 if (i->first == memb->user || !IS_LOCAL(i->first))
152                         continue;
153                 except.insert(i->first);
154         }
155 }
156
157 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
158 {
159         if (memb->chan->IsModeSet(djm))
160                 unjoined.set(memb, 1);
161 }
162
163 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
164 {
165         if (unjoined.set(memb, 0))
166                 populate(except, memb);
167 }
168
169 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
170 {
171         if (unjoined.set(memb, 0))
172                 populate(except, memb);
173 }
174
175 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
176 {
177         for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
178         {
179                 Membership* memb = *i;
180                 if (unjoined.get(memb))
181                         i = include.erase(i);
182                 else
183                         ++i;
184         }
185 }
186
187 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
188 {
189         if (target.type != MessageTarget::TYPE_CHANNEL)
190                 return;
191
192         Channel* channel = target.Get<Channel>();
193         RevealUser(user, channel);
194 }
195
196 void ModuleDelayJoin::RevealUser(User* user, Channel* chan)
197 {
198         Membership* memb = chan->GetUser(user);
199         if (!memb || !unjoined.set(memb, 0))
200                 return;
201
202         /* Display the join to everyone else (the user who joined got it earlier) */
203         CUList except_list;
204         except_list.insert(user);
205         ClientProtocol::Events::Join joinevent(memb);
206         chan->Write(joinevent, 0, except_list);
207 }
208
209 /* make the user visible if he receives any mode change */
210 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
211 {
212         if (!channel || param.empty())
213                 return MOD_RES_PASSTHRU;
214
215         // If not a prefix mode then we got nothing to do here
216         if (!mh->IsPrefixMode())
217                 return MOD_RES_PASSTHRU;
218
219         User* dest;
220         if (IS_LOCAL(user))
221                 dest = ServerInstance->FindNickOnly(param);
222         else
223                 dest = ServerInstance->FindNick(param);
224
225         if (!dest)
226                 return MOD_RES_PASSTHRU;
227
228         RevealUser(dest, channel);
229         return MOD_RES_PASSTHRU;
230 }
231
232 MODULE_INIT(ModuleDelayJoin)