]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hostchange.cpp
Fix crash on propogation after routed squit has reached it's destination. Thx HiroP.
[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         hostchanges_t hostchanges;
36         std::string MySuffix;
37         std::string MyPrefix;
38         std::string MySeparator;
39          
40  public:
41         ModuleHostChange(InspIRCd* Me)
42                 : Module(Me)
43         {
44                 OnRehash(NULL,"");
45         }
46         
47         virtual ~ModuleHostChange()
48         {
49                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
50                 {
51                         DELETE(i->second);
52                 }
53                 hostchanges.clear();
54         }
55
56         Priority Prioritize()
57         {
58                 return (Priority)ServerInstance->PriorityAfter("m_cloaking.so");
59         }
60
61         void Implements(char* List)
62         {
63                 List[I_OnRehash] = List[I_OnUserConnect] = 1;
64         }
65
66         virtual void OnRehash(userrec* user, const std::string &parameter)
67         {
68                 ConfigReader Conf(ServerInstance);
69                 MySuffix = Conf.ReadValue("host","suffix",0);
70                 MyPrefix = Conf.ReadValue("host","prefix","",0);
71                 MySeparator = Conf.ReadValue("host","separator",".",0);
72                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
73                 {
74                         DELETE(i->second);
75                 }
76                 hostchanges.clear();
77                 for (int index = 0; index < Conf.Enumerate("hostchange"); index++)
78                 {
79                         std::string mask = Conf.ReadValue("hostchange","mask",index);
80                         std::string action = Conf.ReadValue("hostchange","action",index);
81                         std::string newhost = Conf.ReadValue("hostchange","value",index);
82                         Host* x = new Host;
83                         x->action = action;
84                         x->newhost = newhost;
85                         hostchanges[mask] = x;
86                 }
87         }
88         
89         virtual Version GetVersion()
90         {
91                 // returns the version number of the module to be
92                 // listed in /MODULES
93                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
94         }
95         
96         virtual void OnUserConnect(userrec* user)
97         {
98                 for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)
99                 {
100                         if (ServerInstance->MatchText(std::string(user->ident)+"@"+std::string(user->host),i->first))
101                         {
102                                 Host* h = (Host*)i->second;
103                                 // host of new user matches a hostchange tag's mask
104                                 std::string newhost;
105                                 if (h->action == "set")
106                                 {
107                                         newhost = h->newhost;
108                                 }
109                                 else if (h->action == "suffix")
110                                 {
111                                         newhost = MySuffix;
112                                 }
113                                 else if (h->action == "addnick")
114                                 {
115                                         // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]
116                                         std::string complete;
117                                         std::string old = user->nick;
118                                         for (unsigned int j = 0; j < old.length(); j++)
119                                         {
120                                                 if  (((old[j] >= 'A') && (old[j] <= 'Z')) ||
121                                                     ((old[j] >= 'a') && (old[j] <= 'z')) ||
122                                                     ((old[j] >= '0') && (old[j] <= '9')) ||
123                                                     (old[j] == '-'))
124                                                 {
125                                                         complete = complete + old[j];
126                                                 }
127                                         }
128                                         if (complete.empty())
129                                                 complete = "i-have-a-lame-nick";
130                                                 
131                                         if (!MyPrefix.empty())
132                                                 newhost = MyPrefix + MySeparator + complete;
133                                         else
134                                                 newhost = complete + MySeparator + MySuffix;
135                                 }
136                                 if (!newhost.empty())
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" DllExport void * init_module( void )
170 {
171         return new ModuleHostChangeFactory;
172 }
173