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