]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Merge pull request #971 from SaberUK/master+numeric-xline
[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         CUList empty;
28  public:
29         DelayJoinMode(Module* Parent) : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
30         {
31                 levelrequired = OP_VALUE;
32         }
33
34         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
35 };
36
37 class ModuleDelayJoin : public Module
38 {
39         DelayJoinMode djm;
40  public:
41         LocalIntExt unjoined;
42         ModuleDelayJoin()
43                 : djm(this)
44                 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
45         {
46         }
47
48         Version GetVersion() CXX11_OVERRIDE;
49         ModResult OnNamesListItem(User* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
50         void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
51         void CleanUser(User* user);
52         void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
53         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
54         void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
55         void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list) CXX11_OVERRIDE;
56         ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
57 };
58
59 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
60 {
61         /* no change */
62         if (channel->IsModeSet(this) == adding)
63                 return MODEACTION_DENY;
64
65         if (!adding)
66         {
67                 /*
68                  * Make all users visible, as +D is being removed. If we don't do this,
69                  * they remain permanently invisible on this channel!
70                  */
71                 const Channel::MemberMap& users = channel->GetUsers();
72                 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
73                         creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
74         }
75         channel->SetMode(this, adding);
76         return MODEACTION_ALLOW;
77 }
78
79 Version ModuleDelayJoin::GetVersion()
80 {
81         return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
82 }
83
84 ModResult ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string& prefixes, std::string& nick)
85 {
86         /* don't prevent the user from seeing themself */
87         if (issuer == memb->user)
88                 return MOD_RES_PASSTHRU;
89
90         /* If the user is hidden by delayed join, hide them from the NAMES list */
91         if (unjoined.get(memb))
92                 return MOD_RES_DENY;
93
94         return MOD_RES_PASSTHRU;
95 }
96
97 static void populate(CUList& except, Membership* memb)
98 {
99         const Channel::MemberMap& users = memb->chan->GetUsers();
100         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
101         {
102                 if (i->first == memb->user || !IS_LOCAL(i->first))
103                         continue;
104                 except.insert(i->first);
105         }
106 }
107
108 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
109 {
110         if (memb->chan->IsModeSet(djm))
111         {
112                 unjoined.set(memb, 1);
113                 populate(except, memb);
114         }
115 }
116
117 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
118 {
119         if (unjoined.set(memb, 0))
120                 populate(except, memb);
121 }
122
123 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
124 {
125         if (unjoined.set(memb, 0))
126                 populate(except, memb);
127 }
128
129 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
130 {
131         for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
132         {
133                 Membership* memb = *i;
134                 if (unjoined.get(memb))
135                         i = include.erase(i);
136                 else
137                         ++i;
138         }
139 }
140
141 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
142 {
143         if (target_type != TYPE_CHANNEL)
144                 return;
145
146         Channel* channel = static_cast<Channel*>(dest);
147
148         Membership* memb = channel->GetUser(user);
149         if (!memb || !unjoined.set(memb, 0))
150                 return;
151
152         /* Display the join to everyone else (the user who joined got it earlier) */
153         channel->WriteAllExceptSender(user, false, 0, "JOIN %s", channel->name.c_str());
154
155         std::string ms = memb->modes;
156         for(unsigned int i=0; i < memb->modes.length(); i++)
157                 ms.append(" ").append(user->nick);
158
159         if (ms.length() > 0)
160                 channel->WriteAllExceptSender(user, false, 0, "MODE %s +%s", channel->name.c_str(), ms.c_str());
161 }
162
163 /* make the user visible if he receives any mode change */
164 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
165 {
166         if (!channel || param.empty())
167                 return MOD_RES_PASSTHRU;
168
169         // If not a prefix mode then we got nothing to do here
170         if (!mh->IsPrefixMode())
171                 return MOD_RES_PASSTHRU;
172
173         User* dest;
174         if (IS_LOCAL(user))
175                 dest = ServerInstance->FindNickOnly(param);
176         else
177                 dest = ServerInstance->FindNick(param);
178
179         if (!dest)
180                 return MOD_RES_PASSTHRU;
181
182         Membership* memb = channel->GetUser(dest);
183         if (memb && unjoined.set(memb, 0))
184                 channel->WriteAllExceptSender(dest, false, 0, "JOIN %s", channel->name.c_str());
185         return MOD_RES_PASSTHRU;
186 }
187
188 MODULE_INIT(ModuleDelayJoin)