]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
Merge pull request #574 from SaberUK/master+build-comment-cleanup
[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 init() CXX11_OVERRIDE
55         {
56                 OnRehash(NULL);
57                 Implementation eventlist[] = { I_OnRehash, I_OnUserConnect };
58                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
59         }
60
61         void OnRehash(User* user) CXX11_OVERRIDE
62         {
63                 ConfigTag* host = ServerInstance->Config->ConfValue("host");
64                 MySuffix = host->getString("suffix");
65                 MyPrefix = host->getString("prefix");
66                 MySeparator = host->getString("separator", ".");
67                 hostchanges.clear();
68
69                 std::set<std::string> dupecheck;
70                 ConfigTagList tags = ServerInstance->Config->ConfTags("hostchange");
71                 for (ConfigIter i = tags.first; i != tags.second; ++i)
72                 {
73                         ConfigTag* tag = i->second;
74                         std::string mask = tag->getString("mask");
75                         if (!dupecheck.insert(mask).second)
76                                 throw ModuleException("Duplicate hostchange entry: " + mask);
77
78                         Host::HostChangeAction act;
79                         std::string newhost;
80                         std::string action = tag->getString("action");
81                         if (!strcasecmp(action.c_str(), "set"))
82                         {
83                                 act = Host::HCA_SET;
84                                 newhost = tag->getString("value");
85                         }
86                         else if (!strcasecmp(action.c_str(), "suffix"))
87                                 act = Host::HCA_SUFFIX;
88                         else if (!strcasecmp(action.c_str(), "addnick"))
89                                 act = Host::HCA_ADDNICK;
90                         else
91                                 throw ModuleException("Invalid hostchange action: " + action);
92
93                         hostchanges.push_back(std::make_pair(mask, Host(act, newhost, tag->getString("ports"))));
94                 }
95         }
96
97         Version GetVersion() CXX11_OVERRIDE
98         {
99                 // returns the version number of the module to be
100                 // listed in /MODULES
101                 return Version("Provides masking of user hostnames in a different way to m_cloaking", VF_VENDOR);
102         }
103
104         void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
105         {
106                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
107                 {
108                         if (((InspIRCd::MatchCIDR(user->MakeHost(), i->first)) || (InspIRCd::MatchCIDR(user->MakeHostIP(), i->first))))
109                         {
110                                 const Host& h = i->second;
111
112                                 if (!h.ports.empty())
113                                 {
114                                         irc::portparser portrange(h.ports, false);
115                                         long portno = -1;
116                                         bool foundany = false;
117
118                                         while ((portno = portrange.GetToken()))
119                                                 if (portno == user->GetServerPort())
120                                                         foundany = true;
121
122                                         if (!foundany)
123                                                 continue;
124                                 }
125
126                                 // host of new user matches a hostchange tag's mask
127                                 std::string newhost;
128                                 if (h.action == Host::HCA_SET)
129                                 {
130                                         newhost = h.newhost;
131                                 }
132                                 else if (h.action == Host::HCA_SUFFIX)
133                                 {
134                                         newhost = MySuffix;
135                                 }
136                                 else if (h.action == Host::HCA_ADDNICK)
137                                 {
138                                         // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
139                                         std::string complete;
140                                         for (std::string::const_iterator j = user->nick.begin(); j != user->nick.end(); ++j)
141                                         {
142                                                 if  (((*j >= 'A') && (*j <= 'Z')) ||
143                                                     ((*j >= 'a') && (*j <= 'z')) ||
144                                                     ((*j >= '0') && (*j <= '9')) ||
145                                                     (*j == '-'))
146                                                 {
147                                                         complete = complete + *j;
148                                                 }
149                                         }
150                                         if (complete.empty())
151                                                 complete = "i-have-a-lame-nick";
152
153                                         if (!MyPrefix.empty())
154                                                 newhost = MyPrefix + MySeparator + complete;
155                                         else
156                                                 newhost = complete + MySeparator + MySuffix;
157                                 }
158                                 if (!newhost.empty())
159                                 {
160                                         user->WriteNotice("Setting your virtual host: " + newhost);
161                                         if (!user->ChangeDisplayedHost(newhost.c_str()))
162                                                 user->WriteNotice("Could not set your virtual host: " + newhost);
163                                         return;
164                                 }
165                         }
166                 }
167         }
168 };
169
170 MODULE_INIT(ModuleHostChange)