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