1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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)
28 if (channel->IsModeSet('D') != adding)
30 if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
32 source->WriteServ("482 %s %s :Only channel operators may %sset channel mode +D", source->nick, channel->name, adding ? "" : "un");
33 return MODEACTION_DENY;
37 if (channel->IsModeSet('D'))
39 /* Make all delayed join users visible, or if an op removes +D
40 * while users exist that havent spoken, they remain permenantly
41 * 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->AddMode(djm))
69 throw ModuleException("Could not add new modes!");
70 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnUserList, I_OnText };
71 ServerInstance->Modules->Attach(eventlist, this, 6);
74 virtual ~ModuleDelayJoin()
76 ServerInstance->Modes->DelMode(djm);
82 /* To ensure that we get priority over namesx for names list generation */
83 Module* namesx = ServerInstance->Modules->Find("m_namesx.so");
84 ServerInstance->Modules->SetPriority(this, I_OnUserList, PRIO_BEFORE, &namesx);
87 virtual Version GetVersion()
89 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
92 void Implements(char* List)
94 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserKick] = List[I_OnUserQuit] = List[I_OnUserList] = List[I_OnText] = 1;
97 virtual int OnUserList(User* user, Channel* Ptr, CUList* &nameslist)
99 CUList* newlist = nameslist ? nameslist : Ptr->GetUsers();
103 /* For +D channels ... */
104 if (Ptr->IsModeSet('D'))
106 std::string key("delayjoin_");
107 key.append(Ptr->name);
109 /* Modify the names list, erasing users with the delay join metadata
110 * for this channel (havent spoken yet)
112 for (CUListIter n = newlist->begin(); n != newlist->end(); ++n)
114 if (!n->first->GetExt(key))
118 /* Always show self */
119 nl[user] = user->nick;
125 virtual void OnUserJoin(User* user, Channel* channel, bool &silent)
127 if (channel->IsModeSet('D'))
130 /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
131 user->WriteFrom(user, "JOIN %s", channel->name);
133 /* This metadata tells the module the user is delayed join on this specific channel */
134 user->Extend(std::string("delayjoin_")+channel->name);
136 /* This metadata tells the module the user is delayed join on at least one (or more) channels.
137 * It is only cleared when the user is no longer on ANY +D channels.
139 if (!user->GetExt("delayjoin"))
140 user->Extend("delayjoin");
144 void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
146 if (channel->IsModeSet('D'))
148 if (user->GetExt(std::string("delayjoin_")+channel->name))
151 /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
152 user->WriteFrom(user, "PART %s%s%s", channel->name, partmessage.empty() ? "" : " :", partmessage.empty() ? "" : partmessage.c_str());
157 void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
159 if (chan->IsModeSet('D'))
161 /* Send silenced event only to the user being kicked and the user doing the kick */
162 if (user->GetExt(std::string("delayjoin_")+chan->name))
165 user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
170 void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
172 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
173 const char* parameters[2];
174 if (parthandler && user->GetExt("delayjoin"))
176 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
178 parameters[0] = f->first->name;
179 /* This triggers our OnUserPart, above, making the PART silent */
180 parthandler->Handle(parameters, 1, user);
185 void OnText(User* user, void* dest, int target_type, const std::string &text, char status, CUList &exempt_list)
187 if (target_type != TYPE_CHANNEL)
190 Channel* channel = (Channel*) dest;
192 if (!user->GetExt(std::string("delayjoin_")+channel->name))
195 /* Display the join to everyone else (the user who joined got it earlier) */
196 this->WriteCommonFrom(user, channel, "JOIN %s", channel->name);
198 /* Shrink off the neccessary metadata for a specific channel */
199 user->Shrink(std::string("delayjoin_")+channel->name);
201 /* Check if the user is left on any other +D channels, if so don't take away the
202 * metadata that says theyre on one or more channels
204 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
205 if (f->first->IsModeSet('D'))
208 user->Shrink("delayjoin");
211 void WriteCommonFrom(User *user, Channel* channel, const char* text, ...)
214 char textbuffer[MAXBUF];
217 va_start(argsPtr, text);
218 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
220 snprintf(tb,MAXBUF,":%s %s",user->GetFullHost(),textbuffer);
222 CUList *ulist = channel->GetUsers();
224 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
226 /* User doesnt get a JOIN sent to themselves */
227 if (user == i->first)
230 /* Users with a visibility state that hides them dont appear */
231 if (user->Visibility && !user->Visibility->VisibleTo(i->first))
234 i->first->Write(std::string(tb));
240 MODULE_INIT(ModuleDelayJoin)