]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_invisible.cpp
Change module API to use LocalUser* where correct
[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(Module* Creator) : ModeHandler(Creator, "invis-oper", '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                         /* User appears to vanish or appear from nowhere */
45                         for (UCListIter f = dest->chans.begin(); f != dest->chans.end(); f++)
46                         {
47                                 const UserMembList *ulist = (*f)->GetUsers();
48                                 char tb[MAXBUF];
49
50                                 snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost().c_str(), adding ? "PART" : "JOIN", (*f)->name.c_str());
51                                 std::string out = tb;
52                                 std::string n = ServerInstance->Modes->ModeString(dest, (*f));
53
54                                 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
55                                 {
56                                         /* User only appears to vanish for non-opers */
57                                         if (IS_LOCAL(i->first) && !IS_OPER(i->first))
58                                         {
59                                                 i->first->Write(out);
60                                                 if (!n.empty() && !adding)
61                                                         i->first->WriteServ("MODE %s +%s", (*f)->name.c_str(), n.c_str());
62                                         }
63                                 }
64                         }
65
66                         ServerInstance->SNO->WriteToSnoMask('a', "\2NOTICE\2: Oper %s has become %svisible (%cQ)", dest->GetFullHost().c_str(), adding ? "in" : "", adding ? '+' : '-');
67                         return MODEACTION_ALLOW;
68                 }
69                 else
70                 {
71                         return MODEACTION_DENY;
72                 }
73         }
74 };
75
76 class InvisibleDeOper : public ModeWatcher
77 {
78  public:
79         InvisibleDeOper(Module* parent) : ModeWatcher(parent, 'o', MODETYPE_USER)
80         {
81         }
82
83         bool BeforeMode(User* source, User* dest, Channel* channel, std::string &param, bool adding, ModeType type)
84         {
85                 /* Users who are opers and have +Q get their +Q removed when they deoper */
86                 if ((!adding) && (dest->IsModeSet('Q')))
87                 {
88                         std::vector<std::string> newmodes;
89                         newmodes.push_back(dest->nick);
90                         newmodes.push_back("-Q");
91                         ServerInstance->Modes->Process(newmodes, source);
92                 }
93                 return true;
94         }
95 };
96
97
98 class ModuleInvisible : public Module
99 {
100  private:
101         InvisibleMode qm;
102         InvisibleDeOper ido;
103  public:
104         ModuleInvisible() : qm(this), ido(this)
105         {
106                 if (!ServerInstance->Modes->AddMode(&qm))
107                         throw ModuleException("Could not add new modes!");
108                 if (!ServerInstance->Modes->AddModeWatcher(&ido))
109                         throw ModuleException("Could not add new mode watcher on usermode +o!");
110
111                 /* Yeah i know people can take this out. I'm not about to obfuscate code just to be a pain in the ass. */
112                 ServerInstance->Users->ServerNoticeAll("*** m_invisible.so has just been loaded on this network. For more information, please visit http://inspircd.org/wiki/Modules/invisible");
113                 Implementation eventlist[] = {
114                         I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserJoin,
115                         I_OnBuildNeighborList, I_OnSendWhoLine, I_OnNamesListItem
116                 };
117                 ServerInstance->Modules->Attach(eventlist, this, 6);
118         };
119
120         ~ModuleInvisible()
121         {
122         };
123
124         Version GetVersion();
125         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts);
126         void OnBuildNeighborList(User* source, UserChanList &include, std::map<User*,bool> &exceptions);
127         ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
128         ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
129         void OnSendWhoLine(User* source, User* user, Channel* channel, std::string& line);
130         void OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick);
131 };
132
133 Version ModuleInvisible::GetVersion()
134 {
135         return Version("Allows opers to join channels invisibly", VF_COMMON | VF_VENDOR);
136 }
137
138 static void BuildExcept(Membership* memb, CUList& excepts)
139 {
140         const UserMembList* users = memb->chan->GetUsers();
141         for(UserMembCIter i = users->begin(); i != users->end(); i++)
142         {
143                 // hide from all local non-opers
144                 if (IS_LOCAL(i->first) && !IS_OPER(i->first))
145                         excepts.insert(i->first);
146         }
147 }
148
149 void ModuleInvisible::OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
150 {
151         if (memb->user->IsModeSet('Q'))
152         {
153                 BuildExcept(memb, excepts);
154                 ServerInstance->SNO->WriteToSnoMask('a', "\2NOTICE\2: Oper %s has joined %s invisibly (+Q)",
155                         memb->user->GetFullHost().c_str(), memb->chan->name.c_str());
156         }
157 }
158
159 void ModuleInvisible::OnBuildNeighborList(User* source, UserChanList &include, std::map<User*,bool> &exceptions)
160 {
161         if (source->IsModeSet('Q'))
162         {
163                 include.clear();
164         }
165 }
166
167 /* No privmsg response when hiding - submitted by Eric at neowin */
168 ModResult ModuleInvisible::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
169 {
170         if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
171         {
172                 User* target = (User*)dest;
173                 if(target->IsModeSet('Q') && !IS_OPER(user))
174                 {
175                         user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), target->nick.c_str());
176                         return MOD_RES_DENY;
177                 }
178         }
179         return MOD_RES_PASSTHRU;
180 }
181
182 ModResult ModuleInvisible::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
183 {
184         return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
185 }
186
187 void ModuleInvisible::OnSendWhoLine(User* source, User* user, Channel* channel, std::string& line)
188 {
189         if (user->IsModeSet('Q') && !IS_OPER(source))
190                 line.clear();
191 }
192
193 void ModuleInvisible::OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick)
194 {
195         if (memb->user->IsModeSet('Q') && !IS_OPER(issuer))
196                 nick.clear();
197 }
198
199 MODULE_INIT(ModuleInvisible)