]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / modules / m_hostchange.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides masking of user hostnames in a different way to m_cloaking */
17
18 /** Holds information on a host set by m_hostchange
19  */
20 class Host : public classbase
21 {
22  public:
23         std::string action;
24         std::string newhost;
25         std::string ports;
26 };
27
28 typedef std::map<std::string,Host*> hostchanges_t;
29
30 class ModuleHostChange : public Module
31 {
32  private:
33         hostchanges_t hostchanges;
34         std::string MySuffix;
35         std::string MyPrefix;
36         std::string MySeparator;
37
38  public:
39         ModuleHostChange()
40                         {
41                 OnRehash(NULL);
42                 Implementation eventlist[] = { I_OnRehash, I_OnUserConnect };
43                 ServerInstance->Modules->Attach(eventlist, this, 2);
44         }
45
46         virtual ~ModuleHostChange()
47         {
48                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
49                 {
50                         delete i->second;
51                 }
52                 hostchanges.clear();
53         }
54
55         void Prioritize()
56         {
57                 Module* cloak = ServerInstance->Modules->Find("m_cloaking.so");
58                 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_AFTER, &cloak);
59         }
60
61
62         virtual void OnRehash(User* user)
63         {
64                 ConfigReader Conf;
65                 MySuffix = Conf.ReadValue("host","suffix",0);
66                 MyPrefix = Conf.ReadValue("host","prefix","",0);
67                 MySeparator = Conf.ReadValue("host","separator",".",0);
68                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
69                 {
70                         delete i->second;
71                 }
72                 hostchanges.clear();
73                 for (int index = 0; index < Conf.Enumerate("hostchange"); index++)
74                 {
75                         std::string mask = Conf.ReadValue("hostchange", "mask", index);
76                         std::string ports = Conf.ReadValue("hosthange", "ports", index);
77                         std::string action = Conf.ReadValue("hostchange", "action", index);
78                         std::string newhost = Conf.ReadValue("hostchange", "value", index);
79                         Host* x = new Host;
80                         x->action = action;
81                         x->ports = ports;
82                         x->newhost = newhost;
83                         hostchanges[mask] = x;
84                 }
85         }
86
87         virtual Version GetVersion()
88         {
89                 // returns the version number of the module to be
90                 // listed in /MODULES
91                 return Version("Provides masking of user hostnames in a different way to m_cloaking", VF_VENDOR, API_VERSION);
92         }
93
94         virtual void OnUserConnect(User* user)
95         {
96                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
97                 {
98                         if (((InspIRCd::MatchCIDR(user->MakeHost(), i->first)) || (InspIRCd::MatchCIDR(user->MakeHostIP(), i->first))))
99                         {
100                                 Host* h = i->second;
101
102                                 if (!h->ports.empty())
103                                 {
104                                         irc::portparser portrange(h->ports, false);
105                                         long portno = -1;
106                                         bool foundany = false;
107
108                                         while ((portno = portrange.GetToken()))
109                                                 if (portno == user->GetServerPort())
110                                                         foundany = true;
111
112                                         if (!foundany)
113                                                 continue;
114                                 }
115
116                                 // host of new user matches a hostchange tag's mask
117                                 std::string newhost;
118                                 if (h->action == "set")
119                                 {
120                                         newhost = h->newhost;
121                                 }
122                                 else if (h->action == "suffix")
123                                 {
124                                         newhost = MySuffix;
125                                 }
126                                 else if (h->action == "addnick")
127                                 {
128                                         // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
129                                         std::string complete;
130                                         std::string old = user->nick;
131                                         for (unsigned int j = 0; j < old.length(); j++)
132                                         {
133                                                 if  (((old[j] >= 'A') && (old[j] <= 'Z')) ||
134                                                     ((old[j] >= 'a') && (old[j] <= 'z')) ||
135                                                     ((old[j] >= '0') && (old[j] <= '9')) ||
136                                                     (old[j] == '-'))
137                                                 {
138                                                         complete = complete + old[j];
139                                                 }
140                                         }
141                                         if (complete.empty())
142                                                 complete = "i-have-a-lame-nick";
143
144                                         if (!MyPrefix.empty())
145                                                 newhost = MyPrefix + MySeparator + complete;
146                                         else
147                                                 newhost = complete + MySeparator + MySuffix;
148                                 }
149                                 if (!newhost.empty())
150                                 {
151                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Setting your virtual host: " + newhost);
152                                         if (!user->ChangeDisplayedHost(newhost.c_str()))
153                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :Could not set your virtual host: " + newhost);
154                                         return;
155                                 }
156                         }
157                 }
158         }
159 };
160
161 MODULE_INIT(ModuleHostChange)