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