7 #include "helperfuncs.h"
9 /* $ModDesc: Forces connecting clients to send a PONG message back to the server before they can complete their connection */
11 static std::string RandString(unsigned int length)
13 unsigned char* tmp = new unsigned char[(length/4)*3];
15 for(unsigned int i = 0; i < (length/4)*3; i++)
16 tmp[i] = (unsigned char)rand();
18 unsigned char* out = new unsigned char[length];
20 to64frombits(out, tmp, (length/4)*3);
22 std::string ret((char*)out);
30 class ModuleWaitPong : public Module
39 ModuleWaitPong(Server* Me)
46 virtual void OnRehash(std::string param)
48 Conf = new ConfigReader;
50 sendsnotice = Conf->ReadFlag("waitpong", "sendsnotice", 0);
52 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
55 killonbadreply = Conf->ReadFlag("waitpong", "killonbadreply", 0);
57 if(Conf->GetError() == CONF_VALUE_NOT_FOUND)
58 killonbadreply = true;
63 void Implements(char* List)
65 List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnPreCommand] = List[I_OnRehash] = List[I_OnUserDisconnect] = 1;
68 virtual void OnUserRegister(userrec* user)
70 std::string* pingrpl = new std::string;
71 *pingrpl = RandString(10);
73 Srv->Send(user->fd, "PING :" + *pingrpl);
76 WriteServ(user->fd, "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->c_str(), pingrpl->c_str());
78 user->Extend("waitpong_pingstr", (char*)pingrpl);
81 virtual int OnPreCommand(std::string command, char** parameters, int pcnt, userrec* user, bool validated)
85 std::string* pingrpl = (std::string*)user->GetExt("waitpong_pingstr");
87 if(pingrpl && (*pingrpl == parameters[0]))
90 user->Shrink("waitpong_pingstr");
93 else if(killonbadreply)
95 Srv->QuitUser(user, "Incorrect ping reply for registration");
103 virtual bool OnCheckReady(userrec* user)
105 return (!user->GetExt("waitpong_pingstr"));
108 virtual void OnUserDisconnect(userrec* user)
110 std::string* pingrpl = (std::string*)user->GetExt("waitpong_pingstr");
115 user->Shrink("waitpong_pingstr");
119 virtual ~ModuleWaitPong()
123 virtual Version GetVersion()
125 return Version(1, 0, 0, 0, VF_VENDOR);
130 class ModuleWaitPongFactory : public ModuleFactory
133 ModuleWaitPongFactory()
137 ~ModuleWaitPongFactory()
141 virtual Module * CreateModule(Server* Me)
143 return new ModuleWaitPong(Me);
148 extern "C" void * init_module( void )
150 return new ModuleWaitPongFactory;