]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_invisible.cpp
More
[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 <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->clientlist->begin(); i != ServerInstance->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)
57         {
58                 if (source != dest)
59                         return MODEACTION_DENY;
60
61                 if (dest->IsModeSet('Q') != adding)
62                 {
63                         bool ok = false;
64
65                         for (int j = 0; j < conf->Enumerate("type"); j++)
66                         {
67                                 std::string opertype = conf->ReadValue("type","name",j);
68                                 if (opertype == source->oper)
69                                 {
70                                         ok = conf->ReadFlag("type", "canquiet", j);
71                                         break;
72                                 }
73                         }
74
75                         if (!ok)
76                         {
77                                 source->WriteServ("481 %s :Permission Denied - You do not have access to become invisible via user mode +Q", source->nick);
78                                 return MODEACTION_DENY;
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(), adding ? "PART" : "JOIN", f->first->name);
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, n.c_str());
115                                         }
116                                 }
117
118                                 ServerInstance->WriteOpers("*** \2NOTICE\2: Oper %s has become %svisible (%sQ)", dest->GetFullHost(), adding ? "in" : "", adding ? "+" : "-");
119                         }
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)
139         {
140                 /* Users who are opers and have +Q get their +Q removed when they deoper */
141                 if ((!adding) && (dest->IsModeSet('Q')))
142                 {
143                         const char* newmodes[] = { dest->nick, "-Q" };
144                         ServerInstance->Modes->Process(newmodes, 2, source, true);
145                 }
146                 return true;
147         }
148 };
149
150
151 class ModuleInvisible : public Module
152 {
153  private:
154         InvisibleMode* qm;
155         InvisibleDeOper* ido;
156  public:
157         ModuleInvisible(InspIRCd* Me)
158                 : Module(Me)
159         {
160                 conf = new ConfigReader(ServerInstance);
161                 qm = new InvisibleMode(ServerInstance);
162                 if (!ServerInstance->AddMode(qm))
163                         throw ModuleException("Could not add new modes!");
164                 ido = new InvisibleDeOper(ServerInstance);
165                 if (!ServerInstance->AddModeWatcher(ido))
166                         throw ModuleException("Could not add new mode watcher on usermode +o!");
167
168                 /* Yeah i know people can take this out. I'm not about to obfuscate code just to be a pain in the ass. */
169                 ServerInstance->ServerNoticeAll("*** m_invisible.so has just been loaded on this network. For more information, please visit http://inspircd.org/wiki/Modules/invisible");
170         }
171
172         virtual ~ModuleInvisible()
173         {
174                 ServerInstance->Modes->DelMode(qm);
175                 ServerInstance->Modes->DelModeWatcher(ido);
176                 DELETE(qm);
177                 DELETE(ido);
178                 DELETE(conf);
179         }
180
181         virtual Version GetVersion()
182         {
183                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
184         }
185
186         void Implements(char* List)
187         {
188                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = List[I_OnRehash] = 1;
189         }
190         
191         virtual void OnUserJoin(User* user, Channel* channel, bool &silent)
192         {
193                 if (user->IsModeSet('Q'))
194                 {
195                         silent = true;
196                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
197                         this->WriteCommonFrom(user, channel, "JOIN %s", channel->name);
198                         ServerInstance->WriteOpers("*** \2NOTICE\2: Oper %s has joined %s invisibly (+Q)", user->GetFullHost(), channel->name);
199                 }
200         }
201
202         virtual void OnRehash(User* user, const std::string &parameter)
203         {
204                 DELETE(conf);
205                 conf = new ConfigReader(ServerInstance);
206         }
207
208         void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
209         {
210                 if (user->IsModeSet('Q'))
211                 {
212                         silent = true;
213                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
214                         this->WriteCommonFrom(user, channel, "PART %s%s%s", channel->name,
215                                         partmessage.empty() ? "" : " :",
216                                         partmessage.empty() ? "" : partmessage.c_str());
217                 }
218         }
219
220         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
221         {
222                 if (user->IsModeSet('Q'))
223                 {
224                         Command* parthandler = ServerInstance->Parser->GetHandler("PART");
225                         std::vector<std::string> to_leave;
226                         const char* parameters[2];
227                         if (parthandler)
228                         {
229                                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
230                                                 to_leave.push_back(f->first->name);
231                                 /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
232                                 for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
233                                 {
234                                         parameters[0] = n->c_str();
235                                         /* This triggers our OnUserPart, above, making the PART silent */
236                                         parthandler->Handle(parameters, 1, user);
237                                 }
238                         }
239                 }
240         }
241
242         /* No privmsg response when hiding - submitted by Eric at neowin */
243         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
244         {
245                 if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
246                 {
247                         User* target = (User*)dest;
248                         if(target->IsModeSet('Q') && !*user->oper)
249                         {
250                                 user->WriteServ("401 %s %s :No such nick/channel",user->nick, target->nick);
251                                 return 1;
252                         }
253                 }
254                 return 0;
255         }
256         
257         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
258         {
259                 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
260         }
261
262         /* Fix by Eric @ neowin.net, thanks :) -- Brain */
263         void WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
264         {
265                 va_list argsPtr;
266                 char textbuffer[MAXBUF];
267                 char tb[MAXBUF];
268         
269                 va_start(argsPtr, text);
270                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
271                 va_end(argsPtr);
272                 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),textbuffer);
273                 
274                 CUList *ulist = channel->GetUsers();
275                 
276                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
277                 {
278                         /* User only appears to vanish for non-opers */
279                         if (IS_LOCAL(i->first) && IS_OPER(i->first))
280                         {
281                                 i->first->Write(std::string(tb));
282                         }
283                 }
284         }
285
286 };
287
288 MODULE_INIT(ModuleInvisible)