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