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