]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_invisible.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[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 "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.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                         /* Set visibility handler object */
87                         dest->Visibility = adding ? qo : NULL;
88
89                         /* User appears to vanish or appear from nowhere */
90                         for (UCListIter f = dest->chans.begin(); f != dest->chans.end(); f++)
91                         {
92                                 CUList *ulist = f->first->GetUsers();
93                                 char tb[MAXBUF];
94
95                                 snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost(), adding ? "PART" : "JOIN", f->first->name);
96                                 std::string out = tb;
97                                 std::string n = this->ServerInstance->Modes->ModeString(dest, f->first);
98
99                                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
100                                 {
101                                         /* User only appears to vanish for non-opers */
102                                         if (IS_LOCAL(i->first) && !IS_OPER(i->first))
103                                         {
104                                                 i->first->Write(out);
105                                                 if (!n.empty() && !adding)
106                                                         i->first->WriteServ("MODE %s +%s", f->first->name, n.c_str());
107                                         }
108                                 }
109
110                                 ServerInstance->WriteOpers("*** \2NOTICE\2: Oper %s has become %svisible (%sQ)", dest->GetFullHost(), adding ? "in" : "", adding ? "+" : "-");
111                         }
112                         return MODEACTION_ALLOW;
113                 }
114                 else
115                 {
116                         return MODEACTION_DENY;
117                 }
118         }
119 };
120
121 class InvisibleDeOper : public ModeWatcher
122 {
123  private:
124         InspIRCd* Srv;
125  public:
126         InvisibleDeOper(InspIRCd* Instance) : ModeWatcher(Instance, 'o', MODETYPE_USER), Srv(Instance)
127         {
128         }
129
130         bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &param, bool adding, ModeType type)
131         {
132                 /* Users who are opers and have +Q get their +Q removed when they deoper */
133                 if ((!adding) && (dest->IsModeSet('Q')))
134                 {
135                         const char* newmodes[] = { dest->nick, "-Q" };
136                         ServerInstance->Modes->Process(newmodes, 2, source, true);
137                 }
138                 return true;
139         }
140 };
141
142
143 class ModuleInvisible : public Module
144 {
145  private:
146         InvisibleMode* qm;
147         InvisibleDeOper* ido;
148  public:
149         ModuleInvisible(InspIRCd* Me)
150                 : Module(Me)
151         {
152                 conf = new ConfigReader(ServerInstance);
153                 qm = new InvisibleMode(ServerInstance);
154                 if (!ServerInstance->AddMode(qm, 'Q'))
155                         throw ModuleException("Could not add new modes!");
156                 ido = new InvisibleDeOper(ServerInstance);
157                 if (!ServerInstance->AddModeWatcher(ido))
158                         throw ModuleException("Could not add new mode watcher on usermode +o!");
159         }
160
161         virtual ~ModuleInvisible()
162         {
163                 ServerInstance->Modes->DelMode(qm);
164                 ServerInstance->Modes->DelModeWatcher(ido);
165                 DELETE(qm);
166                 DELETE(ido);
167                 DELETE(conf);
168         }
169
170         virtual Version GetVersion()
171         {
172                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
173         }
174
175         void Implements(char* List)
176         {
177                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = List[I_OnRehash] = 1;
178         }
179         
180         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
181         {
182                 if (user->IsModeSet('Q'))
183                 {
184                         silent = true;
185                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
186                         this->WriteCommonFrom(user, channel, "JOIN %s", channel->name);
187                         ServerInstance->WriteOpers("*** \2NOTICE\2: Oper %s has joined %s invisibly (+Q)", user->GetFullHost(), channel->name);
188                 }
189         }
190
191         virtual void OnRehash(userrec* user, const std::string &parameter)
192         {
193                 DELETE(conf);
194                 conf = new ConfigReader(ServerInstance);
195         }
196
197         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
198         {
199                 if (user->IsModeSet('Q'))
200                 {
201                         silent = true;
202                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
203                         this->WriteCommonFrom(user, channel, "PART %s%s%s", channel->name,
204                                         partmessage.empty() ? "" : " :",
205                                         partmessage.empty() ? "" : partmessage.c_str());
206                 }
207         }
208
209         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
210         {
211                 if (user->IsModeSet('Q'))
212                 {
213                         command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
214                         std::vector<std::string> to_leave;
215                         const char* parameters[2];
216                         if (parthandler)
217                         {
218                                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
219                                                 to_leave.push_back(f->first->name);
220                                 /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
221                                 for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
222                                 {
223                                         parameters[0] = n->c_str();
224                                         /* This triggers our OnUserPart, above, making the PART silent */
225                                         parthandler->Handle(parameters, 1, user);
226                                 }
227                         }
228                 }
229         }
230
231         /* No privmsg response when hiding - submitted by Eric at neowin */
232         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
233         {
234                 if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
235                 {
236                         userrec* target = (userrec*)dest;
237                         if(target->IsModeSet('Q') && !*user->oper)
238                         {
239                                 user->WriteServ("401 %s %s :No such nick/channel",user->nick, target->nick);
240                                 return 1;
241                         }
242                 }
243                 return 0;
244         }
245         
246         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
247         {
248                 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
249         }
250
251         /* Fix by Eric @ neowin.net, thanks :) -- Brain */
252         void WriteCommonFrom(userrec *user, chanrec* channel, const char* text, ...)
253         {
254                 va_list argsPtr;
255                 char textbuffer[MAXBUF];
256                 char tb[MAXBUF];
257         
258                 va_start(argsPtr, text);
259                 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
260                 va_end(argsPtr);
261                 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),textbuffer);
262                 
263                 CUList *ulist = channel->GetUsers();
264                 
265                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
266                 {
267                         /* User only appears to vanish for non-opers */
268                         if (IS_LOCAL(i->first) && IS_OPER(i->first))
269                         {
270                                 i->first->Write(std::string(tb));
271                         }
272                 }
273         }
274
275 };
276
277 class ModuleInvisibleFactory : public ModuleFactory
278 {
279  public:
280         ModuleInvisibleFactory()
281         {
282         }
283         
284         ~ModuleInvisibleFactory()
285         {
286         }
287         
288         virtual Module * CreateModule(InspIRCd* Me)
289         {
290                 return new ModuleInvisible(Me);
291         }
292         
293 };
294
295 extern "C" DllExport void * init_module( void )
296 {
297         return new ModuleInvisibleFactory;
298 }
299