]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cloaking.cpp
b467c01d5821f557b1acef6792394ff9f1f0e055
[user/henk/code/inspircd.git] / src / modules / m_cloaking.cpp
1 // Hostname cloaking (+x mode) module for inspircd.
2 // version 1.0.0.1 by brain (C. J. Edwards) Mar 2004.
3 //
4 // When loaded this module will automatically set the
5 // +x mode on all connecting clients.
6 //
7 // Setting +x on a client causes the module to change the
8 // dhost entry (displayed host) for each user who has the
9 // mode, cloaking their host. Unlike unreal, the algorithm
10 // is non-reversible as uncloaked hosts are passed along
11 // the server->server link, and all encoding of hosts is
12 // done locally on the server by this module.
13
14 #include <stdio.h>
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18
19 /* $ModDesc: Provides masking of user hostnames */
20
21 class ModuleCloaking : public Module
22 {
23  private:
24
25          Server *Srv;
26          
27  public:
28         ModuleCloaking()
29         {
30                 // We must create an instance of the Server class to work with
31                 Srv = new Server;
32                 
33                 // we must create a new mode. Set the parameters so the
34                 // mode doesn't require oper, and is a client usermode
35                 // with no parameters (actually, you cant have params for a umode!)
36                 if (!Srv->AddExtendedMode('x',MT_CLIENT,false,0,0))
37                 {
38                         // we couldn't claim mode x... possibly anther module has it,
39                         // this might become likely to happen if there are a lot of 3rd
40                         // party modules around in the future -- any 3rd party modules
41                         // SHOULD implement a system of configurable mode letters (e.g.
42                         // from a config file)
43                         Srv->Log(DEFAULT,"*** m_cloaking: ERROR, failed to allocate user mode +x!");
44                         printf("Could not claim usermode +x for this module!");
45                         exit(0);
46                 }
47         }
48         
49         virtual ~ModuleCloaking()
50         {
51                 // not really neccessary, but free it anyway
52                 delete Srv;
53         }
54         
55         virtual Version GetVersion()
56         {
57                 // returns the version number of the module to be
58                 // listed in /MODULES
59                 return Version(1,0,0,1);
60         }
61         
62         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
63         {
64                 // this method is called for any extended mode character.
65                 // all module modes for all modules pass through here
66                 // (unless another module further up the chain claims them)
67                 // so we must be VERY careful to only act upon modes which
68                 // we have claimed ourselves. This is a feature to allow
69                 // modules to 'spy' on extended mode activity if they so wish.
70                 log(DEBUG,"modechar=%c type=%d MT_CLIENT=%d",modechar,type,MT_CLIENT);
71                 
72                 if ((modechar == 'x') && (type == MT_CLIENT))
73                 {
74                         Srv->Log(DEBUG,"Mode x being handled");
75                         // OnExtendedMode gives us a void* as the target, we must cast
76                         // it into a userrec* or a chanrec* depending on the value of
77                         // the 'type' parameter (MT_CLIENT or MT_CHANNEL)
78                         userrec* dest = (userrec*)target;
79                         
80                         // we've now determined that this is our mode character...
81                         // is the user adding the mode to their list or removing it?
82                         if (mode_on)
83                         {
84                                 // the mode is being turned on - so attempt to
85                                 // allocate the user a cloaked host using a non-reversible
86                                 // algorithm (its simple, but its non-reversible so the
87                                 // simplicity doesnt really matter). This algorithm
88                                 // will not work if the user has only one level of domain
89                                 // naming in their hostname (e.g. if they are on a lan or
90                                 // are connecting via localhost) -- this doesnt matter much.
91                                 if (strchr(dest->host,'.'))
92                                 {
93                                         // in inspircd users have two hostnames. A displayed
94                                         // hostname which can be modified by modules (e.g.
95                                         // to create vhosts, implement chghost, etc) and a
96                                         // 'real' hostname which you shouldnt write to.
97                                         std::string a = strstr(dest->host,".");
98                                         char ra[64];
99                                         long seed,s2;
100                                         memcpy(&seed,dest->host,sizeof(long));
101                                         memcpy(&s2,a.c_str(),sizeof(long));
102                                         sprintf(ra,"%.8X",seed*s2*strlen(dest->host));
103                                         std::string b = Srv->GetNetworkName() + "-" + ra + a;
104                                         Srv->Log(DEBUG,"cloak: allocated "+b);
105                                         Srv->ChangeHost(user,b);
106                                 }
107                         }
108                         else
109                         {
110                                 // user is removing the mode, so just restore their real host
111                                 // and make it match the displayed one.
112                                 Srv->ChangeHost(user,user->host);
113                         }
114                         // this mode IS ours, and we have handled it. If we chose not to handle it,
115                         // for example the user cannot cloak as they have a vhost or such, then
116                         // we could return 0 here instead of 1 and the core would not send the mode
117                         // change to the user.
118                         return 1;
119                 }
120                 else
121                 {
122                         // this mode isn't ours, we have to bail and return 0 to not handle it.
123                         return 0;
124                 }
125         }
126
127         virtual void OnUserConnect(userrec* user)
128         {
129                 // Heres the weird bit. When a user connects we must set +x on them, so
130                 // we're going to use the SendMode method of the Server class to send
131                 // the mode to the client. This is basically the same as sending an
132                 // SAMODE in unreal. Note that to the user it will appear as if they set
133                 // the mode on themselves.
134                 
135                 char* modes[2];                 // only two parameters
136                 modes[0] = user->nick;          // first parameter is the nick
137                 modes[1] = "+x";                // second parameter is the mode
138                 Srv->SendMode(modes,2,user);    // send these, forming the command "MODE <nick> +x"
139         }
140
141 };
142
143 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
144
145 class ModuleCloakingFactory : public ModuleFactory
146 {
147  public:
148         ModuleCloakingFactory()
149         {
150         }
151         
152         ~ModuleCloakingFactory()
153         {
154         }
155         
156         virtual Module * CreateModule()
157         {
158                 return new ModuleCloaking;
159         }
160         
161 };
162
163
164 extern "C" void * init_module( void )
165 {
166         return new ModuleCloakingFactory;
167 }
168