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