]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Use Utils->ServerUser instead of ServerInstance->FakeClient in m_spanningtree
[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         Module* Creator;
22  public:
23         DelayJoinMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, 'D', 0, 0, false, MODETYPE_CHANNEL, false, 0, '@'), Creator(Parent) {};
24
25         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool);
26 };
27
28 class ModuleDelayJoin : public Module
29 {
30  private:
31         DelayJoinMode* djm;
32         CUList nl;
33  public:
34         ModuleDelayJoin(InspIRCd* Me) : Module(Me)
35         {
36                 djm = new DelayJoinMode(ServerInstance, this);
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         virtual ~ModuleDelayJoin();
43         virtual Version GetVersion();
44         virtual void OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick);
45         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent);
46         void CleanUser(User* user);
47         bool OnHostCycle(User* user);
48         void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
49         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent);
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         void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5);
53 };
54
55 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
56
57 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
58 {
59         /* no change */
60         if (channel->IsModeSet('D') == adding)
61                 return MODEACTION_DENY;
62
63         if (!adding)
64         {
65                 /*
66                  * Make all users visible, as +D is being removed. If we don't do this,
67                  * they remain permanently invisible on this channel!
68                  */
69                 CUList* names = channel->GetUsers();
70                 for (CUListIter n = names->begin(); n != names->end(); ++n)
71                         Creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
72         }
73         channel->SetMode('D', adding);
74         return MODEACTION_ALLOW;
75 }
76
77 ModuleDelayJoin::~ModuleDelayJoin()
78 {
79         ServerInstance->Modes->DelMode(djm);
80         delete djm;
81 }
82
83 Version ModuleDelayJoin::GetVersion()
84 {
85         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
86 }
87
88 void ModuleDelayJoin::OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
89 {
90         if (!channel->IsModeSet('D'))
91                 return;
92
93         if (nick.empty())
94                 return;
95
96         /* don't prevent the user from seeing themself */
97         if (issuer == user)
98                 return;
99
100         /* If the user is hidden by delayed join, hide them from the NAMES list */
101         if (user->GetExt("delayjoin_"+channel->name))
102                 nick.clear();
103 }
104
105 void ModuleDelayJoin::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
106 {
107         if (channel->IsModeSet('D'))
108         {
109                 silent = true;
110                 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
111                 user->WriteFrom(user, "JOIN %s", channel->name.c_str());
112
113                 /* This metadata tells the module the user is delayed join on this specific channel */
114                 user->Extend("delayjoin_"+channel->name);
115
116                 /* This metadata tells the module the user is delayed join on at least one (or more) channels.
117                  * It is only cleared when the user is no longer on ANY +D channels.
118                  */
119                 if (!user->GetExt("delayjoin"))
120                         user->Extend("delayjoin");
121         }
122 }
123
124 void ModuleDelayJoin::CleanUser(User* user)
125 {
126         /* Check if the user is hidden on any other +D channels, if so don't take away the
127          * metadata that says they're hidden on one or more channels
128          */
129         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
130                 if (user->GetExt("delayjoin_" + f->first->name))
131                         return;
132
133         user->Shrink("delayjoin");
134 }
135
136 void ModuleDelayJoin::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
137 {
138         if (!channel->IsModeSet('D'))
139                 return;
140         if (user->GetExt("delayjoin_"+channel->name))
141         {
142                 user->Shrink("delayjoin_"+channel->name);
143                 silent = true;
144                 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
145                 user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
146                 CleanUser(user);
147         }
148 }
149
150 void ModuleDelayJoin::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
151 {
152         if (!chan->IsModeSet('D'))
153                 return;
154         /* Send silenced event only to the user being kicked and the user doing the kick */
155         if (user->GetExt("delayjoin_"+chan->name))
156         {
157                 user->Shrink("delayjoin_"+chan->name);
158                 silent = true;
159                 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
160                 CleanUser(user);
161         }
162 }
163
164 bool ModuleDelayJoin::OnHostCycle(User* user)
165 {
166         return user->GetExt("delayjoin");
167 }
168
169 void ModuleDelayJoin::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
170 {
171         Command* parthandler = ServerInstance->Parser->GetHandler("PART");
172         if (parthandler && user->GetExt("delayjoin"))
173         {
174                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
175                 {
176                         Channel* chan = f->first;
177                         if (user->GetExt("delayjoin_"+chan->name))
178                         {
179                                 std::vector<std::string> parameters;
180                                 parameters.push_back(chan->name);
181                                 /* Send a fake PART from the channel, which will be silent */
182                                 parthandler->Handle(parameters, user);
183                         }
184                 }
185                 user->Shrink("delayjoin");
186         }
187 }
188
189 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
190 {
191         /* Server origin */
192         if (!user)
193                 return;
194
195         if (target_type != TYPE_CHANNEL)
196                 return;
197
198         Channel* channel = (Channel*) dest;
199
200         if (!user->GetExt("delayjoin_"+channel->name))
201                 return;
202
203         /* Display the join to everyone else (the user who joined got it earlier) */
204         this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
205
206         std::string n = this->ServerInstance->Modes->ModeString(user, channel);
207         if (n.length() > 0)
208                 this->WriteCommonFrom(user, channel, "MODE %s +%s", channel->name.c_str(), n.c_str());
209
210         /* Shrink off the neccessary metadata for a specific channel */
211         user->Shrink("delayjoin_"+channel->name);
212         CleanUser(user);
213 }
214
215 // .. is there a real need to duplicate WriteCommonExcept?
216 void ModuleDelayJoin::WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
217 {
218         va_list argsPtr;
219         char textbuffer[MAXBUF];
220         char tb[MAXBUF];
221
222         va_start(argsPtr, text);
223         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
224         va_end(argsPtr);
225         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
226
227         CUList *ulist = channel->GetUsers();
228
229         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
230         {
231                 /* User doesnt get a JOIN sent to themselves */
232                 if (user == i->first)
233                         continue;
234
235                 /* Users with a visibility state that hides them dont appear */
236                 if (user->Visibility && !user->Visibility->VisibleTo(i->first))
237                         continue;
238
239                 i->first->Write(std::string(tb));
240         }
241 }
242
243 MODULE_INIT(ModuleDelayJoin)
244