]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
937577c5ec2443e67b0758a5374943d0a9eb48cf
[user/henk/code/inspircd.git] / src / modules / m_conn_waitpong.cpp
1 #include <stdlib.h>
2 #include <string>
3 #include "aes.h"
4 #include "users.h"
5 #include "channels.h"
6 #include "modules.h"
7
8 #include "inspircd.h"
9
10 /* $ModDesc: Forces connecting clients to send a PONG message back to the server before they can complete their connection */
11
12
13
14 char* RandString(unsigned int length)
15 {
16         unsigned char* tmp = new unsigned char[(length/4)*3];
17         
18         for(unsigned int i = 0; i < (length/4)*3; i++)
19                 tmp[i] = (unsigned char)rand();
20                 
21         unsigned char* out = new unsigned char[length+1];
22
23         to64frombits(out, tmp, (length/4)*3);
24         
25         out[length] = '\0';
26         
27         DELETE(tmp);
28         
29         return (char*)out;
30 }
31
32 class ModuleWaitPong : public Module
33 {
34         
35         ConfigReader* Conf;
36         
37         bool sendsnotice;
38         bool killonbadreply;
39
40  public:
41         ModuleWaitPong(InspIRCd* Me)
42                 : Module::Module(Me)
43         {
44                 
45                 OnRehash("");
46         }
47         
48         virtual void OnRehash(const std::string &param)
49         {
50                 Conf = new ConfigReader(ServerInstance);
51                 
52                 sendsnotice = Conf->ReadFlag("waitpong", "sendsnotice", 0);
53                 
54                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
55                         sendsnotice = true;
56                 
57                 killonbadreply = Conf->ReadFlag("waitpong", "killonbadreply", 0);
58
59                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
60                         killonbadreply = true;
61                                 
62                 DELETE(Conf);
63         }
64
65         void Implements(char* List)
66         {
67                 List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnPreCommand] = List[I_OnRehash] = List[I_OnUserDisconnect] = List[I_OnCleanup] = 1;
68         }
69
70         virtual void OnUserRegister(userrec* user)
71         {
72                 char* pingrpl = RandString(10);
73                 
74                 user->Write("PING :%s", pingrpl);
75                 
76                 if(sendsnotice)
77                         user->WriteServ("NOTICE %s :*** If you are having problems connecting due to ping timeouts, please type /quote PONG %s or /raw PONG %s now.", user->nick, pingrpl, pingrpl);
78                         
79                 user->Extend("waitpong_pingstr", pingrpl);
80         }
81         
82         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec* user, bool validated)
83         {
84                 if(command == "PONG")
85                 {
86                         char* pingrpl;
87                         user->GetExt("waitpong_pingstr", pingrpl);
88                         
89                         if(pingrpl)
90                         {
91                                 if(strcmp(pingrpl, parameters[0]) == 0)
92                                 {
93                                         DELETE(pingrpl);
94                                         user->Shrink("waitpong_pingstr");
95                                         return 1;
96                                 }
97                                 else
98                                 {
99                                         if(killonbadreply)
100                                                 userrec::QuitUser(ServerInstance, user, "Incorrect ping reply for registration");
101                                         return 1;
102                                 }
103                         }
104                 }
105                 
106                 return 0;
107         }
108
109         virtual bool OnCheckReady(userrec* user)
110         {
111                 char* pingrpl;
112                 return (!user->GetExt("waitpong_pingstr", pingrpl));
113         }
114         
115         virtual void OnUserDisconnect(userrec* user)
116         {
117                 char* pingrpl;
118                 user->GetExt("waitpong_pingstr", pingrpl);
119
120                 if(pingrpl)
121                 {
122                         DELETE(pingrpl);
123                         user->Shrink("waitpong_pingstr");
124                 }
125         }
126         
127         virtual void OnCleanup(int target_type, void* item)
128         {
129                 if(target_type == TYPE_USER)
130                 {
131                         userrec* user = (userrec*)item;
132                         char* pingrpl;
133                         user->GetExt("waitpong_pingstr", pingrpl);
134                         
135                         if(pingrpl)
136                         {
137                                 DELETE(pingrpl);
138                                 user->Shrink("waitpong_pingstr");
139                         } 
140                 }
141         }
142         
143         virtual ~ModuleWaitPong()
144         {
145         }
146         
147         virtual Version GetVersion()
148         {
149                 return Version(1, 0, 0, 1, VF_VENDOR);
150         }
151         
152 };
153
154 class ModuleWaitPongFactory : public ModuleFactory
155 {
156  public:
157         ModuleWaitPongFactory()
158         {
159         }
160         
161         ~ModuleWaitPongFactory()
162         {
163         }
164         
165         virtual Module * CreateModule(InspIRCd* Me)
166         {
167                 return new ModuleWaitPong(Me);
168         }
169 };
170
171
172 extern "C" void * init_module( void )
173 {
174         return new ModuleWaitPongFactory;
175 }