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