]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
d7a4b833a02daa9349d64b5f705557a01e2202f9
[user/henk/code/inspircd.git] / src / modules / m_conn_waitpong.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <stdlib.h>
15 #include <string>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "inspircd.h"
20
21 /* $ModDesc: Forces connecting clients to send a PONG message back to the server before they can complete their connection */
22
23 class ModuleWaitPong : public Module
24 {
25         InspIRCd* Instance;
26         bool sendsnotice;
27         bool killonbadreply;
28         const char* extenstr;
29
30  public:
31         ModuleWaitPong(InspIRCd* Me)
32          : Module::Module(Me), Instance(Me), extenstr("waitpong_pingstr")
33         {
34                 OnRehash(NULL,"");
35         }
36         
37         virtual void OnRehash(userrec* user, const std::string &param)
38         {
39                 ConfigReader Conf(Instance);
40                 
41                 sendsnotice = Conf.ReadFlag("waitpong", "sendsnotice", 0);
42                 
43                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
44                         sendsnotice = true;
45                 
46                 killonbadreply = Conf.ReadFlag("waitpong", "killonbadreply", 0);
47
48                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
49                         killonbadreply = true;
50         }
51
52         void Implements(char* List)
53         {
54                 List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnPreCommand] = List[I_OnRehash] = List[I_OnUserDisconnect] = List[I_OnCleanup] = 1;
55         }
56
57         char* RandString(unsigned int length)
58         {
59                 unsigned char* out = new unsigned char[length+1];
60                 for(unsigned int i = 0; i < length; i++)
61                         out[i] = ((rand() % 26) + 65);
62                 out[length] = '\0';
63         
64                 return (char*)out;
65         }
66         
67         virtual int OnUserRegister(userrec* user)
68         {
69                 char* pingrpl = RandString(10);
70                 
71                 user->Write("PING :%s", pingrpl);
72                 
73                 if(sendsnotice)
74                         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);
75                         
76                 user->Extend(extenstr, pingrpl);
77                 return 0;
78         }
79         
80         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec* user, bool validated, const std::string &original_line)
81         {
82                 if(command == "PONG")
83                 {
84                         ServerInstance->Log(DEBUG,"PONG command");
85                         char* pingrpl;
86                         user->GetExt(extenstr, pingrpl);
87                         
88                         if(pingrpl)
89                         {
90                                 ServerInstance->Log(DEBUG,"PONG command - has extend");
91                                 if(strcmp(pingrpl, parameters[0]) == 0)
92                                 {
93                                         ServerInstance->Log(DEBUG,"PONG command - pong matches ping ");
94                                         DELETE(pingrpl);
95                                         user->Shrink(extenstr);
96                                         return 1;
97                                 }
98                                 else
99                                 {
100                                         ServerInstance->Log(DEBUG,"PONG command - pong doesnt match ping");
101                                         if(killonbadreply)
102                                                 userrec::QuitUser(ServerInstance, user, "Incorrect ping reply for registration");
103                                         return 1;
104                                 }
105                         }
106                 }
107                 
108                 ServerInstance->Log(DEBUG,"PONG command - fall through");
109                 return 0;
110         }
111
112         virtual bool OnCheckReady(userrec* user)
113         {
114                 ServerInstance->Log(DEBUG,"PONG command - oncheckready");
115                 char* pingrpl;
116                 return (!user->GetExt(extenstr, pingrpl));
117         }
118         
119         virtual void OnUserDisconnect(userrec* user)
120         {
121                 char* pingrpl;
122                 user->GetExt(extenstr, pingrpl);
123
124                 if(pingrpl)
125                 {
126                         DELETE(pingrpl);
127                         user->Shrink(extenstr);
128                 }
129         }
130         
131         virtual void OnCleanup(int target_type, void* item)
132         {
133                 if(target_type == TYPE_USER)
134                 {
135                         userrec* user = (userrec*)item;
136                         char* pingrpl;
137                         user->GetExt(extenstr, pingrpl);
138                         
139                         if(pingrpl)
140                         {
141                                 DELETE(pingrpl);
142                                 user->Shrink(extenstr);
143                         } 
144                 }
145         }
146         
147         virtual ~ModuleWaitPong()
148         {
149         }
150         
151         virtual Version GetVersion()
152         {
153                 return Version(1, 1, 0, 1, VF_VENDOR, API_VERSION);
154         }
155         
156 };
157
158 class ModuleWaitPongFactory : public ModuleFactory
159 {
160  public:
161         ModuleWaitPongFactory()
162         {
163         }
164         
165         ~ModuleWaitPongFactory()
166         {
167         }
168         
169         virtual Module * CreateModule(InspIRCd* Me)
170         {
171                 return new ModuleWaitPong(Me);
172         }
173 };
174
175
176 extern "C" void * init_module( void )
177 {
178         return new ModuleWaitPongFactory;
179 }