1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #include "inspircd.h"
24 #include "modules/ctctags.h"
25 
26 class DelayMsgMode : public ParamMode<DelayMsgMode, LocalIntExt>
27 {
28  public:
29 	LocalIntExt jointime;
DelayMsgMode(Module * Parent)30 	DelayMsgMode(Module* Parent)
31 		: ParamMode<DelayMsgMode, LocalIntExt>(Parent, "delaymsg", 'd')
32 		, jointime("delaymsg", ExtensionItem::EXT_MEMBERSHIP, Parent)
33 	{
34 		ranktoset = ranktounset = OP_VALUE;
35 		syntax = "<seconds>";
36 	}
37 
ResolveModeConflict(std::string & their_param,const std::string & our_param,Channel *)38 	bool ResolveModeConflict(std::string& their_param, const std::string& our_param, Channel*) CXX11_OVERRIDE
39 	{
40 		return ConvToNum<intptr_t>(their_param) < ConvToNum<intptr_t>(our_param);
41 	}
42 
43 	ModeAction OnSet(User* source, Channel* chan, std::string& parameter) CXX11_OVERRIDE;
44 	void OnUnset(User* source, Channel* chan) CXX11_OVERRIDE;
45 
SerializeParam(Channel * chan,intptr_t n,std::string & out)46 	void SerializeParam(Channel* chan, intptr_t n, std::string& out)
47 	{
48 		out += ConvToStr(n);
49 	}
50 };
51 
52 class ModuleDelayMsg
53 	: public Module
54 	, public CTCTags::EventListener
55 {
56  private:
57 	DelayMsgMode djm;
58 	bool allownotice;
59 	ModResult HandleMessage(User* user, const MessageTarget& target, bool notice);
60 
61  public:
ModuleDelayMsg()62 	ModuleDelayMsg()
63 		: CTCTags::EventListener(this)
64 		, djm(this)
65 	{
66 	}
67 
68 	Version GetVersion() CXX11_OVERRIDE;
69 	void OnUserJoin(Membership* memb, bool sync, bool created, CUList&) CXX11_OVERRIDE;
70 	ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE;
71 	ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE;
72 	void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
73 };
74 
OnSet(User * source,Channel * chan,std::string & parameter)75 ModeAction DelayMsgMode::OnSet(User* source, Channel* chan, std::string& parameter)
76 {
77 	// Setting a new limit, sanity check
78 	intptr_t limit = ConvToNum<intptr_t>(parameter);
79 	if (limit <= 0)
80 		limit = 1;
81 
82 	ext.set(chan, limit);
83 	return MODEACTION_ALLOW;
84 }
85 
OnUnset(User * source,Channel * chan)86 void DelayMsgMode::OnUnset(User* source, Channel* chan)
87 {
88 	/*
89 	 * Clean up metadata
90 	 */
91 	const Channel::MemberMap& users = chan->GetUsers();
92 	for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
93 		jointime.set(n->second, 0);
94 }
95 
GetVersion()96 Version ModuleDelayMsg::GetVersion()
97 {
98 	return Version("Adds channel mode d (delaymsg) which prevents newly joined users from speaking until the specified number of seconds have passed.", VF_VENDOR);
99 }
100 
OnUserJoin(Membership * memb,bool sync,bool created,CUList &)101 void ModuleDelayMsg::OnUserJoin(Membership* memb, bool sync, bool created, CUList&)
102 {
103 	if ((IS_LOCAL(memb->user)) && (memb->chan->IsModeSet(djm)))
104 	{
105 		djm.jointime.set(memb, ServerInstance->Time());
106 	}
107 }
108 
OnUserPreMessage(User * user,const MessageTarget & target,MessageDetails & details)109 ModResult ModuleDelayMsg::OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details)
110 {
111 	return HandleMessage(user, target, details.type == MSG_NOTICE);
112 }
113 
OnUserPreTagMessage(User * user,const MessageTarget & target,CTCTags::TagMessageDetails & details)114 ModResult ModuleDelayMsg::OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details)
115 {
116 	return HandleMessage(user, target, false);
117 }
118 
HandleMessage(User * user,const MessageTarget & target,bool notice)119 ModResult ModuleDelayMsg::HandleMessage(User* user, const MessageTarget& target, bool notice)
120 {
121 	if (!IS_LOCAL(user))
122 		return MOD_RES_PASSTHRU;
123 
124 	if ((target.type != MessageTarget::TYPE_CHANNEL) || ((!allownotice) && (notice)))
125 		return MOD_RES_PASSTHRU;
126 
127 	Channel* channel = target.Get<Channel>();
128 	Membership* memb = channel->GetUser(user);
129 
130 	if (!memb)
131 		return MOD_RES_PASSTHRU;
132 
133 	time_t ts = djm.jointime.get(memb);
134 
135 	if (ts == 0)
136 		return MOD_RES_PASSTHRU;
137 
138 	int len = djm.ext.get(channel);
139 
140 	if ((ts + len) > ServerInstance->Time())
141 	{
142 		if (channel->GetPrefixValue(user) < VOICE_VALUE)
143 		{
144 			const std::string message = InspIRCd::Format("You cannot send messages to this channel until you have been a member for %d seconds.", len);
145 			user->WriteNumeric(Numerics::CannotSendTo(channel, message));
146 			return MOD_RES_DENY;
147 		}
148 	}
149 	else
150 	{
151 		/* Timer has expired, we can stop checking now */
152 		djm.jointime.set(memb, 0);
153 	}
154 	return MOD_RES_PASSTHRU;
155 }
156 
ReadConfig(ConfigStatus & status)157 void ModuleDelayMsg::ReadConfig(ConfigStatus& status)
158 {
159 	ConfigTag* tag = ServerInstance->Config->ConfValue("delaymsg");
160 	allownotice = tag->getBool("allownotice", true);
161 }
162 
163 MODULE_INIT(ModuleDelayMsg)
164