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