aboutsummaryrefslogtreecommitdiff
path: root/favicon.f90
blob: 6f800dbfa8e3ab56fcc2bc69ab3c8a50f49c5cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module favicon
implicit none

    private
    
    public::get_favicon

contains


    subroutine get_server_favicon_file(server, filename)
    use platform, only: dir_sep, get_settings_directory
    implicit none
    
        character(*), intent(in)::server
        character(*), intent(out)::filename
        
        character(len=256)::dir
        
        call get_settings_directory(dir)
        filename = trim(dir)//dir_sep//trim(server)//".favicon.txt"
    
    end subroutine get_server_favicon_file

    function get_favicon_from_cache(server) result(f)
    implicit none
    
        character(len=4)::f
        character(*), intent(in)::server
        character(len=384)::filename
        
        integer::unumber, iostatus
        
        call get_server_favicon_file(server, filename) 
        
        f = ' '
        
        open(newunit=unumber, file=filename, &
             action="read", iostat=iostatus)
        
        if(iostatus == 0) then
            ! second line
            read(unumber, '(A)') f
            if(trim(f) /= "NA") then
                read(unumber, '(A)') f
            end if
            close(unumber)
        end if
    
    end function get_favicon_from_cache
    
    function request_favicon(server) result(f)
    use gemini_protocol, only: request_url, is_failure_code
    implicit none
    
        character(len=4)::f
        character(*), intent(in)::server
        character(len=1024)::url
        character(len=128)::return_type
        
        character(len=384)::cache_filename
        
        integer::return_code, unum, ios
        
        call get_server_favicon_file(server, cache_filename)
        open(newunit=unum, file=cache_filename, action="write", iostat=ios)
        if(ios == 0) then
        
            ! Per gemini://mozz.us/files/rfc_gemini_favicon.gmi
            url = "gemini://"//trim(server)//"/favicon.txt"
            
            return_code = request_url(url, unum, return_type)
            close(unum)
            
            if(is_failure_code(return_code)) then
                open(newunit=unum, file=cache_filename, action="write", iostat=ios)
                write(unum, '(A2,2X)') "NA"
                close(unum)
            end if
    
            f = get_favicon_from_cache(server)
        
        else
        
            f = ' '
        
        end if
    
    end function request_favicon

    function get_favicon(server) result(f)
    implicit none
    
        character(len=4)::f
        character(*), intent(in)::server
        
        f = get_favicon_from_cache(server)
        
        ! Blank - request it
        if(len_trim(f) == 0) then
            f = request_favicon(server)
        end if
        
        ! NA - server said no at some point
        if(trim(f) == "NA") then
            f = ' '
        end if
        
    end function get_favicon

end module favicon