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