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