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