]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Allow configuring whether SETNAME sends snotices and is oper-only.
[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  private:
28         LocalIntExt& unjoined;
29
30  public:
31         DelayJoinMode(Module* Parent, LocalIntExt& ext)
32                 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
33                 , unjoined(ext)
34         {
35                 ranktoset = ranktounset = OP_VALUE;
36         }
37
38         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
39         void RevealUser(User* user, Channel* chan);
40 };
41
42
43 namespace
44 {
45
46 /** Hook handler for join client protocol events.
47  * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
48  * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
49  * such as hostcycle generates a join.
50  */
51 class JoinHook : public ClientProtocol::EventHook
52 {
53         const LocalIntExt& unjoined;
54
55  public:
56         JoinHook(Module* mod, const LocalIntExt& unjoinedref)
57                 : ClientProtocol::EventHook(mod, "JOIN", 10)
58                 , unjoined(unjoinedref)
59         {
60         }
61
62         ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
63         {
64                 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
65                 const Membership* const memb = join.GetMember();
66                 const User* const u = memb->user;
67                 if ((unjoined.get(memb)) && (u != user))
68                         return MOD_RES_DENY;
69                 return MOD_RES_PASSTHRU;
70         }
71 };
72
73 }
74
75 class ModuleDelayJoin : public Module
76 {
77  public:
78         LocalIntExt unjoined;
79         JoinHook joinhook;
80         DelayJoinMode djm;
81
82         ModuleDelayJoin()
83                 : unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
84                 , joinhook(this, unjoined)
85                 , djm(this, unjoined)
86         {
87         }
88
89         Version GetVersion() CXX11_OVERRIDE;
90         ModResult OnNamesListItem(User* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
91         void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
92         void CleanUser(User* user);
93         void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
94         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
95         void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
96         void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
97         ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
98 };
99
100 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
101 {
102         /* no change */
103         if (channel->IsModeSet(this) == adding)
104                 return MODEACTION_DENY;
105
106         if (!adding)
107         {
108                 /*
109                  * Make all users visible, as +D is being removed. If we don't do this,
110                  * they remain permanently invisible on this channel!
111                  */
112                 const Channel::MemberMap& users = channel->GetUsers();
113                 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
114                         RevealUser(n->first, channel);
115         }
116         channel->SetMode(this, adding);
117         return MODEACTION_ALLOW;
118 }
119
120 Version ModuleDelayJoin::GetVersion()
121 {
122         return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
123 }
124
125 ModResult ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string& prefixes, std::string& nick)
126 {
127         /* don't prevent the user from seeing themself */
128         if (issuer == memb->user)
129                 return MOD_RES_PASSTHRU;
130
131         /* If the user is hidden by delayed join, hide them from the NAMES list */
132         if (unjoined.get(memb))
133                 return MOD_RES_DENY;
134
135         return MOD_RES_PASSTHRU;
136 }
137
138 static void populate(CUList& except, Membership* memb)
139 {
140         const Channel::MemberMap& users = memb->chan->GetUsers();
141         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
142         {
143                 if (i->first == memb->user || !IS_LOCAL(i->first))
144                         continue;
145                 except.insert(i->first);
146         }
147 }
148
149 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
150 {
151         if (memb->chan->IsModeSet(djm))
152                 unjoined.set(memb, 1);
153 }
154
155 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
156 {
157         if (unjoined.set(memb, 0))
158                 populate(except, memb);
159 }
160
161 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
162 {
163         if (unjoined.set(memb, 0))
164                 populate(except, memb);
165 }
166
167 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
168 {
169         for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
170         {
171                 Membership* memb = *i;
172                 if (unjoined.get(memb))
173                         i = include.erase(i);
174                 else
175                         ++i;
176         }
177 }
178
179 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
180 {
181         if (target.type != MessageTarget::TYPE_CHANNEL)
182                 return;
183
184         Channel* channel = target.Get<Channel>();
185         djm.RevealUser(user, channel);
186 }
187
188 void DelayJoinMode::RevealUser(User* user, Channel* chan)
189 {
190         Membership* memb = chan->GetUser(user);
191         if (!memb || !unjoined.set(memb, 0))
192                 return;
193
194         /* Display the join to everyone else (the user who joined got it earlier) */
195         CUList except_list;
196         except_list.insert(user);
197         ClientProtocol::Events::Join joinevent(memb);
198         chan->Write(joinevent, 0, except_list);
199 }
200
201 /* make the user visible if he receives any mode change */
202 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
203 {
204         if (!channel || param.empty())
205                 return MOD_RES_PASSTHRU;
206
207         // If not a prefix mode then we got nothing to do here
208         if (!mh->IsPrefixMode())
209                 return MOD_RES_PASSTHRU;
210
211         User* dest;
212         if (IS_LOCAL(user))
213                 dest = ServerInstance->FindNickOnly(param);
214         else
215                 dest = ServerInstance->FindNick(param);
216
217         if (!dest)
218                 return MOD_RES_PASSTHRU;
219
220         djm.RevealUser(dest, channel);
221         return MOD_RES_PASSTHRU;
222 }
223
224 MODULE_INIT(ModuleDelayJoin)