]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_invisible.cpp
Check visibility states
[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                         /* User appears to vanish or appear from nowhere */
85                         for (UCListIter f = dest->chans.begin(); f != dest->chans.end(); f++)
86                         {
87                                 CUList *ulist = f->first->GetUsers();
88                                 char tb[MAXBUF];
89
90                                 snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost(), adding ? "PART" : "JOIN", f->first->name);
91                                 std::string out = tb;
92                                 std::string n = this->ServerInstance->Modes->ModeString(dest, f->first);
93
94                                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
95                                 {
96                                         /* User only appears to vanish for non-opers */
97                                         if (IS_LOCAL(i->second) && !IS_OPER(i->second))
98                                         {
99                                                 i->second->Write(out);
100                                                 if (!n.empty() && !adding)
101                                                         i->second->WriteServ("MODE %s +%s", f->first->name, n.c_str());
102                                         }
103                                 }
104                         }
105                         return MODEACTION_ALLOW;
106                 }
107                 else
108                 {
109                         return MODEACTION_DENY;
110                 }
111         }
112 };
113
114 class InvisibleDeOper : public ModeWatcher
115 {
116  private:
117         InspIRCd* Srv;
118  public:
119         InvisibleDeOper(InspIRCd* Instance) : ModeWatcher(Instance, 'o', MODETYPE_USER), Srv(Instance)
120         {
121         }
122
123         bool BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &param, bool adding, ModeType type)
124         {
125                 /* Users who are opers and have +Q get their +Q removed when they deoper */
126                 if ((!adding) && (dest->IsModeSet('Q')))
127                 {
128                         const char* newmodes[] = { dest->nick, "-Q" };
129                         ServerInstance->Modes->Process(newmodes, 2, source, true);
130                 }
131                 return true;
132         }
133 };
134
135
136 class ModuleInvisible : public Module
137 {
138  private:
139         InvisibleMode* qm;
140         InvisibleDeOper* ido;
141  public:
142         ModuleInvisible(InspIRCd* Me)
143                 : Module::Module(Me)
144         {
145                 conf = new ConfigReader(ServerInstance);
146                 qm = new InvisibleMode(ServerInstance);
147                 if (!ServerInstance->AddMode(qm, 'Q'))
148                         throw ModuleException("Could not add new modes!");
149                 ido = new InvisibleDeOper(ServerInstance);
150                 if (!ServerInstance->AddModeWatcher(ido))
151                         throw ModuleException("Could not add new mode watcher on usermode +o!");
152         }
153
154         virtual ~ModuleInvisible()
155         {
156                 ServerInstance->Modes->DelMode(qm);
157                 ServerInstance->Modes->DelModeWatcher(ido);
158                 DELETE(qm);
159                 DELETE(ido);
160                 DELETE(conf);
161         }
162
163         virtual Version GetVersion()
164         {
165                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
166         }
167
168         void Implements(char* List)
169         {
170                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserQuit] = List[I_OnRehash] = 1;
171         }
172         
173         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
174         {
175                 if (user->IsModeSet('Q'))
176                 {
177                         silent = true;
178                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
179                         user->WriteFrom(user, "JOIN %s", channel->name);
180                 }
181         }
182
183         virtual void OnRehash(userrec* user, const std::string &parameter)
184         {
185                 DELETE(conf);
186                 conf = new ConfigReader(ServerInstance);
187         }
188
189         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
190         {
191                 if (user->IsModeSet('Q'))
192                 {
193                         silent = true;
194                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
195                         user->WriteFrom(user, "PART %s%s%s", channel->name,
196                                         partmessage.empty() ? "" : " :",
197                                         partmessage.empty() ? "" : partmessage.c_str());
198                 }
199         }
200
201         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
202         {
203                 if (user->IsModeSet('Q'))
204                 {
205                         command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
206                         std::vector<std::string> to_leave;
207                         const char* parameters[2];
208                         if (parthandler)
209                         {
210                                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
211                                                 to_leave.push_back(f->first->name);
212                                 /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
213                                 for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
214                                 {
215                                         parameters[0] = n->c_str();
216                                         /* This triggers our OnUserPart, above, making the PART silent */
217                                         parthandler->Handle(parameters, 1, user);
218                                 }
219                         }
220                 }
221         }
222 };
223
224 class ModuleInvisibleFactory : public ModuleFactory
225 {
226  public:
227         ModuleInvisibleFactory()
228         {
229         }
230         
231         ~ModuleInvisibleFactory()
232         {
233         }
234         
235         virtual Module * CreateModule(InspIRCd* Me)
236         {
237                 return new ModuleInvisible(Me);
238         }
239         
240 };
241
242 extern "C" void * init_module( void )
243 {
244         return new ModuleInvisibleFactory;
245 }
246