Initial commit
This commit is contained in:
commit
045a40ba56
|
@ -0,0 +1,95 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
local socket = require("socket")
|
||||
local urlparser = require("socket.url")
|
||||
|
||||
table.split = function(inputstr, sep)
|
||||
if sep == nil then
|
||||
sep = "%s"
|
||||
end
|
||||
local t={}; i=1
|
||||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
|
||||
t[i] = str
|
||||
i = i + 1
|
||||
end
|
||||
return t
|
||||
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:connect(host, port);
|
||||
tcp:send(query.."\n");
|
||||
|
||||
while true do
|
||||
local s, status, partial = tcp:receive()
|
||||
if status == "closed" then break end
|
||||
table.insert(response, (s or partial))
|
||||
end
|
||||
|
||||
tcp:close()
|
||||
end
|
||||
|
||||
|
||||
function getch_unix()
|
||||
-- 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")
|
||||
local key = io.read(1)
|
||||
os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
|
||||
return(key);
|
||||
end
|
||||
|
||||
|
||||
function mainloop()
|
||||
while true do
|
||||
print('(G)o to url, (B)ack, (#) Visit link, (Q)uit')
|
||||
io.write('> ')
|
||||
local key = string.lower(getch_unix())
|
||||
io.write('\n')
|
||||
if key == 'q' then
|
||||
os.exit(0)
|
||||
elseif key == 'g' then
|
||||
io.write('Enter gopher url: ')
|
||||
local url = io.read()
|
||||
url = parse_url(url)
|
||||
if not url.host or url.scheme ~= 'gopher' then
|
||||
print('Invalid url')
|
||||
else
|
||||
print(url.scheme)
|
||||
print(url.host)
|
||||
print(url.port)
|
||||
print(url.gophertype)
|
||||
print(url.path)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function parse_url(u)
|
||||
if not string.find(u, '://', 1, true) then
|
||||
u = 'gopher://'..u
|
||||
end
|
||||
|
||||
local default = {port=70, scheme='gopher'}
|
||||
local parsed = urlparser.parse(u, default)
|
||||
if not parsed.path then
|
||||
parsed.path = '/'
|
||||
end
|
||||
if string.find(parsed.path, '^/%d/') then
|
||||
parsed.gophertype = string.sub(parsed.path, 2, 2)
|
||||
parsed.path = string.sub(parsed.path, 3)
|
||||
elseif string.sub(parsed.path, -1) == '/' then
|
||||
parsed.gophertype = '1'
|
||||
else
|
||||
parsed.gophertype = '0'
|
||||
end
|
||||
|
||||
return parsed
|
||||
end
|
||||
|
||||
-- run the program
|
||||
mainloop()
|
Loading…
Reference in New Issue