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