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