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