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 delay-join channels (+D) where users dont appear to join until they speak */
19 class DelayJoinMode : public ModeHandler
24 DelayJoinMode(InspIRCd* Instance, Module* Parent) : ModeHandler(Instance, 'D', 0, 0, false, MODETYPE_CHANNEL, false), Creator(Parent) { }
26 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
28 if (channel->IsModeSet('D') != adding)
30 if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
32 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only channel operators may %sset channel mode +D", source->nick.c_str(), channel->name.c_str(), adding ? "" : "un");
33 return MODEACTION_DENY;
37 if (channel->IsModeSet('D'))
40 * Make all users visible, as +D is being removed. If we don't do this,
41 * they remain permanently invisible on this channel!
43 CUList* names = channel->GetUsers();
44 for (CUListIter n = names->begin(); n != names->end(); ++n)
45 Creator->OnText(n->first, channel, TYPE_CHANNEL, "", 0, empty);
47 channel->SetMode('D', adding);
48 return MODEACTION_ALLOW;
53 return MODEACTION_DENY;
58 class ModuleDelayJoin : public Module
64 ModuleDelayJoin(InspIRCd* Me)
67 djm = new DelayJoinMode(ServerInstance, this);
68 if (!ServerInstance->Modes->AddMode(djm))
69 throw ModuleException("Could not add new modes!");
70 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnText };
71 ServerInstance->Modules->Attach(eventlist, this, 6);
74 virtual ~ModuleDelayJoin()
76 ServerInstance->Modes->DelMode(djm);
80 virtual Version GetVersion()
82 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
85 virtual void OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
87 if (!channel->IsModeSet('D'))
93 /* If the user is hidden by delayed join, hide them from the NAMES list */
94 std::string key("delayjoin_");
95 key.append(channel->name);
97 if (user->GetExt(key))
101 virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
103 if (channel->IsModeSet('D'))
106 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
107 user->WriteFrom(user, "JOIN %s", channel->name.c_str());
109 /* This metadata tells the module the user is delayed join on this specific channel */
110 user->Extend("delayjoin_"+channel->name);
112 /* This metadata tells the module the user is delayed join on at least one (or more) channels.
113 * It is only cleared when the user is no longer on ANY +D channels.
115 if (!user->GetExt("delayjoin"))
116 user->Extend("delayjoin");
120 void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
122 if (channel->IsModeSet('D'))
124 if (user->GetExt("delayjoin_"+channel->name))
127 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
128 user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
133 void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
135 if (chan->IsModeSet('D'))
137 /* Send silenced event only to the user being kicked and the user doing the kick */
138 if (user->GetExt("delayjoin_"+chan->name))
141 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
146 void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
148 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
149 if (parthandler && user->GetExt("delayjoin"))
151 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
153 std::vector<std::string> parameters;
154 parameters.push_back(f->first->name);
155 /* This triggers our OnUserPart, above, making the PART silent */
156 parthandler->Handle(parameters, user);
161 void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
167 if (target_type != TYPE_CHANNEL)
170 Channel* channel = (Channel*) dest;
172 if (!user->GetExt("delayjoin_"+channel->name))
175 /* Display the join to everyone else (the user who joined got it earlier) */
176 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name.c_str());
178 std::string n = this->ServerInstance->Modes->ModeString(user, channel);
180 this->WriteCommonFrom(user, channel, "MODE %s +%s", channel->name.c_str(), n.c_str());
182 /* Shrink off the neccessary metadata for a specific channel */
183 user->Shrink("delayjoin_"+channel->name);
185 /* Check if the user is left on any other +D channels, if so don't take away the
186 * metadata that says theyre on one or more channels
188 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
189 if (f->first->IsModeSet('D'))
192 user->Shrink("delayjoin");
195 // .. is there a real need to duplicate WriteCommonExcept?
196 void WriteCommonFrom(User *user, Channel* channel, const char* text, ...) CUSTOM_PRINTF(4, 5)
199 char textbuffer[MAXBUF];
202 va_start(argsPtr, text);
203 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
205 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost().c_str(), textbuffer);
207 CUList *ulist = channel->GetUsers();
209 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
211 /* User doesnt get a JOIN sent to themselves */
212 if (user == i->first)
215 /* Users with a visibility state that hides them dont appear */
216 if (user->Visibility && !user->Visibility->VisibleTo(i->first))
219 i->first->Write(std::string(tb));
225 MODULE_INIT(ModuleDelayJoin)