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