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