]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
Get rid of Server::GetUsers(chanrec) - a throwback to before chanrec could do this...
[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                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Setting your virtual host: " + newhost);
137                                         if (!user->ChangeDisplayedHost(newhost.c_str()))
138                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :Could not set your virtual host: " + newhost);
139                                         return;
140                                 }
141                         }
142                 }
143         }
144 };
145
146 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
147
148 class ModuleHostChangeFactory : public ModuleFactory
149 {
150  public:
151         ModuleHostChangeFactory()
152         {
153         }
154         
155         ~ModuleHostChangeFactory()
156         {
157         }
158         
159         virtual Module * CreateModule(Server* Me)
160         {
161                 return new ModuleHostChange(Me);
162         }
163         
164 };
165
166
167 extern "C" void * init_module( void )
168 {
169         return new ModuleHostChangeFactory;
170 }
171