]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
fixed some indentation and spacing in modules
[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 /* $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, bool)
27         {
28                 if (channel->IsModeSet('D') != adding)
29                 {
30                         if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
31                         {
32                                 source->WriteNumeric(482, "%s %s :Only channel operators may %sset channel mode +D", source->nick.c_str(), channel->name.c_str(), adding ? "" : "un");
33                                 return MODEACTION_DENY;
34                         }
35                         else
36                         {
37                                 if (channel->IsModeSet('D'))
38                                 {
39                                         /*
40                                          * Make all users visible, as +D is being removed. If we don't do this,
41                                          * they remain permanently 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->Modes->AddMode(djm))
69                         throw ModuleException("Could not add new modes!");
70                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnText };
71                 ServerInstance->Modules->Attach(eventlist, this, 6);
72         }
73
74         virtual ~ModuleDelayJoin()
75         {
76                 ServerInstance->Modes->DelMode(djm);
77                 delete djm;
78         }
79
80         virtual Version GetVersion()
81         {
82                 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
83         }
84
85         virtual void OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
86         {
87                 if (!channel->IsModeSet('D'))
88                         return;
89
90                 if (nick.empty())
91                         return;
92
93                 /* If the user is hidden by delayed join, hide them from the NAMES list */
94                 std::string key("delayjoin_");
95                 key.append(channel->name);
96
97                 if (user->GetExt(key))
98                         nick.clear();
99         }
100
101         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
102         {
103                 if (channel->IsModeSet('D'))
104                 {
105                         silent = true;
106                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
107                         user->WriteFrom(user, "JOIN %s", channel->name.c_str());
108
109                         /* This metadata tells the module the user is delayed join on this specific channel */
110                         user->Extend("delayjoin_"+channel->name);
111
112                         /* This metadata tells the module the user is delayed join on at least one (or more) channels.
113                          * It is only cleared when the user is no longer on ANY +D channels.
114                          */
115                         if (!user->GetExt("delayjoin"))
116                                 user->Extend("delayjoin");
117                 }
118         }
119
120         void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
121         {
122                 if (channel->IsModeSet('D'))
123                 {
124                         if (user->GetExt("delayjoin_"+channel->name))
125                         {
126                                 silent = true;
127                                 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
128                                 user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
129                         }
130                 }
131         }
132
133         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
134         {
135                 if (chan->IsModeSet('D'))
136                 {
137                         /* Send silenced event only to the user being kicked and the user doing the kick */
138                         if (user->GetExt("delayjoin_"+chan->name))
139                         {
140                                 silent = true;
141                                 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
142                         }
143                 }
144         }
145
146         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
147         {
148                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
149                 if (parthandler && user->GetExt("delayjoin"))
150                 {
151                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
152                         {
153                                 std::vector<std::string> parameters;
154                                 parameters.push_back(f->first->name);
155                                 /* This triggers our OnUserPart, above, making the PART silent */
156                                 parthandler->Handle(parameters, user);
157                         }
158                 }
159         }
160
161         void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
162         {
163                 /* Server origin */
164                 if (!user)
165                         return;
166
167                 if (target_type != TYPE_CHANNEL)
168                         return;
169
170                 Channel* channel = (Channel*) dest;
171
172                 if (!user->GetExt("delayjoin_"+channel->name))
173                         return;
174
175                 /* Display the join to everyone else (the user who joined got it earlier) */
176                 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
177
178                 std::string n = this->ServerInstance->Modes->ModeString(user, channel);
179                 if (n.length() > 0)
180                         this->WriteCommonFrom(user, channel, "MODE %s +%s", channel->name.c_str(), n.c_str());
181
182                 /* Shrink off the neccessary metadata for a specific channel */
183                 user->Shrink("delayjoin_"+channel->name);
184
185                 /* Check if the user is left on any other +D channels, if so don't take away the
186                  * metadata that says theyre on one or more channels
187                  */
188                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
189                         if (f->first->IsModeSet('D'))
190                                 return;
191
192                 user->Shrink("delayjoin");
193         }
194
195         // .. is there a real need to duplicate WriteCommonExcept?
196         void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5)
197         {
198                 va_list argsPtr;
199                 char textbuffer[MAXBUF];
200                 char tb[MAXBUF];
201
202                 va_start(argsPtr, text);
203                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
204                 va_end(argsPtr);
205                 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
206
207                 CUList *ulist = channel->GetUsers();
208
209                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
210                 {
211                         /* User doesnt get a JOIN sent to themselves */
212                         if (user == i->first)
213                                 continue;
214
215                         /* Users with a visibility state that hides them dont appear */
216                         if (user->Visibility && !user->Visibility->VisibleTo(i->first))
217                                 continue;
218
219                         i->first->Write(std::string(tb));
220                 }
221         }
222
223 };
224
225 MODULE_INIT(ModuleDelayJoin)
226