]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_invisible.cpp
Forgot something important!
[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
19 /* $ModDesc: Allows for opered clients to join channels without being seen, similar to unreal 3.1 +I mode */
20
21 static ConfigReader* conf;
22
23 class QuietOper : public VisData
24 {
25  public:
26         QuietOper()
27         {
28         }
29
30         virtual ~QuietOper()
31         {
32         }
33
34         virtual bool VisibleTo(userrec* user)
35         {
36                 return IS_OPER(user);
37         }
38 };
39
40
41 class InvisibleMode : public ModeHandler
42 {
43         QuietOper* qo;
44  public:
45         InvisibleMode(InspIRCd* Instance) : ModeHandler(Instance, 'Q', 0, 0, false, MODETYPE_USER, true)
46         {
47                 qo = new QuietOper();
48         }
49
50         ~InvisibleMode()
51         {
52                 for (user_hash::iterator i = ServerInstance->clientlist->begin(); i != ServerInstance->clientlist->end(); i++)
53                         if (i->second->Visibility == qo)
54                                 i->second->Visibility = NULL;
55                 delete qo;
56         }
57
58         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
59         {
60                 if ((source != dest) && (!*source->oper))
61                         return MODEACTION_DENY;
62
63                 if (dest->IsModeSet('Q') != adding)
64                 {
65                         bool ok = false;
66
67                         for (int j = 0; j < conf->Enumerate("type"); j++)
68                         {
69                                 std::string opertype = conf->ReadValue("type","name",j);
70                                 if (opertype == source->oper)
71                                 {
72                                         ok = conf->ReadFlag("type", "canquiet", j);
73                                         break;
74                                 }
75                         }
76
77                         if (!ok)
78                         {
79                                 source->WriteServ("481 %s :Permission Denied - You do not have access to become invisible via user mode +Q", source->nick);
80                                 return MODEACTION_DENY;
81                         }
82
83                         dest->SetMode('Q', adding);
84
85                         /* Set visibility handler object */
86                         dest->Visibility = adding ? qo : NULL;
87
88                         /* User appears to vanish or appear from nowhere */
89                         for (UCListIter f = dest->chans.begin(); f != dest->chans.end(); f++)
90                         {
91                                 CUList *ulist = f->first->GetUsers();
92                                 char tb[MAXBUF];
93
94                                 snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost(), adding ? "PART" : "JOIN", f->first->name);
95                                 std::string out = tb;
96                                 std::string n = this->ServerInstance->Modes->ModeString(dest, f->first);
97
98                                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
99                                 {
100                                         /* User only appears to vanish for non-opers */
101                                         if (IS_LOCAL(i->second) && !IS_OPER(i->second))
102                                         {
103                                                 i->second->Write(out);
104                                                 if (!n.empty() && !adding)
105                                                         i->second->WriteServ("MODE %s +%s", f->first->name, n.c_str());
106                                         }
107                                 }
108                         }
109                         return MODEACTION_ALLOW;
110                 }
111                 else
112                 {
113                         return MODEACTION_DENY;
114                 }
115         }
116 };
117
118 class InvisibleDeOper : public ModeWatcher
119 {
120  private:
121         InspIRCd* Srv;
122  public:
123         InvisibleDeOper(InspIRCd* Instance) : ModeWatcher(Instance, 'o', MODETYPE_USER), Srv(Instance)
124         {
125         }
126
127         bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &param, bool adding, ModeType type)
128         {
129                 /* Users who are opers and have +Q get their +Q removed when they deoper */
130                 if ((!adding) && (dest->IsModeSet('Q')))
131                 {
132                         const char* newmodes[] = { dest->nick, "-Q" };
133                         ServerInstance->Modes->Process(newmodes, 2, source, true);
134                 }
135                 return true;
136         }
137 };
138
139
140 class ModuleInvisible : public Module
141 {
142  private:
143         InvisibleMode* qm;
144         InvisibleDeOper* ido;
145  public:
146         ModuleInvisible(InspIRCd* Me)
147                 : Module::Module(Me)
148         {
149                 conf = new ConfigReader(ServerInstance);
150                 qm = new InvisibleMode(ServerInstance);
151                 if (!ServerInstance->AddMode(qm, 'Q'))
152                         throw ModuleException("Could not add new modes!");
153                 ido = new InvisibleDeOper(ServerInstance);
154                 if (!ServerInstance->AddModeWatcher(ido))
155                         throw ModuleException("Could not add new mode watcher on usermode +o!");
156         }
157
158         virtual ~ModuleInvisible()
159         {
160                 ServerInstance->Modes->DelMode(qm);
161                 ServerInstance->Modes->DelModeWatcher(ido);
162                 DELETE(qm);
163                 DELETE(ido);
164                 DELETE(conf);
165         }
166
167         virtual Version GetVersion()
168         {
169                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
170         }
171
172         void Implements(char* List)
173         {
174                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = List[I_OnRehash] = 1;
175         }
176         
177         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
178         {
179                 if (user->IsModeSet('Q'))
180                 {
181                         silent = true;
182                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
183                         user->WriteFrom(user, "JOIN %s", channel->name);
184                 }
185         }
186
187         virtual void OnRehash(userrec* user, const std::string &parameter)
188         {
189                 DELETE(conf);
190                 conf = new ConfigReader(ServerInstance);
191         }
192
193         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
194         {
195                 if (user->IsModeSet('Q'))
196                 {
197                         silent = true;
198                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
199                         user->WriteFrom(user, "PART %s%s%s", channel->name,
200                                         partmessage.empty() ? "" : " :",
201                                         partmessage.empty() ? "" : partmessage.c_str());
202                 }
203         }
204
205         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
206         {
207                 if (user->IsModeSet('Q'))
208                 {
209                         command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
210                         std::vector<std::string> to_leave;
211                         const char* parameters[2];
212                         if (parthandler)
213                         {
214                                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
215                                                 to_leave.push_back(f->first->name);
216                                 /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
217                                 for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
218                                 {
219                                         parameters[0] = n->c_str();
220                                         /* This triggers our OnUserPart, above, making the PART silent */
221                                         parthandler->Handle(parameters, 1, user);
222                                 }
223                         }
224                 }
225         }
226 };
227
228 class ModuleInvisibleFactory : public ModuleFactory
229 {
230  public:
231         ModuleInvisibleFactory()
232         {
233         }
234         
235         ~ModuleInvisibleFactory()
236         {
237         }
238         
239         virtual Module * CreateModule(InspIRCd* Me)
240         {
241                 return new ModuleInvisible(Me);
242         }
243         
244 };
245
246 extern "C" void * init_module( void )
247 {
248         return new ModuleInvisibleFactory;
249 }
250