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 /* $ModDesc: Provides masking of user hostnames in a different way to m_cloaking */
26 /** Holds information on a host set by m_hostchange
38 HostChangeAction action;
42 Host(HostChangeAction Action, const std::string& Newhost, const std::string& Ports) :
43 action(Action), newhost(Newhost), ports(Ports) {}
46 typedef std::vector<std::pair<std::string, Host> > hostchanges_t;
48 class ModuleHostChange : public Module
51 hostchanges_t hostchanges;
54 std::string MySeparator;
60 Implementation eventlist[] = { I_OnRehash, I_OnUserConnect };
61 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
64 virtual void OnRehash(User* user)
66 ConfigTag* host = ServerInstance->Config->ConfValue("host");
67 MySuffix = host->getString("suffix");
68 MyPrefix = host->getString("prefix");
69 MySeparator = host->getString("separator", ".");
72 std::set<std::string> dupecheck;
73 ConfigTagList tags = ServerInstance->Config->ConfTags("hostchange");
74 for (ConfigIter i = tags.first; i != tags.second; ++i)
76 ConfigTag* tag = i->second;
77 std::string mask = tag->getString("mask");
78 if (!dupecheck.insert(mask).second)
79 throw ModuleException("Duplicate hostchange entry: " + mask);
81 Host::HostChangeAction act;
83 std::string action = tag->getString("action");
84 if (!strcasecmp(action.c_str(), "set"))
87 newhost = tag->getString("value");
89 else if (!strcasecmp(action.c_str(), "suffix"))
90 act = Host::HCA_SUFFIX;
91 else if (!strcasecmp(action.c_str(), "addnick"))
92 act = Host::HCA_ADDNICK;
94 throw ModuleException("Invalid hostchange action: " + action);
96 hostchanges.push_back(std::make_pair(mask, Host(act, newhost, tag->getString("ports"))));
100 virtual Version GetVersion()
102 // returns the version number of the module to be
103 // listed in /MODULES
104 return Version("Provides masking of user hostnames in a different way to m_cloaking", VF_VENDOR);
107 virtual void OnUserConnect(LocalUser* user)
109 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
111 if (((InspIRCd::MatchCIDR(user->MakeHost(), i->first)) || (InspIRCd::MatchCIDR(user->MakeHostIP(), i->first))))
113 const Host& h = i->second;
115 if (!h.ports.empty())
117 irc::portparser portrange(h.ports, false);
119 bool foundany = false;
121 while ((portno = portrange.GetToken()))
122 if (portno == user->GetServerPort())
129 // host of new user matches a hostchange tag's mask
131 if (h.action == Host::HCA_SET)
135 else if (h.action == Host::HCA_SUFFIX)
139 else if (h.action == Host::HCA_ADDNICK)
141 // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
142 std::string complete;
143 for (std::string::const_iterator j = user->nick.begin(); j != user->nick.end(); ++j)
145 if (((*j >= 'A') && (*j <= 'Z')) ||
146 ((*j >= 'a') && (*j <= 'z')) ||
147 ((*j >= '0') && (*j <= '9')) ||
150 complete = complete + *j;
153 if (complete.empty())
154 complete = "i-have-a-lame-nick";
156 if (!MyPrefix.empty())
157 newhost = MyPrefix + MySeparator + complete;
159 newhost = complete + MySeparator + MySuffix;
161 if (!newhost.empty())
163 user->WriteServ("NOTICE "+user->nick+" :Setting your virtual host: " + newhost);
164 if (!user->ChangeDisplayedHost(newhost.c_str()))
165 user->WriteServ("NOTICE "+user->nick+" :Could not set your virtual host: " + newhost);
173 MODULE_INIT(ModuleHostChange)