1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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 InvisibleMode : public ModeHandler
24 InvisibleMode(InspIRCd* Instance, Module* Creator) : ModeHandler(Instance, Creator, 'Q', 0, 0, false, MODETYPE_USER, true)
32 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
34 if (dest->IsModeSet('Q') != adding)
36 dest->SetMode('Q', adding);
38 /* Fix for bug #379 reported by stealth. On +/-Q make m_watch think the user has signed on/off */
39 Module* m = ServerInstance->Modules->Find("m_watch.so");
41 /* This must come before setting/unsetting the handler */
43 m->OnUserQuit(dest, "Connection closed", "Connection closed");
45 /* This has to come after setting/unsetting the handler */
47 m->OnPostConnect(dest);
49 /* User appears to vanish or appear from nowhere */
50 for (UCListIter f = dest->chans.begin(); f != dest->chans.end(); f++)
52 const UserMembList *ulist = (*f)->GetUsers();
55 snprintf(tb,MAXBUF,":%s %s %s", dest->GetFullHost().c_str(), adding ? "PART" : "JOIN", (*f)->name.c_str());
57 std::string n = this->ServerInstance->Modes->ModeString(dest, (*f));
59 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
61 /* User only appears to vanish for non-opers */
62 if (IS_LOCAL(i->first) && !IS_OPER(i->first))
65 if (!n.empty() && !adding)
66 i->first->WriteServ("MODE %s +%s", (*f)->name.c_str(), n.c_str());
71 ServerInstance->SNO->WriteToSnoMask('a', "\2NOTICE\2: Oper %s has become %svisible (%cQ)", dest->GetFullHost().c_str(), adding ? "in" : "", adding ? '+' : '-');
72 return MODEACTION_ALLOW;
76 return MODEACTION_DENY;
81 class InvisibleDeOper : public ModeWatcher
84 InvisibleDeOper(InspIRCd* Instance) : ModeWatcher(Instance, 'o', MODETYPE_USER)
88 bool BeforeMode(User* source, User* dest, Channel* channel, std::string ¶m, bool adding, ModeType type)
90 /* Users who are opers and have +Q get their +Q removed when they deoper */
91 if ((!adding) && (dest->IsModeSet('Q')))
93 std::vector<std::string> newmodes;
94 newmodes.push_back(dest->nick);
95 newmodes.push_back("-Q");
96 ServerInstance->Modes->Process(newmodes, source);
103 class ModuleInvisible : public Module
109 ModuleInvisible(InspIRCd* Me)
110 : Module(Me), qm(Me, this), ido(Me)
112 conf = new ConfigReader(ServerInstance);
113 if (!ServerInstance->Modes->AddMode(&qm))
114 throw ModuleException("Could not add new modes!");
115 if (!ServerInstance->Modes->AddModeWatcher(&ido))
116 throw ModuleException("Could not add new mode watcher on usermode +o!");
118 /* Yeah i know people can take this out. I'm not about to obfuscate code just to be a pain in the ass. */
119 ServerInstance->Users->ServerNoticeAll("*** m_invisible.so has just been loaded on this network. For more information, please visit http://inspircd.org/wiki/Modules/invisible");
120 Implementation eventlist[] = {
121 I_OnUserPreMessage, I_OnUserPreNotice, I_OnUserJoin, I_OnUserPart, I_OnUserQuit,
122 I_OnRehash, I_OnHostCycle, I_OnSendWhoLine
124 ServerInstance->Modules->Attach(eventlist, this, 8);
127 virtual ~ModuleInvisible()
129 ServerInstance->Modes->DelMode(&qm);
130 ServerInstance->Modes->DelModeWatcher(&ido);
134 Version GetVersion();
135 void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created);
136 void OnRehash(User* user);
137 void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
138 void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
139 ModResult OnHostCycle(User* user);
140 ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
141 ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
142 void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5);
143 void OnSendWhoLine(User* source, User* user, Channel* channel, std::string& line);
146 Version ModuleInvisible::GetVersion()
148 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
151 void ModuleInvisible::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
153 if (user->IsModeSet('Q'))
156 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
157 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
158 ServerInstance->SNO->WriteToSnoMask('a', "\2NOTICE\2: Oper %s has joined %s invisibly (+Q)", user->GetFullHost().c_str(), channel->name.c_str());
162 void ModuleInvisible::OnRehash(User* user)
165 conf = new ConfigReader(ServerInstance);
168 void ModuleInvisible::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
170 if (user->IsModeSet('Q'))
173 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
174 this->WriteCommonFrom(user, channel, "PART %s%s%s", channel->name.c_str(),
175 partmessage.empty() ? "" : " :",
176 partmessage.empty() ? "" : partmessage.c_str());
180 void ModuleInvisible::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
182 if (user->IsModeSet('Q'))
184 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
185 std::vector<std::string> to_leave;
188 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
189 to_leave.push_back((*f)->name);
190 /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
191 for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
193 std::vector<std::string> parameters;
194 parameters.push_back(*n);
195 /* This triggers our OnUserPart, above, making the PART silent */
196 parthandler->Handle(parameters, user);
202 ModResult ModuleInvisible::OnHostCycle(User* user)
204 return user->IsModeSet('Q') ? MOD_RES_DENY : MOD_RES_PASSTHRU;
207 /* No privmsg response when hiding - submitted by Eric at neowin */
208 ModResult ModuleInvisible::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
210 if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
212 User* target = (User*)dest;
213 if(target->IsModeSet('Q') && !IS_OPER(user))
215 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), target->nick.c_str());
219 return MOD_RES_PASSTHRU;
222 ModResult ModuleInvisible::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
224 return OnUserPreNotice(user, dest, target_type, text, status, exempt_list);
227 /* Fix by Eric @ neowin.net, thanks :) -- Brain */
228 void ModuleInvisible::WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
231 char textbuffer[MAXBUF];
234 va_start(argsPtr, text);
235 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
237 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
239 const UserMembList *ulist = channel->GetUsers();
241 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
243 /* User only appears to vanish for non-opers */
244 if (IS_LOCAL(i->first) && IS_OPER(i->first))
246 i->first->Write(std::string(tb));
251 void ModuleInvisible::OnSendWhoLine(User* source, User* user, Channel* channel, std::string& line)
253 if (user->IsModeSet('Q') && !IS_OPER(source))
257 MODULE_INIT(ModuleInvisible)