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