]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
Merge branch 'insp20' into master.
[user/henk/code/inspircd.git] / src / modules / m_hostchange.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20
21
22 #include "inspircd.h"
23
24 /** Holds information on a host set by m_hostchange
25  */
26 class Host
27 {
28  public:
29         enum HostChangeAction
30         {
31                 HCA_SET,
32                 HCA_SUFFIX,
33                 HCA_ADDNICK
34         };
35
36         HostChangeAction action;
37         std::string newhost;
38         std::string ports;
39
40         Host(HostChangeAction Action, const std::string& Newhost, const std::string& Ports) :
41                 action(Action), newhost(Newhost), ports(Ports) {}
42 };
43
44 typedef std::vector<std::pair<std::string, Host> > hostchanges_t;
45
46 class ModuleHostChange : public Module
47 {
48         hostchanges_t hostchanges;
49         std::string MySuffix;
50         std::string MyPrefix;
51         std::string MySeparator;
52
53  public:
54         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
55         {
56                 ConfigTag* host = ServerInstance->Config->ConfValue("host");
57                 MySuffix = host->getString("suffix");
58                 MyPrefix = host->getString("prefix");
59                 MySeparator = host->getString("separator", ".");
60                 hostchanges.clear();
61
62                 std::set<std::string> dupecheck;
63                 ConfigTagList tags = ServerInstance->Config->ConfTags("hostchange");
64                 for (ConfigIter i = tags.first; i != tags.second; ++i)
65                 {
66                         ConfigTag* tag = i->second;
67                         std::string mask = tag->getString("mask");
68                         if (!dupecheck.insert(mask).second)
69                                 throw ModuleException("Duplicate hostchange entry: " + mask);
70
71                         Host::HostChangeAction act;
72                         std::string newhost;
73                         std::string action = tag->getString("action");
74                         if (!strcasecmp(action.c_str(), "set"))
75                         {
76                                 act = Host::HCA_SET;
77                                 newhost = tag->getString("value");
78                         }
79                         else if (!strcasecmp(action.c_str(), "suffix"))
80                                 act = Host::HCA_SUFFIX;
81                         else if (!strcasecmp(action.c_str(), "addnick"))
82                                 act = Host::HCA_ADDNICK;
83                         else
84                                 throw ModuleException("Invalid hostchange action: " + action);
85
86                         hostchanges.push_back(std::make_pair(mask, Host(act, newhost, tag->getString("ports"))));
87                 }
88         }
89
90         Version GetVersion() CXX11_OVERRIDE
91         {
92                 // returns the version number of the module to be
93                 // listed in /MODULES
94                 return Version("Provides masking of user hostnames in a different way to m_cloaking", VF_VENDOR);
95         }
96
97         void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
98         {
99                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
100                 {
101                         if (((InspIRCd::MatchCIDR(user->MakeHost(), i->first)) || (InspIRCd::MatchCIDR(user->MakeHostIP(), i->first))))
102                         {
103                                 const Host& h = i->second;
104
105                                 if (!h.ports.empty())
106                                 {
107                                         irc::portparser portrange(h.ports, false);
108                                         long portno = -1;
109                                         bool foundany = false;
110
111                                         while ((portno = portrange.GetToken()))
112                                                 if (portno == user->GetServerPort())
113                                                         foundany = true;
114
115                                         if (!foundany)
116                                                 continue;
117                                 }
118
119                                 // host of new user matches a hostchange tag's mask
120                                 std::string newhost;
121                                 if (h.action == Host::HCA_SET)
122                                 {
123                                         newhost = h.newhost;
124                                 }
125                                 else if (h.action == Host::HCA_SUFFIX)
126                                 {
127                                         newhost = MySuffix;
128                                 }
129                                 else if (h.action == Host::HCA_ADDNICK)
130                                 {
131                                         // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
132                                         std::string complete;
133                                         for (std::string::const_iterator j = user->nick.begin(); j != user->nick.end(); ++j)
134                                         {
135                                                 if  (((*j >= 'A') && (*j <= 'Z')) ||
136                                                     ((*j >= 'a') && (*j <= 'z')) ||
137                                                     ((*j >= '0') && (*j <= '9')) ||
138                                                     (*j == '-'))
139                                                 {
140                                                         complete = complete + *j;
141                                                 }
142                                         }
143                                         if (complete.empty())
144                                                 complete = "i-have-a-lame-nick";
145
146                                         if (!MyPrefix.empty())
147                                                 newhost = MyPrefix + MySeparator + complete;
148                                         else
149                                                 newhost = complete + MySeparator + MySuffix;
150                                 }
151                                 if (!newhost.empty())
152                                 {
153                                         user->WriteNotice("Setting your virtual host: " + newhost);
154                                         if (!user->ChangeDisplayedHost(newhost))
155                                                 user->WriteNotice("Could not set your virtual host: " + newhost);
156                                         return;
157                                 }
158                         }
159                 }
160         }
161 };
162
163 MODULE_INIT(ModuleHostChange)