From e279c897a2d1e36bcee18888439e2e8727c0004a Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Sat, 9 Feb 2019 11:21:27 -0800 Subject: [PATCH] Added support for non-text gophertypes --- stubb | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/stubb b/stubb index 5a7b984..365bacf 100755 --- a/stubb +++ b/stubb @@ -5,7 +5,7 @@ local socket = require("socket") local urlparser = require("socket.url") local separator = '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n' 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 = {} } 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' @@ -90,7 +90,7 @@ function request_gopher(host, port, query, gtype) end if request then - if download_types[gtype] then + if gtype ~= 'h' and download_types[gtype] then local s = tcp:receive('*a') response = s else @@ -137,15 +137,20 @@ end function go_to_url(u, add2history, noprint) 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] if not url.host or url.scheme ~= 'gopher' then print(separator) 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 t.filedata = request_gopher(url.host, url.port, url.path, url.gophertype) print(separator) - local res = handle_response(t.filedata, url.gophertype) if noprint then return res @@ -172,6 +177,7 @@ function download_file(u) local fn = nil io.write('Save file as: ') fn = io.read() + if not fn or fn == '' then return print('Cancelled') end io.write(string.format('Saving as %q, is this correct? ', fn)) local r = string.lower(io.read()) if r ~= 'y' and r ~= 'yes' then return false end @@ -187,7 +193,7 @@ end function handle_response(res_table, gtype) 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') else 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 '' else local leader = download_types[gophertype] - if not leader then return nil end - local url = string.format('%s:%s/%s%s', val[3] or '', val[4] or '', gophertype, val[2] or '/') - table.insert(t.current_links, url) + if not leader then return '' end + if gophertype == 'h' and string.sub(val[2],1,3) == 'URL' then + 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) return string.format(' %s %5s %s', leader, linknum, text) or '' end @@ -717,19 +727,19 @@ function save_file() end -function handle_response(res_table, gtype) - local out = '' - if gtype == '0' or gtype == 0 then - out = table.concat(res_table, '\n') - else - session.tabs[session.current_tab].current_links = {} - for i, v in ipairs(res_table) do - out = out .. (display_gophermap_row(v) or '').. '\n' - end - end +-- function handle_response(res_table, gtype) +-- local out = '' +-- if gtype == '0' or gtype == 0 then +-- out = table.concat(res_table, '\n') +-- else +-- session.tabs[session.current_tab].current_links = {} +-- for i, v in ipairs(res_table) do +-- out = out .. (display_gophermap_row(v) or '').. '\n' +-- end +-- end - return out -end +-- return out +-- end function show_favorites()