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