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