]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Get rid of ModePair
[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(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 };
40                 ServerInstance->Modules->Attach(eventlist, this, 6);
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 };
52
53 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
54
55 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
56 {
57         /* no change */
58         if (channel->IsModeSet('D') == adding)
59                 return MODEACTION_DENY;
60
61         if (!adding)
62         {
63                 /*
64                  * Make all users visible, as +D is being removed. If we don't do this,
65                  * they remain permanently invisible on this channel!
66                  */
67                 const UserMembList* names = channel->GetUsers();
68                 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
69                         creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
70         }
71         channel->SetMode('D', adding);
72         return MODEACTION_ALLOW;
73 }
74
75 ModuleDelayJoin::~ModuleDelayJoin()
76 {
77 }
78
79 Version ModuleDelayJoin::GetVersion()
80 {
81         return Version("Allows for delay-join channels (+D) where users dont appear to join until they speak", VF_COMMON | VF_VENDOR);
82 }
83
84 void ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick)
85 {
86         /* don't prevent the user from seeing themself */
87         if (issuer == memb->user)
88                 return;
89
90         /* If the user is hidden by delayed join, hide them from the NAMES list */
91         if (unjoined.get(memb))
92                 nick.clear();
93 }
94
95 static void populate(CUList& except, Membership* memb)
96 {
97         const UserMembList* users = memb->chan->GetUsers();
98         for(UserMembCIter i = users->begin(); i != users->end(); i++)
99         {
100                 if (i->first == memb->user || !IS_LOCAL(i->first))
101                         continue;
102                 except.insert(i->first);
103         }
104 }
105
106 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
107 {
108         if (memb->chan->IsModeSet('D'))
109         {
110                 unjoined.set(memb, 1);
111                 populate(except, memb);
112         }
113 }
114
115 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
116 {
117         if (unjoined.set(memb, 0))
118                 populate(except, memb);
119 }
120
121 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
122 {
123         if (unjoined.set(memb, 0))
124                 populate(except, memb);
125 }
126
127 void ModuleDelayJoin::OnBuildNeighborList(User* source, UserChanList &include, std::map<User*,bool> &exception)
128 {
129         UCListIter i = include.begin();
130         while (i != include.end())
131         {
132                 Channel* c = *i++;
133                 Membership* memb = c->GetUser(source);
134                 if (memb && unjoined.get(memb))
135                         include.erase(c);
136         }
137 }
138
139 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
140 {
141         /* Server origin */
142         if (!user)
143                 return;
144
145         if (target_type != TYPE_CHANNEL)
146                 return;
147
148         Channel* channel = (Channel*) dest;
149
150         Membership* memb = channel->GetUser(user);
151         if (!memb || !unjoined.set(memb, 0))
152                 return;
153
154         /* Display the join to everyone else (the user who joined got it earlier) */
155         channel->WriteAllExceptSender(user, false, 0, "JOIN %s", channel->name.c_str());
156
157         std::string ms = memb->modes;
158         for(unsigned int i=0; i < memb->modes.length(); i++)
159                 ms.append(" ").append(user->nick);
160
161         if (ms.length() > 0)
162                 channel->WriteAllExceptSender(user, false, 0, "MODE %s +%s", channel->name.c_str(), ms.c_str());
163 }
164
165 MODULE_INIT(ModuleDelayJoin)
166