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