]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[user/henk/code/inspircd.git] / src / modules / m_hostchange.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <stdio.h>
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "inspircd.h"
19
20 /* $ModDesc: Provides masking of user hostnames in a different way to m_cloaking */
21
22 /** Holds information on a host set by m_hostchange
23  */
24 class Host : public classbase
25 {
26  public:
27         std::string action;
28         std::string newhost;
29 };
30
31 typedef std::map<std::string,Host*> hostchanges_t;
32
33 class ModuleHostChange : public Module
34 {
35  private:
36
37         
38         ConfigReader *Conf;
39         hostchanges_t hostchanges;
40         std::string MySuffix;
41          
42  public:
43         ModuleHostChange(InspIRCd* Me)
44                 : Module(Me)
45         {
46                 Conf = new ConfigReader(ServerInstance);
47                 OnRehash(NULL,"");
48         }
49         
50         virtual ~ModuleHostChange()
51         {
52                 DELETE(Conf);
53         }
54
55         Priority Prioritize()
56         {
57                 return (Priority)ServerInstance->PriorityAfter("m_cloaking.so");
58         }
59
60         void Implements(char* List)
61         {
62                 List[I_OnRehash] = List[I_OnUserConnect] = 1;
63         }
64
65         virtual void OnRehash(userrec* user, const std::string &parameter)
66         {
67                 DELETE(Conf);
68                 Conf = new ConfigReader(ServerInstance);
69                 MySuffix = Conf->ReadValue("host","suffix",0);
70                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
71                 {
72                         DELETE(i->second);
73                 }
74                 hostchanges.clear();
75                 for (int index = 0; index < Conf->Enumerate("hostchange"); index++)
76                 {
77                         std::string mask = Conf->ReadValue("hostchange","mask",index);
78                         std::string action = Conf->ReadValue("hostchange","action",index);
79                         std::string newhost = Conf->ReadValue("hostchange","value",index);
80                         Host* x = new Host;
81                         x->action = action;
82                         x->newhost = newhost;
83                         hostchanges[mask] = x;
84                 }
85         }
86         
87         virtual Version GetVersion()
88         {
89                 // returns the version number of the module to be
90                 // listed in /MODULES
91                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
92         }
93         
94         virtual void OnUserConnect(userrec* user)
95         {
96                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
97                 {
98                         if (ServerInstance->MatchText(std::string(user->ident)+"@"+std::string(user->host),i->first))
99                         {
100                                 Host* h = (Host*)i->second;
101                                 // host of new user matches a hostchange tag's mask
102                                 std::string newhost = "";
103                                 if (h->action == "set")
104                                 {
105                                         newhost = h->newhost;
106                                 }
107                                 else if (h->action == "suffix")
108                                 {
109                                         newhost = MySuffix;
110                                 }
111                                 else if (h->action == "addnick")
112                                 {
113                                         // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
114                                         std::string complete = "";
115                                         std::string old = user->nick;
116                                         for (unsigned int j = 0; j < old.length(); j++)
117                                         {
118                                                 if  (((old[j] >= 'A') && (old[j] <= 'Z')) ||
119                                                     ((old[j] >= 'a') && (old[j] <= 'z')) ||
120                                                     ((old[j] >= '0') && (old[j] <= '9')) ||
121                                                     (old[j] == '-'))
122                                                 {
123                                                         complete = complete + old[j];
124                                                 }
125                                         }
126                                         if (complete == "")
127                                                 complete = "i-have-a-lame-nick";
128                                         newhost = complete + "." + MySuffix;
129                                 }
130                                 if (newhost != "")
131                                 {
132                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Setting your virtual host: " + newhost);
133                                         if (!user->ChangeDisplayedHost(newhost.c_str()))
134                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :Could not set your virtual host: " + newhost);
135                                         return;
136                                 }
137                         }
138                 }
139         }
140 };
141
142 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
143
144 class ModuleHostChangeFactory : public ModuleFactory
145 {
146  public:
147         ModuleHostChangeFactory()
148         {
149         }
150         
151         ~ModuleHostChangeFactory()
152         {
153         }
154         
155         virtual Module * CreateModule(InspIRCd* Me)
156         {
157                 return new ModuleHostChange(Me);
158         }
159         
160 };
161
162
163 extern "C" DllExport void * init_module( void )
164 {
165         return new ModuleHostChangeFactory;
166 }
167