Subversion Repositories Games.Chess Giants

Rev

Rev 147 | Rev 164 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 147 Rev 153
Line 13... Line 13...
13
 
13
 
14
 
14
 
15
// prototypes of local functions
15
// prototypes of local functions
16
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
16
static int CALLBACK DialogProc_ThisDialog (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
17
static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason);
17
static unsigned __int32 RetrieveActivationCode (wchar_t *candidate_email, wchar_t **failure_reason);
18
static int recv_with_timeout (int socket_id, float timeout_in_seconds, char *outbuf, size_t outbuf_size, int flags);
-
 
19
 
18
 
20
 
19
 
21
void DialogBox_Registration (void)
20
void DialogBox_Registration (void)
22
{
21
{
23
   // helper function to fire up the modal dialog box
22
   // helper function to fire up the modal dialog box
Line 60... Line 59...
60
      SetWindowText (hWnd, LOCALIZE (L"Registration_Title"));
59
      SetWindowText (hWnd, LOCALIZE (L"Registration_Title"));
61
 
60
 
62
      // set the static texts
61
      // set the static texts
63
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_QUESTION), LOCALIZE (L"Registration_Question"));
62
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_QUESTION), LOCALIZE (L"Registration_Question"));
64
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_INSTRUCTIONS), LOCALIZE (L"Registration_Instructions"));
63
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_INSTRUCTIONS), LOCALIZE (L"Registration_Instructions"));
-
 
64
      // set email address font
65
      Static_SetText (GetDlgItem (hWnd, STATICTEXT_REGISTRATION_EMAILADDRESS), LOCALIZE (L"Registration_EmailAddress"));
65
      SendMessage (GetDlgItem (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS), WM_SETFONT, (WPARAM) GetStockObject (SYSTEM_FONT), 0);
66
      if (options.registration.user_email[0] != 0)
66
      if (options.registration.user_email[0] != 0)
67
         SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, options.registration.user_email);
67
         SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, options.registration.user_email);
68
      else
68
      else
69
         SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, LOCALIZE (L"Registration_YourEmailHere"));
69
         SetDlgItemText (hWnd, EDITBOX_REGISTRATION_EMAILADDRESS, LOCALIZE (L"Registration_YourEmailHere"));
70
      SetWindowText (GetDlgItem (hWnd, BUTTON_VALIDATECODE), LOCALIZE (L"Registration_ValidateCode"));
70
      SetWindowText (GetDlgItem (hWnd, BUTTON_VALIDATECODE), LOCALIZE (L"Registration_ValidateCode"));
Line 117... Line 117...
117
         // query the remote server for the code associated with this email. If it fails, ask why.
117
         // query the remote server for the code associated with this email. If it fails, ask why.
118
         candidate_code = RetrieveActivationCode (candidate_email, &failure_reason);
118
         candidate_code = RetrieveActivationCode (candidate_email, &failure_reason);
119
 
119
 
120
         // was there a problem retrieving the code ?
120
         // was there a problem retrieving the code ?
121
         if ((failure_reason != NULL) && (failure_reason[0] != 0))
121
         if ((failure_reason != NULL) && (failure_reason[0] != 0))
122
            MessageBox (hWnd, failure_reason, PROGRAM_NAME, MB_ICONERROR | MB_OK); // registration failed. Display an error and DON'T exit the program
122
            MessageBox (hWnd, failure_reason, PROGRAM_NAME, MB_ICONERROR | MB_OK); // registration failed. Display an error and DON'T make the dialog box disappear
123
 
123
 
124
         // else is the retrieved data correct ?
124
         // else is the retrieved data correct ?
125
         else if (IsRegistrationCorrect (candidate_email, candidate_code))
125
         else if (IsRegistrationCorrect (candidate_email, candidate_code))
126
         {
126
         {
127
            // if so, save activation email and activation code
127
            // if so, save activation email and activation code
128
            wcscpy_s (options.registration.user_email, WCHAR_SIZEOF (options.registration.user_email), candidate_email);
128
            wcscpy_s (options.registration.user_email, WCHAR_SIZEOF (options.registration.user_email), candidate_email);
129
            options.registration.activation_code = candidate_code;
129
            options.registration.activation_code = candidate_code;
130
            MessageBox (hWnd, LOCALIZE (L"Registration_ThankYou"), PROGRAM_NAME, MB_ICONINFORMATION | MB_OK);
130
            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
131
            EndDialog (hWnd, 0); // then display a thank you dialog box and make the dialog box disappear
132
         }
132
         }
