Rev 23 | Rev 85 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 23 | Rev 83 | ||
|---|---|---|---|
| Line 3... | Line 3... | ||
| 3 | #include "../common.h" |
3 | #include "../common.h" |
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | // dialog template |
6 | // dialog template |
| 7 | #define THIS_DIALOG DIALOG_REGISTER |
7 | #define THIS_DIALOG DIALOG_REGISTER |
| - | 8 | ||
| - | 9 | ||
| - | 10 | // global variables used in this module only |
|
| - | 11 | static bool was_donatebutton_clicked = false; |
|
| - | 12 | static wchar_t candidate_email[128] = L""; |
|
| - | 13 | static unsigned __int32 candidate_code = 0; // 32 bits |
|
| 8 | 14 | ||
| 9 | 15 | ||
| 10 | // prototypes of local functions |
16 | // prototypes of local functions |
| 11 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
17 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam); |
| - | 18 | static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason); |
|
| 12 | 19 | ||
| 13 | 20 | ||
| 14 | void DialogBox_Registration (void) |
21 | void DialogBox_Registration (void) |
| 15 | { |
22 | { |
| 16 | // helper function to fire up the modal dialog box |
23 | // helper function to fire up the modal dialog box |
| Line 24... | Line 31... | ||
| 24 | 31 | ||
| 25 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
32 | static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam) |
| 26 | { |
33 | { |
| 27 | // message handler for the dialog box |
34 | // message handler for the dialog box |
| 28 | 35 | ||
| 29 | static wchar_t |
36 | static wchar_t temp_string[1024]; |
| 30 | static wchar_t candidate_email[256]; |
- | |
| 31 | 37 | ||
| 32 | unsigned long candidate_code; |
- | |
| 33 | unsigned short wParam_hiword; |
38 | unsigned short wParam_hiword; |
| 34 | unsigned short wParam_loword; |
39 | unsigned short wParam_loword; |
| 35 | BOOL was_translated; // needs to be of type BOOL (int) |
- | |
| 36 |
|
40 | wchar_t *failure_reason; |
| 37 | int write_index; |
41 | int write_index; |
| - | 42 | int read_index; |
|
| 38 | int length; |
43 | int length; |
| 39 | 44 | ||
| 40 | // filter out the commonly used message values |
45 | // filter out the commonly used message values |
| 41 | wParam_hiword = HIWORD (wParam); |
46 | wParam_hiword = HIWORD (wParam); |
| 42 | wParam_loword = LOWORD (wParam); |
47 | wParam_loword = LOWORD (wParam); |
| Line 59... | Line 64... | ||
| 59 | SetWindowText (GetDlgItem (hWnd, BUTTON_DONATE), LOCALIZE (L"Registration_Donate")); |
64 | SetWindowText (GetDlgItem (hWnd, BUTTON_DONATE), LOCALIZE (L"Registration_Donate")); |
| 60 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_NOPAYPAL), LOCALIZE (L"Registration_NoPayPal")); |
65 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_NOPAYPAL), LOCALIZE (L"Registration_NoPayPal")); |
| 61 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_INSTRUCTIONS), LOCALIZE (L"Registration_Instructions")); |
66 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_INSTRUCTIONS), LOCALIZE (L"Registration_Instructions")); |
| 62 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_CODENOTRECEIVED), LOCALIZE (L"Registration_CodeNotReceived")); |
67 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_CODENOTRECEIVED), LOCALIZE (L"Registration_CodeNotReceived")); |
| 63 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_EMAILADDRESS), LOCALIZE (L"Registration_EmailAddress")); |
68 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_EMAILADDRESS), LOCALIZE (L"Registration_EmailAddress")); |
| - | 69 | if (options.registration.user_email[0] != 0) |
|
| 64 | SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, options.registration.user_email); |
70 | SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, options.registration.user_email); |
| - | 71 | else |
|
| - | 72 | SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, L"your@email.here"); |
|
| 65 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_ACTIVATIONCODE), LOCALIZE (L"Registration_ActivationCode")); |
73 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_ACTIVATIONCODE), LOCALIZE (L"Registration_ActivationCode")); |
| 66 | //SetDlgItemInt (hWnd, EDITBOX_REGISTRATION_ACTIVATIONCODE, options.registration.activation_code, false); |
74 | //SetDlgItemInt (hWnd, EDITBOX_REGISTRATION_ACTIVATIONCODE, options.registration.activation_code, false); |
| 67 | SetWindowText (GetDlgItem (hWnd, BUTTON_VALIDATECODE), LOCALIZE (L"Registration_ValidateCode")); |
75 | SetWindowText (GetDlgItem (hWnd, BUTTON_VALIDATECODE), LOCALIZE (L"Registration_ValidateCode")); |
| 68 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_STATUSBAR), LOCALIZE (L"Registration_StatusBar")); |
76 | Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_STATUSBAR), LOCALIZE (L"Registration_StatusBar")); |
| 69 | 77 | ||
| Line 84... | Line 92... | ||
| 84 | // else did we take action on one of the controls ? |
92 | // else did we take action on one of the controls ? |
| 85 | else if (message == WM_COMMAND) |
93 | else if (message == WM_COMMAND) |
| 86 | { |
94 | { |
| 87 | // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) |
95 | // did we cancel the dialog box ? (IDCANCEL is a system-wide dialog box handler value, that catches the ESC key) |
| 88 | if (wParam_loword == IDCANCEL) |
96 | if (wParam_loword == IDCANCEL) |
| - | 97 | { |
|
| - | 98 | // was the donate button clicked ? |
|
| - | 99 | if (was_donatebutton_clicked) |
|
| - | 100 | { |
|
| - | 101 | // did the user fill in his email address ? |
|
| - | 102 | } |
|
| 89 | EndDialog (hWnd, 0); // close the dialog box |
103 | EndDialog (hWnd, 0); // close the dialog box |
| - | 104 | } |
|
| 90 | 105 | ||
| 91 | // else did the user enter something in the edit boxes ? |
106 | // else did the user enter something in the edit boxes ? |
| 92 | else if (wParam_hiword == EN_CHANGE) |
107 | else if (wParam_hiword == EN_CHANGE) |
| 93 | { |
108 | { |
| 94 | // enable the validation button only if the |
109 | // enable the validation button only if the PayPal email field has data |
| 95 | GetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, candidate_email, WCHAR_SIZEOF (candidate_email)); |
110 | GetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, candidate_email, WCHAR_SIZEOF (candidate_email)); |
| 96 | candidate_code = GetDlgItemInt (hWnd, EDITBOX_REGISTRATION_ACTIVATIONCODE, &was_translated, false); |
- | |
| 97 | EnableWindow (GetDlgItem (hWnd, BUTTON_VALIDATECODE), ((candidate_email[0] != 0) && (candidate_code != 0))); |
- | |
| 98 | } |
- | |
| 99 | - | ||
| 100 | // else was it the validation button ? |
- | |
| 101 | else if (wParam_loword == BUTTON_VALIDATECODE) |
- | |
| 102 | { |
- | |
| 103 | // retrieve form data |
- | |
| 104 | GetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, candidate_email, WCHAR_SIZEOF (candidate_email)); |
- | |
| 105 | candidate_code = GetDlgItemInt (hWnd, EDITBOX_REGISTRATION_ACTIVATIONCODE, &was_translated, false); |
- | |
| 106 | 111 | ||
| 107 | // strip spaces and unprintable characters from candidate |
112 | // strip spaces and unprintable characters from candidate email, and bring it lowercase |
| 108 | length = wcslen (candidate_email); |
113 | length = wcslen (candidate_email); |
| 109 | write_index = 0; |
114 | write_index = 0; |
| 110 | for (read_index = 0; read_index < length; read_index++) |
115 | for (read_index = 0; read_index < length; read_index++) |
| 111 | if (!iswspace (candidate_email[read_index])) |
116 | if (!iswspace (candidate_email[read_index])) |
| 112 | { |
117 | { |
| 113 | candidate_email[write_index] = candidate_email[read_index]; |
118 | candidate_email[write_index] = tolower (candidate_email[read_index]); // force lowercase |
| 114 | write_index++; |
119 | write_index++; // keep only valid signs and discard spaces |
| 115 | } |
120 | } |
| 116 | candidate_email[write_index] = 0; // ensure string is correctly terminated |
121 | candidate_email[write_index] = 0; // ensure string is correctly terminated |
| 117 | 122 | ||
| - | 123 | EnableWindow (GetDlgItem (hWnd, BUTTON_VALIDATECODE), ((candidate_email[0] != 0) && (wcslen (candidate_email) > 5) && (wcsncmp (candidate_email, L"your@email.", 11) != 0) |
|
| - | 124 | && (wcschr (candidate_email, L'@') != NULL) && (wcschr (candidate_email, L'.') != NULL))); |
|
| - | 125 | } |
|
| - | 126 | ||
| 118 |
|
127 | // else was it the validation button ? |
| - | 128 | else if (wParam_loword == BUTTON_VALIDATECODE) |
|
| - | 129 | { |
|
| - | 130 | // query the remote server for the code associated with this email. If it fails, ask why. |
|
| 119 |
|
131 | candidate_code = RetrieveActivationCode (candidate_email, &failure_reason); |
| 120 | 132 | ||
| 121 | // |
133 | // was there a problem retrieving the code ? |
| 122 | if (( |
134 | if ((failure_reason != NULL) && (failure_reason[0] != 0)) |
| 123 |
|
135 | MessageBox (hWnd, failure_reason, PROGRAM_NAME, MB_ICONERROR | MB_OK); // registration failed. Display an error and DON'T exit the program |
| 124 | 136 | ||
| 125 | // else is the |
137 | // else is the retrieved data correct ? |
| 126 | else if (IsRegistrationCorrect (candidate_email, candidate_code)) |
138 | else if (IsRegistrationCorrect (candidate_email, candidate_code)) |
| 127 | { |
139 | { |
| 128 | // if so, save activation code |
140 | // if so, save activation email and activation code |
| - | 141 | wcscpy_s (options.registration.user_email, WCHAR_SIZEOF (options.registration.user_email), candidate_email); |
|
| 129 | options.registration.activation_code = candidate_code; |
142 | options.registration.activation_code = candidate_code; |
| 130 | MessageBox (hWnd, LOCALIZE (L"Registration_ThankYou"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK); |
143 | MessageBox (hWnd, LOCALIZE (L"Registration_ThankYou"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK); |
| 131 | EndDialog (hWnd, 0); // then display a thank you dialog box and exit the program |
144 | EndDialog (hWnd, 0); // then display a thank you dialog box and exit the program |
| 132 | } |
145 | } |
| 133 | 146 | ||
| 134 | // else the supplied data is wrong |
147 | // else the supplied data is wrong |
| 135 | else |
148 | else |
| 136 | { |
149 | { |
| 137 | swprintf_s ( |
150 | swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Registration_Error"), AUTHOR_EMAIL); |
| 138 | MessageBox (hWnd, |
151 | MessageBox (hWnd, temp_string, PROGRAM_NAME, MB_ICONERROR | MB_OK); // wrong activation. Display an error and DON'T exit the program |
| 139 | } |
152 | } |
| 140 | } |
153 | } |
| 141 | 154 | ||
| 142 | // else was it the PayPal button ? |
155 | // else was it the PayPal button ? |
| 143 | else if (wParam_loword == BUTTON_DONATE) |
156 | else if (wParam_loword == BUTTON_DONATE) |
| - | 157 | { |
|
| 144 | ShellExecute (NULL, L"open", PAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open PayPal in the default browser, maximized |
158 | ShellExecute (NULL, L"open", PAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open PayPal in the default browser, maximized |
| - | 159 | was_donatebutton_clicked = true; // remember the donate button was clicked |
|
| - | 160 | } |
|
| 145 | 161 | ||
| 146 | // else was it the "no PayPal" hyperlink ? |
162 | // else was it the "no PayPal" hyperlink ? |
| 147 | else if (wParam_loword == STATICTEXT_REGISTRATION_NOPAYPAL) |
163 | else if (wParam_loword == STATICTEXT_REGISTRATION_NOPAYPAL) |
| 148 | ShellExecute (NULL, L"open", NOPAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open the bank account page in the default browser, maximized |
164 | ShellExecute (NULL, L"open", NOPAYPAL_URL, NULL, NULL, SW_MAXIMIZE); // open the bank account page in the default browser, maximized |
| 149 | - | ||
| 150 | // else was it the "code not received" hyperlink ? |
- | |
| 151 | else if (wParam_loword == STATICTEXT_REGISTRATION_CODENOTRECEIVED) |
- | |
| 152 | ShellExecute (NULL, L"open", PAYPAL_RETURNURL, NULL, NULL, SW_MAXIMIZE); // open the manual code retrieval page in the default browser, maximized |
- | |
| 153 | 165 | ||
| 154 | // else was it the status bar hyperlink ? |
166 | // else was it the status bar hyperlink ? |
| 155 | else if (wParam_loword == STATICTEXT_REGISTRATION_STATUSBAR) |
167 | else if (wParam_loword == STATICTEXT_REGISTRATION_STATUSBAR) |
| 156 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the main page in the default browser, maximized |
168 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the main page in the default browser, maximized |
| 157 | } |
169 | } |
| 158 | 170 | ||
| 159 | // call the default dialog message processing function to keep things going |
171 | // call the default dialog message processing function to keep things going |
| 160 | return (false); |
172 | return (false); |
| - | 173 | } |
|
| - | 174 | ||
| - | 175 | ||
| - | 176 | static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason) |
|
| - | 177 | { |
|
| - | 178 | // this function queries the remote server for registration status. If OK, the received code is returned. |
|
| - | 179 | // On error, 0 is returned and error_string points to the cause of the error (as reported by the server) |
|
| - | 180 | // which is stored in a static buffer in this very function. |
|
| - | 181 | ||
| - | 182 | static char http_buffer[1024]; // used both for request and reply |
|
| - | 183 | static char ascii_email[128]; |
|
| - | 184 | ||
| - | 185 | struct sockaddr_in service; |
|
| - | 186 | struct hostent *hostinfo; |
|
| - | 187 | char *data_start; |
|
| - | 188 | WSADATA wsa_data; |
|
| - | 189 | int write_index; |
|
| - | 190 | int read_index; |
|
| - | 191 | int length; |
|
| - | 192 | SOCKET s; |
|
| - | 193 | ||
| - | 194 | // initialize WinSock |
|
| - | 195 | if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0) |
|
| - | 196 | { |
|
| - | 197 | *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); |
|
| - | 198 | return (0); // couldn't initialize WinSock, return an error condition |
|
| - | 199 | } |
|
| - | 200 | ||
| - | 201 | // get our distribution server's IP address from the host name |
|
| - | 202 | hostinfo = gethostbyname ("pmbaty.com"); |
|
| - | 203 | if (hostinfo == NULL) |
|
| - | 204 | { |
|
| - | 205 | *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); |
|
| - | 206 | return (0); // couldn't resolve hostname, return an error condition |
|
| - | 207 | } |
|
| - | 208 | ||
| - | 209 | // fill in the sockaddr server structure with the server hostinfo data |
|
| - | 210 | service.sin_family = AF_INET; |
|
| - | 211 | service.sin_addr.s_addr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0])); |
|
| - | 212 | service.sin_port = htons (80); // connect to webserver there (port 80) |
|
| - | 213 | ||
| - | 214 | // create our socket |
|
| - | 215 | if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) |
|
| - | 216 | { |
|
| - | 217 | *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); |
|
| - | 218 | return (0); // couldn't create socket, return an error condition |
|
| - | 219 | } |
|
| - | 220 | ||
| - | 221 | // connect to the distributor's webserver using that socket |
|
| - | 222 | if (connect (s, (struct sockaddr *) &service, sizeof (service)) == -1) |
|
| - | 223 | { |
|
| - | 224 | *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); |
|
| - | 225 | return (0); // unable to connect to webserver, return an error condition |
|
| - | 226 | } |
|
| - | 227 | ||
| - | 228 | // build the HTTP query and send it |
|
| - | 229 | ConvertTo7BitASCII (ascii_email, sizeof (ascii_email), candidate_email); |
|
| - | 230 | sprintf_s (http_buffer, sizeof (http_buffer), |
|
| - | 231 | "GET /chess/whatsmycode.php?email=%s HTTP/1.1\r\n" |
|
| - | 232 | "Host: pmbaty.com\r\n" |
|
| - | 233 | "Connection: close\r\n" |
|
| - | 234 | "\r\n", ascii_email); |
|
| - | 235 | length = strlen (http_buffer); |
|
| - | 236 | write_index = send (s, http_buffer, length, 0); // send the HTTP query |
|
| - | 237 | if (write_index != length) |
|
| - | 238 | { |
|
| - | 239 | *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); |
|
| - | 240 | return (0); // unable to send HTTP query, return an error condition |
|
| - | 241 | } |
|
| - | 242 | ||
| - | 243 | // read the reply |
|
| - | 244 | read_index = recv (s, http_buffer, sizeof (http_buffer), 0); |
|
| - | 245 | if (read_index < 1) |
|
| - | 246 | { |
|
| - | 247 | *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); |
|
| - | 248 | return (0); // empty or erroneous HTTP reply, return an error condition |
|
| - | 249 | } |
|
| - | 250 | ||
| - | 251 | closesocket (s); // finished communicating, close TCP socket |
|
| - | 252 | WSACleanup (); // and clean up WinSock |
|
| - | 253 | ||
| - | 254 | // terminate recv buffer and see where the useful data starts |
|
| - | 255 | http_buffer[read_index] = 0; |
|
| - | 256 | //MessageBoxA (NULL, http_buffer, "HTTP response", MB_OK); |
|
| - | 257 | if ((data_start = strstr (http_buffer, "Success=")) != NULL) |
|
| - | 258 | { |
|
| - | 259 | *failure_reason = L""; // no error, set an empty string as the failure reason |
|
| - | 260 | return ((unsigned __int32) atoll (&data_start[strlen ("Success=")])); // and return the candidate code we got |
|
| - | 261 | } |
|
| - | 262 | else if ((data_start = strstr (http_buffer, "Error=")) != NULL) |
|
| - | 263 | { |
|
| - | 264 | data_start += strlen ("Error="); |
|
| - | 265 | if (strchr (data_start, '\r') != NULL) *strchr (data_start, '\r') = 0; |
|
| - | 266 | if (strchr (data_start, '\n') != NULL) *strchr (data_start, '\n') = 0; |
|
| - | 267 | if (_stricmp (data_start, "WrongEmail") == 0) *failure_reason = LOCALIZE (L"Registration_WrongEmail"); |
|
| - | 268 | else if (_stricmp (data_start, "UnknownEmail") == 0) *failure_reason = LOCALIZE (L"Registration_UnknownEmail"); |
|
| - | 269 | else if (_stricmp (data_start, "TooManyActivations") == 0) *failure_reason = LOCALIZE (L"Registration_TooManyActivations");; |
|
| - | 270 | return (0); // and return the candidate code we got |
|
| - | 271 | } |
|
| - | 272 | ||
| - | 273 | *failure_reason = L"Unknown error"; // FIXME: this should never happen |
|
| - | 274 | return (0); // server returned an error, return an error condition |
|
| 161 | } |
275 | } |