1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2012-2013, 2017-2018, 2020-2021 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
11  *   Copyright (C) 2005, 2007, 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/whois.h"
29 
30 class CommandSwhois : public Command
31 {
32  public:
33 	LocalIntExt operblock;
34 	StringExtItem swhois;
CommandSwhois(Module * Creator)35 	CommandSwhois(Module* Creator)
36 		: Command(Creator, "SWHOIS", 2, 2)
37 		, operblock("swhois_operblock", ExtensionItem::EXT_USER, Creator)
38 		, swhois("swhois", ExtensionItem::EXT_USER, Creator)
39 	{
40 		flags_needed = 'o';
41 		syntax = "<nick> :<swhois>";
42 		TRANSLATE2(TR_NICK, TR_TEXT);
43 	}
44 
Handle(User * user,const Params & parameters)45 	CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
46 	{
47 		User* dest = ServerInstance->FindNick(parameters[0]);
48 
49 		if (!dest) // allow setting swhois using SWHOIS before reg
50 		{
51 			user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
52 			return CMD_FAILURE;
53 		}
54 
55 		std::string* text = swhois.get(dest);
56 		if (text)
57 		{
58 			// We already had it set...
59 			if (!user->server->IsULine())
60 				// Ulines set SWHOISes silently
61 				ServerInstance->SNO->WriteGlobalSno('a', "%s used SWHOIS to set %s's extra whois from '%s' to '%s'", user->nick.c_str(), dest->nick.c_str(), text->c_str(), parameters[1].c_str());
62 		}
63 		else if (!user->server->IsULine())
64 		{
65 			// Ulines set SWHOISes silently
66 			ServerInstance->SNO->WriteGlobalSno('a', "%s used SWHOIS to set %s's extra whois to '%s'", user->nick.c_str(), dest->nick.c_str(), parameters[1].c_str());
67 		}
68 
69 		operblock.set(user, 0);
70 		if (parameters[1].empty())
71 			swhois.unset(dest);
72 		else
73 			swhois.set(dest, parameters[1]);
74 
75 		/* Bug #376 - feature request -
76 		 * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
77 		 * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
78 		 * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
79 		 * -- Brain
80 		 */
81 		ServerInstance->PI->SendMetaData(dest, "swhois", parameters[1]);
82 
83 		return CMD_SUCCESS;
84 	}
85 
86 };
87 
88 class ModuleSWhois CXX11_FINAL
89 	: public Module
90 	, public Whois::LineEventListener
91 {
92 private:
93 	CommandSwhois cmd;
94 	UserModeReference hideopermode;
95 
96  public:
ModuleSWhois()97 	ModuleSWhois()
98 		: Whois::LineEventListener(this)
99 		, cmd(this)
100 		, hideopermode(this, "hideoper")
101 	{
102 	}
103 
104 	// :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
OnWhoisLine(Whois::Context & whois,Numeric::Numeric & numeric)105 	ModResult OnWhoisLine(Whois::Context& whois, Numeric::Numeric& numeric) CXX11_OVERRIDE
106 	{
107 		// We use this and not OnWhois because this triggers for remote users too.
108 		if (numeric.GetNumeric() != RPL_WHOISSERVER)
109 			return MOD_RES_PASSTHRU;
110 
111 		// Don't send soper swhois if hideoper is set.
112 		if (cmd.operblock.get(whois.GetTarget()) && whois.GetTarget()->IsModeSet(hideopermode))
113 			return MOD_RES_PASSTHRU;
114 
115 		// Insert our numeric before RPL_WHOISSERVER.
116 		const std::string* swhois = cmd.swhois.get(whois.GetTarget());
117 		if (swhois && !swhois->empty())
118 			whois.SendLine(RPL_WHOISSPECIAL, *swhois);
119 
120 		return MOD_RES_PASSTHRU;
121 	}
122 
OnPostOper(User * user,const std::string & opertype,const std::string & opername)123 	void OnPostOper(User* user, const std::string &opertype, const std::string &opername) CXX11_OVERRIDE
124 	{
125 		if (!IS_LOCAL(user))
126 			return;
127 
128 		std::string swhois = user->oper->getConfig("swhois");
129 
130 		if (!swhois.length())
131 			return;
132 
133 		cmd.operblock.set(user, 1);
134 		cmd.swhois.set(user, swhois);
135 		ServerInstance->PI->SendMetaData(user, "swhois", swhois);
136 	}
137 
OnPostDeoper(User * user)138 	void OnPostDeoper(User* user) CXX11_OVERRIDE
139 	{
140 		std::string* swhois = cmd.swhois.get(user);
141 		if (!swhois)
142 			return;
143 
144 		if (!cmd.operblock.get(user))
145 			return;
146 
147 		cmd.operblock.set(user, 0);
148 		cmd.swhois.unset(user);
149 		ServerInstance->PI->SendMetaData(user, "swhois", "");
150 	}
151 
OnDecodeMetaData(Extensible * target,const std::string & extname,const std::string &)152 	void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string&) CXX11_OVERRIDE
153 	{
154 		User* dest = static_cast<User*>(target);
155 		if (dest && (extname == "swhois"))
156 			cmd.operblock.set(dest, 0);
157 	}
158 
GetVersion()159 	Version GetVersion() CXX11_OVERRIDE
160 	{
161 		return Version("Adds the /SWHOIS command which adds custom lines to a user's WHOIS response.", VF_OPTCOMMON | VF_VENDOR);
162 	}
163 };
164 
165 MODULE_INIT(ModuleSWhois)
166