1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2013, 2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012-2013, 2016 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #include "inspircd.h"
28 #include "modules/account.h"
29 
30 typedef std::vector<std::string> AllowList;
31 
32 class ModuleSecureList : public Module
33 {
34  private:
35 	AllowList allowlist;
36 	bool exemptregistered;
37 	bool showmsg;
38 	unsigned int WaitTime;
39 
40  public:
GetVersion()41 	Version GetVersion() CXX11_OVERRIDE
42 	{
43 		return Version("Prevents users from using the /LIST command until a predefined period has passed.", VF_VENDOR);
44 	}
45 
ReadConfig(ConfigStatus & status)46 	void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
47 	{
48 		AllowList newallows;
49 
50 		ConfigTagList tags = ServerInstance->Config->ConfTags("securehost");
51 		for (ConfigIter i = tags.first; i != tags.second; ++i)
52 		{
53 			std::string host = i->second->getString("exception");
54 			if (host.empty())
55 				throw ModuleException("<securehost:exception> is a required field at " + i->second->getTagLocation());
56 			newallows.push_back(host);
57 		}
58 
59 		ConfigTag* tag = ServerInstance->Config->ConfValue("securelist");
60 		exemptregistered = tag->getBool("exemptregistered");
61 		showmsg = tag->getBool("showmsg", true);
62 		WaitTime = tag->getDuration("waittime", 60, 1);
63 		allowlist.swap(newallows);
64 	}
65 
OnPreCommand(std::string & command,CommandBase::Params & parameters,LocalUser * user,bool validated)66 	ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
67 	{
68 		/* If the command doesnt appear to be valid, we dont want to mess with it. */
69 		if (!validated)
70 			return MOD_RES_PASSTHRU;
71 
72 		time_t waitallowed = user->signon + WaitTime;
73 		if ((command == "LIST") && (ServerInstance->Time() < waitallowed) && (!user->IsOper()))
74 		{
75 			/* Normally wouldnt be allowed here, are they exempt? */
76 			for (std::vector<std::string>::iterator x = allowlist.begin(); x != allowlist.end(); x++)
77 				if (InspIRCd::Match(user->MakeHost(), *x, ascii_case_insensitive_map))
78 					return MOD_RES_PASSTHRU;
79 
80 			const AccountExtItem* ext = GetAccountExtItem();
81 			if (exemptregistered && ext && ext->get(user))
82 				return MOD_RES_PASSTHRU;
83 
84 			if (showmsg)
85 			{
86 				user->WriteNotice(InspIRCd::Format("*** You cannot view the channel list right now. Please %stry again in %s.",
87 					(exemptregistered ? "login to an account or " : ""),
88 					InspIRCd::DurationString(waitallowed - ServerInstance->Time()).c_str()));
89 			}
90 
91 			// The client might be waiting on a response to do something so send them an
92 			// empty list response to satisfy that.
93 			user->WriteNumeric(RPL_LISTSTART, "Channel", "Users Name");
94 			user->WriteNumeric(RPL_LISTEND, "End of channel list.");
95 			return MOD_RES_DENY;
96 		}
97 		return MOD_RES_PASSTHRU;
98 	}
99 
On005Numeric(std::map<std::string,std::string> & tokens)100 	void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
101 	{
102 		if (showmsg)
103 			tokens["SECURELIST"] = ConvToStr(WaitTime);
104 	}
105 };
106 
107 MODULE_INIT(ModuleSecureList)
108