]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_invisible.cpp
Don't check permission to +Q for remote users, probably fixes bug #619 reported by...
[user/henk/code/inspircd.git] / src / modules / m_invisible.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 opered clients to join channels without being seen, similar to unreal 3.1 +I mode */
18
19 static ConfigReader* conf;
20
21 class QuietOper : public VisData
22 {
23  public:
24         QuietOper()
25         {
26         }
27
28         virtual ~QuietOper()
29         {
30         }
31
32         virtual bool VisibleTo(User* user)
33         {
34                 return IS_OPER(user);
35         }
36 };
37
38
39 class InvisibleMode : public ModeHandler
40 {
41         QuietOper* qo;
42  public:
43         InvisibleMode(InspIRCd* Instance) : ModeHandler(Instance, 'Q', 0, 0, false, MODETYPE_USER, true)
44         {
45                 qo = new QuietOper();
46         }
47
48         ~InvisibleMode()
49         {
50                 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
51                         if (i->second->Visibility == qo)
52                                 i->second->Visibility = NULL;
53                 delete qo;
54         }
55
56         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
57         {
58                 if (dest->IsModeSet('Q') != adding)
59                 {
60                         bool ok = false;
61
62                         if (IS_LOCAL(source))
63                         {
64                                 for (int j = 0; j < conf->Enumerate("type"); j++)
65                                 {
66                                         std::string opertype = conf->ReadValue("type","name",j);
67                                         if (opertype == source->oper)
68                                         {
69                                                 ok = conf->ReadFlag("type", "canquiet", j);
70                                                 break;
71                                         }
72                                 }
73
74                                 if (!ok)
75                                 {
76                                         source->WriteNumeric(481, "%s :Permission Denied - You do not have access to become invisible via user mode +Q", source->nick.c_str());
77                                         return MODEACTION_DENY;
78                                 }
79                         }
80
81                         dest->SetMode('Q', adding);
82
83                         /* Fix for bug #379 reported by stealth. On +/-Q make m_watch think the user has signed on/off */
84                         Module* m = ServerInstance->Modules->Find("m_watch.so");
85
86                         /* This must come before setting/unsetting the handler */
87                         if (m && adding)
88                                 m->OnUserQuit(dest, "Connection closed", "Connection closed");
89
90                         /* Set visibility handler object */
91                         dest->Visibility = adding ? qo : NULL;
92
93                         /* This has to come after setting/unsetting the handler */
94                         if (m && !adding)
95                                 m->OnPostConnect(dest);
96
97                         /* User appears to vanish or appear from nowhere */
98                         for (UCListIter f = dest->chans.begin(); f != dest->chans.end(); f++)
99                         {
100                                 CUList *ulist = f->first->GetUsers();
101                                 char tb[MAXBUF];
102
103                                 snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost().c_str(), adding ? "PART" : "JOIN", f->first->name.c_str());
104                                 std::string out = tb;
105                                 std::string n = this->ServerInstance->Modes->ModeString(dest, f->first);
106
107                                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
108                                 {
109                                         /* User only appears to vanish for non-opers */
110                                         if (IS_LOCAL(i->first) && !IS_OPER(i->first))
111                                         {
112                                                 i->first->Write(out);
113                                                 if (!n.empty() && !adding)
114                                                         i->first->WriteServ("MODE %s +%s", f->first->name.c_str(), n.c_str());
115                                         }
116                                 }
117                         }
118
119                         ServerInstance->SNO->WriteToSnoMask('A', "\2NOTICE\2: Oper %s has become %svisible (%sQ)", dest->GetFullHost().c_str(), adding ? "in" : "", adding ? "+" : "-");
120                         return MODEACTION_ALLOW;
121                 }
122                 else
123                 {
124                         return MODEACTION_DENY;
125                 }
126         }
127 };
128
129 class InvisibleDeOper : public ModeWatcher
130 {
131  private:
132         InspIRCd* Srv;
133  public:
134         InvisibleDeOper(InspIRCd* Instance) : ModeWatcher(Instance, 'o', MODETYPE_USER), Srv(Instance)
135         {
136         }
137
138         bool BeforeMode(User* source, User* dest, Channel* channel, std::string &param, bool adding, ModeType type, bool)
139         {
140                 /* Users who are opers and have +Q get their +Q removed when they deoper */
141                 if ((!adding) && (dest->IsModeSet('Q')))
142                 {
143                         std::vector<std::string> newmodes;
144                         newmodes.push_back(dest->nick);
145                         newmodes.push_back("-Q");
146                         ServerInstance->Modes->Process(newmodes, source, true);
147                 }
148                 return true;
149         }
150 };
151
152
153 class ModuleInvisible : public Module
154 {
155  private:
156         InvisibleMode* qm;
157         InvisibleDeOper* ido;
158  public:
159         ModuleInvisible(InspIRCd* Me)
160                 : Module(Me)
161         {
162                 conf = new ConfigReader(ServerInstance);
163                 qm = new InvisibleMode(ServerInstance);
164                 if (!ServerInstance->Modes->AddMode(qm))
165                         throw ModuleException("Could not add new modes!");
166                 ido = new InvisibleDeOper(ServerInstance);
167                 if (!ServerInstance->Modes->AddModeWatcher(ido))
168                         throw ModuleException("Could not add new mode watcher on usermode +o!");
169
170                 /* Yeah i know people can take this out. I'm not about to obfuscate code just to be a pain in the ass. */
171                 ServerInstance->Users->ServerNoticeAll("*** m_invisible.so has just been loaded on this network. For more information, please visit http://inspircd.org/wiki/Modules/invisible");
172                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserJoin, I_OnUserPart, I_OnUserQuit, I_OnRehash, I_OnHostCycle };
173                 ServerInstance->Modules->Attach(eventlist, this, 7);
174         };
175
176         virtual ~ModuleInvisible()
177         {
178                 ServerInstance->Modes->DelMode(qm);
179                 ServerInstance->Modes->DelModeWatcher(ido);
180                 delete qm;
181                 delete ido;
182                 delete conf;
183         };
184
185         virtual Version GetVersion();
186         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent);
187         virtual void OnRehash(User* user, const std::string &parameter);
188         void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
189         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
190         bool OnHostCycle(User* user);
191         /* No privmsg response when hiding - submitted by Eric at neowin */
192         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
193         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
194         /* Fix by Eric @ neowin.net, thanks :) -- Brain */
195         void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5);
196 };
197
198 Version ModuleInvisible::GetVersion()
199 {
200         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
201 }
202
203 void ModuleInvisible::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
204 {
205         if (user->IsModeSet('Q'))
206         {
207                 silent = true;
208                 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
209                 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
210                 ServerInstance->SNO->WriteToSnoMask('A', "\2NOTICE\2: Oper %s has joined %s invisibly (+Q)", user->GetFullHost().c_str(), channel->name.c_str());
211         }
212 }
213
214 void ModuleInvisible::OnRehash(User* user, const std::string &parameter)
215 {
216         delete conf;
217         conf = new ConfigReader(ServerInstance);
218 }
219
220 void ModuleInvisible::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
221 {
222         if (user->IsModeSet('Q'))
223         {
224                 silent = true;
225                 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
226                 this->WriteCommonFrom(user, channel, "PART %s%s%s", channel->name.c_str(),
227                                 partmessage.empty() ? "" : " :",
228                                 partmessage.empty() ? "" : partmessage.c_str());
229         }
230 }
231
232 void ModuleInvisible::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
233 {
234         if (user->IsModeSet('Q'))
235         {
236                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
237                 std::vector<std::string> to_leave;
238                 if (parthandler)
239                 {
240                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
241                                         to_leave.push_back(f->first->name);
242                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
243                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
244                         {
245                                 std::vector<std::string> parameters;
246                                 parameters.push_back(*n);
247                                 /* This triggers our OnUserPart, above, making the PART silent */
248                                 parthandler->Handle(parameters, user);
249                         }
250                 }
251         }
252 }
253
254 bool ModuleInvisible::OnHostCycle(User* user)
255 {
256         return user->IsModeSet('Q');
257 }
258
259 /* No privmsg response when hiding - submitted by Eric at neowin */
260 int ModuleInvisible::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
261 {
262         if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
263         {
264                 User* target = (User*)dest;
265                 if(target->IsModeSet('Q') && !IS_OPER(user))
266                 {
267                         user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), target->nick.c_str());
268                         return 1;
269                 }
270         }
271         return 0;
272 }
273
274 int ModuleInvisible::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
275 {
276         return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
277 }
278
279 /* Fix by Eric @ neowin.net, thanks :) -- Brain */
280 void ModuleInvisible::WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
281 {
282         va_list argsPtr;
283         char textbuffer[MAXBUF];
284         char tb[MAXBUF];
285
286         va_start(argsPtr, text);
287         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
288         va_end(argsPtr);
289         snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
290
291         CUList *ulist = channel->GetUsers();
292
293         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
294         {
295                 /* User only appears to vanish for non-opers */
296                 if (IS_LOCAL(i->first) && IS_OPER(i->first))
297                 {
298                         i->first->Write(std::string(tb));
299                 }
300         }
301 }
302
303 MODULE_INIT(ModuleInvisible)