Added ability to download files
This commit is contained in:
parent
de13bc5bee
commit
8eb7ba1a22
64
stubb
64
stubb
|
@ -1,10 +1,11 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
local version = 'v0.12.0'
|
||||
local version = 'v1.0.0'
|
||||
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'}
|
||||
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'
|
||||
|
@ -77,7 +78,7 @@ end
|
|||
-- TCP Requests and processing -----------------------------
|
||||
------------------------------------------------------------
|
||||
|
||||
function request_gopher(host, port, query)
|
||||
function request_gopher(host, port, query, gtype)
|
||||
local tcp = assert(socket.tcp())
|
||||
local response = {}
|
||||
tcp:settimeout(4, 't')
|
||||
|
@ -89,21 +90,25 @@ function request_gopher(host, port, query)
|
|||
end
|
||||
|
||||
if request then
|
||||
while true do
|
||||
local s, status, partial = tcp:receive()
|
||||
if status == "closed" then break end
|
||||
if s ~= '.' then
|
||||
table.insert(response, (s or partial))
|
||||
if download_types[gtype] then
|
||||
local s = tcp:receive('*a')
|
||||
response = s
|
||||
else
|
||||
while true do
|
||||
local s, status, partial = tcp:receive()
|
||||
if status == "closed" then break end
|
||||
if s ~= '.' then
|
||||
table.insert(response, (s or partial))
|
||||
end
|
||||
end
|
||||
end
|
||||
tcp:close()
|
||||
elseif connection then
|
||||
response[1] = 'iConnection error. Bad request.'
|
||||
tcp:close()
|
||||
else
|
||||
response[1] = 'iConnection error. Bad host.'
|
||||
end
|
||||
|
||||
tcp:close()
|
||||
return response
|
||||
end
|
||||
|
||||
|
@ -138,7 +143,7 @@ function go_to_url(u, add2history, noprint)
|
|||
print('Invalid url')
|
||||
elseif table.has(valid_types, url.gophertype) 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)
|
||||
t.filedata = request_gopher(url.host, url.port, url.path, url.gophertype)
|
||||
print(separator)
|
||||
|
||||
local res = handle_response(t.filedata, url.gophertype)
|
||||
|
@ -155,10 +160,31 @@ function go_to_url(u, add2history, noprint)
|
|||
table.insert(t.history.items, o)
|
||||
t.history.loc = t.history.loc + 1
|
||||
end
|
||||
elseif download_types[url.gophertype] then
|
||||
download_file(url)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function download_file(u)
|
||||
print(separator)
|
||||
local save_path = assert(os.getenv('HOME'))
|
||||
local fn = nil
|
||||
io.write('Save file as: ')
|
||||
fn = io.read()
|
||||
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
|
||||
print(string.format('Downloading %q...', fn))
|
||||
local data = request_gopher(u.host, u.port, u.path, u.gophertype)
|
||||
if not data then return false end
|
||||
local f = io.open(save_path .. '/' .. fn, 'wb')
|
||||
f:write(data)
|
||||
f:close()
|
||||
print(string.format('%q saved to %s/%s', fn, save_path, fn))
|
||||
end
|
||||
|
||||
|
||||
function handle_response(res_table, gtype)
|
||||
local out = ''
|
||||
if gtype == '0' or gtype == 0 then
|
||||
|
@ -188,6 +214,13 @@ function display_gophermap_row(row)
|
|||
table.insert(t.current_links, url)
|
||||
local linknum = string.format('(%d)', #t.current_links)
|
||||
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)
|
||||
local linknum = string.format('(%d)', #t.current_links)
|
||||
return string.format(' %s %5s %s', leader, linknum, text) or ''
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -214,6 +247,10 @@ end
|
|||
------------------------------------------------------------
|
||||
|
||||
function init()
|
||||
if arg[1] == '-h' or arg[1] == '--help' then
|
||||
print_help()
|
||||
os.exit(0)
|
||||
end
|
||||
new_tab()
|
||||
if file_exists(save_file_location) then
|
||||
dofile(save_file_location)
|
||||
|
@ -274,9 +311,10 @@ function print_help()
|
|||
print('\nStubb ' .. version)
|
||||
local helpstring = [[
|
||||
|
||||
Stubb is a text based gopher client that only displays and supports gophertypes:
|
||||
0 (text) and 1 (gopher map). The application is named for the character Stubb
|
||||
from Moby Dick known for his imaginative patter and good humor.
|
||||
Stubb is a text based gopher client gophertypes 0 (text) and 1 (gopher map) are
|
||||
rendered to the screen, all other types are downloaded to a user's home folder.
|
||||
The application is named for the character Stubb from Moby Dick known for his
|
||||
imaginative patter and good humor.
|
||||
|
||||
The command bar lists the current tab number, out of how many total tabs. It also
|
||||
lists the current place in history (for the tab you are currently on).
|
||||
|
|
Loading…
Reference in New Issue