2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2005-2007 Craig Edwards <craigedwards@brainbox.cc>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "modules/account.h"
25 // Holds information about a <hostchange> rule.
31 // Add the user's account name to their hostname.
34 // Add the user's nickname to their hostname.
37 // Set the user's hostname to the specific value.
42 HostChangeAction action;
45 insp::flat_set<int> ports;
50 HostRule(const std::string& Mask, const std::string& Host, const insp::flat_set<int>& Ports)
58 HostRule(HostChangeAction Action, const std::string& Mask, const insp::flat_set<int>& Ports, const std::string& Prefix, const std::string& Suffix)
67 HostChangeAction GetAction() const
72 const std::string& GetHost() const
77 bool Matches(LocalUser* user) const
79 if (!ports.empty() && !ports.count(user->server_sa.port()))
82 if (InspIRCd::MatchCIDR(user->MakeHost(), mask))
85 return InspIRCd::MatchCIDR(user->MakeHostIP(), mask);
88 void Wrap(const std::string& value, std::string& out) const
100 typedef std::vector<HostRule> HostRules;
102 class ModuleHostChange : public Module
105 std::bitset<UCHAR_MAX> hostmap;
108 std::string CleanName(const std::string& name)
111 buffer.reserve(name.length());
112 for (std::string::const_iterator iter = name.begin(); iter != name.end(); ++iter)
114 if (hostmap.test(static_cast<unsigned char>(*iter)))
116 buffer.push_back(*iter);
123 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
127 ConfigTagList tags = ServerInstance->Config->ConfTags("hostchange");
128 for (ConfigIter i = tags.first; i != tags.second; ++i)
130 ConfigTag* tag = i->second;
132 // Ensure that we have the <hostchange:mask> parameter.
133 const std::string mask = tag->getString("mask");
135 throw ModuleException("<hostchange:mask> is a mandatory field, at " + tag->getTagLocation());
137 insp::flat_set<int> ports;
138 const std::string portlist = tag->getString("ports");
141 irc::portparser portrange(portlist, false);
142 while (int port = portrange.GetToken())
146 // Determine what type of host rule this is.
147 const std::string action = tag->getString("action");
148 if (stdalgo::string::equalsci(action, "addaccount"))
150 // The hostname is in the format [prefix]<account>[suffix].
151 rules.push_back(HostRule(HostRule::HCA_ADDACCOUNT, mask, ports, tag->getString("prefix"), tag->getString("suffix")));
153 else if (stdalgo::string::equalsci(action, "addnick"))
155 // The hostname is in the format [prefix]<nick>[suffix].
156 rules.push_back(HostRule(HostRule::HCA_ADDNICK, mask, ports, tag->getString("prefix"), tag->getString("suffix")));
158 else if (stdalgo::string::equalsci(action, "set"))
160 // Ensure that we have the <hostchange:value> parameter.
161 const std::string value = tag->getString("value");
163 throw ModuleException("<hostchange:value> is a mandatory field when using the 'set' action, at " + tag->getTagLocation());
165 // The hostname is in the format <value>.
166 rules.push_back(HostRule(mask, value, ports));
171 throw ModuleException(action + " is an invalid <hostchange:action> type, at " + tag->getTagLocation());
175 const std::string hmap = ServerInstance->Config->ConfValue("hostname")->getString("charmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_/0123456789");
177 for (std::string::const_iterator iter = hmap.begin(); iter != hmap.end(); ++iter)
178 hostmap.set(static_cast<unsigned char>(*iter));
179 hostrules.swap(rules);
182 Version GetVersion() CXX11_OVERRIDE
184 return Version("Provides rule-based masking of user hostnames", VF_VENDOR);
187 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
189 for (HostRules::const_iterator iter = hostrules.begin(); iter != hostrules.end(); ++iter)
191 const HostRule& rule = *iter;
192 if (!rule.Matches(user))
196 if (rule.GetAction() == HostRule::HCA_ADDACCOUNT)
198 // Retrieve the account name.
199 const AccountExtItem* accountext = GetAccountExtItem();
200 const std::string* accountptr = accountext ? accountext->get(user) : NULL;
204 // Remove invalid hostname characters.
205 std::string accountname = CleanName(*accountptr);
206 if (accountname.empty())
209 // Create the hostname.
210 rule.Wrap(accountname, newhost);
212 else if (rule.GetAction() == HostRule::HCA_ADDNICK)
214 // Remove invalid hostname characters.
215 const std::string nickname = CleanName(user->nick);
216 if (nickname.empty())
219 // Create the hostname.
220 rule.Wrap(nickname, newhost);
222 else if (rule.GetAction() == HostRule::HCA_SET)
224 newhost.assign(rule.GetHost());
227 if (!newhost.empty())
229 user->WriteNotice("Setting your virtual host: " + newhost);
230 if (!user->ChangeDisplayedHost(newhost))
231 user->WriteNotice("Could not set your virtual host: " + newhost);
238 MODULE_INIT(ModuleHostChange)