]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_conn_waitpong.cpp
Some more to fix still, modules probably wont load correctly atm
[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                 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         void Implements(char* List)
49         {
50                 List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnPreCommand] = List[I_OnRehash] = List[I_OnUserDisconnect] = List[I_OnCleanup] = 1;
51         }
52
53         char* RandString(unsigned int length)
54         {
55                 unsigned char* out = new unsigned char[length+1];
56                 for(unsigned int i = 0; i < length; i++)
57                         out[i] = ((rand() % 26) + 65);
58                 out[length] = '\0';
59         
60                 return (char*)out;
61         }
62         
63         virtual int OnUserRegister(User* user)
64         {
65                 char* pingrpl = RandString(10);
66                 
67                 user->Write("PING :%s", pingrpl);
68                 
69                 if(sendsnotice)
70                         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);
71                         
72                 user->Extend(extenstr, pingrpl);
73                 return 0;
74         }
75         
76         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, User* user, bool validated, const std::string &original_line)
77         {
78                 if (command == "PONG")
79                 {
80                         char* pingrpl;
81                         user->GetExt(extenstr, pingrpl);
82                         
83                         if (pingrpl)
84                         {
85                                 if (strcmp(pingrpl, parameters[0]) == 0)
86                                 {
87                                         delete[] pingrpl;
88                                         user->Shrink(extenstr);
89                                         return 1;
90                                 }
91                                 else
92                                 {
93                                         if(killonbadreply)
94                                                 User::QuitUser(ServerInstance, user, "Incorrect ping reply for registration");
95                                         return 1;
96                                 }
97                         }
98                 }
99                 return 0;
100         }
101
102         virtual bool OnCheckReady(User* user)
103         {
104                 char* pingrpl;
105                 return (!user->GetExt(extenstr, pingrpl));
106         }
107         
108         virtual void OnUserDisconnect(User* user)
109         {
110                 char* pingrpl;
111                 user->GetExt(extenstr, pingrpl);
112
113                 if (pingrpl)
114                 {
115                         delete[] pingrpl;
116                         user->Shrink(extenstr);
117                 }
118         }
119         
120         virtual void OnCleanup(int target_type, void* item)
121         {
122                 if (target_type == TYPE_USER)
123                 {
124                         User* user = (User*)item;
125                         char* pingrpl;
126                         user->GetExt(extenstr, pingrpl);
127                         
128                         if (pingrpl)
129                         {
130                                 delete[] pingrpl;
131                                 user->Shrink(extenstr);
132                         } 
133                 }
134         }
135         
136         virtual ~ModuleWaitPong()
137         {
138         }
139         
140         virtual Version GetVersion()
141         {
142                 return Version(1, 1, 0, 1, VF_VENDOR, API_VERSION);
143         }
144         
145 };
146
147 MODULE_INIT(ModuleWaitPong)