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