]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
5d4a9e94c797abde445697f9204bd003014164b6
[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 static std::string 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]; 
19
20         to64frombits(out, tmp, (length/4)*3);
21         
22         std::string ret((char*)out);
23
24         delete out;
25         delete tmp;
26         
27         return ret;
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(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_OnUserDisconnect] = List[I_OnPreCommand] = List[I_OnRehash] = 1;
66         }
67
68         virtual void OnUserRegister(userrec* user)
69         {
70                 std::string* pingrpl = new std::string;
71                 *pingrpl = RandString(10);
72                 
73                 Srv->Send(user->fd, "PING :" + *pingrpl);
74                 
75                 if(sendsnotice)
76                         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->c_str(), pingrpl->c_str());
77                         
78                 user->Extend("waitpong_pingstr", (char*)pingrpl);
79         }
80         
81         virtual int OnPreCommand(std::string command, char** parameters, int pcnt,      userrec* user,  bool validated)
82         {
83                 if(command == "PONG")
84                 {
85                         std::string* pingrpl = (std::string*)user->GetExt("waitpong_pingstr");
86                         
87                         if(pingrpl && (*pingrpl == parameters[0]))
88                         {
89                                 delete pingrpl;
90                                 user->Shrink("waitpong_pingstr");
91                                 return 1;
92                         }
93                         else if(killonbadreply)
94                         {
95                                 Srv->QuitUser(user, "Incorrect ping reply for registration");
96                         }
97                 }
98                 
99                 return 0;
100         }
101
102         virtual bool OnCheckReady(userrec* user)
103         {
104                 return (!user->GetExt("waitpong_pingstr"));
105         }
106                 
107         virtual ~ModuleWaitPong()
108         {
109         }
110         
111         virtual Version GetVersion()
112         {
113                 return Version(1, 0, 0, 0, VF_VENDOR);
114         }
115         
116 };
117
118 class ModuleWaitPongFactory : public ModuleFactory
119 {
120  public:
121         ModuleWaitPongFactory()
122         {
123         }
124         
125         ~ModuleWaitPongFactory()
126         {
127         }
128         
129         virtual Module * CreateModule(Server* Me)
130         {
131                 return new ModuleWaitPong(Me);
132         }
133 };
134
135
136 extern "C" void * init_module( void )
137 {
138         return new ModuleWaitPongFactory;
139 }