133
 
133
 
134
         // else the supplied data is wrong
134
         // else the supplied data is wrong
135
         else
135
         else
136
         {
136
         {
137
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Registration_Error"), AUTHOR_EMAIL);
137
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Registration_Error"), AUTHOR_EMAIL);
138
            MessageBox (hWnd, temp_string, PROGRAM_NAME, MB_ICONERROR | MB_OK); // wrong activation. Display an error and DON'T exit the program
138
            MessageBox (hWnd, temp_string, PROGRAM_NAME, MB_ICONERROR | MB_OK); // wrong activation. Display an error and DON'T make the dialog box disappear
139
         }
139
         }
140
      }
140
      }
141
 
141
 
142
      // else was it the PayPal button ?
142
      // else was it the PayPal button ?
143
      else if (wParam_loword == BUTTON_DONATE)
143
      else if (wParam_loword == BUTTON_DONATE)
144
      {
144
      {
-
 
145
         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
-
 
146
 
145
         if (wcscmp (languages[language_id].name, L"French") == 0)
147
         if (wcscmp (languages[language_id].name, L"French") == 0)
146
            ShellExecute (NULL, L"open", PAYPAL_URL_FR, NULL, NULL, SW_MAXIMIZE); // open PayPal in French in the default browser, maximized
148
            ShellExecute (NULL, L"open", PAYPAL_URL_FR, NULL, NULL, SW_MAXIMIZE); // open PayPal in French in the default browser, maximized
147
         else
149
         else
148
            ShellExecute (NULL, L"open", PAYPAL_URL_XX, NULL, NULL, SW_MAXIMIZE); // open PayPal in English (default) the default browser, maximized
150
            ShellExecute (NULL, L"open", PAYPAL_URL_XX, NULL, NULL, SW_MAXIMIZE); // open PayPal in English (default) the default browser, maximized
149
      }
151
      }
Line 168... Line 170...
168
   static char ascii_email[128];
170
   static char ascii_email[128];
169
 
171
 
170
   struct sockaddr_in service;
172
   struct sockaddr_in service;
171
   struct hostent *hostinfo;
173
   struct hostent *hostinfo;
172
   char *data_start;
174
   char *data_start;
173
   WSADATA wsa_data;
-
 
174
   int write_index;
175
   int write_index;
175
   int read_index;
176
   int read_index;
176
   int length;
177
   int length;
177
   SOCKET s;
178
   SOCKET s;
178
 
179
 
179
   // initialize WinSock
180
   // initialize the network subsystem if required
180
   if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0)
181
   if (!Network_Init ())
181
   {
182
   {
182
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
183
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
183
      return (0); // couldn't initialize WinSock, return an error condition
184
      return (0); // couldn't initialize WinSock, return an error condition
184
   }
185
   }
185
 
186
 
Line 196... Line 197...
196
   service.sin_addr.s_addr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0]));
197
   service.sin_addr.s_addr = inet_addr (inet_ntoa (*(struct in_addr *) hostinfo->h_addr_list[0]));
197
   service.sin_port = htons (80); // connect to webserver there (port 80)
198
   service.sin_port = htons (80); // connect to webserver there (port 80)
198
 
199
 
199
   // create our socket
200
   // create our socket
200
   if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
201
   if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
201
   {
202
   {
202
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
203
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
203
      return (0); // couldn't create socket, return an error condition
204
      return (0); // couldn't create socket, return an error condition
204
   }
205
   }
205
 
206
 
206
   // connect to the distributor's webserver using that socket
207
   // connect to the distributor's webserver using that socket
207
   if (connect (s, (struct sockaddr *) &service, sizeof (service)) == -1)
208
   if (connect (s, (struct sockaddr *) &service, sizeof (service)) == -1)
Line 225... Line 226...
225
      return (0); // unable to send HTTP query, return an error condition
