The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_conn_waitpong.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 
00016 /* $ModDesc: Forces connecting clients to send a PONG message back to the server before they can complete their connection */
00017 
00018 class ModuleWaitPong : public Module
00019 {
00020         bool sendsnotice;
00021         bool killonbadreply;
00022         const char* extenstr;
00023 
00024  public:
00025         ModuleWaitPong(InspIRCd* Me)
00026          : Module(Me), extenstr("waitpong_pingstr")
00027         {
00028                 OnRehash(NULL,"");
00029                 Implementation eventlist[] = { I_OnUserRegister, I_OnCheckReady, I_OnPreCommand, I_OnRehash, I_OnUserDisconnect, I_OnCleanup };
00030                 ServerInstance->Modules->Attach(eventlist, this, 6);
00031         }
00032 
00033         virtual void OnRehash(User* user, const std::string &param)
00034         {
00035                 ConfigReader Conf(ServerInstance);
00036 
00037                 sendsnotice = Conf.ReadFlag("waitpong", "sendsnotice", 0);
00038 
00039                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
00040                         sendsnotice = true;
00041 
00042                 killonbadreply = Conf.ReadFlag("waitpong", "killonbadreply", 0);
00043 
00044                 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
00045                         killonbadreply = true;
00046         }
00047 
00048 
00049         char* RandString(unsigned int length)
00050         {
00051                 unsigned char* out = new unsigned char[length+1];
00052                 for(unsigned int i = 0; i < length; i++)
00053                         out[i] = ((rand() % 26) + 65);
00054                 out[length] = '\0';
00055 
00056                 return (char*)out;
00057         }
00058 
00059         virtual int OnUserRegister(User* user)
00060         {
00061                 char* pingrpl = RandString(10);
00062 
00063                 user->Write("PING :%s", pingrpl);
00064 
00065                 if(sendsnotice)
00066                         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);
00067 
00068                 user->Extend(extenstr, pingrpl);
00069                 return 0;
00070         }
00071 
00072         virtual int OnPreCommand(std::string &command, std::vector<std::string> &parameters, User* user, bool validated, const std::string &original_line)
00073         {
00074                 if (command == "PONG")
00075                 {
00076                         char* pingrpl;
00077                         user->GetExt(extenstr, pingrpl);
00078 
00079                         if (pingrpl)
00080                         {
00081                                 if (strcmp(pingrpl, parameters[0].c_str()) == 0)
00082                                 {
00083                                         delete[] pingrpl;
00084                                         user->Shrink(extenstr);
00085                                         return 1;
00086                                 }
00087                                 else
00088                                 {
00089                                         if(killonbadreply)
00090                                                 ServerInstance->Users->QuitUser(user, "Incorrect ping reply for registration");
00091                                         return 1;
00092                                 }
00093                         }
00094                 }
00095                 return 0;
00096         }
00097 
00098         virtual bool OnCheckReady(User* user)
00099         {
00100                 char* pingrpl;
00101                 return (!user->GetExt(extenstr, pingrpl));
00102         }
00103 
00104         virtual void OnUserDisconnect(User* user)
00105         {
00106                 char* pingrpl;
00107                 user->GetExt(extenstr, pingrpl);
00108 
00109                 if (pingrpl)
00110                 {
00111                         delete[] pingrpl;
00112                         user->Shrink(extenstr);
00113                 }
00114         }
00115 
00116         virtual void OnCleanup(int target_type, void* item)
00117         {
00118                 if (target_type == TYPE_USER)
00119                 {
00120                         User* user = (User*)item;
00121                         char* pingrpl;
00122                         user->GetExt(extenstr, pingrpl);
00123 
00124                         if (pingrpl)
00125                         {
00126                                 delete[] pingrpl;
00127                                 user->Shrink(extenstr);
00128                         }
00129                 }
00130         }
00131 
00132         virtual ~ModuleWaitPong()
00133         {
00134         }
00135 
00136         virtual Version GetVersion()
00137         {
00138                 return Version("$Id: m_conn_waitpong.cpp 10292 2008-08-25 20:38:22Z w00t $", VF_VENDOR, API_VERSION);
00139         }
00140 
00141 };
00142 
00143 MODULE_INIT(ModuleWaitPong)