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