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