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