]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
Removed a pointless check in ./configure --clean that made it only work with one...
[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 <stdio.h>
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "inspircd.h"
19
20 /* $ModDesc: Provides masking of user hostnames in a different way to m_cloaking */
21
22 /** Holds information on a host set by m_hostchange
23  */
24 class Host : public classbase
25 {
26  public:
27         std::string action;
28         std::string newhost;
29 };
30
31 typedef std::map<std::string,Host*> hostchanges_t;
32
33 class ModuleHostChange : public Module
34 {
35  private:
36
37         
38         ConfigReader *Conf;
39         hostchanges_t hostchanges;
40         std::string MySuffix;
41          
42  public:
43         ModuleHostChange(InspIRCd* Me)
44                 : Module::Module(Me)
45         {
46                 
47                 Conf = new ConfigReader(ServerInstance);
48                 OnRehash("");
49         }
50         
51         virtual ~ModuleHostChange()
52         {
53                 DELETE(Conf);
54         }
55
56         Priority Prioritize()
57         {
58                 return (Priority)ServerInstance->PriorityAfter("m_cloaking.so");
59         }
60
61         void Implements(char* List)
62         {
63                 List[I_OnRehash] = List[I_OnUserConnect] = 1;
64         }
65
66         virtual void OnRehash(const std::string &parameter)
67         {
68                 DELETE(Conf);
69                 Conf = new ConfigReader(ServerInstance);
70                 MySuffix = Conf->ReadValue("host","suffix",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 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->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(1,1,0,1,VF_VENDOR,API_VERSION);
93         }
94         
95         virtual void OnUserConnect(userrec* user)
96         {
97                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
98                 {
99                         if (ServerInstance->MatchText(std::string(user->ident)+"@"+std::string(user->host),i->first))
100                         {
101                                 Host* h = (Host*)i->second;
102                                 // host of new user matches a hostchange tag's mask
103                                 std::string newhost = "";
104                                 if (h->action == "set")
105                                 {
106                                         newhost = h->newhost;
107                                 }
108                                 else if (h->action == "suffix")
109                                 {
110                                         newhost = MySuffix;
111                                 }
112                                 else if (h->action == "addnick")
113                                 {
114                                         // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
115                                         std::string complete = "";
116                                         std::string old = user->nick;
117                                         for (unsigned int j = 0; j < old.length(); j++)
118                                         {
119                                                 if  (((old[j] >= 'A') && (old[j] <= 'Z')) ||
120                                                     ((old[j] >= 'a') && (old[j] <= 'z')) ||
121                                                     ((old[j] >= '0') && (old[j] <= '9')) ||
122                                                     (old[j] == '-'))
123                                                 {
124                                                         complete = complete + old[j];
125                                                 }
126                                         }
127                                         if (complete == "")
128                                                 complete = "i-have-a-lame-nick";
129                                         newhost = complete + "." + MySuffix;
130                                 }
131                                 if (newhost != "")
132                                 {
133                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Setting your virtual host: " + newhost);
134                                         if (!user->ChangeDisplayedHost(newhost.c_str()))
135                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :Could not set your virtual host: " + newhost);
136                                         return;
137                                 }
138                         }
139                 }
140         }
141 };
142
143 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
144
145 class ModuleHostChangeFactory : public ModuleFactory
146 {
147  public:
148         ModuleHostChangeFactory()
149         {
150         }
151         
152         ~ModuleHostChangeFactory()
153         {
154         }
155         
156         virtual Module * CreateModule(InspIRCd* Me)
157         {
158                 return new ModuleHostChange(Me);
159         }
160         
161 };
162
163
164 extern "C" void * init_module( void )
165 {
166         return new ModuleHostChangeFactory;
167 }
168