]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
f182e18d0f4c74e7b3d1db521ce78a39ec464b2e
[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(Instance, Parent, 'D', 0, 0, false, MODETYPE_CHANNEL, false, 0, '@') {};
23
24         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding);
25 };
26
27 class ModuleDelayJoin : public Module
28 {
29         DelayJoinMode djm;
30  public:
31         LocalIntExt unjoined;
32         ModuleDelayJoin(InspIRCd* Me) : Module(Me), djm(Me, this), unjoined("delayjoin", this)
33         {
34                 if (!ServerInstance->Modes->AddMode(&djm))
35                         throw ModuleException("Could not add new modes!");
36                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnText, I_OnHostCycle };
37                 ServerInstance->Modules->Attach(eventlist, this, 7);
38         }
39         ~ModuleDelayJoin();
40         Version GetVersion();
41         void OnNamesListItem(User* issuer, Membership*, std::string &prefixes, std::string &nick);
42         void OnUserJoin(Membership*, bool, bool, CUList&);
43         void CleanUser(User* user);
44         ModResult OnHostCycle(User* user);
45         void OnUserPart(Membership*, std::string &partmessage, CUList&);
46         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&);
47         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
48         void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list);
49 };
50
51 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
52
53 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
54 {
55         /* no change */
56         if (channel->IsModeSet('D') == adding)
57                 return MODEACTION_DENY;
58
59         if (!adding)
60         {
61                 /*
62                  * Make all users visible, as +D is being removed. If we don't do this,
63                  * they remain permanently invisible on this channel!
64                  */
65                 const UserMembList* names = channel->GetUsers();
66                 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
67                         creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
68         }
69         channel->SetMode('D', adding);
70         return MODEACTION_ALLOW;
71 }
72
73 ModuleDelayJoin::~ModuleDelayJoin()
74 {
75         ServerInstance->Modes->DelMode(&djm);
76 }
77
78 Version ModuleDelayJoin::GetVersion()
79 {
80         return Version("$Id$", VF_COMMON | VF_VENDOR);
81 }
82
83 void ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick)
84 {
85         /* don't prevent the user from seeing themself */
86         if (issuer == memb->user)
87                 return;
88
89         /* If the user is hidden by delayed join, hide them from the NAMES list */
90         if (unjoined.get(memb))
91                 nick.clear();
92 }
93
94 static void populate(CUList& except, Membership* memb)
95 {
96         const UserMembList* users = memb->chan->GetUsers();
97         for(UserMembCIter i = users->begin(); i != users->end(); i++)
98         {
99                 if (i->first == memb->user || !IS_LOCAL(i->first))
100                         continue;
101                 except.insert(i->first);
102         }
103 }
104
105 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
106 {
107         if (memb->chan->IsModeSet('D'))
108         {
109                 unjoined.set(memb, 1);
110                 populate(except, memb);
111         }
112 }
113
114 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
115 {
116         if (unjoined.set(memb, 0))
117                 populate(except, memb);
118 }
119
120 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
121 {
122         if (unjoined.set(memb, 0))
123                 populate(except, memb);
124 }
125
126 ModResult ModuleDelayJoin::OnHostCycle(User* user)
127 {
128         // TODO
129         return MOD_RES_DENY;
130 }
131
132 void ModuleDelayJoin::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
133 {
134         Command* parthandler = ServerInstance->Parser->GetHandler("PART");
135         if (!parthandler)
136                 return;
137         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
138         {
139                 Channel* chan = *f;
140                 Membership* memb = chan->GetUser(user);
141                 if (memb && unjoined.get(memb))
142                 {
143                         std::vector<std::string> parameters;
144                         parameters.push_back(chan->name);
145                         /* Send a fake PART from the channel, which will be silent */
146                         parthandler->Handle(parameters, user);
147                 }
148         }
149 }
150
151 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
152 {
153         /* Server origin */
154         if (!user)
155                 return;
156
157         if (target_type != TYPE_CHANNEL)
158                 return;
159
160         Channel* channel = (Channel*) dest;
161
162         Membership* memb = channel->GetUser(user);
163         if (!memb || !unjoined.set(memb, 0))
164                 return;
165
166         /* Display the join to everyone else (the user who joined got it earlier) */
167         channel->WriteAllExceptSender(user, false, 0, "JOIN %s", channel->name.c_str());
168
169         std::string n = this->ServerInstance->Modes->ModeString(user, channel);
170         if (n.length() > 0)
171                 channel->WriteAllExceptSender(user, false, 0, "MODE %s +%s", channel->name.c_str(), n.c_str());
172 }
173
174 MODULE_INIT(ModuleDelayJoin)
175