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