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