]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Dont echo the JOIN to the user whos speaking
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
18
19 class DelayJoinMode : public ModeHandler
20 {
21         CUList empty;
22         Module* Creator;
23  public:
24         DelayJoinMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, 'D', 0, 0, false, MODETYPE_CHANNEL, false), Creator(Parent) { }
25
26         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
27         {
28                 if (channel->IsModeSet('D') != adding)
29                 {
30                         if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
31                         {
32                                 source->WriteServ("482 %s %s :Only channel operators may %sset channel mode +D", source->nick, channel->name, adding ? "" : "un");
33                                 return MODEACTION_DENY;
34                         }
35                         else
36                         {
37                                 if (channel->IsModeSet('D'))
38                                 {
39                                         /* Make all delayed join users visible, or if an op removes +D
40                                          * while users exist that havent spoken, they remain permenantly
41                                          * invisible on this channel!
42                                          */
43                                         CUList* names = channel->GetUsers();
44                                         for (CUListIter n = names->begin(); n != names->end(); ++n)
45                                                 Creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
46                                 }
47                                 channel->SetMode('D', adding);
48                                 return MODEACTION_ALLOW;
49                         }
50                 }
51                 else
52                 {
53                         return MODEACTION_DENY;
54                 }
55         }
56 };
57
58 class ModuleDelayJoin : public Module
59 {
60  private:
61         DelayJoinMode* djm;
62         CUList nl;
63  public:
64         ModuleDelayJoin(InspIRCd* Me)
65                 : Module(Me)
66         {
67                 djm = new DelayJoinMode(ServerInstance, this);
68                 if (!ServerInstance->AddMode(djm))
69                         throw ModuleException("Could not add new modes!");
70         }
71         
72         virtual ~ModuleDelayJoin()
73         {
74                 ServerInstance->Modes->DelMode(djm);
75                 DELETE(djm);
76         }
77
78         Priority Prioritize()
79         {
80                 /* To ensure that we get priority over namesx for names list generation on +u channels */
81                 return (Priority)ServerInstance->Modules->PriorityBefore("m_namesx.so");
82         }
83
84         virtual Version GetVersion()
85         {
86                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
87         }
88
89         void Implements(char* List)
90         {
91                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserKick] = List[I_OnUserQuit] = List[I_OnUserList] = List[I_OnText] = 1;
92         }
93
94         virtual int OnUserList(User* user, Channel* Ptr, CUList* &nameslist)
95         {
96                 CUList* newlist = nameslist ? nameslist : Ptr->GetUsers();
97
98                 nl.clear();
99
100                 /* For +D channels ... */
101                 if (Ptr->IsModeSet('D'))
102                 {
103                         std::string key("delayjoin_");
104                         key.append(Ptr->name);
105
106                         /* Modify the names list, erasing users with the delay join metadata
107                          * for this channel (havent spoken yet)
108                          */
109                         for (CUListIter n = newlist->begin(); n != newlist->end(); ++n)
110                         {
111                                 if (!n->first->GetExt(key))
112                                         nl.insert(*n);
113                         }
114
115                         /* Always show self */
116                         nl[user] = user->nick;
117                         nameslist = &nl;
118                 }
119                 return 0;
120         }
121
122         virtual void OnUserJoin(User* user, Channel* channel, bool &silent)
123         {
124                 if (channel->IsModeSet('D'))
125                 {
126                         silent = true;
127                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
128                         user->WriteFrom(user, "JOIN %s", channel->name);
129
130                         /* This metadata tells the module the user is delayed join on this specific channel */
131                         user->Extend(std::string("delayjoin_")+channel->name);
132
133                         /* This metadata tells the module the user is delayed join on at least one (or more) channels.
134                          * It is only cleared when the user is no longer on ANY +D channels.
135                          */
136                         if (!user->GetExt("delayjoin"))
137                                 user->Extend("delayjoin");
138                 }
139         }
140
141         void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
142         {
143                 if (channel->IsModeSet('D'))
144                 {
145                         if (user->GetExt(std::string("delayjoin_")+channel->name))
146                         {
147                                 silent = true;
148                                 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
149                                 user->WriteFrom(user, "PART %s%s%s", channel->name, partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
150                         }
151                 }
152         }
153
154         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
155         {
156                 if (chan->IsModeSet('D'))
157                 {
158                         /* Send silenced event only to the user being kicked and the user doing the kick */
159                         if (user->GetExt(std::string("delayjoin_")+chan->name))
160                         {
161                                 silent = true;
162                                 user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
163                         }
164                 }
165         }
166
167         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
168         {
169                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
170                 const char* parameters[2];
171                 if (parthandler && user->GetExt("delayjoin"))
172                 {
173                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
174                         {
175                                 parameters[0] = f->first->name;
176                                 /* This triggers our OnUserPart, above, making the PART silent */
177                                 parthandler->Handle(parameters, 1, user);
178                         }
179                 }
180         }
181
182         void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
183         {
184                 if (target_type != TYPE_CHANNEL)
185                         return;
186
187                 Channel* channel = (Channel*) dest;
188
189                 if (!user->GetExt(std::string("delayjoin_")+channel->name))
190                         return;
191
192                 /* Display the join to everyone else (the user who joined got it earlier) */
193                 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name);
194
195                 /* Shrink off the neccessary metadata for a specific channel */
196                 user->Shrink(std::string("delayjoin_")+channel->name);
197
198                 /* Check if the user is left on any other +D channels, if so don't take away the
199                  * metadata that says theyre on one or more channels 
200                  */
201                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
202                         if (f->first->IsModeSet('D'))
203                                 return;
204
205                 user->Shrink("delayjoin");
206         }
207
208         void WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
209         {
210                 va_list argsPtr;
211                 char textbuffer[MAXBUF];
212                 char tb[MAXBUF];
213
214                 va_start(argsPtr, text);
215                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
216                 va_end(argsPtr);
217                 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),textbuffer);
218
219                 CUList *ulist = channel->GetUsers();
220
221                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
222                 {
223                         /* User doesnt get a JOIN sent to themselves */
224                         if (user == i->first)
225                                 continue;
226
227                         /* Users with a visibility state that hides them dont appear */
228                         if (user->Visibility && !user->Visibility->VisibleTo(i->first))
229                                 continue;
230
231                         i->first->Write(std::string(tb));
232                 }
233         }
234
235 };
236
237 MODULE_INIT(ModuleDelayJoin)
238