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