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