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