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 class DelayJoinMode : public ModeHandler
23 DelayJoinMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, 'D', 0, 0, false, MODETYPE_CHANNEL, false, 0, '@'), Creator(Parent) {};
25 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool);
28 class ModuleDelayJoin : public Module
34 ModuleDelayJoin(InspIRCd* Me) : Module(Me)
36 djm = new DelayJoinMode(ServerInstance, this);
37 if (!ServerInstance->Modes->AddMode(djm))
38 throw ModuleException("Could not add new modes!");
39 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnText, I_OnHostCycle };
40 ServerInstance->Modules->Attach(eventlist, this, 7);
42 virtual ~ModuleDelayJoin();
43 virtual Version GetVersion();
44 virtual void OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick);
45 virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent);
46 void CleanUser(User* user);
47 bool OnHostCycle(User* user);
48 void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
49 void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent);
50 void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
51 void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list);
52 void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5);
55 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
57 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
60 if (channel->IsModeSet('D') == adding)
61 return MODEACTION_DENY;
66 * Make all users visible, as +D is being removed. If we don't do this,
67 * they remain permanently invisible on this channel!
69 CUList* names = channel->GetUsers();
70 for (CUListIter n = names->begin(); n != names->end(); ++n)
71 Creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
73 channel->SetMode('D', adding);
74 return MODEACTION_ALLOW;
77 ModuleDelayJoin::~ModuleDelayJoin()
79 ServerInstance->Modes->DelMode(djm);
83 Version ModuleDelayJoin::GetVersion()
85 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
88 void ModuleDelayJoin::OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
90 if (!channel->IsModeSet('D'))
96 /* don't prevent the user from seeing themself */
100 /* If the user is hidden by delayed join, hide them from the NAMES list */
101 if (user->GetExt("delayjoin_"+channel->name))
105 void ModuleDelayJoin::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
107 if (channel->IsModeSet('D'))
110 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
111 user->WriteFrom(user, "JOIN %s", channel->name.c_str());
113 /* This metadata tells the module the user is delayed join on this specific channel */
114 user->Extend("delayjoin_"+channel->name);
116 /* This metadata tells the module the user is delayed join on at least one (or more) channels.
117 * It is only cleared when the user is no longer on ANY +D channels.
119 if (!user->GetExt("delayjoin"))
120 user->Extend("delayjoin");
124 void ModuleDelayJoin::CleanUser(User* user)
126 /* Check if the user is hidden on any other +D channels, if so don't take away the
127 * metadata that says they're hidden on one or more channels
129 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
130 if (user->GetExt("delayjoin_" + f->first->name))
133 user->Shrink("delayjoin");
136 void ModuleDelayJoin::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
138 if (!channel->IsModeSet('D'))
140 if (user->GetExt("delayjoin_"+channel->name))
142 user->Shrink("delayjoin_"+channel->name);
144 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
145 user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
150 void ModuleDelayJoin::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
152 if (!chan->IsModeSet('D'))
154 /* Send silenced event only to the user being kicked and the user doing the kick */
155 if (user->GetExt("delayjoin_"+chan->name))
157 user->Shrink("delayjoin_"+chan->name);
159 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
164 bool ModuleDelayJoin::OnHostCycle(User* user)
166 return user->GetExt("delayjoin");
169 void ModuleDelayJoin::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
171 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
172 if (parthandler && user->GetExt("delayjoin"))
174 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
176 Channel* chan = f->first;
177 if (user->GetExt("delayjoin_"+chan->name))
179 std::vector<std::string> parameters;
180 parameters.push_back(chan->name);
181 /* Send a fake PART from the channel, which will be silent */
182 parthandler->Handle(parameters, user);
185 user->Shrink("delayjoin");
189 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
195 if (target_type != TYPE_CHANNEL)
198 Channel* channel = (Channel*) dest;
200 if (!user->GetExt("delayjoin_"+channel->name))
203 /* Display the join to everyone else (the user who joined got it earlier) */
204 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
206 std::string n = this->ServerInstance->Modes->ModeString(user, channel);
208 this->WriteCommonFrom(user, channel, "MODE %s +%s", channel->name.c_str(), n.c_str());
210 /* Shrink off the neccessary metadata for a specific channel */
211 user->Shrink("delayjoin_"+channel->name);
215 // .. is there a real need to duplicate WriteCommonExcept?
216 void ModuleDelayJoin::WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
219 char textbuffer[MAXBUF];
222 va_start(argsPtr, text);
223 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
225 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
227 CUList *ulist = channel->GetUsers();
229 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
231 /* User doesnt get a JOIN sent to themselves */
232 if (user == i->first)
235 /* Users with a visibility state that hides them dont appear */
236 if (user->Visibility && !user->Visibility->VisibleTo(i->first))
239 i->first->Write(std::string(tb));
243 MODULE_INIT(ModuleDelayJoin)