]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
Blah
[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                 
49                 OnRehash("");
50         }
51         
52         virtual void OnRehash(const std::string &param)
53         {
54                 Conf = new ConfigReader(ServerInstance);
55                 
56                 sendsnotice = Conf->ReadFlag("waitpong", "sendsnotice", 0);
57                 
58                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
59                         sendsnotice = true;
60                 
61                 killonbadreply = Conf->ReadFlag("waitpong", "killonbadreply", 0);
62
63                 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
64                         killonbadreply = true;
65                                 
66                 DELETE(Conf);
67         }
68
69         void Implements(char* List)
70         {
71                 List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnPreCommand] = List[I_OnRehash] = List[I_OnUserDisconnect] = List[I_OnCleanup] = 1;
72         }
73
74         virtual int OnUserRegister(userrec* user)
75         {
76                 char* pingrpl = RandString(10);
77                 
78                 user->Write("PING :%s", pingrpl);
79                 
80                 if(sendsnotice)
81                         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);
82                         
83                 user->Extend("waitpong_pingstr", pingrpl);
84                 return 0;
85         }
86         
87         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec* user, bool validated, const std::string &original_line)
88         {
89                 if(command == "PONG")
90                 {
91                         char* pingrpl;
92                         user->GetExt("waitpong_pingstr", pingrpl);
93                         
94                         if(pingrpl)
95                         {
96                                 if(strcmp(pingrpl, parameters[0]) == 0)
97                                 {
98                                         DELETE(pingrpl);
99                                         user->Shrink("waitpong_pingstr");
100                                         return 1;
101                                 }
102                                 else
103                                 {
104                                         if(killonbadreply)
105                                                 userrec::QuitUser(ServerInstance, user, "Incorrect ping reply for registration");
106                                         return 1;
107                                 }
108                         }
109                 }
110                 
111                 return 0;
112         }
113
114         virtual bool OnCheckReady(userrec* user)
115         {
116                 char* pingrpl;
117                 return (!user->GetExt("waitpong_pingstr", pingrpl));
118         }
119         
120         virtual void OnUserDisconnect(userrec* user)
121         {
122                 char* pingrpl;
123                 user->GetExt("waitpong_pingstr", pingrpl);
124
125                 if(pingrpl)
126                 {
127                         DELETE(pingrpl);
128                         user->Shrink("waitpong_pingstr");
129                 }
130         }
131         
132         virtual void OnCleanup(int target_type, void* item)
133         {
134                 if(target_type == TYPE_USER)
135                 {
136                         userrec* user = (userrec*)item;
137                         char* pingrpl;
138                         user->GetExt("waitpong_pingstr", pingrpl);
139                         
140                         if(pingrpl)
141                         {
142                                 DELETE(pingrpl);
143                                 user->Shrink("waitpong_pingstr");
144                         } 
145                 }
146         }
147         
148         virtual ~ModuleWaitPong()
149         {
150         }
151         
152         virtual Version GetVersion()
153         {
154                 return Version(1, 1, 0, 1, VF_VENDOR, API_VERSION);
155         }
156         
157 };
158
159 class ModuleWaitPongFactory : public ModuleFactory
160 {
161  public:
162         ModuleWaitPongFactory()
163         {
164         }
165         
166         ~ModuleWaitPongFactory()
167         {
168         }
169         
170         virtual Module * CreateModule(InspIRCd* Me)
171         {
172                 return new ModuleWaitPong(Me);
173         }
174 };
175
176
177 extern "C" void * init_module( void )
178 {
179         return new ModuleWaitPongFactory;
180 }