226
      return (0); // unable to send HTTP query, return an error condition
226
   }
227
   }
227
 
228
 
228
   // read the reply (10 seconds timeout)
229
   // read the reply (10 seconds timeout)
229
   http_buffer[0] = 0;
230
   http_buffer[0] = 0;
230
   read_index = recv_with_timeout (s, 10.0f, http_buffer, sizeof (http_buffer), 0);
231
   read_index = RecvWithTimeout (s, 10.0f, http_buffer, sizeof (http_buffer), 0);
231
   if (read_index < 1)
232
   if (read_index < 1)
232
   {
233
   {
233
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
234
      *failure_reason = LOCALIZE (L"Registration_NetworkFailure");
234
      return (0); // empty or erroneous HTTP reply, return an error condition
235
      return (0); // empty or erroneous HTTP reply, return an error condition
235
   }
236
   }
236
 
237
 
237
   closesocket (s); // finished communicating, close TCP socket
238
   closesocket (s); // finished communicating, close TCP socket
238
   WSACleanup (); // and clean up WinSock
-
 
239
 
239
 
240
   // terminate recv buffer and see where the useful data starts
240
   // terminate recv buffer and see where the useful data starts
241
   http_buffer[read_index] = 0;
241
   http_buffer[read_index] = 0;
242
   //MessageBoxA (NULL, http_buffer, "HTTP response", MB_OK);
242
   //MessageBoxA (NULL, http_buffer, "HTTP response", MB_OK);
243
   if ((data_start = strstr (http_buffer, "Success=")) != NULL)
243
   if ((data_start = strstr (http_buffer, "Success=")) != NULL)
Line 256... Line 256...
256
      return (0); // server returned an error, return an error condition
256
      return (0); // server returned an error, return an error condition
257
   }
257
   }
258
 
258
 
259
   *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); // this should never happen
259
   *failure_reason = LOCALIZE (L"Registration_NetworkFailure"); // this should never happen
260
   return (0); // server returned an error, return an error condition
260
   return (0); // server returned an error, return an error condition
261
}
-
 
262
 
-
 
263
 
-
 
264
static int recv_with_timeout (int socket_id, float timeout_in_seconds, char *outbuf, size_t outbuf_size, int flags)
-
 
265
{
-
 
266
   unsigned long nonblocking_mode;
-
 
267
   unsigned long msec_start;
-
 
268
   float timediff;
-
 
269
   int total_size;
-
 
270
   int recv_size;
-
 
271
 
-
 
272
   // make socket non blocking
-
 
273
   nonblocking_mode = 1;
-
 
274
   ioctlsocket (socket_id, FIONBIO, &nonblocking_mode);
-
 
275
 
-
 
276
   // loop endlessly, noting the time at which we start
-
 
277
   msec_start = GetTickCount ();
-
 
278
   total_size = 0;
-
 
279
   for (;;)
-
 
280
   {
-
 
281
      // see how much time elapsed since the last time we received data
-
 
282
      timediff = (GetTickCount () - msec_start) / 1000.0f;
-
 
283
      if (timediff > timeout_in_seconds)
-
 
284
         break; // if we've waited long enough, give up
-
 
285
 
-
 
286
      // see if we have something to read from the socket
-
 
287
      recv_size = recv (socket_id, &outbuf[total_size], outbuf_size - total_size, flags);
-
 
288
      if (recv_size == 0)
-
 
289
         break; // on TCP disconnection, give up too
-
 
290
      else if (recv_size < 0)
-
 
291
      {
-
 
292
         Sleep (100); // if nothing was received then we want to wait a little before trying again, 0.1 seconds
-
 
293
         continue;
-
 
294
      }
-
 
295
 
-
 
296
      total_size += recv_size; // increase the received bytes count
-
 
297
      outbuf[total_size] = 0; // terminate outbuf ourselves
-
 
298
      if (total_size == outbuf_size)
-
 
299
         break; // if the output buffer is full, give up
-
 
300
      msec_start = GetTickCount (); // and remember when we last received data (i.e. now)
-
 
301
   }
-
 
302
 
-
 
303
   return (total_size); // and return the number of bytes received
-
 
304
}
261
}