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/>.
24 /** Holds information on a host set by m_hostchange
36 HostChangeAction action;
40 Host(HostChangeAction Action, const std::string& Newhost, const std::string& Ports) :
41 action(Action), newhost(Newhost), ports(Ports) {}
44 typedef std::vector<std::pair<std::string, Host> > hostchanges_t;
46 class ModuleHostChange : public Module
48 hostchanges_t hostchanges;
51 std::string MySeparator;
54 void init() CXX11_OVERRIDE
59 void OnRehash(User* user) CXX11_OVERRIDE
61 ConfigTag* host = ServerInstance->Config->ConfValue("host");
62 MySuffix = host->getString("suffix");
63 MyPrefix = host->getString("prefix");
64 MySeparator = host->getString("separator", ".");
67 std::set<std::string> dupecheck;
68 ConfigTagList tags = ServerInstance->Config->ConfTags("hostchange");
69 for (ConfigIter i = tags.first; i != tags.second; ++i)
71 ConfigTag* tag = i->second;
72 std::string mask = tag->getString("mask");
73 if (!dupecheck.insert(mask).second)
74 throw ModuleException("Duplicate hostchange entry: " + mask);
76 Host::HostChangeAction act;
78 std::string action = tag->getString("action");
79 if (!strcasecmp(action.c_str(), "set"))
82 newhost = tag->getString("value");
84 else if (!strcasecmp(action.c_str(), "suffix"))
85 act = Host::HCA_SUFFIX;
86 else if (!strcasecmp(action.c_str(), "addnick"))
87 act = Host::HCA_ADDNICK;
89 throw ModuleException("Invalid hostchange action: " + action);
91 hostchanges.push_back(std::make_pair(mask, Host(act, newhost, tag->getString("ports"))));
95 Version GetVersion() CXX11_OVERRIDE
97 // returns the version number of the module to be
99 return Version("Provides masking of user hostnames in a different way to m_cloaking", VF_VENDOR);
102 void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
104 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
106 if (((InspIRCd::MatchCIDR(user->MakeHost(), i->first)) || (InspIRCd::MatchCIDR(user->MakeHostIP(), i->first))))
108 const Host& h = i->second;
110 if (!h.ports.empty())
112 irc::portparser portrange(h.ports, false);
114 bool foundany = false;
116 while ((portno = portrange.GetToken()))
117 if (portno == user->GetServerPort())
124 // host of new user matches a hostchange tag's mask
126 if (h.action == Host::HCA_SET)
130 else if (h.action == Host::HCA_SUFFIX)
134 else if (h.action == Host::HCA_ADDNICK)
136 // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
137 std::string complete;
138 for (std::string::const_iterator j = user->nick.begin(); j != user->nick.end(); ++j)
140 if (((*j >= 'A') && (*j <= 'Z')) ||
141 ((*j >= 'a') && (*j <= 'z')) ||
142 ((*j >= '0') && (*j <= '9')) ||
145 complete = complete + *j;
148 if (complete.empty())
149 complete = "i-have-a-lame-nick";
151 if (!MyPrefix.empty())
152 newhost = MyPrefix + MySeparator + complete;
154 newhost = complete + MySeparator + MySuffix;
156 if (!newhost.empty())
158 user->WriteNotice("Setting your virtual host: " + newhost);
159 if (!user->ChangeDisplayedHost(newhost.c_str()))
160 user->WriteNotice("Could not set your virtual host: " + newhost);
168 MODULE_INIT(ModuleHostChange)