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