- // 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 
- static bool was_donatebutton_clicked = false; 
-   
-   
- // 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); 
-   
-   
- 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")); 
-       Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_INSTRUCTIONS), LOCALIZE (L"Registration_Instructions")); 
-       // set email address font 
-       SendMessage (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS), WM_SETFONT, (WPARAM) GetStockObject (SYSTEM_FONT), 0); 
-       if (options.registration.user_email[0] != 0) 
-          SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, options.registration.user_email); 
-       else 
-          SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, LOCALIZE (L"Registration_YourEmailHere")); 
-       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 Paypal bitmap and the status bar message to hyperlinks 
-       ConvertStaticToHyperlink (GetDlgItem (hWnd, BUTTON_DONATE)); 
-       ConvertStaticToHyperlink (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_STATUSBAR)); 
-    } 
-   
-    // else did we click the close button on the title bar ? 
-    else if (message == WM_CLOSE) 
-    { 
-       if (was_donatebutton_clicked) 
-       { 
-          MessageBox (hWnd, LOCALIZE (L"Registration_DontForget"), PROGRAM_NAME, MB_ICONWARNING | MB_OK); // users need to be reminded that they MUST write their email in the activation form 
-          SendMessage (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS), EM_SETSEL, 0, -1); // select all text 
-          SetFocus (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS)); // focus on the email address field 
-          was_donatebutton_clicked = false; // once is enough 
-          return (true); // prevent window close this time 
-       } 
-       else 
-          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) && (wcscmp (candidate_email, LOCALIZE (L"Registration_YourEmailHere")) != 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 make the dialog box disappear 
-   
-          // 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 make the dialog box disappear 
-          } 
-   
-          // 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 make the dialog box disappear 
-          } 
-       } 
-   
-       // else was it the PayPal button ? 
-       else if (wParam_loword == BUTTON_DONATE) 
-       { 
-          MessageBox (hWnd, LOCALIZE (L"Registration_DontForget"), PROGRAM_NAME, MB_ICONWARNING | MB_OK); // users need to be reminded that they MUST write their email in the activation form 
-   
-          if (wcscmp (languages[language_id].name, L"French") == 0) 
-             ShellExecute (NULL, L"open", PAYPAL_URL_FR, NULL, NULL, SW_MAXIMIZE); // open PayPal in French in the default browser, maximized 
-          else 
-             ShellExecute (NULL, L"open", PAYPAL_URL_XX, NULL, NULL, SW_MAXIMIZE); // open PayPal in English (default) the default browser, maximized 
-   
-          was_donatebutton_clicked = true; // remember the user clicked the Donate button 
-       } 
-   
-       // 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; 
-    int write_index; 
-    int read_index; 
-    int length; 
-    SOCKET s; 
-   
-    // 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 = RecvWithTimeout (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 
-   
-    // 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) _atoi64 (&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 
- } 
-