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 class DelayJoinMode : public ModeHandler
23 DelayJoinMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, 'D', 0, 0, false, MODETYPE_CHANNEL, false), 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 bool OnHostCycle(User* user);
47 void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent);
48 void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent);
49 void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message);
50 void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list);
51 void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5);
54 /* $ModDesc: Allows for delay-join channels (+D) where users dont appear to join until they speak */
56 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
58 if (channel->IsModeSet('D') != adding)
60 if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
62 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only channel operators may %sset channel mode +D", source->nick.c_str(), channel->name.c_str(), adding ? "" : "un");
63 return MODEACTION_DENY;
67 if (channel->IsModeSet('D'))
70 * Make all users visible, as +D is being removed. If we don't do this,
71 * they remain permanently invisible on this channel!
73 CUList* names = channel->GetUsers();
74 for (CUListIter n = names->begin(); n != names->end(); ++n)
75 Creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
77 channel->SetMode('D', adding);
78 return MODEACTION_ALLOW;
83 return MODEACTION_DENY;
87 ModuleDelayJoin::~ModuleDelayJoin()
89 ServerInstance->Modes->DelMode(djm);
93 Version ModuleDelayJoin::GetVersion()
95 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
98 void ModuleDelayJoin::OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
100 if (!channel->IsModeSet('D'))
106 /* If the user is hidden by delayed join, hide them from the NAMES list */
107 std::string key("delayjoin_");
108 key.append(channel->name);
110 if (user->GetExt(key))
114 void ModuleDelayJoin::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
116 if (channel->IsModeSet('D'))
119 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
120 user->WriteFrom(user, "JOIN %s", channel->name.c_str());
122 /* This metadata tells the module the user is delayed join on this specific channel */
123 user->Extend("delayjoin_"+channel->name);
125 /* This metadata tells the module the user is delayed join on at least one (or more) channels.
126 * It is only cleared when the user is no longer on ANY +D channels.
128 if (!user->GetExt("delayjoin"))
129 user->Extend("delayjoin");
133 void ModuleDelayJoin::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
135 if (channel->IsModeSet('D'))
137 if (user->GetExt("delayjoin_"+channel->name))
140 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
141 user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
146 void ModuleDelayJoin::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
148 if (chan->IsModeSet('D'))
150 /* Send silenced event only to the user being kicked and the user doing the kick */
151 if (user->GetExt("delayjoin_"+chan->name))
154 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
159 bool ModuleDelayJoin::OnHostCycle(User* user)
161 return user->GetExt("delayjoin");
164 void ModuleDelayJoin::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
166 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
167 if (parthandler && user->GetExt("delayjoin"))
169 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
171 std::vector<std::string> parameters;
172 parameters.push_back(f->first->name);
173 /* This triggers our OnUserPart, above, making the PART silent */
174 parthandler->Handle(parameters, user);
179 void ModuleDelayJoin::OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
185 if (target_type != TYPE_CHANNEL)
188 Channel* channel = (Channel*) dest;
190 if (!user->GetExt("delayjoin_"+channel->name))
193 /* Display the join to everyone else (the user who joined got it earlier) */
194 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
196 std::string n = this->ServerInstance->Modes->ModeString(user, channel);
198 this->WriteCommonFrom(user, channel, "MODE %s +%s", channel->name.c_str(), n.c_str());
200 /* Shrink off the neccessary metadata for a specific channel */
201 user->Shrink("delayjoin_"+channel->name);
203 /* Check if the user is left on any other +D channels, if so don't take away the
204 * metadata that says theyre on one or more channels
206 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
207 if (f->first->IsModeSet('D'))
210 user->Shrink("delayjoin");
213 // .. is there a real need to duplicate WriteCommonExcept?
214 void ModuleDelayJoin::WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
217 char textbuffer[MAXBUF];
220 va_start(argsPtr, text);
221 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
223 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
225 CUList *ulist = channel->GetUsers();
227 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
229 /* User doesnt get a JOIN sent to themselves */
230 if (user == i->first)
233 /* Users with a visibility state that hides them dont appear */
234 if (user->Visibility && !user->Visibility->VisibleTo(i->first))
237 i->first->Write(std::string(tb));
241 MODULE_INIT(ModuleDelayJoin)