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