// dialog_registration.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// dialog template
 
#define THIS_DIALOG DIALOG_REGISTER
 
 
 
 
 
// global variables used in this module only
 
static wchar_t candidate_email[128] = L"";
 
static unsigned __int32 candidate_code = 0; // 32 bits
 
 
 
 
 
// prototypes of local functions
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason);
 
static int recv_with_timeout (int socket_id, float timeout_in_seconds, char *outbuf, size_t outbuf_size, int flags);
 
 
 
 
 
void DialogBox_Registration (void)
 
{
 
   // helper function to fire up the modal dialog box
 
 
 
   // display the dialog box (set the parent to be the desktop)
 
   DialogBox (hAppInstance, MAKEINTRESOURCE (THIS_DIALOG), NULL, DialogProc_ThisDialog);
 
 
 
   return; // finished processing this dialog
 
}
 
 
 
 
 
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
 
{
 
   // message handler for the dialog box
 
 
 
   static wchar_t temp_string[1024];
 
 
 
   unsigned short wParam_hiword;
 
   unsigned short wParam_loword;
 
   wchar_t *failure_reason;
 
   int write_index;
 
   int read_index;
 
   int length;
 
 
 
   // filter out the commonly used message values
 
   wParam_hiword = HIWORD (wParam);
 
   wParam_loword = LOWORD (wParam);
 
 
 
   // have we just fired up this window ?
 
   if (message == WM_INITDIALOG)
 
   {
 
      // center the window
 
      CenterWindow (hWnd, hMainWnd);
 
 
 
      // set dialog icons (small one for title bar & big one for task manager)
 
      SendMessage (hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
      SendMessage (hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon (hAppInstance, MAKEINTRESOURCE (ICON_MAIN)));
 
 
 
      // set window title and control texts
 
      SetWindowText (hWnd, LOCALIZE (L"Registration_Title"));
 
 
 
      // set the static texts
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_QUESTION), LOCALIZE (L"Registration_Question"));
 
      SetWindowText (GetDlgItem (hWnd, BUTTON_DONATE), LOCALIZE (L"Registration_Donate"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_NOPAYPAL), LOCALIZE (L"Registration_NoPayPal"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_INSTRUCTIONS), LOCALIZE (L"Registration_Instructions"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_CODENOTRECEIVED), LOCALIZE (L"Registration_CodeNotReceived"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_EMAILADDRESS), LOCALIZE (L"Registration_EmailAddress"));
 
      if (options.registration.user_email[0] != 0)
 
         SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, options.registration.user_email);
 
      else
 
         SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, L"your@email.here");
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_ACTIVATIONCODE), LOCALIZE (L"Registration_ActivationCode"));
 
      //SetDlgItemInt (hWnd, EDITBOX_REGISTRATION_ACTIVATIONCODE, options.registration.activation_code, false);
 
      SetWindowText (GetDlgItem (hWnd, BUTTON_VALIDATECODE), LOCALIZE (L"Registration_ValidateCode"));
 
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_STATUSBAR), LOCALIZE (L"Registration_StatusBar"));
 
 
 
      // set focus to the first settable item
 
      SendMessage (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS), EM_SETSEL, 0, -1); // select all text
 
      SetFocus (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS));
 
 
 
      // convert the "no paypal" and the status bar message to hyperlinks
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_NOPAYPAL));
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_CODENOTRECEIVED));
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_STATUSBAR));
 
   }
 
 
 
   // else did we click the close button on the title bar ?
 
   else if (message == WM_CLOSE)
 
      EndDialog (hWnd, 0); // close the dialog box
 
 
 
   // else did we take action on one of the controls ?
 
   else if (message == WM_COMMAND)
 
   {
 
      // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key)
 
      if (wParam_loword == IDCANCEL)
 
         EndDialog (hWnd, 0); // close the dialog box
 
 
 
      // else did the user enter something in the edit boxes ?
 
      else if (wParam_hiword == EN_CHANGE)
 
      {
 
         // enable the validation button only if the PayPal email field has data
 
         GetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, candidate_email, WCHAR_SIZEOF (candidate_email));
 
 
 
         // strip spaces and unprintable characters from candidate email, and bring it lowercase
 
         length = wcslen (candidate_email);
 
         write_index = 0;
 
         for (read_index = 0; read_index < length; read_index++)
 
            if (!iswspace (candidate_email[read_index]))
 
            {
 
               candidate_email[write_index] = tolower (candidate_email[read_index]); // force lowercase
 
               write_index++; // keep only valid signs and discard spaces
 
            }
 
         candidate_email[write_index] = 0; // ensure string is correctly terminated
 
 
 
         EnableWindow (GetDlgItem (hWnd, BUTTON_VALIDATECODE), ((candidate_email[0] != 0) && (wcslen (candidate_email) > 5) && (wcsncmp (candidate_email, L"your@email.", 11) != 0)
 
                                                                && (wcschr (candidate_email, L'@') != NULL) && (wcschr (candidate_email, L'.') != NULL)));
 
      }
 
 
 
      // else was it the validation button ?
 
      else if (wParam_loword == BUTTON_VALIDATECODE)
 
      {
 
         // query the remote server for the code associated with this email. If it fails, ask why.
 
         candidate_code = RetrieveActivationCode (candidate_email, &failure_reason);
 
 
 
         // was there a problem retrieving the code ?
 
         if ((failure_reason != NULL) && (failure_reason[0] != 0))
 
            MessageBox (hWnd, failure_reason, PROGRAM_NAME, MB_ICONERROR | MB_OK); // registration failed. Display an error and DON'T exit the program
 
 
 
         // else is the retrieved data correct ?
 
         else if (IsRegistrationCorrect (candidate_email, candidate_code))
 
         {
 
            // if so, save activation email and activation code
 
            wcscpy_s (options.registration.user_email, WCHAR_SIZEOF (options.registration.user_email), candidate_email);
 
            options.registration.activation_code = candidate_code;
 
            MessageBox (hWnd, LOCALIZE (L"Registration_ThankYou"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK);
 
            EndDialog (hWnd, 0); // then display a thank you dialog box and exit the program
 
         }
 
 
 
         // else the supplied data is wrong
 
         else
 
         {
 
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Registration_Error"), AUTHOR_EMAIL);
 
            MessageBox (hWnd, temp_string, PROGRAM_NAME, MB_ICONERROR | MB_OK); // wrong activation. Display an error and DON'T exit the program
 
         }
 
      }
 
 
 
      // else was it the PayPal button ?
 
      else if (wParam_loword == BUTTON_DONATE)
 
         ShellExecute (NULL, L"open", PAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open PayPal in the default browser, maximized
 
 
 
      // else was it the "no PayPal" hyperlink ?
 
      else if (wParam_loword == STATICTEXT_REGISTRATION_NOPAYPAL)
 
         ShellExecute (NULL, L"open", NOPAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open the bank account page in the default browser, maximized
 
 
 
      // else was it the status bar hyperlink ?
 
      else if (wParam_loword == STATICTEXT_REGISTRATION_STATUSBAR)
 
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the main page in the default browser, maximized
 
   }
 
 
 
   // call the default dialog message processing function to keep things going
 
   return (false);
 
}
 
 
 
 
 
static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason)
 
