]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
Add random number generation functions to InspIRCd class.
[user/henk/code/inspircd.git] / src / modules / m_conn_waitpong.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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         LocalStringExt ext;
23
24  public:
25         ModuleWaitPong()
26          : ext("waitpong_pingstr", this)
27         {
28                 OnRehash(NULL);
29                 Implementation eventlist[] = { I_OnUserRegister, I_OnCheckReady, I_OnPreCommand, I_OnRehash };
30                 ServerInstance->Modules->Attach(eventlist, this, 4);
31         }
32
33         void OnRehash(User* user)
34         {
35                 ConfigReader Conf;
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         ModResult OnUserRegister(LocalUser* user)
49         {
50                 std::string pingrpl = ServerInstance->GenRandomStr(10);
51
52                 user->Write("PING :%s", pingrpl.c_str());
53
54                 if(sendsnotice)
55                         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.c_str(), pingrpl.c_str());
56
57                 ext.set(user, pingrpl);
58                 return MOD_RES_PASSTHRU;
59         }
60
61         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser* user, bool validated, const std::string &original_line)
62         {
63                 if (command == "PONG")
64                 {
65                         std::string* pingrpl = ext.get(user);
66
67                         if (pingrpl)
68                         {
69                                 if (!parameters.empty() && *pingrpl == parameters[0])
70                                 {
71                                         ext.unset(user);
72                                         return MOD_RES_DENY;
73                                 }
74                                 else
75                                 {
76                                         if(killonbadreply)
77                                                 ServerInstance->Users->QuitUser(user, "Incorrect ping reply for registration");
78                                         return MOD_RES_DENY;
79                                 }
80                         }
81                 }
82                 return MOD_RES_PASSTHRU;
83         }
84
85         ModResult OnCheckReady(LocalUser* user)
86         {
87                 return ext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU;
88         }
89
90         ~ModuleWaitPong()
91         {
92         }
93
94         Version GetVersion()
95         {
96                 return Version("Require pong prior to registration", VF_VENDOR);
97         }
98
99 };
100
101 MODULE_INIT(ModuleWaitPong)