]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
ee4b3bba5a4eb31ad266ba09203c7297679d05c8
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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(Module* Parent) : ModeHandler(Parent, "delayjoin", '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() : djm(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_OnBuildNeighborList, I_OnNamesListItem, I_OnText, I_OnRawMode };
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         void OnUserPart(Membership*, std::string &partmessage, CUList&);
48         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&);
49         void OnBuildNeighborList(User* source, UserChanList &include, std::map<User*,bool> &exception);
50         void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list);
51         ModResult OnRawMode(User* user, Channel* channel, const char mode, const std::string &param, bool adding, int pcnt);
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 }
79
80 Version ModuleDelayJoin::GetVersion()
81 {
82         return Version("Allows for delay-join channels (+D) where users dont appear to join until they speak", VF_VENDOR);
83 }
84
85 void ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick)
86 {
87         /* don't prevent the user from seeing themself */
88         if (issuer == memb->user)
89                 return;
90
91         /* If the user is hidden by delayed join, hide them from the NAMES list */
92         if (unjoined.get(memb))
93                 nick.clear();
94 }
95
96 static void populate(CUList& except, Membership* memb)
97 {
98         const UserMembList* users = memb->chan->GetUsers();
99         for(UserMembCIter i = users->begin(); i != users->end(); i++)
100         {
101                 if (i->first == memb->user || !IS_LOCAL(i->first))
102                         continue;
103                 except.insert(i->first);
104         }
105 }
106
107 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
108 {
109         if (memb->chan->IsModeSet('D'))
110         {
111                 unjoined.set(memb, 1);
112                 populate(except, memb);
113         }
114 }
115
116 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
117 {
118         if (unjoined.set(memb, 0))
119                 populate(except, memb);
120 }
121
122 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
123 {
124         if (unjoined.set(memb, 0))
125                 populate(except, memb);
126 }
127
128 void ModuleDelayJoin::OnBuildNeighborList(User* source, UserChanList &include, std::map<User*,bool> &exception)
129 {
130         UCListIter i = include.begin();
131         while (i != include.end())
132         {
133                 Channel* c = *i++;
134                 Membership* memb = c->GetUser(source);
135                 if (memb && unjoined.get(memb))
136                         include.erase(c);
137         }
138 }
139
140 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
141 {
142         /* Server origin */
143         if (!user)
144                 return;
145
146         if (target_type != TYPE_CHANNEL)
147                 return;
148
149         Channel* channel = static_cast<Channel*>(dest);
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, const char mode, const std::string &param, bool adding, int pcnt)
168 {
169         if (!user || !channel || param.empty())
170                 return MOD_RES_PASSTHRU;
171
172         User* dest;
173         if (IS_LOCAL(user))
174                 dest = ServerInstance->FindNickOnly(param);
175         else
176                 dest = ServerInstance->FindNick(param);
177
178         if (!dest)
179                 return MOD_RES_PASSTHRU;
180
181         Membership* memb = channel->GetUser(dest);
182         if (memb && unjoined.set(memb, 0))
183                 channel->WriteAllExceptSender(dest, false, 0, "JOIN %s", channel->name.c_str());
184         return MOD_RES_PASSTHRU;
185 }
186
187 MODULE_INIT(ModuleDelayJoin)
188