]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
7c557eb35a0abdf9c31c15c59b53f7ef3749d322
[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 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
95 {
96         /* no change */
97         if (channel->IsModeSet(this) == adding)
98                 return MODEACTION_DENY;
99
100         if (!adding)
101         {
102                 /*
103                  * Make all users visible, as +D is being removed. If we don't do this,
104                  * they remain permanently invisible on this channel!
105                  */
106                 MessageTarget msgtarget(channel, 0);
107                 MessageDetails msgdetails(MSG_PRIVMSG, "", ClientProtocol::TagMap());
108                 const Channel::MemberMap& users = channel->GetUsers();
109                 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
110                 {
111                         creator->OnUserMessage(n->first, msgtarget, msgdetails);
112                 }
113         }
114         channel->SetMode(this, adding);
115         return MODEACTION_ALLOW;
116 }
117
118 Version ModuleDelayJoin::GetVersion()
119 {
120         return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
121 }
122
123 ModResult ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string& prefixes, std::string& nick)
124 {
125         /* don't prevent the user from seeing themself */
126         if (issuer == memb->user)
127                 return MOD_RES_PASSTHRU;
128
129         /* If the user is hidden by delayed join, hide them from the NAMES list */
130         if (unjoined.get(memb))
131                 return MOD_RES_DENY;
132
133         return MOD_RES_PASSTHRU;
134 }
135
136 static void populate(CUList& except, Membership* memb)
137 {
138         const Channel::MemberMap& users = memb->chan->GetUsers();
139         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
140         {
141                 if (i->first == memb->user || !IS_LOCAL(i->first))
142                         continue;
143                 except.insert(i->first);
144         }
145 }
146
147 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
148 {
149         if (memb->chan->IsModeSet(djm))
150                 unjoined.set(memb, 1);
151 }
152
153 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
154 {
155         if (unjoined.set(memb, 0))
156                 populate(except, memb);
157 }
158
159 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
160 {
161         if (unjoined.set(memb, 0))
162                 populate(except, memb);
163 }
164
165 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
166 {
167         for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
168         {
169                 Membership* memb = *i;
170                 if (unjoined.get(memb))
171                         i = include.erase(i);
172                 else
173                         ++i;
174         }
175 }
176
177 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
178 {
179         if (target.type != MessageTarget::TYPE_CHANNEL)
180                 return;
181
182         Channel* channel = target.Get<Channel>();
183         RevealUser(user, channel);
184 }
185
186 void ModuleDelayJoin::RevealUser(User* user, Channel* chan)
187 {
188         Membership* memb = chan->GetUser(user);
189         if (!memb || !unjoined.set(memb, 0))
190                 return;
191
192         /* Display the join to everyone else (the user who joined got it earlier) */
193         CUList except_list;
194         except_list.insert(user);
195         ClientProtocol::Events::Join joinevent(memb);
196         chan->Write(joinevent, 0, except_list);
197 }
198
199 /* make the user visible if he receives any mode change */
200 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
201 {
202         if (!channel || param.empty())
203                 return MOD_RES_PASSTHRU;
204
205         // If not a prefix mode then we got nothing to do here
206         if (!mh->IsPrefixMode())
207                 return MOD_RES_PASSTHRU;
208
209         User* dest;
210         if (IS_LOCAL(user))
211                 dest = ServerInstance->FindNickOnly(param);
212         else
213                 dest = ServerInstance->FindNick(param);
214
215         if (!dest)
216                 return MOD_RES_PASSTHRU;
217
218         RevealUser(dest, channel);
219         return MOD_RES_PASSTHRU;
220 }
221
222 MODULE_INIT(ModuleDelayJoin)