]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
0d7060f8b7a1e29d0f404d4c1e0f21a53f3583e6
[user/henk/code/inspircd.git] / src / modules / m_hostchange.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23
24 /* $ModDesc: Provides masking of user hostnames in a different way to m_cloaking */
25
26 class ModuleHostChange : public Module
27 {
28  private:
29
30         Server *Srv;
31         ConfigReader *Conf;
32         std::string MySuffix;
33          
34  public:
35         ModuleHostChange(Server* Me)
36                 : Module::Module(Me)
37         {
38                 Srv = Me;
39                 Conf = new ConfigReader;
40                 MySuffix = Conf->ReadValue("host","suffix",0);
41         }
42         
43         virtual ~ModuleHostChange()
44         {
45                 delete Conf;
46         }
47
48         void Implements(char* List)
49         {
50                 List[I_OnRehash] = List[I_OnUserConnect] = 1;
51         }
52
53         virtual void OnRehash(std::string parameter)
54         {
55                 delete Conf;
56                 Conf = new ConfigReader;
57                 MySuffix = Conf->ReadValue("host","suffix",0);
58         }
59         
60         virtual Version GetVersion()
61         {
62                 // returns the version number of the module to be
63                 // listed in /MODULES
64                 return Version(1,0,0,1,VF_VENDOR);
65         }
66         
67         virtual void OnUserConnect(userrec* user)
68         {
69                 for (int index = 0; index < Conf->Enumerate("hostchange"); index++)
70                 {
71                         std::string mask = Conf->ReadValue("hostchange","mask",index);
72                         if (Srv->MatchText(std::string(user->ident)+"@"+std::string(user->host),mask))
73                         {
74                                 std::string newhost = "";
75                                 // host of new user matches a hostchange tag's mask
76                                 std::string action = Conf->ReadValue("hostchange","action",index);
77                                 if (action == "set")
78                                 {
79                                         newhost = Conf->ReadValue("hostchange","value",index);
80                                 }
81                                 else if (action == "suffix")
82                                 {
83                                         newhost = MySuffix;
84                                 }
85                                 else if (action == "addnick")
86                                 {
87                                         // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
88                                         std::string complete = "";
89                                         std::string old = user->nick;
90                                         for (unsigned int j = 0; j < old.length(); j++)
91                                         {
92                                                 if  (((old[j] >= 'A') && (old[j] <= 'Z')) ||
93                                                     ((old[j] >= 'a') && (old[j] <= 'z')) ||
94                                                     ((old[j] >= '0') && (old[j] <= '9')) ||
95                                                     (old[j] == '-'))
96                                                 {
97                                                         complete = complete + old[j];
98                                                 }
99                                         }
100                                         if (complete == "")
101                                                 complete = "i-have-a-lame-nick";
102                                         newhost = complete + "." + MySuffix;
103                                 }
104                                 if (newhost != "")
105                                 {
106                                         Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Setting your VHost: " + newhost);
107                                         Srv->ChangeHost(user,newhost);
108                                         return;
109                                 }
110                         }
111                 }
112         }
113 };
114
115 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
116
117 class ModuleHostChangeFactory : public ModuleFactory
118 {
119  public:
120         ModuleHostChangeFactory()
121         {
122         }
123         
124         ~ModuleHostChangeFactory()
125         {
126         }
127         
128         virtual Module * CreateModule(Server* Me)
129         {
130                 return new ModuleHostChange(Me);
131         }
132         
133 };
134
135
136 extern "C" void * init_module( void )
137 {
138         return new ModuleHostChangeFactory;
139 }
140