1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 /* $ModDesc: Allows for opered clients to join channels without being seen, similar to unreal 3.1 +I mode */
19 static ConfigReader* conf;
21 class QuietOper : public VisData
32 virtual bool VisibleTo(User* user)
39 class InvisibleMode : public ModeHandler
43 InvisibleMode(InspIRCd* Instance) : ModeHandler(Instance, 'Q', 0, 0, false, MODETYPE_USER, true)
50 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
51 if (i->second->Visibility == qo)
52 i->second->Visibility = NULL;
56 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
58 if (dest->IsModeSet('Q') != adding)
62 for (int j = 0; j < conf->Enumerate("type"); j++)
64 std::string opertype = conf->ReadValue("type","name",j);
65 if (opertype == source->oper)
67 ok = conf->ReadFlag("type", "canquiet", j);
74 source->WriteNumeric(481, "%s :Permission Denied - You do not have access to become invisible via user mode +Q", source->nick.c_str());
75 return MODEACTION_DENY;
78 dest->SetMode('Q', adding);
80 /* Fix for bug #379 reported by stealth. On +/-Q make m_watch think the user has signed on/off */
81 Module* m = ServerInstance->Modules->Find("m_watch.so");
83 /* This must come before setting/unsetting the handler */
85 m->OnUserQuit(dest, "Connection closed", "Connection closed");
87 /* Set visibility handler object */
88 dest->Visibility = adding ? qo : NULL;
90 /* This has to come after setting/unsetting the handler */
92 m->OnPostConnect(dest);
94 /* User appears to vanish or appear from nowhere */
95 for (UCListIter f = dest->chans.begin(); f != dest->chans.end(); f++)
97 CUList *ulist = f->first->GetUsers();
100 snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost().c_str(), adding ? "PART" : "JOIN", f->first->name.c_str());
101 std::string out = tb;
102 std::string n = this->ServerInstance->Modes->ModeString(dest, f->first);
104 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
106 /* User only appears to vanish for non-opers */
107 if (IS_LOCAL(i->first) && !IS_OPER(i->first))
109 i->first->Write(out);
110 if (!n.empty() && !adding)
111 i->first->WriteServ("MODE %s +%s", f->first->name.c_str(), n.c_str());
116 ServerInstance->SNO->WriteToSnoMask('A', "\2NOTICE\2: Oper %s has become %svisible (%sQ)", dest->GetFullHost().c_str(), adding ? "in" : "", adding ? "+" : "-");
117 return MODEACTION_ALLOW;
121 return MODEACTION_DENY;
126 class InvisibleDeOper : public ModeWatcher
131 InvisibleDeOper(InspIRCd* Instance) : ModeWatcher(Instance, 'o', MODETYPE_USER), Srv(Instance)
135 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type, bool)
137 /* Users who are opers and have +Q get their +Q removed when they deoper */
138 if ((!adding) && (dest->IsModeSet('Q')))
140 std::vector<std::string> newmodes;
141 newmodes.push_back(dest->nick);
142 newmodes.push_back("-Q");
143 ServerInstance->Modes->Process(newmodes, source, true);
150 class ModuleInvisible : public Module
154 InvisibleDeOper* ido;
156 ModuleInvisible(InspIRCd* Me)
159 conf = new ConfigReader(ServerInstance);
160 qm = new InvisibleMode(ServerInstance);
161 if (!ServerInstance->Modes->AddMode(qm))
162 throw ModuleException("Could not add new modes!");
163 ido = new InvisibleDeOper(ServerInstance);
164 if (!ServerInstance->Modes->AddModeWatcher(ido))
165 throw ModuleException("Could not add new mode watcher on usermode +o!");
167 /* Yeah i know people can take this out. I'm not about to obfuscate code just to be a pain in the ass. */
168 ServerInstance->Users->ServerNoticeAll("*** m_invisible.so has just been loaded on this network. For more information, please visit http://inspircd.org/wiki/Modules/invisible");
169 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserJoin, I_OnUserPart, I_OnUserQuit, I_OnRehash, I_OnHostCycle };
170 ServerInstance->Modules->Attach(eventlist, this, 7);
173 virtual ~ModuleInvisible()
175 ServerInstance->Modes->DelMode(qm);
176 ServerInstance->Modes->DelModeWatcher(ido);
182 virtual Version GetVersion();
183 virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent);
184 virtual void OnRehash(User* user, const std::string ¶meter);
185 void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
186 void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
187 bool OnHostCycle(User* user);
188 /* No privmsg response when hiding - submitted by Eric at neowin */
189 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
190 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
191 /* Fix by Eric @ neowin.net, thanks :) -- Brain */
192 void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5);
195 Version ModuleInvisible::GetVersion()
197 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
200 void ModuleInvisible::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
202 if (user->IsModeSet('Q'))
205 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
206 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
207 ServerInstance->SNO->WriteToSnoMask('A', "\2NOTICE\2: Oper %s has joined %s invisibly (+Q)", user->GetFullHost().c_str(), channel->name.c_str());
211 void ModuleInvisible::OnRehash(User* user, const std::string ¶meter)
214 conf = new ConfigReader(ServerInstance);
217 void ModuleInvisible::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
219 if (user->IsModeSet('Q'))
222 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
223 this->WriteCommonFrom(user, channel, "PART %s%s%s", channel->name.c_str(),
224 partmessage.empty() ? "" : " :",
225 partmessage.empty() ? "" : partmessage.c_str());
229 void ModuleInvisible::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
231 if (user->IsModeSet('Q'))
233 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
234 std::vector<std::string> to_leave;
237 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
238 to_leave.push_back(f->first->name);
239 /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
240 for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
242 std::vector<std::string> parameters;
243 parameters.push_back(*n);
244 /* This triggers our OnUserPart, above, making the PART silent */
245 parthandler->Handle(parameters, user);
251 bool ModuleInvisible::OnHostCycle(User* user)
253 return user->IsModeSet('Q');
256 /* No privmsg response when hiding - submitted by Eric at neowin */
257 int ModuleInvisible::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
259 if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
261 User* target = (User*)dest;
262 if(target->IsModeSet('Q') && !IS_OPER(user))
264 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), target->nick.c_str());
271 int ModuleInvisible::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
273 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
276 /* Fix by Eric @ neowin.net, thanks :) -- Brain */
277 void ModuleInvisible::WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
280 char textbuffer[MAXBUF];
283 va_start(argsPtr, text);
284 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
286 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
288 CUList *ulist = channel->GetUsers();
290 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
292 /* User only appears to vanish for non-opers */
293 if (IS_LOCAL(i->first) && IS_OPER(i->first))
295 i->first->Write(std::string(tb));
300 MODULE_INIT(ModuleInvisible)