]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
Tidyup
[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 int 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                 return 0;
81         }
82         
83         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec* user, bool validated, const std::string &original_line)
84         {
85                 if(command == "PONG")
86                 {
87                         char* pingrpl;
88                         user->GetExt("waitpong_pingstr", pingrpl);
89                         
90                         if(pingrpl)
91                         {
92                                 if(strcmp(pingrpl, parameters[0]) == 0)
93                                 {
94                                         DELETE(pingrpl);
95                                         user->Shrink("waitpong_pingstr");
96                                         return 1;
97                                 }
98                                 else
99                                 {
100                                         if(killonbadreply)
101                                                 userrec::QuitUser(ServerInstance, user, "Incorrect ping reply for registration");
102                                         return 1;
103                                 }
104                         }
105                 }
106                 
107                 return 0;
108         }
109
110         virtual bool OnCheckReady(userrec* user)
111         {
112                 char* pingrpl;
113                 return (!user->GetExt("waitpong_pingstr", pingrpl));
114         }
115         
116         virtual void OnUserDisconnect(userrec* user)
117         {
118                 char* pingrpl;
119                 user->GetExt("waitpong_pingstr", pingrpl);
120
121                 if(pingrpl)
122                 {
123                         DELETE(pingrpl);
124                         user->Shrink("waitpong_pingstr");
125                 }
126         }
127         
128         virtual void OnCleanup(int target_type, void* item)
129         {
130                 if(target_type == TYPE_USER)
131                 {
132                         userrec* user = (userrec*)item;
133                         char* pingrpl;
134                         user->GetExt("waitpong_pingstr", pingrpl);
135                         
136                         if(pingrpl)
137                         {
138                                 DELETE(pingrpl);
139                                 user->Shrink("waitpong_pingstr");
140                         } 
141                 }
142         }
143         
144         virtual ~ModuleWaitPong()
145         {
146         }
147         
148         virtual Version GetVersion()
149         {
150                 return Version(1, 1, 0, 1, VF_VENDOR, API_VERSION);
151         }
152         
153 };
154
155 class ModuleWaitPongFactory : public ModuleFactory
156 {
157  public:
158         ModuleWaitPongFactory()
159         {
160         }
161         
162         ~ModuleWaitPongFactory()
163         {
164         }
165         
166         virtual Module * CreateModule(InspIRCd* Me)
167         {
168                 return new ModuleWaitPong(Me);
169         }
170 };
171
172
173 extern "C" void * init_module( void )
174 {
175         return new ModuleWaitPongFactory;
176 }