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