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