2010-03-24 17:22:56 +00:00
// Unicode support by Jim Park -- 08/20/2007
2005-06-20 08:56:22 +00:00
# include "makensisw.h"
# include "update.h"
# include "jnetlib/httpget.h"
2010-03-24 17:22:56 +00:00
# include "../ExDLL/nsis_tchar.h"
2005-06-20 08:56:22 +00:00
static BOOL update_initialized = FALSE ;
static JNL_AsyncDNS * g_dns = NULL ;
void InitializeUpdate ( ) {
if ( update_initialized )
return ;
update_initialized = TRUE ;
JNL : : open_socketlib ( ) ;
g_dns = new JNL_AsyncDNS ( ) ;
}
void FinalizeUpdate ( ) {
if ( ! update_initialized )
return ;
delete g_dns ;
JNL : : close_socketlib ( ) ;
}
int getProxyInfo ( char * out ) {
DWORD v = 0 ;
2005-11-12 17:24:14 +00:00
HKEY hKey ;
2010-03-24 17:22:56 +00:00
if ( RegOpenKeyExA ( HKEY_CURRENT_USER , " Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Internet Settings " , 0 , KEY_READ , & hKey ) = = ERROR_SUCCESS ) {
2005-11-12 17:24:14 +00:00
DWORD l = 4 ;
DWORD t ;
2010-03-24 17:22:56 +00:00
if ( RegQueryValueExA ( hKey , " ProxyEnable " , NULL , & t , ( unsigned char * ) & v , & l ) = = ERROR_SUCCESS & & t = = REG_DWORD ) {
2005-06-20 08:56:22 +00:00
l = 8192 ;
2010-03-24 17:22:56 +00:00
if ( RegQueryValueExA ( hKey , " ProxyServer " , NULL , & t , ( unsigned char * ) out , & l ) ! = ERROR_SUCCESS | | t ! = REG_SZ ) {
2005-06-20 08:56:22 +00:00
v = 0 ;
* out = 0 ;
}
}
else v = 0 ;
out [ 8192 - 1 ] = 0 ;
RegCloseKey ( hKey ) ;
}
return v ;
}
DWORD CALLBACK UpdateThread ( LPVOID v ) {
# define RSZ 30
int len ;
char * response = ( char * ) GlobalAlloc ( GPTR , RSZ ) ;
char * r ;
char url [ 300 ] ;
BOOL error = FALSE ;
static char pbuf [ 8192 ] ;
2010-03-24 17:22:56 +00:00
static char ansiBuf [ 1024 ] ;
2005-06-20 08:56:22 +00:00
char * p = NULL ;
* response = 0 ;
if ( getProxyInfo ( pbuf ) )
{
2010-04-12 16:00:17 +00:00
p = strstr ( pbuf , " http= " ) ;
2005-06-20 08:56:22 +00:00
if ( ! p ) p = pbuf ;
else {
p + = 5 ;
}
2010-04-12 16:00:17 +00:00
char * tp = strstr ( p , " ; " ) ;
2005-06-20 08:56:22 +00:00
if ( tp ) * tp = 0 ;
2010-04-12 16:00:17 +00:00
char * p2 = strstr ( p , " = " ) ;
2005-06-20 08:56:22 +00:00
if ( p2 ) p = 0 ; // we found the wrong proxy
}
InitializeUpdate ( ) ;
JNL_HTTPGet * get = new JNL_HTTPGet ( g_dns , 8192 , ( p & & p [ 0 ] ) ? p : NULL ) ; ;
2010-03-24 17:22:56 +00:00
lstrcpyA ( url , NSIS_UPDATE ) ;
# ifdef _UNICODE
WideCharToMultiByte ( CP_ACP , 0 , g_sdata . brandingv , - 1 , ansiBuf , sizeof ( ansiBuf ) , NULL , NULL ) ;
lstrcatA ( url , ansiBuf ) ;
# else
lstrcatA ( url , g_sdata . brandingv ) ;
# endif
lstrcpyA ( response , " " ) ;
2005-06-20 08:56:22 +00:00
get - > addheader ( " User-Agent: MakeNSISw (jnetlib) " ) ;
get - > addheader ( " Accept:*/* " ) ;
get - > connect ( url ) ;
while ( 1 ) {
int st = get - > run ( ) ;
if ( st < 0 ) { error = TRUE ; break ; } //error
if ( get - > get_status ( ) = = 2 ) {
while ( len = get - > bytes_available ( ) ) {
char b [ RSZ ] ;
if ( len > RSZ ) len = RSZ ;
2010-03-24 17:22:56 +00:00
if ( lstrlenA ( response ) + len > RSZ ) break ;
2005-06-20 08:56:22 +00:00
len = get - > get_bytes ( b , len ) ;
b [ len ] = 0 ;
2010-03-24 17:22:56 +00:00
lstrcatA ( response , b ) ;
2005-06-20 08:56:22 +00:00
}
}
if ( st = = 1 ) break ; //closed
}
r = response ;
while ( r & & * r ) {
if ( * r = = ' \n ' ) { * r = 0 ; break ; }
r + + ;
}
if ( error ) {
char buf [ 1000 ] ;
2010-03-24 17:22:56 +00:00
wsprintfA ( buf , " There was a problem checking for an update. Please try again later. \n \n Error: %s " , get - > geterrorstr ( ) ) ;
MessageBoxA ( g_sdata . hwnd , buf , " NSIS Update " , MB_OK | MB_ICONINFORMATION ) ;
2005-06-20 08:56:22 +00:00
}
2010-03-24 17:22:56 +00:00
else if ( * response = = ' 1 ' & & lstrlenA ( response ) > 2 ) {
2005-06-20 08:56:22 +00:00
char buf [ 200 ] ;
response + = 2 ;
2010-03-24 17:22:56 +00:00
wsprintfA ( buf , " NSIS %s is now available. Would you like to download it now? " , response ) ;
if ( MessageBoxA ( g_sdata . hwnd , buf , " NSIS Update " , MB_YESNO | MB_ICONINFORMATION ) = = IDYES ) {
ShellExecuteA ( g_sdata . hwnd , " open " , NSIS_DL_URL , NULL , NULL , SW_SHOWNORMAL ) ;
2005-06-20 08:56:22 +00:00
}
}
2010-03-24 17:22:56 +00:00
else if ( * response = = ' 2 ' & & lstrlenA ( response ) > 2 ) {
2005-06-20 08:56:22 +00:00
char buf [ 200 ] ;
response + = 2 ;
2010-03-24 17:22:56 +00:00
wsprintfA ( buf , " NSIS %s is now available. Would you like to download this preview release now? " , response ) ;
if ( MessageBoxA ( g_sdata . hwnd , buf , " NSIS Update " , MB_YESNO | MB_ICONINFORMATION ) = = IDYES ) {
ShellExecuteA ( g_sdata . hwnd , " open " , NSIS_DL_URL , NULL , NULL , SW_SHOWNORMAL ) ;
2005-06-20 08:56:22 +00:00
}
}
2010-03-24 17:22:56 +00:00
else MessageBoxA ( g_sdata . hwnd , " There is no update available for NSIS at this time. " , " NSIS Update " , MB_OK | MB_ICONINFORMATION ) ;
2005-06-20 08:56:22 +00:00
GlobalFree ( response ) ;
delete get ;
EnableMenuItem ( g_sdata . menu , IDM_NSISUPDATE , MF_ENABLED ) ;
return 0 ;
}
void Update ( ) {
DWORD dwThreadId ;
2010-04-12 16:00:17 +00:00
if ( _tcsstr ( g_sdata . brandingv , _T ( " cvs " ) ) )
2005-06-20 08:56:22 +00:00
{
2010-03-24 17:22:56 +00:00
MessageBox ( g_sdata . hwnd , _T ( " Cannot check for new version of nightly builds. To update, download a new nightly build. " ) , _T ( " NSIS Update " ) , MB_OK | MB_ICONSTOP ) ;
2005-06-20 08:56:22 +00:00
return ;
}
EnableMenuItem ( g_sdata . menu , IDM_NSISUPDATE , MF_GRAYED ) ;
CloseHandle ( CreateThread ( NULL , 0 , UpdateThread , ( LPVOID ) NULL , 0 , & dwThreadId ) ) ;
}