{
 
   // this function queries the remote server for registration status. If OK, the received code is returned.
 
   // On error, 0 is returned and error_string points to the cause of the error (as reported by the server)
 
   // which is stored in a static buffer in this very function.
 
 
 
   static char http_buffer[1024]; // used both for request and reply
 
   static char ascii_email[128];
 
 
 
   struct sockaddr_in service;
 
   struct hostent *hostinfo;
 
   char *data_start;
 
   WSADATA wsa_data;
 
   int write_index;
 
   int read_index;
 
   int length;
 
   SOCKET s;
 
 
 
   // initialize WinSock
 
   if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0)
 
   {
 
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
 
      return (0); // couldn't initialize WinSock, return an error condition
 
   }
 
 
 
   // get our distribution server's IP address from the host name
 
   hostinfo = gethostbyname ("pmbaty.com");
 
   if (hostinfo == NULL)
 
   {
 
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
 
      return (0); // couldn't resolve hostname, return an error condition
 
   }
 
 
 
   // fill in the sockaddr server structure with the server hostinfo data
 
   service.sin_family = AF_INET;
 
   service.sin_addr.s_addr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0]));
 
   service.sin_port = htons (80); // connect to webserver there (port 80)
 
 
 
   // create our socket
 
   if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
 
   {
 
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
 
      return (0); // couldn't create socket, return an error condition
 
   }
 
 
 
   // connect to the distributor's webserver using that socket
 
   if (connect (s, (struct sockaddr *) &service, sizeof (service)) == -1)
 
   {
 
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
 
      return (0); // unable to connect to webserver, return an error condition
 
   }
 
 
 
   // build the HTTP query and send it
 
   ConvertTo7BitASCII (ascii_email, sizeof (ascii_email), candidate_email);
 
   sprintf_s (http_buffer, sizeof (http_buffer),
 
      "GET /chess/whatsmycode.php?email=%s HTTP/1.1\r\n"
 
      "Host: pmbaty.com\r\n"
 
      "Connection: close\r\n"
 
      "\r\n", ascii_email);
 
   length = strlen (http_buffer);
 
   write_index = send (s, http_buffer, length, 0); // send the HTTP query
 
   if (write_index != length)
 
   {
 
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
 
      return (0); // unable to send HTTP query, return an error condition
 
   }
 
 
 
   // read the reply (10 seconds timeout)
 
   http_buffer[0] = 0;
 
   read_index = recv_with_timeout (s, 10.0f, http_buffer, sizeof (http_buffer), 0);
 
   if (read_index < 1)
 
   {
 
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
 
      return (0); // empty or erroneous HTTP reply, return an error condition
 
   }
 
 
 
   closesocket (s); // finished communicating, close TCP socket
 
   WSACleanup (); // and clean up WinSock
 
 
 
   // terminate recv buffer and see where the useful data starts
 
   http_buffer[read_index] = 0;
 
   //MessageBoxA (NULL, http_buffer, "HTTP response", MB_OK);
 
   if ((data_start = strstr (http_buffer, "Success=")) != NULL)
 
   {
 
      *failure_reason = L""; // no error, set an empty string as the failure reason
 
      return ((unsigned __int32) atoll (&data_start[strlen ("Success=")])); // and return the candidate code we got
 
   }
 
   else if ((data_start = strstr (http_buffer, "Error=")) != NULL)
 
   {
 
      data_start += strlen ("Error=");
 
      if (strchr (data_start, '\r') != NULL) *strchr (data_start, '\r') = 0;
 
      if (strchr (data_start, '\n') != NULL) *strchr (data_start, '\n') = 0;
 
      if      (_stricmp (data_start, "WrongEmail") == 0)         *failure_reason = LOCALIZE (L"Registration_WrongEmail");
 
      else if (_stricmp (data_start, "UnknownEmail") == 0)       *failure_reason = LOCALIZE (L"Registration_UnknownEmail");
 
      else if (_stricmp (data_start, "TooManyActivations") == 0) *failure_reason = LOCALIZE (L"Registration_TooManyActivations");;
 
      return (0); // server returned an error, return an error condition
 
   }
 
 
 
   *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); // this should never happen
 
   return (0); // server returned an error, return an error condition
 
}
 
 
 
 
 
