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




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

        .data
                szTrilPropAdd   db "\users\global\profiles.ini",0
                szTrilDefault   db "\users\default",0
                szTrilProfiles  db "Profiles",0
                szTrilName      db "Name",0
                szTrilType      db "Preferences Type",0
                szTrilLocation  db "Preferences Location",0
                szTrilDir       db "\Trillian\User Settings\",0
                szTrilAIM       db "\aim.ini",0
                szTrilPath      db "Trillian",0
                szTrilNum       db "num",0


        .data?
                pTrilPath       db 1024 dup(?)                
        
        .code

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

ReadTrillianIniFile proc uses esi
        ; Read maximum 16 profiles
        mov     esi, 0Fh

@loop:
        szText  szTrilSuperProfile, "profile %lu"
        invoke  wsprintf, offset lpBuf3, addr szTrilSuperProfile, esi

        invoke  GetPrivateProfileString, offset lpBuf3, offset szTrilName, offset szNilStr, offset lpBuf1, 1024, offset pTrilPath
        invoke  GetPrivateProfileString, offset lpBuf3, offset szPassword, offset szNilStr, offset lpBuf2, 1024, offset pTrilPath

        .IF     (byte ptr[lpBuf1] != 0 && byte ptr[lpBuf2] != 0)
                szText  szTrilSuperEntry, "%s: %s",13,10
                invoke  wsprintf, offset lpBuf3, addr szTrilSuperEntry, offset lpBuf1, offset lpBuf2
                invoke  lstrcat, GlobalBuffer, offset lpBuf3
        .ENDIF

        dec     esi
        jns     @loop
        
        ret
ReadTrillianIniFile endp

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

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

@loop_end:
        mov     byte ptr[ecx], 0

        ret
RemoveLastSlash endp

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



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

GetTrillianOutput proc uses esi edi
        LOCAL   path[MAX_PATH+1]: BYTE
        LOCAL   lpDir[MAX_PATH+1]: BYTE

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

        ; Get path to user's dir
        invoke  SHGetSpecialFolderPath, 0, addr path, CSIDL_PERSONAL, 0
        lea     esi, path
        invoke  RemoveLastSlash
        invoke  lstrcat, addr path, offset szTrilDir

        ; Find Trillian install path
        lea     esi, lpDir
        invoke  GetUnPath, offset szTrilPath
        cmp     byte ptr[esi], 0
        jz      @ret

        invoke  RemoveLastSlash
        mov     edi, ecx

        invoke  lstrcat, addr lpDir, offset szTrilPropAdd

        invoke  GetPrivateProfileInt, offset szTrilProfiles, offset szTrilNum, offset szNilStr, addr lpDir
        test    eax, eax
        jz      @ret

        dec     eax
        mov     esi, eax

@pro_loop:
        szText  szTrilProfile, "Profile%.3lu"
        invoke  wsprintf, offset lpBuf4, addr szTrilProfile, esi

        invoke  GetPrivateProfileString, offset lpBuf4, offset szTrilName, offset szNilStr, offset lpBuf1, 1024, addr lpDir
        invoke  GetPrivateProfileString, offset lpBuf4, offset szTrilType, offset szNilStr, offset lpBuf2, 1024, addr lpDir
        invoke  GetPrivateProfileString, offset lpBuf4, offset szTrilLocation, offset szNilStr, offset lpBuf3, 1024, addr lpDir

        ; Custom dir
        .IF     byte ptr[lpBuf2] == '2'
                invoke  lstrcpy, offset pTrilPath, offset lpBuf3
        ; Default system user dir
        .ELSEIF byte ptr[lpBuf2] == '1'
                szText  szTCatBuf, "%s%s"
                invoke  wsprintf, offset pTrilPath, addr szTCatBuf, addr path, offset lpBuf1
        ; Default dir
        .ELSEIF byte ptr[lpBuf2] == '0'
                mov     byte ptr[edi], 0
                invoke  lstrcpy, offset pTrilPath, addr lpDir
                invoke  lstrcat, offset pTrilPath, offset szTrilDefault
                mov     byte ptr[edi], '\'
        .ELSE
                jmp     @try
        .ENDIF

        invoke  lstrcat, offset pTrilPath, offset szTrilAIM

        ; Finaly read ini file
        invoke  ReadTrillianIniFile
        
@try:
        dec     esi
        jns     @pro_loop

@ret:
        ret
GetTrillianOutput endp
