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