static int recv_with_timeout (int socket_id, float timeout_in_seconds, char *outbuf, size_t outbuf_size, int flags)
 
{
 
   unsigned long nonblocking_mode;
 
   unsigned long msec_start;
 
   float timediff;
 
   int total_size;
 
   int recv_size;
 
 
 
   // make socket non blocking
 
   nonblocking_mode = 1;
 
   ioctlsocket (socket_id, FIONBIO, &nonblocking_mode);
 
 
 
   // loop endlessly, noting the time at which we start
 
   msec_start = GetTickCount ();
 
   total_size = 0;
 
   for (;;)
 
   {
 
      // see how much time elapsed since the last time we received data
 
      timediff = (GetTickCount () - msec_start) / 1000.0f;
 
      if (timediff > timeout_in_seconds)
 
         break; // if we've waited long enough, give up
 
 
 
      // see if we have something to read from the socket
 
      recv_size = recv (socket_id, &outbuf[total_size], outbuf_size - total_size, flags);
 
      if (recv_size == 0)
 
         break; // on TCP disconnection, give up too
 
      else if (recv_size < 0)
 
      {
 
         Sleep (100); // if nothing was received then we want to wait a little before trying again, 0.1 seconds
 
         continue;
 
      }
 
 
 
      total_size += recv_size; // increase the received bytes count
 
      outbuf[total_size] = 0; // terminate outbuf ourselves
 
      if (total_size == outbuf_size)
 
         break; // if the output buffer is full, give up
 
      msec_start = GetTickCount (); // and remember when we last received data (i.e. now)
 
   }
 
 
 
   return (total_size); // and return the number of bytes received
 
}