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