Added support for non-text gophertypes

This commit is contained in:
sloumdrone 2019-02-09 11:21:27 -08:00
parent 8eb7ba1a22
commit e279c897a2
1 changed files with 30 additions and 20 deletions

50
stubb
View File

@ -5,7 +5,7 @@ local socket = require("socket")
local urlparser = require("socket.url") local urlparser = require("socket.url")
local separator = '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n' local separator = '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
local valid_types = {'0', '1', 'i', '7'} local valid_types = {'0', '1', 'i', '7'}
local download_types = {I = 'IMG', h = 'WEB', ['9'] = 'BIN', g = 'GIF', s = 'SND'} local download_types = {I = 'IMG', h = 'HTM', ['9'] = 'BIN', g = 'GIF', s = 'SND'}
session = { tabs = { }, favorites = {} } session = { tabs = { }, favorites = {} }
local save_file_location = string.format('%s/.stubbfaves', assert(os.getenv('HOME'), 'Unable to get home directory')) local save_file_location = string.format('%s/.stubbfaves', assert(os.getenv('HOME'), 'Unable to get home directory'))
local search_provider = 'gopher://gopher.floodgap.com:70/7/v2/vs' local search_provider = 'gopher://gopher.floodgap.com:70/7/v2/vs'
@ -90,7 +90,7 @@ function request_gopher(host, port, query, gtype)
end end
if request then if request then
if download_types[gtype] then if gtype ~= 'h' and download_types[gtype] then
local s = tcp:receive('*a') local s = tcp:receive('*a')
response = s response = s
else else
@ -137,15 +137,20 @@ end
function go_to_url(u, add2history, noprint) function go_to_url(u, add2history, noprint)
local url = parse_url(u) local url = parse_url(u)
if url.scheme == 'http' then
print('Attempting to open http link in "lynx"...')
os.execute(string.format('lynx %s', url.scheme .. '://' .. url.authority .. url.path))
print('Operation complete')
return true
end
local t = session.tabs[session.current_tab] local t = session.tabs[session.current_tab]
if not url.host or url.scheme ~= 'gopher' then if not url.host or url.scheme ~= 'gopher' then
print(separator) print(separator)
print('Invalid url') print('Invalid url')
elseif table.has(valid_types, url.gophertype) then elseif table.has(valid_types, url.gophertype) or url.gophertype == 'h' then
if url.query ~= '' and url.query then url.path = url.path .. '?' .. url.query end if url.query ~= '' and url.query then url.path = url.path .. '?' .. url.query end
t.filedata = request_gopher(url.host, url.port, url.path, url.gophertype) t.filedata = request_gopher(url.host, url.port, url.path, url.gophertype)
print(separator) print(separator)
local res = handle_response(t.filedata, url.gophertype) local res = handle_response(t.filedata, url.gophertype)
if noprint then if noprint then
return res return res
@ -172,6 +177,7 @@ function download_file(u)
local fn = nil local fn = nil
io.write('Save file as: ') io.write('Save file as: ')
fn = io.read() fn = io.read()
if not fn or fn == '' then return print('Cancelled') end
io.write(string.format('Saving as %q, is this correct? ', fn)) io.write(string.format('Saving as %q, is this correct? ', fn))
local r = string.lower(io.read()) local r = string.lower(io.read())
if r ~= 'y' and r ~= 'yes' then return false end if r ~= 'y' and r ~= 'yes' then return false end
@ -187,7 +193,7 @@ end
function handle_response(res_table, gtype) function handle_response(res_table, gtype)
local out = '' local out = ''
if gtype == '0' or gtype == 0 then if gtype == '0' or gtype == 0 or gtype == 'h' then
out = table.concat(res_table, '\n') out = table.concat(res_table, '\n')
else else
session.tabs[session.current_tab].current_links = {} session.tabs[session.current_tab].current_links = {}
@ -216,9 +222,13 @@ function display_gophermap_row(row)
return string.format(' %s %5s %s', leader, linknum, text) or '' return string.format(' %s %5s %s', leader, linknum, text) or ''
else else
local leader = download_types[gophertype] local leader = download_types[gophertype]
if not leader then return nil end if not leader then return '' end
local url = string.format('%s:%s/%s%s', val[3] or '', val[4] or '', gophertype, val[2] or '/') if gophertype == 'h' and string.sub(val[2],1,3) == 'URL' then
table.insert(t.current_links, url) table.insert(t.current_links, string.sub(val[2], 5))
else
local url = string.format('%s:%s/%s%s', val[3] or '', val[4] or '', gophertype, val[2] or '/')
table.insert(t.current_links, url)
end
local linknum = string.format('(%d)', #t.current_links) local linknum = string.format('(%d)', #t.current_links)
return string.format(' %s %5s %s', leader, linknum, text) or '' return string.format(' %s %5s %s', leader, linknum, text) or ''
end end
@ -717,19 +727,19 @@ function save_file()
end end
function handle_response(res_table, gtype) -- function handle_response(res_table, gtype)
local out = '' -- local out = ''
if gtype == '0' or gtype == 0 then -- if gtype == '0' or gtype == 0 then
out = table.concat(res_table, '\n') -- out = table.concat(res_table, '\n')
else -- else
session.tabs[session.current_tab].current_links = {} -- session.tabs[session.current_tab].current_links = {}
for i, v in ipairs(res_table) do -- for i, v in ipairs(res_table) do
out = out .. (display_gophermap_row(v) or '').. '\n' -- out = out .. (display_gophermap_row(v) or '').. '\n'
end -- end
end -- end
return out -- return out
end -- end
function show_favorites() function show_favorites()