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