2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2018-2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
8 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10 * Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/account.h"
29 // Holds information about a <hostchange> rule.
35 // Add the user's account name to their hostname.
38 // Add the user's nickname to their hostname.
41 // Set the user's hostname to the specific value.
46 HostChangeAction action;
49 insp::flat_set<int> ports;
54 HostRule(const std::string& Mask, const std::string& Host, const insp::flat_set<int>& Ports)
62 HostRule(HostChangeAction Action, const std::string& Mask, const insp::flat_set<int>& Ports, const std::string& Prefix, const std::string& Suffix)
71 HostChangeAction GetAction() const
76 const std::string& GetHost() const
81 bool Matches(LocalUser* user) const
83 if (!ports.empty() && !ports.count(user->server_sa.port()))
86 if (InspIRCd::MatchCIDR(user->MakeHost(), mask))
89 return InspIRCd::MatchCIDR(user->MakeHostIP(), mask);
92 void Wrap(const std::string& value, std::string& out) const
104 typedef std::vector<HostRule> HostRules;
106 class ModuleHostChange : public Module
109 std::bitset<UCHAR_MAX> hostmap;
112 std::string CleanName(const std::string& name)
115 buffer.reserve(name.length());
116 for (std::string::const_iterator iter = name.begin(); iter != name.end(); ++iter)
118 if (hostmap.test(static_cast<unsigned char>(*iter)))
120 buffer.push_back(*iter);
127 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
131 ConfigTagList tags = ServerInstance->Config->ConfTags("hostchange");
132 for (ConfigIter i = tags.first; i != tags.second; ++i)
134 ConfigTag* tag = i->second;
136 // Ensure that we have the <hostchange:mask> parameter.
137 const std::string mask = tag->getString("mask");
139 throw ModuleException("<hostchange:mask> is a mandatory field, at " + tag->getTagLocation());
141 insp::flat_set<int> ports;
142 const std::string portlist = tag->getString("ports");
145 irc::portparser portrange(portlist, false);
146 while (int port = portrange.GetToken())
150 // Determine what type of host rule this is.
151 const std::string action = tag->getString("action");
152 if (stdalgo::string::equalsci(action, "addaccount"))
154 // The hostname is in the format [prefix]<account>[suffix].
155 rules.push_back(HostRule(HostRule::HCA_ADDACCOUNT, mask, ports, tag->getString("prefix"), tag->getString("suffix")));
157 else if (stdalgo::string::equalsci(action, "addnick"))
159 // The hostname is in the format [prefix]<nick>[suffix].
160 rules.push_back(HostRule(HostRule::HCA_ADDNICK, mask, ports, tag->getString("prefix"), tag->getString("suffix")));
162 else if (stdalgo::string::equalsci(action, "set"))
164 // Ensure that we have the <hostchange:value> parameter.
165 const std::string value = tag->getString("value");
167 throw ModuleException("<hostchange:value> is a mandatory field when using the 'set' action, at " + tag->getTagLocation());
169 // The hostname is in the format <value>.
170 rules.push_back(HostRule(mask, value, ports));
175 throw ModuleException(action + " is an invalid <hostchange:action> type, at " + tag->getTagLocation());
179 const std::string hmap = ServerInstance->Config->ConfValue("hostname")->getString("charmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_/0123456789");
181 for (std::string::const_iterator iter = hmap.begin(); iter != hmap.end(); ++iter)
182 hostmap.set(static_cast<unsigned char>(*iter));
183 hostrules.swap(rules);
186 Version GetVersion() CXX11_OVERRIDE
188 return Version("Provides rule-based masking of user hostnames", VF_VENDOR);
191 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
193 for (HostRules::const_iterator iter = hostrules.begin(); iter != hostrules.end(); ++iter)
195 const HostRule& rule = *iter;
196 if (!rule.Matches(user))
200 if (rule.GetAction() == HostRule::HCA_ADDACCOUNT)
202 // Retrieve the account name.
203 const AccountExtItem* accountext = GetAccountExtItem();
204 const std::string* accountptr = accountext ? accountext->get(user) : NULL;
208 // Remove invalid hostname characters.
209 std::string accountname = CleanName(*accountptr);
210 if (accountname.empty())
213 // Create the hostname.
214 rule.Wrap(accountname, newhost);
216 else if (rule.GetAction() == HostRule::HCA_ADDNICK)
218 // Remove invalid hostname characters.
219 const std::string nickname = CleanName(user->nick);
220 if (nickname.empty())
223 // Create the hostname.
224 rule.Wrap(nickname, newhost);
226 else if (rule.GetAction() == HostRule::HCA_SET)
228 newhost.assign(rule.GetHost());
231 if (!newhost.empty())
233 user->WriteNotice("Setting your virtual host: " + newhost);
234 if (!user->ChangeDisplayedHost(newhost))
235 user->WriteNotice("Could not set your virtual host: " + newhost);
242 MODULE_INIT(ModuleHostChange)