]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
c79fa03810197562bd8e72c533695c99bbb37ba8
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <stdarg.h>
16
17 class DelayJoinMode : public ModeHandler
18 {
19  private:
20         CUList empty;
21  public:
22         DelayJoinMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Parent, 'D', PARAM_NONE, MODETYPE_CHANNEL)
23         {
24                 levelrequired = OP_VALUE;
25         }
26
27         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
28 };
29
30 class ModuleDelayJoin : public Module
31 {
32         DelayJoinMode djm;
33  public:
34         LocalIntExt unjoined;
35         ModuleDelayJoin(InspIRCd* Me) : Module(Me), djm(Me, this), unjoined("delayjoin", this)
36         {
37                 if (!ServerInstance->Modes->AddMode(&djm))
38                         throw ModuleException("Could not add new modes!");
39                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnText, I_OnHostCycle };
40                 ServerInstance->Modules->Attach(eventlist, this, 7);
41         }
42         ~ModuleDelayJoin();
43         Version GetVersion();
44         void OnNamesListItem(User* issuer, Membership*, std::string &prefixes, std::string &nick);
45         void OnUserJoin(Membership*, bool, bool, CUList&);
46         void CleanUser(User* user);
47         ModResult OnHostCycle(User* user);
48         void OnUserPart(Membership*, std::string &partmessage, CUList&);
49         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&);
50         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
51         void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list);
52 };
53
54 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
55
56 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
57 {
58         /* no change */
59         if (channel->IsModeSet('D') == adding)
60                 return MODEACTION_DENY;
61
62         if (!adding)
63         {
64                 /*
65                  * Make all users visible, as +D is being removed. If we don't do this,
66                  * they remain permanently invisible on this channel!
67                  */
68                 const UserMembList* names = channel->GetUsers();
69                 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
70                         creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
71         }
72         channel->SetMode('D', adding);
73         return MODEACTION_ALLOW;
74 }
75
76 ModuleDelayJoin::~ModuleDelayJoin()
77 {
78         ServerInstance->Modes->DelMode(&djm);
79 }
80
81 Version ModuleDelayJoin::GetVersion()
82 {
83         return Version("$Id$", VF_COMMON | VF_VENDOR);
84 }
85
86 void ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick)
87 {
88         /* don't prevent the user from seeing themself */
89         if (issuer == memb->user)
90                 return;
91
92         /* If the user is hidden by delayed join, hide them from the NAMES list */
93         if (unjoined.get(memb))
94                 nick.clear();
95 }
96
97 static void populate(CUList& except, Membership* memb)
98 {
99         const UserMembList* users = memb->chan->GetUsers();
100         for(UserMembCIter 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('D'))
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 ModResult ModuleDelayJoin::OnHostCycle(User* user)
130 {
131         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
132         {
133                 Channel* chan = *f;
134                 Membership* memb = chan->GetUser(user);
135
136                 if (memb && unjoined.get(memb))
137                 {
138                         return MOD_RES_DENY;
139                 }
140         }
141         return MOD_RES_PASSTHRU;
142 }
143
144 void ModuleDelayJoin::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
145 {
146         Command* parthandler = ServerInstance->Parser->GetHandler("PART");
147         if (!parthandler)
148                 return;
149         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
150         {
151                 Channel* chan = *f;
152                 Membership* memb = chan->GetUser(user);
153                 if (memb && unjoined.get(memb))
154                 {
155                         std::vector<std::string> parameters;
156                         parameters.push_back(chan->name);
157                         /* Send a fake PART from the channel, which will be silent */
158                         parthandler->Handle(parameters, user);
159                 }
160         }
161 }
162
163 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
164 {
165         /* Server origin */
166         if (!user)
167                 return;
168
169         if (target_type != TYPE_CHANNEL)
170                 return;
171
172         Channel* channel = (Channel*) dest;
173
174         Membership* memb = channel->GetUser(user);
175         if (!memb || !unjoined.set(memb, 0))
176                 return;
177
178         /* Display the join to everyone else (the user who joined got it earlier) */
179         channel->WriteAllExceptSender(user, false, 0, "JOIN %s", channel->name.c_str());
180
181         std::string n = ServerInstance->Modes->ModeString(user, channel);
182         if (n.length() > 0)
183                 channel->WriteAllExceptSender(user, false, 0, "MODE %s +%s", channel->name.c_str(), n.c_str());
184 }
185
186 MODULE_INIT(ModuleDelayJoin)
187