Got gophermap working

This commit is contained in:
sloumdrone 2019-01-02 23:00:25 -08:00
parent 14a2f2cf6e
commit 7179c65bff
1 changed files with 83 additions and 11 deletions

View File

@ -3,8 +3,11 @@
local socket = require("socket")
local urlparser = require("socket.url")
local separator = '\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n'
local valid_types = {'0', '1', 'i'}
local links = {}
table.split = function(inputstr, sep)
string.split = function(inputstr, sep)
if sep == nil then
sep = "%s"
end
@ -17,12 +20,46 @@ table.split = function(inputstr, sep)
end
table.has = function(t, item)
for i, v in pairs(t) do
if v == item then
return true
end
end
return false
end
table.join = function(inputtable, sep)
local s = ''
if type(inputtable) ~= 'table' then
do
return nil
end
end
for i, v in ipairs(inputtable) do
if type(v) == 'number' then
v = tostring(v)
end
if type(v) == 'string' then
s = s..v
s = s..sep
end
end
return s
end
function request_gopher(host, port, query)
--based on: https://stackoverflow.com/questions/9013290/lua-socket-client/9014261#9014261
local tcp = assert(socket.tcp())
local response = {}
tcp:settimeout(2000)
tcp:settimeout(4, 't')
local connection = tcp:connect(host, port);
local request
if connection then
@ -39,17 +76,32 @@ function request_gopher(host, port, query)
end
tcp:close()
elseif connection then
response[1] = 'Connection error. Bad request.'
response[1] = 'iConnection error. Bad request.'
tcp:close()
else
response[1] = 'Connection error. Bad host.'
response[1] = 'iConnection error. Bad host.'
end
return response
end
function display_gophermap_row(row)
local val = string.split(row, '\t')
local gophertype = string.sub(val[1], 1, 1)
local text = string.sub(val[1], 2)
local spacer = ' '
if gophertype == 'i' then
print(spacer..text)
elseif table.has(valid_types, gophertype) then
local leader = gophertype == '0' and 'TXT' or 'MAP'
local url = string.format('%s:%s/%s%s', val[3], val[4], gophertype, val[2])
table.insert(links, url)
local linknum = string.format('[%d]', #links)
print(string.format(' %s %6s %s', leader, linknum, text))
end
end
function getch()
-- taken from: http://lua.2524044.n2.nabble.com/How-to-get-one-keystroke-without-hitting-Enter-td5858614.html
os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
@ -60,8 +112,13 @@ end
function mainloop()
print('\n\27[7m * S t u b b * \27[0m')
print('| |')
print('| A gopher browser rendering gophermaps and |')
print('| text files. |')
print('| |')
while true do
print('\n\27[7m (G)o to url, (B)ack, (#) Visit link, (Q)uit \27[0m')
print('\27[7m (G)o to url, (B)ack, (#) Visit link, (Q)uit \27[0m')
local key = string.lower(getch())
io.write('\n')
if key == 'q' then
@ -71,21 +128,36 @@ function mainloop()
local url = io.read()
url = parse_url(url)
if not url.host or url.scheme ~= 'gopher' then
print('\nInvalid url\n')
print(separator)
print(' I n v a l i d u r l')
print(separator)
else
filedata = request_gopher(url.host, url.port, url.path)
print(separator)
for i, v in ipairs(filedata) do
print(v)
end
handle_response(filedata, url.gophertype)
print(separator)
end
elseif type(tonumber(key)) == 'number' then
local linkurl = links[tonumber(key)]
print(linkurl or 'No such link')
end
end
end
function handle_response(res_table, gtype)
if gtype == 0 then
for i, v in ipairs(res_table) do
print(v)
end
else
links = {}
for i, v in ipairs(res_table) do
display_gophermap_row(v)
end
end
end
function parse_url(u)
if not string.find(u, '://', 1, true) then
u = 'gopher://'..u