2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* $ModDesc: Provides channelmode +d <int>, to deny messages to a channel until <int> seconds. */
24 class DelayMsgMode : public ModeHandler
28 DelayMsgMode(Module* Parent) : ModeHandler(Parent, "delaymsg", 'd', PARAM_SETONLY, MODETYPE_CHANNEL)
29 , jointime("delaymsg", Parent)
31 levelrequired = OP_VALUE;
34 bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel*)
36 return (atoi(their_param.c_str()) < atoi(our_param.c_str()));
39 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding);
42 class ModuleDelayMsg : public Module
47 ModuleDelayMsg() : djm(this)
53 if (!ServerInstance->Modes->AddMode(&djm))
54 throw ModuleException("Could not add new modes!");
55 ServerInstance->Extensions.Register(&djm.jointime);
56 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPreMessage};
57 ServerInstance->Modules->Attach(eventlist, this, 2);
60 void OnUserJoin(Membership* memb, bool sync, bool created, CUList&);
61 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list);
64 ModeAction DelayMsgMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
68 if ((channel->IsModeSet('d')) && (channel->GetModeParameter('d') == parameter))
69 return MODEACTION_DENY;
71 /* Setting a new limit, sanity check */
72 long limit = atoi(parameter.c_str());
74 /* Wrap low values at 32768 */
78 parameter = ConvToStr(limit);
82 if (!channel->IsModeSet('d'))
83 return MODEACTION_DENY;
88 const UserMembList* names = channel->GetUsers();
89 for (UserMembCIter n = names->begin(); n != names->end(); ++n)
90 jointime.set(n->second, 0);
92 channel->SetModeParam('d', adding ? parameter : "");
93 return MODEACTION_ALLOW;
96 Version ModuleDelayMsg::GetVersion()
98 return Version("Provides channelmode +d <int>, to deny messages to a channel until <int> seconds.", VF_VENDOR);
101 void ModuleDelayMsg::OnUserJoin(Membership* memb, bool sync, bool created, CUList&)
103 if ((IS_LOCAL(memb->user)) && (memb->chan->IsModeSet('d')))
105 djm.jointime.set(memb, ServerInstance->Time());
109 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, void* dest, int target_type, std::string &text, char status, CUList &exempt_list)
112 if ((!user) || (!IS_LOCAL(user)))
113 return MOD_RES_PASSTHRU;
115 if (target_type != TYPE_CHANNEL)
116 return MOD_RES_PASSTHRU;
118 Channel* channel = (Channel*) dest;
119 Membership* memb = channel->GetUser(user);
122 return MOD_RES_PASSTHRU;
124 time_t ts = djm.jointime.get(memb);
127 return MOD_RES_PASSTHRU;
129 std::string len = channel->GetModeParameter('d');
131 if (ts + atoi(len.c_str()) > ServerInstance->Time())
133 if (channel->GetPrefixValue(user) < VOICE_VALUE)
135 user->WriteNumeric(404, "%s %s :You must wait %s seconds after joining to send to channel (+d)",
136 user->nick.c_str(), channel->name.c_str(), len.c_str());
142 /* Timer has expired, we can stop checking now */
143 djm.jointime.set(memb, 0);
145 return MOD_RES_PASSTHRU;
148 MODULE_INIT(ModuleDelayMsg)