]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
2bb1b107a5b48a3efcc3ad04a0a133cd2dabfd0f
[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
20 #include "inspircd.h"
21
22 /* $ModDesc: Forces connecting clients to send a PONG message back to the server before they can complete their connection */
23
24
25
26 char* RandString(unsigned int length)
27 {
28         unsigned char* out = new unsigned char[length+1];
29         for(unsigned int i = 0; i < length; i++)
30                 out[i] = ((rand() % 26) + 65);
31         out[length] = '\0';
32
33         return (char*)out;
34 }
35
36 class ModuleWaitPong : public Module
37 {
38         
39         ConfigReader* Conf;
40         
41         bool sendsnotice;
42         bool killonbadreply;
43
44  public:
45         ModuleWaitPong(InspIRCd* Me)
46                 : Module::Module(Me)
47         {
48                 OnRehash(NULL,"");
49         }
50         
51         virtual void OnRehash(userrec* user, const std::string &param)
52         {
53                 Conf = new ConfigReader(ServerInstance);
54                 
55                 sendsnotice = Conf->ReadFlag("waitpong", "sendsnotice", 0);
56                 
57                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
58                         sendsnotice = true;
59                 
60                 killonbadreply = Conf->ReadFlag("waitpong", "killonbadreply", 0);
61
62                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
63                         killonbadreply = true;
64                                 
65                 DELETE(Conf);
66         }
67
68         void Implements(char* List)
69         {
70                 List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnPreCommand] = List[I_OnRehash] = List[I_OnUserDisconnect] = List[I_OnCleanup] = 1;
71         }
72
73         virtual int OnUserRegister(userrec* user)
74         {
75                 char* pingrpl = RandString(10);
76                 
77                 user->Write("PING :%s", pingrpl);
78                 
79                 if(sendsnotice)
80                         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);
81                         
82                 user->Extend("waitpong_pingstr", pingrpl);
83                 return 0;
84         }
85         
86         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec* user, bool validated, const std::string &original_line)
87         {
88                 if(command == "PONG")
89                 {
90                         char* pingrpl;
91                         user->GetExt("waitpong_pingstr", pingrpl);
92                         
93                         if(pingrpl)
94                         {
95                                 if(strcmp(pingrpl, parameters[0]) == 0)
96                                 {
97                                         DELETE(pingrpl);
98                                         user->Shrink("waitpong_pingstr");
99                                         return 1;
100                                 }
101                                 else
102                                 {
103                                         if(killonbadreply)
104                                                 userrec::QuitUser(ServerInstance, user, "Incorrect ping reply for registration");
105                                         return 1;
106                                 }
107                         }
108                 }
109                 
110                 return 0;
111         }
112
113         virtual bool OnCheckReady(userrec* user)
114         {
115                 char* pingrpl;
116                 return (!user->GetExt("waitpong_pingstr", pingrpl));
117         }
118         
119         virtual void OnUserDisconnect(userrec* user)
120         {
121                 char* pingrpl;
122                 user->GetExt("waitpong_pingstr", pingrpl);
123
124                 if(pingrpl)
125                 {
126                         DELETE(pingrpl);
127                         user->Shrink("waitpong_pingstr");
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("waitpong_pingstr", pingrpl);
138                         
139                         if(pingrpl)
140                         {
141                                 DELETE(pingrpl);
142                                 user->Shrink("waitpong_pingstr");
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 }