]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Of course, it DOES help to actually initialise the Mutex objects, and delete them...
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 };
40                 ServerInstance->Modules->Attach(eventlist, this, 6);
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 OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
47         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent);
48         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
49         void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list);
50         void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5);
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, bool)
56 {
57         if (channel->IsModeSet('D') != adding)
58         {
59                 if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
60                 {
61                         source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only channel operators may %sset channel mode +D", source->nick.c_str(), channel->name.c_str(), adding ? "" : "un");
62                         return MODEACTION_DENY;
63                 }
64                 else
65                 {
66                         if (channel->IsModeSet('D'))
67                         {
68                                 /*
69                                  * Make all users visible, as +D is being removed. If we don't do this,
70                                  * they remain permanently invisible on this channel!
71                                  */
72                                 CUList* names = channel->GetUsers();
73                                 for (CUListIter n = names->begin(); n != names->end(); ++n)
74                                         Creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
75                         }
76                         channel->SetMode('D', adding);
77                         return MODEACTION_ALLOW;
78                 }
79         }
80         else
81         {
82                 return MODEACTION_DENY;
83         }
84 }
85
86 ModuleDelayJoin::~ModuleDelayJoin()
87 {
88         ServerInstance->Modes->DelMode(djm);
89         delete djm;
90 }
91
92 Version ModuleDelayJoin::GetVersion()
93 {
94         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
95 }
96
97 void ModuleDelayJoin::OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
98 {
99         if (!channel->IsModeSet('D'))
100                 return;
101
102         if (nick.empty())
103                 return;
104
105         /* If the user is hidden by delayed join, hide them from the NAMES list */
106         std::string key("delayjoin_");
107         key.append(channel->name);
108
109         if (user->GetExt(key))
110                 nick.clear();
111 }
112
113 void ModuleDelayJoin::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
114 {
115         if (channel->IsModeSet('D'))
116         {
117                 silent = true;
118                 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
119                 user->WriteFrom(user, "JOIN %s", channel->name.c_str());
120
121                 /* This metadata tells the module the user is delayed join on this specific channel */
122                 user->Extend("delayjoin_"+channel->name);
123
124                 /* This metadata tells the module the user is delayed join on at least one (or more) channels.
125                  * It is only cleared when the user is no longer on ANY +D channels.
126                  */
127                 if (!user->GetExt("delayjoin"))
128                         user->Extend("delayjoin");
129         }
130 }
131
132 void ModuleDelayJoin::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
133 {
134         if (channel->IsModeSet('D'))
135         {
136                 if (user->GetExt("delayjoin_"+channel->name))
137                 {
138                         silent = true;
139                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
140                         user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
141                 }
142         }
143 }
144
145 void ModuleDelayJoin::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
146 {
147         if (chan->IsModeSet('D'))
148         {
149                 /* Send silenced event only to the user being kicked and the user doing the kick */
150                 if (user->GetExt("delayjoin_"+chan->name))
151                 {
152                         silent = true;
153                         user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
154                 }
155         }
156 }
157
158 void ModuleDelayJoin::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
159 {
160         Command* parthandler = ServerInstance->Parser->GetHandler("PART");
161         if (parthandler && user->GetExt("delayjoin"))
162         {
163                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
164                 {
165                         std::vector<std::string> parameters;
166                         parameters.push_back(f->first->name);
167                         /* This triggers our OnUserPart, above, making the PART silent */
168                         parthandler->Handle(parameters, user);
169                 }
170         }
171 }
172
173 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
174 {
175         /* Server origin */
176         if (!user)
177                 return;
178
179         if (target_type != TYPE_CHANNEL)
180                 return;
181
182         Channel* channel = (Channel*) dest;
183
184         if (!user->GetExt("delayjoin_"+channel->name))
185                 return;
186
187         /* Display the join to everyone else (the user who joined got it earlier) */
188         this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
189
190         std::string n = this->ServerInstance->Modes->ModeString(user, channel);
191         if (n.length() > 0)
192                 this->WriteCommonFrom(user, channel, "MODE %s +%s", channel->name.c_str(), n.c_str());
193
194         /* Shrink off the neccessary metadata for a specific channel */
195         user->Shrink("delayjoin_"+channel->name);
196
197         /* Check if the user is left on any other +D channels, if so don't take away the
198          * metadata that says theyre on one or more channels
199          */
200         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
201                 if (f->first->IsModeSet('D'))
202                         return;
203
204         user->Shrink("delayjoin");
205 }
206
207 // .. is there a real need to duplicate WriteCommonExcept?
208 void ModuleDelayJoin::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().c_str(), 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 MODULE_INIT(ModuleDelayJoin)
236